[R-SIG-Finance] correction
Mark Leeds
markleeds2 at gmail.com
Mon Aug 17 19:25:10 CEST 2015
Hi Eric: A friend of mine pointed out that my answer to your question about
constraining the regression is incorrect because the way I recommended
bounding the parameters won't guarantee feasibility.
Below is a small example that implements what I described but note that the
sum constraint won't necessarily hold if the other 2 parameters end up both
being greater than 0.5. ( it does in this example I created but that's just
fortunate ). So don't use my approach.
There are probably optimizers out there that handle sum constraints. One
that does but I have little experience with it is Ravi Varadhan's BB
package. Also, check out John Nash's book
if you have it because that gives nice coverage of a lot ( if not all ) of
all of the optimization packages available in R.
Or as you noted, there is probably an R package that estimates that type of
regression directly. Good luck and sorry for noise.
#====================================================================
set.seed(123)
x1 <- rnorm(1000)
x2 <- rnorm(1000)
x3 <- rnorm(1000)
y <- rnorm(1000, 2 + .5 * x1 + .3 * x2 + .2 * x3)
residsfunc <- function(par, x1,x2,x3,y) {
Xmod <- cbind(par[1], par[2]*x1, par[3]*x2, (1-par[2]-par[3])*x3)
sum((y - Xmod)^2)
}
mypar <- c(mean(y),0.9,0.9)
result <- optim(par = mypar, fn=residsfunc, method = "L-BFGS-B", lower =
c(0,0,0), upper = c(Inf,1,1),
x1 = x1, x2 = x2, x3 = x3, y = y)
lastpar <- 1-result$par[2]-result$par[3]
print(c(result$par,lastpar))
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list