[R-SIG-Finance] [R-sig-finance] portfolio.optim and assets with weigth equals to zero...

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 5 01:42:14 CEST 2008


Your question has already been answered but note that the code
can be simplified somewhat giving the following (which is not
exactly the same as yours but may be what you meant):

library(tseries)
qq <- do.call(cbind, sapply(sym, get.hist.quote, start = "2006-09-03",
	compression = "w", quote = "Close", simplify = FALSE))
res <- portfolio.optim(na.omit(diff(log(qq))))
res$pw


On Thu, Sep 4, 2008 at 12:57 PM, Alberto Santini
<albertosantini at gmail.com> wrote:
>
> After posting  on R-Help it makes sense posting on r-sig-finance too.
>
> 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
>
> ----------------------------------------
>
>
> 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
>
> -----------------------------
>
> The asset allocation (in Rmetrics):
>
>
>    F.MI   ENG.MI   TIS.MI   ^SPMIB
>  0.00000 24.30756  0.00000 75.69244
>
>  ENG.MI   ^SPMIB
> 24.48980 75.51020
>
>
>> cov(R)
>
>               F.MI       ENG.MI       TIS.MI       ^SPMIB
> F.MI   0.0029905040 0.0008974621 0.0010289887 0.0009954717
> ENG.MI 0.0008974621 0.0014935276 0.0004278133 0.0002809398
> TIS.MI 0.0010289887 0.0004278133 0.0052708621 0.0007186357
> ^SPMIB 0.0009954717 0.0002809398 0.0007186357 0.0005617209
>> cov(R2)
>             ENG.MI       ^SPMIB
> ENG.MI 0.0014197889 0.0002578205
> ^SPMIB 0.0002578205 0.0005791276
>
> I would think that if a weight is within epsilon of zero, then removing it
> from the computation should have minimal impact on the result.
>
>> eigen(cov(R))
> $values
> [1] 0.0060129474 0.0030233942 0.0010997423 0.0001805307
>
> $vectors
>           [,1]       [,2]        [,3]        [,4]
> [1,] -0.4145084  0.7702681  0.36304798 -0.32103879
> [2,] -0.1770771  0.3541481 -0.91735850  0.04094150
> [3,] -0.8700674 -0.4871272 -0.02330958 -0.07173836
> [4,] -0.1995222  0.2096978  0.16157418  0.94345721
>
>> eigen(cov(R2))
> $values
> [1] 0.0014925599 0.0005063566
>
> $vectors
>           [,1]       [,2]
> [1,] -0.9623985  0.2716415
> [2,] -0.2716415 -0.9623985
>
> The difference of the asset allocation is small. It is not so for
> portfolio.optim. I will investigate the difference between Rmetric and
> tseries, because in my context the solver is the same (quadprog).
>
> Maybe the measure of the risk used in Rmetrics is not the variance as used
> in tseries and it is less sensitive. In fact, the two asset allocations are
> different.
>
> I have short constraints and the weights don't change if I change the order
> of the assets.
>
>
> Thanks in advance,
> Alberto Santini
> --
> View this message in context: http://www.nabble.com/portfolio.optim-and-assets-with-weigth-equals-to-zero...-tp19315076p19315076.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>



More information about the R-SIG-Finance mailing list