NIFTY industry indexes is used for our exercise. Data for BANK,ENERGY,FMCG,IT & REALTY indexes has been downloaded from http://www.nseindia.com/content/indices/ind_histvalues.htm, for 1st April 2010 to1st April 2011.
Save all the raw data files into a directory and name according to your convenience (Risk Blog). Next step is to convert required data into vector form and import to R Language. For that purpose we use following commands on terminal
cd /home/sidharth/Risk\ Blog/
R
This will open R interface, Read all the csv data and copy closing price row to bank using following set of commands.
>banktemp<- read.csv("BANK NIFTY01-04-2010-01-04-2011.csv")
>Bank <- banktemp[[2]]
For the calculation of rate of return and standard deviation, we need daily return data. I will find out arithmetic and log normal return by writing following functions.
Function for arthimetic return
bankr ← function(first,second)
{
a= first
a[1]=0
k=length(second)
i=2
while(i<=k) {
a[i]= (second[i]-first[i-1])/first[i-1]
i=i+1
}
a
}
Function for lognormal return
bankl <- function(first,second)
{
a= first
a[1]=0
k=length(second)
i=2
while(i<=k) {
a[i]= log(second[i]/first[i-1])
i=i+1
}
a
}
Call above two functions by passing bank vector as argument, it will return a vector of daily rate of return data . Use following commands
>bankR <- bankr(bank,bank)
>BankL <- bankl(bank,bank)
Plot graph of normal returns using hist(bankR) similarly plot for log normal returns can be ploted using hist(bankL)
Now it is time to calcuate mean return and vairance for the bank index of nifty, this can be done suing following commands
>bankrm <- mean(bankR)
>banklm <- mean(bankL)
Lognormal return is nothing but geometric return and it is better measure of return so we will be using this. Variance is
>bankRsd <- sd(bankR)
>bankLsd <- sd(bankL)
Same set of commands are repeated for energy sector to capture and calculate required figures.
>energytemp<- read.csv("CNX ENERGY01-04-2010-01-04-2011.csv")
>energy<- energytemp[[5]]
Let us calculate correlation, mean and standardization of two assets. Following commands will do this
>corr <- cor(bankL,energyL)
>energyM<- mean(energyL)
>energySD<- sd(energyL)
>bankM <- mean(bankL)
>bankSD<- sd(bankL)
Next step is to use different combination of portfolios weights, and find out corresponding returns and variance of returns.
> w1 <- seq (0,1,by=.05)
>w2<-1-w1
I have defined following two functions to find out return vector and variance vector for all possible combination of weights.
Return function
function (first,second,w1,w2)
{
a= w1
k=length(w1)
i=1
while(i<=k) {
a[i]= second*w2[i]+first*w1[i]
i=i+1
}
a
}
Function to find Standard Deviation
function (first,second,corr,w1,w2)
{
a= w1
k=length(w1)
i=1
while(i<=k) {
a[i]= (second*w2[i])^2+(first*w1[i])^2+2*w1[i]*w2[i]*first*second*corr
i=i+1
}
a
}
Call both the functions using following commands
>X <- cal_ret(bankM,energyM)
>Y <- cal_sd(bankSD,energySD,corr,w1,w2)
Plot (Y,X) will give following graph It shows by changing weights assigned to two assets , one can earn higher amount of returns by having same amount of risk. It is in line with a capital asset pricing market theory. Most left point having return of 4e-04 is most efficient portfolios, all points right above this points are part of efficient frontier.
After finding out risk appetite of investor , we can use this graph to make appropriate allocation to both the assets.
Using Risk free Asset
Let us repeat same exercise by exchanging one asset with risk free asset (Govt. India Securities) and other to be bank index. Following commands do the required calculations and following graphs shows the relation.
>X <- cal_ret(bankM,Rf,w1,w2)
>RfSD <- 0
> Y <- cal_sd(bankSD,RfSD,0,w1,w2)
>plot(Y,X)
Graph start from risk free rate of return and is straight line, which means risk will decrease by sacrificing returns and one can earn extra returns by leveraging on the portfolio.
No comments:
Post a Comment