Portfolio Optimization – I

What is meant by portfolio optimization?

Portfolio means grouping of financial assets like Bonds, stocks etc. Portfolio optimization is the process of choosing the proportions of various assets to be held in a portfolio so that with min risk one can get maximum return.

Suppose we want to make a portfolio consisting of 5 tyre companies MRF, JK TYRES, APOLLO, Goodyear, CEAT for which we have already extracted the stock prices.

R Program to do:

  1. Read the csv file:

  2. Select the data for the 5 companies in columns 3 to 7:
  3. Calculate the return:
  4. Define a function to calculate the optimum portfolio weights:

    OptWeights <- function(return, mu = 0.005) {
    Q <- rbind(cov(return), rep(1, ncol(assets)), colMeans(return))
    Q <- cbind(Q, rbind(t(tail(Q, 2)), matrix(0, 2, 2)))
    b <- c(rep(0, ncol(assets)), 1, mu)
    head(solve(Q, b),-2)}
  5. Call the function with data and note that the weights should add up to 1:
  6. Define a function to create the graph of the Efficient frontier:

    frontier <- function(return,minRet,maxRet){
    Q <- cov(return)
    n <- ncol(assets)
    r <- colMeans(return)
    Q1 <- rbind(Q,rep(1,n),r)
    Q1 <- cbind(Q1,rbind(t(tail(Q1,2)),matrix(0,2,2)))
    rbase <- seq(minRet,maxRet,length=100)
    s <- sapply(rbase,function(x){
    y <- head(solve(Q1,c(rep(0,n),1,x)),n)
    y %*% Q %*% y})
    plot(s,rbase,xlab=”Variance”,ylab=”Return”, main = “Custom”)}
  7. Call the function with the data and plot the frontier between two values of returns:

  8. Weights for some stocks are -ve which means that the portfolio allows “short” positions.

What is “short” position

Expectation is that the asset will fall in value.

Share the joy

Leave a Reply

Notify of