R
software and of the following R
-Packages.
tseries
copula
PortRisk
install.packages(c("tseries","copula","PortRisk"))
R
-Studio (after installing the base-R
).R
and walks you through its most essential features.R
for Windows looks like:R
is an object-oriented language.R
works fundamentally by the question-and-answer model.mean=0
and sd=1
. Then draw a histogram of the random sample.r<-rnorm(n=1000,mean=0,sd=1)
hist(r,col="red")
Arithmetic expression and Standard Calculation
2+2
## [1] 4
exp(-2)
## [1] 0.1353353
pi
## [1] 3.141593
cos(pi/3)
## [1] 0.5
Assignments
x
, you can enterx<-2
y<-3
x+y
## [1] 5
<-
is known as the assignment operator.Vector
R
handles entire data vector as a single object.Nifty.50 <- c(8114.30, 7965.50, 8033.30, 8002.30, 7929.10 )
Nifty.50
## [1] 8114.3 7965.5 8033.3 8002.3 7929.1
c(...)
is used to define vectors. It combine values into a vector.c(...)
construct is used extensivelyDowload stock price data from Yahoo
library(tseries)
Nifty.50<-get.hist.quote(instrument = "^NSEI"
,start="2016-11-21"
,end="2016-11-25"
,quote="AdjClose"
,provider = "yahoo")
Nifty.50
## AdjClose
## 2016-11-21 7929.1
## 2016-11-22 8002.3
## 2016-11-23 8033.3
## 2016-11-24 7965.5
## 2016-11-25 8114.3
Compute log-return
\[ \begin{eqnarray*} r_t &=& \log(P_t) - \log(P_{t-1})\\ &=& \log\Big(\frac{P_t}{P_{t-1}}\Big) \end{eqnarray*} \] where \(P_t\) is the price of a stock (or value of an index) at time point \(t\).
R
in the following wayln_rt<-diff(log(Nifty.50))
ln_rt
## AdjClose
## 2016-11-22 0.009189427
## 2016-11-23 0.003866402
## 2016-11-24 -0.008475662
## 2016-11-25 0.018508197
Compute simple return
Simple return can be calculated as \[ \begin{eqnarray*} R_t &=&\frac{P_t-P_{t-1}}{P_{t-1}}\\ &=&\frac{P_t}{P_{t-1}}-1\\ &=& e^{r_t}-1, \end{eqnarray*} \] wher \(r_t\) is the log-return.
We can calculate the simple return in R
in the following way
Rt<-exp(ln_rt)-1
Rt
## AdjClose
## 2016-11-22 0.009231780
## 2016-11-23 0.003873886
## 2016-11-24 -0.008439845
## 2016-11-25 0.018680535
Rt*100
## AdjClose
## 2016-11-22 0.9231780
## 2016-11-23 0.3873886
## 2016-11-24 -0.8439845
## 2016-11-25 1.8680535
ln_rt*100
## AdjClose
## 2016-11-22 0.9189427
## 2016-11-23 0.3866402
## 2016-11-24 -0.8475662
## 2016-11-25 1.8508197
K-period Simple Return
\[ \begin{eqnarray*} R_t(k)&=&\frac{P_t-P_{t-k}}{P_{t-k}}=\frac{P_t}{P_{t-k}}-1\\ 1+R_t(k)&=&\frac{P_t}{P_{t-k}}=\Big(\frac{P_t}{P_{t-1}}\Big)\Big(\frac{P_{t-1}}{P_{t-2}}\Big)...\Big(\frac{P_{t-k+1}}{P_{t-k}}\Big)\\ &=& (1+R_t)(1+R_{t-1})...(1+R_{t-k+1}) \end{eqnarray*} \]
K-period log Return
\[ \begin{eqnarray*} r_t(k)&=&\log\{1+R_t(k)\}\\ &=& \log\{(1+R_t)(1+R_{t-1})...(1+R_{t-k+1})\}\\ &=& \log(1+R_t)+\log(1+R_{t-1})+...+\log(1+R_{t-k+1})\}\\ &=& r_t+r_{t-1}+...+r_{t-k+1} \end{eqnarray*} \]
Average Return
mean(ln_rt)
## [1] 0.005772091
Volatility (aka. Standard Deviation)
sd(ln_rt)*100
## [1] 1.126228
sd(ln_rt)*100
is calculated using daily log-return; so calculated volatility is daily volatility in percentage.1.12623%
on a given day.How to compute 30-days Volatility
Suppose \(r_t,r_{t-1},...,r_{t-k+1}\) are \(k\) single period log-return of an asset, where \[ \begin{eqnarray} \mathbb{E}(r_{t-i+1})&=&\mu~~~ \forall i=1,2,...,k\\ \mathbb{V}ar(r_{t-i+1})&=&\sigma^2~~ \forall i\\ \mathbb{C}ov(r_{t-i+1},r_{t-j+1})&=& 0~~~ \forall i\neq j=1,2...,k. \end{eqnarray} \] That is the covariance matrix is \[ \begin{eqnarray} \Sigma=\left(\begin{array}{cccc} \sigma^2 & 0 & ... & 0 \\ 0 & \sigma^2 & ... & 0 \\ \vdots & \vdots & ... & \vdots\\ 0 & 0 & ... & \sigma^2 \end{array}\right)_{k \times k} \end{eqnarray} \]
The \(k\)-period return can be presented in matrix notation as \[ \begin{eqnarray*} r_{t}(k)&=&r_t+r_{t-1}+...+r_{t-k+1}\\ &=&c^T\mathbf{r}, \end{eqnarray*} \] where \(c^T=(1,1,...,1)_{k}\) is an unit vector of order \(k\) and \(\mathbf{r}=\{r_t,r_{t-1},...,r_{t-k+1}\}\). The mean and variance of the \(k\)-period return are \[ \begin{eqnarray*} \mathbb{E}(r_{t}(k))&=& c^T\mathbf{\mu}=k\mu,\\ \mathbb{V}ar(r_{t}(k))&=&c^T\Sigma c =k\sigma^2 \end{eqnarray*} \]
Therefore k-period volatility is \(\sqrt{k}\sigma\).
Monthly volatility of Nifty 50
sqrt(22)*sd(ln_rt)*100
## [1] 5.282477
Yearly volatility of Nifty 50
sqrt(252)*sd(ln_rt)*100
## [1] 17.87831
Why are we using \(k=22\) and \(k=252\) for monthly and yearly volatility of Nifty 50?