May 4, 2016

Shiny R code for multiple plots using ggplot2 and gridextra

Shiny R code for multiple plots using ggplot2 and gridextra

Sample code to use shiny for multiple graphs in same plot
Use ggplot2 and gridextra



Jan 7, 2016

Model free implied volatility

Risk-Neutral Skewness and Kurtosis:

See this post for R code to calculate model free implied volatility.

Let stock price return for period $\tau$ is given by:

\begin{equation}
R(t,\tau) \equiv ln[S(t, \tau)]-ln[S(t)]
\end{equation}

\begin{equation}
H[S]\left\{\begin{matrix}
V(t,\tau) \equiv R(t, \tau)^{2}    \text{              Volatility contract }
 & \\W(t,\tau) \equiv R(t, \tau)^{3} \text{            Cubic contract }
 & \\ X(t,\tau) \equiv R(t, \tau)^{4}\text{       Quadrartic contract }
 &
\end{matrix}\right.
\end{equation}

The price of the volatility contract:
\begin{equation}
V(t,\tau)=\int_{S(t)}^{\infty }\frac{2(1-ln(K/S(t)))}{K^{2}}C(t,\tau;K)dK+\int_{0}^{S(t)}\frac{2(1-ln(K/S(t)))}{K^{2}}P(t,\tau;K)dK
\label{ref1}
\end{equation}

The price of the cubic contract:
\begin{equation}
W(t,\tau)=\int_{S(t)}^{\infty }\frac{6ln[\frac{K}{S(t)}]-3([\frac{K}{S(t)}])^2}{K^{2}}C(t,\tau;K)dK+\int_{0}^{S(t)}\frac{6ln[\frac{K}{S(t)}]+3([\frac{K}{S(t)}])^2}{K^{2}}P(t,\tau;K)dK
\label{ref2}
\end{equation}

The price of the quadratic contract:
\begin{equation}
X(t,\tau)=\int_{S(t)}^{\infty }\frac{12(ln[\frac{K}{S(t)}])^2-4([\frac{K}{S(t)}])^3}{K^{2}}C(t,\tau;K)dK+\int_{0}^{S(t)}\frac{12(ln[\frac{K}{S(t)}])^2-4([\frac{K}{S(t)}])^3}{K^{2}}P(t,\tau;K)dK
\label{ref3}
\end{equation}

Define $\mu(t, \tau)$:
\begin{equation}
\mu(t,\tau) = e^{r\tau}-1-\frac{e^{r\tau}}{2}V(t,\tau)-\frac{e^{r\tau}}{6}W(t,\tau)-\frac{e^{r\tau}}{24}X(t,\tau)
\end{equation}

For $\tau$-period model-free implied volatility (*MFIV*) is:
\begin{equation}
MFIV(t, \tau) = (V(t, \tau))^{1/2}
\end{equation}


For $\tau$-period model free implied skewness(*MFIS*) is:
\begin{equation}
MFIS(t, \tau) = \frac{e^{r\tau}W(t,\tau)-3\mu(t,\tau)e^{r\tau}V(t,\tau)+2(\mu(t,\tau))^3}{(e^{r\tau}V(t,\tau)-(\mu(t,tau))^2)^{3/2}}
\end{equation}

To calculate  the integral in equation (\ref{ref1}), (\ref{ref2}), and (\ref{ref3}), I require continuous option prices from 0 to $\infty$. For simplicity, I set lower limit and upper limit for integrals.

In the calculation, I use lower limit of strike price $K_{L}$ as the minimum strike price for OTM put option with non zero contract size (or trade quantity).

For upper limit for strike price, $K_{U}$, I use the maximum strike price of OTM call option with non zero trading size.

References:
Bakshi, G., Kapadia, N., & Madan, D. (2003). Stock return characteristics, skew laws, and the differential pricing of individual equity options. Review of Financial Studies, 16(1), 101-143.

Jiang, George J., and Yisong S. Tian. "The model-free implied volatility and its information content." Review of Financial Studies 18.4 (2005): 1305-1342

Jan 1, 2016

Volatility smile

Following code downloads option price data for a day $d$ from NSE website and plot volatility smile  :)



Dec 31, 2015

R code to download Options and Futures data from NSE

The code below can download daily bhav for Futures and Options from the NSE website

I have also include sample code that downloads indices level from the National Stock Exchange

Sep 3, 2015

Factor to explain factor

The new fama-french model replaces the existing five-factor model.  The new five-factor fama-french model includes  profitability and investment factors.
Profitability factor: Robust minus weak operating profit (RWA )= (SR + BR) / 2 – (SW + BW) / 2 = [(SR – SW) + ( BR - BW)] / 2 

Investment factor:  Conservative minus aggressive Investment (CMA)  = (SC + BC) / 2 – (SA + BA) / 2 = [(SC – SA) + ( BC -BA)] / 2

S = small ; B = big; R = Robust; W= Weak; C= Conservative; A = Aggressive 

The new model is:

$R_{t}-RF_{t} = \alpha + \beta_{1}[RM_{t}-RF_{t}] + \beta_{2}SMB_{t}  + \beta_{3}HML_{t}  + \beta_{4}RMW_{t}  + \beta_{5}CMA_{t}  + \epsilon_{t}$

$RM_{t}-RF_{t}$ = excess market return
$SMB_{t}$ = the Size factor
$HML_{t}$ = the value factor
$RMW_{t}$ = the profitability factor
$CMA_{t}$ = the investment factor
$RF_{t}$ = risk free rate