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.

  • $\begingroup$ Isn't the documentation of the package clarifying what is being done inside? $\endgroup$ – SRKX Oct 28 '15 at 9:06
  • $\begingroup$ @SRKX Are you talking about this documentation? link. I didn't find much details here. $\endgroup$ – HasnainMamdani Oct 28 '15 at 19:19
  • $\begingroup$ 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... $\endgroup$ – SRKX Oct 29 '15 at 1:11

You can use the package quadprog and define everything yourself.

Code can look like this:

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.

  • 1
    $\begingroup$ why not add to this to your question? $\endgroup$ – Gordon Oct 28 '15 at 20:07
  • $\begingroup$ 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. $\endgroup$ – SRKX Oct 29 '15 at 1:13
  • $\begingroup$ I edited my answer. Apparently you can enter the covariance but not the expected values. $\endgroup$ – Richard Oct 29 '15 at 15:03
  • $\begingroup$ 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. $\endgroup$ – HasnainMamdani Oct 30 '15 at 4:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.