5

I have a collection of $n$ datapoints $(y_i,\bf{x}_i)$ in $\mathbb{R}^{p+1}$ and would like to estimate the following model in R:

$$\underset{\bf{b}\in\mathbb{R}^{p}}{\arg.\min}\;\sum_{i=1}^n(y_i-\bf{x}_i'\bf{b})^2$$

$$u.c.\;\;\;0\leq \bf{x}_i'\bf{b}\leq 1\;\;\;\forall i$$

anybody has a pointer to an efficient way of doing this? is there a re-parametrization of the OLS problem that would allow for this?

Ferdi
  • 4,882
  • 7
  • 42
  • 62
user603
  • 21,225
  • 3
  • 71
  • 135

1 Answers1

1

It is a quadratic optimization problem, that can be solved with solve.QP (just expand the square to put it in the form expected by the function).

# Sample data
n <- 100
k <- 3
b <- c(-.1,.5,1)
x <- matrix( runif(n*k), nc=k )
y <- x %*% b + .1*rnorm(n)

# Solve the quadratic program
library(quadprog)
r <- solve.QP(
  t(x) %*% x,
  t(y) %*% x,
  t(rbind( x, -x )),
  c(rep(0,n), rep(-1,n))
)
r$solution

# Check that the constraints are satisfied
range( x %*% r$solution )  # in [0,1]
range( x %*% b          )  # wider
Vincent Zoonekynd
  • 1,268
  • 10
  • 10