Tuesday, October 21, 2014

A Risk Free Asset and the Tangency Portfolio

In many cases, we have a risk free asset ( like a Government Bond ) that offers zero risk and very small return and we would like to introduce the same into our portfolio.

Once again, we will see how we can create this portfolio that consists of a basket of risky assets ( the tangency portfolio) plus the risk free security. In the R program, shown below, we have taken the previous function ( created in an earlier post ) and enhanced the covariance matrix by forcibly adding a row and column of 0s and then further enhancing it with a row of returns that now includes the risk free return of the extra risk free asset.

   setwd("C:/Users/admin/Desktop/Data Analytics/QuantitativeFinance/QFLabs")
India5 <- read.csv("India5.csv")
assets <- India5[,3:7]
histRets <- log(tail(assets, -1) / head(assets, -1))

## modifying the previous function

OptWeightsRF <- function(rets,mu,rf){
  ## we need one more column for riskless assets
  n <- ncol(rets)+1  
  ## we add a column of 0s to the right of covariance matrix
  Q <- cbind(cov(rets), rep(0, n - 1)) 
  ## we add a rows of 0s to the bottom covariance matrix
  Q <- rbind(Q, rep(0, n))
  ## we create a new row consisting of existing mean returns + risk free
  r <- c(colMeans(rets), rf)
  ## the cov matrix is enhanced by adding this extra rwo at bottom
  Q <- rbind(Q, rep(1, n), r)
  ## here the matrix is made into a square matrix
  Q <- cbind(Q, rbind(t(tail(Q, 2)), matrix(0, 2, 2)))
  b <- c(rep(0, n), 1, mu)
  w0 <- solve(Q, b)
  head(w0, -2)



p2 <- portfolio.optim(as.matrix(histRets),pm=0.005,riskless=TRUE,shorts=TRUE,rf=0.0001)
p2 <- portfolio.optim(as.matrix(histRets),riskless=TRUE,shorts=FALSE,rf=0.0001)


on running the program, the output from the OptWeightsRF function is as follows

> OptWeightsRF(histRets,0.005,0.0001)
      Rel      HDFC     Tisco       LnT       HUL        
 4.166645  4.136603  1.622548 -2.219220  3.104350 -9.810924
> sum(OptWeightsRF(histRets,0.005,0.0001))
[1] 1
where we see the weights for the 5 risky securities and that of the risk free security and we note that the sum adds up to 1. However this includes shorts.

When we run the same data through the portfolio.optim ( introduced in an earlier post)  function from the package tseries, we get the identical answer for the risky assets

> p2 <- portfolio.optim(as.matrix(histRets),pm=0.005,riskless=TRUE,shorts=TRUE,rf=0.0001)
> p2$pw
[1]  4.166645  4.136603  1.622548 -2.219220  3.104350
> sum(p2$pw)
[1] 10.81092

since the weight of the risk-free asset is not shown in the output but it can be calculated so that the sum adds up to 1. This means that the weight of the risk free asset should be -9.81 which is what was determined in the OptWeightsRF function.

finally, we run portfolio.optim so that SHORTs are not allowed and the answer is as follows

> p2 <- portfolio.optim(as.matrix(histRets),riskless=TRUE,shorts=FALSE,rf=0.0001)
> p2$pw
[1] 0.25225852 0.23839309 0.08740829 0.00000000 0.22403593
> sum(p2$pw)
[1] 0.8020958
> p2$pm
[1] 0.0003773413

here we note that all weights are positive including the riskfree asset = 1 - 0.802 = 0.198


  1. No doubt this is an excellent post I got a lot of knowledge after reading good luck. Theme of blog is excellent there is almost everything to read, Brilliant post. Fahim Imam-Sadeque