[R-SIG-Finance] Efficient Portfolio

AIE ATUMA gttga2000 at yahoo.com
Tue Jun 9 17:26:55 CEST 2015


Dear All,
I need help. Below is my 17 Assets portfolio code and the error message and I recieve the following error; 
Error in optimize(f = optim.callback, interval = c(ef$ret[max.sh - 1],  :   invalid 'xmin' valueIn addition: Warning messages:1: In min(interval) : no non-missing arguments to min; returning Inf2: In max(interval) : no non-missing arguments to max; returning -Inf
How can I resolve this error?

Attached also ie the data used.
return <- read.csv(file.choose(),header=T)head(return)exReturn <- read.csv(file.choose(),header=T)head(exReturn)exReturn <- exReturn[,-1]head(exReturn)
#exReturn <- exReturn[,-5]head(exReturn)Mean=sapply(exReturn,mean)SD=sapply(exReturn,sd)MSD=cbind(Mean,SD)MSDMSD=write.csv(MSD,"1.MSD.csv")EXCOV=cov(exReturn)head(EXCOV)muhat.vals = colMeans(exReturn)muhat.valsstd.vals=sapply(exReturn,sd)std.valscov=EXCOVaveret = as.matrix(exReturn)averetrcov = var(exReturn)rcovrcov=write.csv(rcov,"1rcov.csv")
ret.mat = as.matrix(averet)  ret.matstd.vals=sapply(averet,sd)muhat.vals = colMeans(averet)muhat.valscov.mat = var(averet)  cov.matbeta.vals = cov.mat[,1]/cov.mat[1,1]       alpha.vals = muhat.vals[-1] - beta.vals[-1]*muhat.vals["INDEX"]             beta.vals alpha.vals beta = rcov[,1]/rcov[1,1]betaalpha.vals = muhat.vals[-1] - beta[-1]*muhat.vals["INDEX"]alpha.valsexReturn <- exReturn[,-1]averet=matrix(c(-0.012651225,  -0.033357407, -0.028047833, -0.025091085, -0.023927971, -0.027173414, -0.009168244, -0.042440445, -0.033081353, 0.029539516, -0.032630073, -0.032380113, -0.015888686, 0.011734781, -0.029664476, -0.026563804, -0.025612785,nrow=1))rcov=matrix(c(0.021619201,  0.002879315, 0.006796186, 0.008391409, 0.007303084, 0.009429538, 0.00573083, 0.010721487, 0.008871943, 0.014952162, -0.003564898, 0.007847258, 0.007609481, 0.002344691, 0.004787781, 0.010340954, 0.004500699,              0.002879315, 0.014408062, 0.002060355, 0.005357955, 0.004425948, 0.006323749, 0.003004733, 0.007581034, 0.0033889, 0.006163729, 0.00762899, 0.006048443, 0.006755555, 0.006579867, 0.001538254, 0.00535515, 0.00423201,              0.006796186, 0.002060355, 0.015059936, 0.00891385, 0.005257287, 0.008325, 0.003395422, 0.009127993, 0.003691545, 0.010585709, 0.001536639, 0.007490256, 0.006343576, 0.008617788, 0.004600133, 0.006585048, 0.001828033,              0.008391409, 0.005357955, 0.00891385, 0.02791027, 0.013095152, 0.016164161, 0.008256585, 0.012325478, 0.007432512, 0.014322006, 0.005322147, 0.017576018, 0.015498928, 0.015339811, 0.002455833, 0.008407568, 0.00653364,              0.007303084, 0.004425948, 0.005257287, 0.013095152, 0.013055333, 0.011190138, 0.006119818, 0.010232633, 0.007234362, 0.015678921, 0.004607435, 0.010380325, 0.010131716, 0.016960135, 0.002122254, 0.006599095, 0.005009183,              0.009429538, 0.006323749, 0.008325, 0.016164161, 0.011190138, 0.021004977, 0.006835194, 0.015059864, 0.010132806, 0.013964084, 0.004975523, 0.012950363, 0.013898241, 0.015876255, 0.002220538, 0.005983095, 0.006013043,              0.00573083, 0.003004733, 0.003395422, 0.008256585, 0.006119818, 0.006835194, 0.01019401, 0.005233176, 0.002164439, 0.009700644, 0.003439956, 0.009572046, 0.00745657, 0.0038553, 0.00141304, 0.00217913, 0.002879114,              0.010721487, 0.007581034, 0.009127993, 0.012325478, 0.010232633, 0.015059864, 0.005233176, 0.026000738, 0.010254279, 0.02294916, 0.003796425, 0.010711662, 0.012234193, 0.016874167, 0.003985803, 0.010581223, 0.003974903,              0.008871943, 0.0033889, 0.003691545, 0.007432512, 0.007234362, 0.010132806, 0.002164439, 0.010254279, 0.015017643, 0.014692357, -0.000736742, 0.004488725, 0.00821531, 0.009904755, 0.002407663, 0.006487495, 0.001534648,              0.014952162, 0.006163729, 0.010585709, 0.014322006, 0.015678921, 0.013964084, 0.009700644, 0.02294916, 0.014692357, 0.140827011, -0.001987559, 0.013425049, 0.017874043, 0.104137758, 0.023654807, 0.028920937, 0.0193293,              -0.003564898, 0.00762899, 0.001536639, 0.005322147, 0.004607435, 0.004975523, 0.003439956, 0.003796425, -0.000736742, -0.001987559, 0.038080975, 0.006497129, 0.002908321, 0.007966071, 0.000112123, 0.008822285, -0.0000721,              0.007847258, 0.006048443, 0.007490256, 0.017576018, 0.010380325, 0.012950363, 0.009572046, 0.010711662, 0.004488725, 0.013425049, 0.006497129, 0.026641993, 0.015017809, 0.01686881, 0.00494474, 0.0038542, 0.007596542,              0.007609481, 0.006755555, 0.006343576, 0.015498928, 0.010131716, 0.013898241, 0.00745657, 0.012234193, 0.00821531, 0.017874043, 0.002908321, 0.015017809, 0.019603086, 0.013108311, 0.003248666, 0.000507636, 0.004733304,              0.002344691, 0.006579867, 0.008617788, 0.015339811, 0.016960135, 0.015876255, 0.0038553, 0.016874167, 0.009904755, 0.104137758, 0.007966071, 0.01686881, 0.013108311, 0.186943324, 0.023100834, 0.028316641, 0.025079888,              0.004787781, 0.001538254, 0.004600133, 0.002455833, 0.002122254, 0.002220538, 0.00141304, 0.003985803, 0.002407663, 0.023654807, 0.000112123, 0.00494474, 0.003248666, 0.023100834, 0.021991544, 0.009805182, 0.010515751,              0.0103409542, 0.00535515, 0.006585048, 0.008407568, 0.006599095, 0.005983095, 0.00217913, 0.010581223, 0.006487495, 0.028920937, 0.008822285, 0.0038542, 0.000507636, 0.028316641, 0.009805182, 0.051615239, 0.003343432,              0.004500699, 0.00423201, 0.001828033, 0.00653364, 0.005009183, 0.006013043, 0.002879114, 0.003974903, 0.001534648, 0.0193293, -0.0000721, 0.007596542, 0.004733304, 0.025079888, 0.010515751, 0.003343432, 0.026693819),nrow=17,ncol=17)
library(tseries)
effFrontier = function (averet, cov.mat, nports = 10, shorts=T, wmax=1){  mxret = max(abs(averet))  mnret = -mxret  n.assets = ncol(averet)  reshigh = rep(wmax,n.assets)  if( shorts )  {    reslow = rep(-wmax,n.assets)  } else {    reslow = rep(0,n.assets)  }  min.rets = seq(mnret, mxret, len = nports)  vol = rep(NA, nports)  ret = rep(NA, nports)  for (k in 1:nports)  {    port.sol = NULL    try(port.sol <- portfolio.optim(x=averet, pm=min.rets[k], covmat=cov.mat,                                    reshigh=reshigh, reslow=reslow,shorts=shorts),silent=T)    if ( !is.null(port.sol) )    {      vol[k] = sqrt(as.vector(port.sol$pw %*%cov.mat %*% port.sol$pw))      ret[k] = averet %*% port.sol$pw    }  }  return(list(vol = vol, ret = ret))}
maxSharpe = function (averet,cov.mat, shorts=T, wmax = 1){  optim.callback = function(param,averet,cov.mat,reshigh,reslow,shorts)  {    port.sol = NULL    try(port.sol <- portfolio.optim(x=averet, pm=param, covmat=cov.mat,                                    reshigh=reshigh, reslow=reslow, shorts=shorts), silent = T)    if (is.null(port.sol)) {      ratio = 10^9    } else {      m.return = averet %*% port.sol$pw      m.risk = sqrt(as.vector(port.sol$pw %*% cov.mat %*% port.sol$pw))      ratio = -m.return/m.risk      assign("w",port.sol$pw,inherits=T)    }    return(ratio)  }  ef = effFrontier(averet=averet, cov.mat=cov.mat, shorts=shorts, wmax=wmax, nports = 10)  n = ncol(averet)  reshigh = rep(wmax,n)  if( shorts ) {    reslow = -reshigh  } else {    reslow = rep(0,n)  }  max.sh = which.max(ef$ret/ef$vol)  w = rep(0,ncol(averet))  xmin = optimize(f=optim.callback, interval=c(ef$ret[max.sh-1], upper=ef$ret[max.sh+1]),                  averet=averet,rcov=cov.mat,reshigh=reshigh,reslow=reslow,shorts=shorts)  return(w)  }


weights <- maxSharpe(averet,cov.mat,shorts=F)
weights

 Thank You and Best Regards,

Emeka .I. A
Integrity is work your talk don't talk your work
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20150609/0a9211c4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1MPP.csv
Type: application/vnd.ms-excel
Size: 18917 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20150609/0a9211c4/attachment.xlb>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1ppmexret.csv
Type: application/vnd.ms-excel
Size: 20994 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20150609/0a9211c4/attachment-0001.xlb>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 1.1rcov.csv
Type: application/vnd.ms-excel
Size: 9082 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20150609/0a9211c4/attachment-0002.xlb>


More information about the R-SIG-Finance mailing list