# How to perform portfolio optimization with user-defined expected return and variances using R?

I found some functions for Markowitz mean variance portfolio optimization in R such as portfolio.optim in tseries package.

However, I was not able to figure out how to use this function if I want to use my own calculated expected mean/return and variance.

Any ideas on how to achieve that with this or any other function?

And does portfolio.optim simply calculate the expected return as the mean of return series and expected volatility/risk as the standard-deviation of the return series? I cannot find it's detailed implementation in the documentation.

• Isn't the documentation of the package clarifying what is being done inside? – SRKX Oct 28 '15 at 9:06
• Ok, indeed the package documentation does not explain how expected returns are computed, which means they estimate it from the historical time series you provide as input somehow. You should have mentioned this in your question it gives your more credibility I think. I can see you have found a way around the problem at the end... – SRKX Oct 29 '15 at 1:11

You can use the package quadprog and define everything yourself.

Code can look like this:

library(quadprog)
Sigma = cov(data)
mu = mean(data)
Amat_in # define constraints here
bvec_in # define rhs of constraints here
solve.QP( Dmat = 2*Sigma, dvec = mu, meq=0,Amat=Amat_in,bvec=bvec_in)


EDIT: Yes, and reading the documentation we see that

portfolio.optim(x, pm = mean(x), riskless = FALSE,
shorts = FALSE, rf = 0.0, reslow = NULL, reshigh = NULL,
covmat = cov(x), ...)


the argument covmat can be set. As it seems the single assets' expected return can not be set as pm is the desired portfolio return. The documentation says that solve.QP is used.

Just came up with the thought that if I supply my expected return vector instead of entire return series matrix to portfolio.optim, and also provide my own covariance matrix using argument covmat=.., then this might work.

• why not add to this to your question? – Gordon Oct 28 '15 at 20:07
• That's ok if he realized this afterwards it seems an honest attempt to answer your own question after a bit of advice. Please make sure the what you "expect" is the case and that's fine as far as I'm concerned. – SRKX Oct 29 '15 at 1:13
• I edited my answer. Apparently you can enter the covariance but not the expected values. – Ric Oct 29 '15 at 15:03
• Will this technique of entering expected return values - providing expected return vector instead of return series matrix to portfolio.optim - work? I didn't find in the documentation that how do they find expected return from return series matrix. – HasnainMamdani Oct 30 '15 at 4:39