i trying use chart.efficientfrontier function in portfolioanalytics package in r chart efficient frontier object have created keeps failing. trying find frontier minimize annaulized standard deviation. once working maximize annualized return.
firstly created annualized standard deviation function using code
pasd <- function(r, weights){ as.numeric(stddev(r=r, weights=weights)*sqrt(12)) # hardcoded monthly data # as.numeric(stddev(r=r, weights=weights)*sqrt(4)) # hardcoded quarterly data }
i imported csv file monthly returns , portfolio object looks this:
> prt ************************************************** portfolioanalytics portfolio specification ************************************************** call: portfolio.spec(assets = colnames(returns)) number of assets: 3 asset names [1] "global reits" "au reits" "au util , infra" constraints enabled constraint types - leverage - long_only objectives: enabled objective names - mean - pasd
now create efficient frontier object using line:
prt.ef <- create.efficientfrontier(r = returns, portfolio = prt, type = "deoptim", match.col = "pasd")
but when try plot getting following error messages.
> chart.efficientfrontier(prt.ef, match.col="pasd") error in stddev(r = r, weights = weights) : argument "weights" missing, no default in addition: there 26 warnings (use warnings() see them) error in stddev(r = r, weights = weights) : argument "weights" missing, no default error in stddev(r = r, weights = weights) : argument "weights" missing, no default error in xlim[2] * 1.15 : non-numeric argument binary operator
anyone know why case? when use summary(prt.ef) can see weights, why chart.efficientfrontier function failing?
as @walts suggested, need consistent in implementing functions annualize mean , risk returns.
but annualized statistics have 2 options, not using any:
1) make optimization monthly data, original risk return functions in specification. plotting can anualize making
port.anua.returns=prt.ef$frontier[,1]*12 port.anua.stdev=prt.ef$frontier[,2]*12^.5
the weights same monthly or annualized portfolios.
prt.ef$frontier[,-(1:3)]
2) transform monthly returns in annualized returns multiplying 12. optimization usual procedure, risk , return annualized in prt.ef$frontier
.
related jagged line in ef. using portfolio specification able recreate same behavior. following plot used edhec
data, specification original mean
, stddev
in objectives:
data(edhec) returns <- edhec[,1:3]
that behavior must influenced specification or optimization algorithm using. did same optimization solve.qp
package quadprog
. result.
update
the code here:
require(quadprog) #min_x(-d^t x + 1/2 b^t d x) r.t a.x>=b mv_qp<-function(nx, tarret, sig=null,long_only=false){ if (is.null(sig)) sig=cov(nx) dvec=rep(0,ncol(sig)) meq=2 amat=rbind(rep(1,ncol(sig)), apply(nx,2,mean) ) bvec=c(1,tarret ) if (long_only) { meq=1 amat=amat[-1,] amat=rbind(amat, diag(1,ncol(sig)), rep(1,ncol(sig)), rep(-1,ncol(sig))) bvec=bvec[-1] bvec=c(bvec, rep(0,ncol(sig)),.98,-1.02) } sol <- solve.qp(dmat=sig, dvec, t(amat), bvec, meq=meq)$solution } steps=50 x=returns µ.b <- apply(x = x, 2, fun = mean) long_only=true range.bl <- seq(from = min(µ.b), = max(µ.b)*ifelse(long_only,1,1.6), length.out = steps) risk.bl <- t(sapply(range.bl, function(targetreturn) { w <- mv_qp(x, targetreturn,long_only=long_only) c(sd(x %*% w),w) })) weigthsl=round(risk.bl[,-1],4) colnames(weigthsl)=colnames(x) weigthsl risk.bl=risk.bl[,1] rets.bl= weigthsl%*%µ.b fan=12 plot(x = risk.bl*fan^.5, y = rets.bl*fan,col=2,pch=21, xlab = "annualized risk ", ylab = "annualized return", main = "long ef solve.qp")
Comments
Post a Comment