# Modern Portfolio Optimization Theory: The idea

**We think therefore we R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Suppose you are in a universe of only risky assets, i.e all assets that have a positive sigma. Then what Harry Markowitz did, with mathematics, was to compute the set of Expected returns that you could achieve with minimum risk (sigma). To illustrate this result I took stock returns for these 7 random companies for about 251 days, computed their respective expected returns, and the variance-co-variance matrix of the returns. With this I have all the necessary fodder to compute the E(r) and sigma for all the possible combination of weights that I can assign between these securities, meaning all possible different combinations of parking my money among these securities.

# Map your working directory using setwd()

# Read the relevant file

r1 <- read.csv(“Markowitz.csv”)

mu = colMeans(r1[,-7]) ## Calculate the column means and storing in “mu”, I have removed the 7th column from the data frame as I have a risk free asset’s return there, I will come back to this later in the post.

bigsig = cov(r1[,-7]) ## Variance-co-variance matrix

m = nrow(bigsig)-1

w = diff(c(0,sort(runif(m)), 1)); ## Assigning random weights between 0-1 to “w” which will have the dimension = no. of securities between which you have to divide your investment.

rb = sum(w*mu); ## Creating matrix “rb” which stores the E(r)

sb = sum(w*bigsig*w); ## Creating matrix “sb” which stores sigma’s

N = 2000 ## Number of different combinations of “w” you want to look at

## Simulating the different combinations of weights “w’s” and storing the E(r) and sigma^2

With only Risky assets |

I did this simulation for 200000 different values of weights too, for those who are curious the plot can be obtained from here.

If we close the envelope of the above mentioned points with a smooth curve (from the left hand side) we get the efficient portfolio frontier (EFF). So what the graph suggests is that one would want to choose the weights (allocations) that lie, more specifically, on the above half of the curve. It makes intuitive sense too, why would one take on more risk to get a lesser return (which is implied by the lower half of the curve). Also another observation that we can make from the above plot is that for every additional unit of returns we **will have to take on an increasingly higher risk** (illustrated by the upper half of the plot). This risk-return trade off graph changes its characteristics when a risk free assets comes into the picture. Lets see what happens is we incorporate the risk free asset in our data frame. Note that a risk free assets could be thought of as a government security, whose risk (sigma) is 0, hence even its co-variances with the other risky assets would be 0. The only addition to the above codes would be to replace r1[,-7] with just r1 that is include the risk free asset too.

With risk-free asset |

Simulation with N = 200000 here.

Look what happened to our risk-return trade off relationship, its linear!! So its a straight line instead of a smooth curve. Meaning for every additional unit of return you will **have to take a fixed additional amount of risk**, which will be given by the slope of the above half of the graph. This result, which sounds to be a trivial finding, can be extended to the entire universe of assets and one can compute the optimal portfolio that will give you a desired rate of return with minimum sigma. However the implementation of this methodology in the actual world is the real challenge. Computation of E(r), sigma, getting the up to date data for all the securities or assets in the universe are some of the challenges that one would face. But nevertheless it is inspiring to see how Harry Markowitz stuck to his idea and triumphed in the end with this path breaking research that shaped the financial advisory industry as we see it now.

P.S In case you want to replicate the exercise the data can be downloaded from here.

**leave a comment**for the author, please follow the link and comment on their blog:

**We think therefore we R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.