Alberto Santini
2008-Sep-03 13:48 UTC
[R] portfolio.optim and assets with weigth equals to zero...
Hello.
I don't understand a particular output of portfolio.optim (tseries).
I have 4 assets and the portfolio.optim returns an asset with weight equals
to zero.
If I do a portfolio.optim with 3 assets, without the asset with weight
equals to zero,
it returns a completely different result.
That's I would expected the same weights as the run with 4 assets.
Below the code.
Thanks in advance,
Alberto Santini
-------------------------------------
require(tseries)
f.mi <- coredata(get.hist.quote("F.MI",
start="2006-09-03", compression="w",
quote="Close"))
eng.mi <- coredata(get.hist.quote("ENG.MI",
start="2006-09-03",
compression="w", quote="Close"))
tis.mi <- coredata(get.hist.quote("TIS.MI",
start="2006-09-03",
compression="w", quote="Close"))
spmib <- coredata(get.hist.quote("^SPMIB",
start="2006-09-03",
compression="w", quote="Close"))
f.mi.rets <- diff(log(f.mi[1:(length(f.mi)-1)]))
eng.mi.rets <- diff(log(eng.mi[1:(length(eng.mi)-1)]))
tis.mi.rets <- diff(log(tis.mi[1:(length(tis.mi)-1)]))
spmib.rets <- diff(log(spmib[1:(length(spmib)-1)]))
x <- cbind(f.mi.rets, eng.mi.rets, tis.mi.rets, spmib.rets)
res <- portfolio.optim(x);
res$pw
x2 <- cbind(f.mi.rets, eng.mi.rets, spmib.rets)
res <- portfolio.optim(x2);
res$pw
--
View this message in context:
http://www.nabble.com/portfolio.optim-and-assets-with-weigth-equals-to-zero...-tp19289567p19289567.html
Sent from the R help mailing list archive at Nabble.com.
Alberto Santini
2008-Sep-03 22:13 UTC
[R] portfolio.optim and assets with weigth equals to zero...
I tried with Rmetrics too, but the behaviour is the same. So I am missing
something...
Very interesting to understand why. :)
I think it's a numerical problem: maybe, zero is not zero, it's very
near to
zero.
Regards,
Alberto Santini
-----------------------------
require(fPortfolio)
require(tseries)
assets <- c(
"F.MI",
"ENG.MI",
"TIS.MI",
"^SPMIB"
)
f.mi <- get.hist.quote("F.MI", start="2006-09-03",
end="2008-09-03",
compression="w", quote="Close")
eng.mi <- get.hist.quote("ENG.MI", start="2006-09-03",
end="2008-09-03",
compression="w", quote="Close")
tis.mi <- get.hist.quote("TIS.MI", start="2006-09-03",
end="2008-09-03",
compression="w", quote="Close")
spmib <- get.hist.quote("^SPMIB", start="2006-09-03",
end="2008-09-03",
compression="w", quote="Close")
X <- cbind(f.mi[1:(length(f.mi)-1)], eng.mi[1:(length(eng.mi)-1)],
tis.mi[1:(length(tis.mi)-1)], spmib[1:(length(spmib)-1)])
colnames(X) <- assets
R <- as.timeSeries(returns(X))
# Spec = portfolioSpec(model = list(type = c("MV"),
# estimator = c("mean", "cov"), tailRisk = list(), params =
list()),
# portfolio = list(weights = NULL, targetReturn = 0,
# targetRisk = 0, targetAlpha = 0.05, riskFreeRate = NULL,
# nFrontierPoints = 50),
# solver = list(solver = c("quadprog"), trace = FALSE))
Spec = portfolioSpec()
frontier <- portfolioFrontier(R, Spec, c("minW[1:nAssets]=0"))
# weightsSlider(frontier)
ptf <-
frontier at portfolio$weights[which.min(getTargetRisk(frontier)[,1])+1,]*100
ptf
assets2 <- c(
"ENG.MI",
"^SPMIB"
)
X2 <- cbind(eng.mi[1:(length(eng.mi)-1)], spmib[1:(length(spmib)-1)])
colnames(X2) <- assets2
R2 <- as.timeSeries(returns(X2))
frontier <- portfolioFrontier(R2, Spec, c("minW[1:nAssets]=0"))
ptf <-
frontier at portfolio$weights[which.min(getTargetRisk(frontier)[,1])+1,]*100
ptf
--
View this message in context:
http://www.nabble.com/portfolio.optim-and-assets-with-weigth-equals-to-zero...-tp19289567p19299309.html
Sent from the R help mailing list archive at Nabble.com.