Local Volatility Examples - Maple Help

Home : Support : Online Help : Mathematics : Finance : Financial Instruments : Local Volatility Examples

Compute Local Volatility and Implied Volatility Using the Finance Package

Fitting Implied Volatility Surface

 > $\mathrm{restart}:$
 > $\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{with}\left(\mathrm{LinearAlgebra}\right):$

First let us import prices of  S&P 500 call options available on October 27, 2006.

 > $\mathrm{Data}:=\mathrm{ImportMatrix}\left(\mathrm{FileTools}:-\mathrm{JoinPath}\left(\left[\mathrm{kernelopts}\left(\mathrm{datadir}\right),"finance","SP-500-CALLS.txt"\right]\right)\right)$
 ${\mathrm{Data}}{≔}\left[\begin{array}{c}{\mathrm{309 x 10}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (1.1)

This data can be stored in a DataFrame.

 > $\mathrm{DF}≔\mathrm{DataFrame}\left(\mathrm{Data}\left[2..,..\right],\mathrm{columns}=\mathrm{Data}\left[1,..\right]\right)$
 ${\mathrm{DF}}{≔}\left[\begin{array}{cccccccccc}{}& {"Month"}& {"Year"}& {"Strike"}& {"Symbol"}& {"Last"}& {"Change"}& {"Bid"}& {"Ask"}& {\mathrm{...}}\\ {1}& {"November"}& {2006}& {800.00000}& {"SPXKT"}& {578.00000}& {-}{7.40000}& {578.00000}& {580.00000}& {\mathrm{...}}\\ {2}& {"November"}& {2006}& {850.00000}& {"SPXKJ"}& {527.90000}& {-}{7.70000}& {527.90000}& {529.90000}& {\mathrm{...}}\\ {3}& {"November"}& {2006}& {900.00000}& {"SXBKT"}& {478.80000}& {10.80000}& {478.00000}& {480.00000}& {\mathrm{...}}\\ {4}& {"November"}& {2006}& {950.00000}& {"SXBKJ"}& {4.60000}& {-}{2.90000}& {428.50000}& {430.50000}& {\mathrm{...}}\\ {5}& {"November"}& {2006}& {1000.00000}& {"SPQKT"}& {378.70000}& {-}{7.40000}& {378.70000}& {380.70000}& {\mathrm{...}}\\ {6}& {"November"}& {2006}& {1025.00000}& {"SPQKE"}& {353.70000}& {-}{7.50000}& {353.70000}& {355.70000}& {\mathrm{...}}\\ {7}& {"November"}& {2006}& {1050.00000}& {"SPQKJ"}& {0.25000}& {-}{0.20000}& {328.80000}& {330.80000}& {\mathrm{...}}\\ {8}& {"November"}& {2006}& {1075.00000}& {"SPQKO"}& {303.90000}& {-}{7.50000}& {303.90000}& {305.90000}& {\mathrm{...}}\\ {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}\end{array}\right]$ (1.2)

Extract data from this DataFrame.

 > $M,N{≔}_{}\mathrm{upperbound}\left(\mathrm{DF}\right)$
 ${M}{,}{N}{≔}{308}{,}{10}$ (1.3)

Value of the underlying, risk-free rate and dividend yield.

 > $\mathrm{S0}:=1377$
 ${\mathrm{S0}}{≔}{1377}$ (1.4)
 > $r:=0.0532$
 ${r}{≔}{0.05320}$ (1.5)
 > $d:=0.0182$
 ${d}{≔}{0.01820}$ (1.6)

Extract exercise dates and times for which data is available.

 > $\mathrm{ExerciseDates}≔\left[\mathrm{seq}\left(\mathrm{NthWeekday}\left(3,\mathrm{Friday},\mathrm{DF}\left[i,"Month"\right],\mathrm{DF}\left[i,"Year"\right]\right),i=1..M\right)\right]:$
 > $\mathrm{Times}:=\mathrm{map}\left(t\to \mathrm{YearFraction}\left("October 27, 2006",t\right),\mathrm{ExerciseDates}\right):$



Implied volatilities for options maturing in December 2006.

 >
 ${{\mathrm{Time}}}_{{1}}{≔}{0.13425}$ (1.7)

Extract a subset of the DataFrame corresponding to the observations in December 2006:

 >
 ${\mathrm{Dec2006}}{≔}\left[\begin{array}{cccccccccc}{}& {"Month"}& {"Year"}& {"Strike"}& {"Symbol"}& {"Last"}& {"Change"}& {"Bid"}& {"Ask"}& {\mathrm{...}}\\ {79}& {"December"}& {2006}& {600.00000}& {"SYGLT"}& {788.00000}& {19.20000}& {777.90000}& {779.90000}& {\mathrm{...}}\\ {80}& {"December"}& {2006}& {700.00000}& {"SPZLT"}& {633.00000}& {23.10000}& {678.60000}& {680.60000}& {\mathrm{...}}\\ {81}& {"December"}& {2006}& {750.00000}& {"SPZLJ"}& {629.00000}& {-}{7.40000}& {629.00000}& {631.00000}& {\mathrm{...}}\\ {82}& {"December"}& {2006}& {800.00000}& {"SPXLT"}& {480.00000}& {-}{2.00000}& {579.30000}& {581.30000}& {\mathrm{...}}\\ {83}& {"December"}& {2006}& {850.00000}& {"SPXLJ"}& {483.00000}& {0.00000}& {529.70000}& {531.70000}& {\mathrm{...}}\\ {84}& {"December"}& {2006}& {900.00000}& {"SXBLT"}& {429.00000}& {75.00000}& {480.10000}& {482.10000}& {\mathrm{...}}\\ {85}& {"December"}& {2006}& {925.00000}& {"SXBLE"}& {398.50000}& {0.00000}& {455.30000}& {457.30000}& {\mathrm{...}}\\ {86}& {"December"}& {2006}& {950.00000}& {"SXBLJ"}& {308.50000}& {0.00000}& {430.40000}& {432.40000}& {\mathrm{...}}\\ {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}\end{array}\right]$ (1.8)

Compute the implied volatility for these dates and plot the results:

 >
 ${\mathrm{L1}}{≔}\left[\begin{array}{c}{\mathrm{72 x 2}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (1.9)
 > $\mathrm{dataplot}\left(\mathrm{L1}\left[..,1\right],\mathrm{L1}\left[..,2\right],\mathrm{gridlines},\mathrm{size}=\left[600,"golden"\right]\right)$

Implied volatilities for options maturing in December 2007.

 > ${\mathrm{Time}}_{2}≔\mathrm{YearFraction}\left("October 27, 2006",\mathrm{NthWeekday}\left(3,\mathrm{Friday},\mathrm{December},2007\right)\right)$
 ${{\mathrm{Time}}}_{{2}}{≔}{1.15068}$ (1.10)

Extract a subset of the DataFrame corresponding to the observations in December 2007:

 >
 ${\mathrm{Dec2007}}{≔}\left[\begin{array}{cccccccccc}{}& {"Month"}& {"Year"}& {"Strike"}& {"Symbol"}& {"Last"}& {"Change"}& {"Bid"}& {"Ask"}& {\mathrm{...}}\\ {256}& {"December"}& {2007}& {900.00000}& {"SZJLT"}& {25.00000}& {-}{1.50000}& {500.20000}& {503.20000}& {\mathrm{...}}\\ {257}& {"December"}& {2007}& {950.00000}& {"SZJLW"}& {128.50000}& {4.90000}& {454.20000}& {457.20000}& {\mathrm{...}}\\ {258}& {"December"}& {2007}& {1000.00000}& {"SZTLR"}& {330.00000}& {0.00000}& {409.90000}& {412.90000}& {\mathrm{...}}\\ {259}& {"December"}& {2007}& {1050.00000}& {"SZTLJ"}& {251.00000}& {0.00000}& {365.00000}& {368.00000}& {\mathrm{...}}\\ {260}& {"December"}& {2007}& {1075.00000}& {"SZTLX"}& {342.80000}& {-}{0.50000}& {342.80000}& {345.80000}& {\mathrm{...}}\\ {261}& {"December"}& {2007}& {1100.00000}& {"SZTLT"}& {224.50000}& {-}{22.00000}& {320.90000}& {323.90000}& {\mathrm{...}}\\ {262}& {"December"}& {2007}& {1125.00000}& {"SZTLC"}& {227.00000}& {24.50000}& {297.90000}& {300.90000}& {\mathrm{...}}\\ {263}& {"December"}& {2007}& {1150.00000}& {"SZTLY"}& {260.00000}& {4.00000}& {277.80000}& {280.80000}& {\mathrm{...}}\\ {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}& {\mathrm{...}}\end{array}\right]$ (1.11)

Compute the implied volatility for these dates and plot the results:

 >
 ${\mathrm{L2}}{≔}\left[\begin{array}{c}{\mathrm{24 x 2}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{{\mathrm{float}}}_{{8}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (1.12)
 > $\mathrm{dataplot}\left(\mathrm{L2}\left[..,1\right],\mathrm{L2}\left[..,2\right],\mathrm{gridlines},\mathrm{size}=\left[600,"golden"\right]\right)$

We will use the following model for the volatility surface.

 > $\mathrm{Σ}:=\left(S,T,K\right)→{\mathrm{α}}_{1}+{\mathrm{α}}_{2}\mathrm{ln}\left(\frac{K}{S}\right)+\frac{{\mathrm{α}}_{3}}{\sqrt{T}}+{\mathrm{α}}_{4}{\mathrm{ln}\left(\frac{K}{S}\right)}^{2}+\frac{{\mathrm{α}}_{5}\mathrm{ln}\left(\frac{K}{S}\right)}{\sqrt{T}}+\frac{{\mathrm{α}}_{6}{\mathrm{ln}\left(\frac{K}{S}\right)}^{2}}{\sqrt{T}}$
 ${\mathrm{Σ}}{≔}\left({S}{,}{T}{,}{K}\right){→}{{\mathrm{α}}}_{{1}}{+}{{\mathrm{α}}}_{{2}}{}{\mathrm{ln}}{}\left(\frac{{K}}{{S}}\right){+}\frac{{{\mathrm{α}}}_{{3}}}{\sqrt{{T}}}{+}{{\mathrm{α}}}_{{4}}{}{{\mathrm{ln}}{}\left(\frac{{K}}{{S}}\right)}^{{2}}{+}\frac{{{\mathrm{α}}}_{{5}}{}{\mathrm{ln}}{}\left(\frac{{K}}{{S}}\right)}{\sqrt{{T}}}{+}\frac{{{\mathrm{α}}}_{{6}}{}{{\mathrm{ln}}{}\left(\frac{{K}}{{S}}\right)}^{{2}}}{\sqrt{{T}}}$ (1.13)

We can compute the corresponding Black-Scholes price as a function of strike and maturity.

 > $C:=\mathrm{BlackScholesPrice}\left(\mathrm{S0},K,T,\mathrm{Σ}\left(\mathrm{S0},T,K\right),r,d\right)$
 ${C}{≔}{1377.00000}{}{{ⅇ}}^{{-}{0.01820}{}{T}}{}\left({0.50000}{+}{0.50000}{}{\mathrm{erf}}{}\left(\frac{{0.70711}{}\left({\mathrm{ln}}{}\left(\frac{{1377.00000}}{{K}}\right){+}{0.03500}{}{T}{+}{0.50000}{}{\left({{\mathrm{α}}}_{{1}}{+}{{\mathrm{α}}}_{{2}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right){+}\frac{{{\mathrm{α}}}_{{3}}}{\sqrt{{T}}}{+}{{\mathrm{α}}}_{{4}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}{+}\frac{{{\mathrm{α}}}_{{5}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{{\mathrm{α}}}_{{6}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}}{\sqrt{{T}}}\right)}^{{2}}{}{T}\right)}{\left({{\mathrm{α}}}_{{1}}{+}{{\mathrm{α}}}_{{2}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right){+}\frac{{{\mathrm{α}}}_{{3}}}{\sqrt{{T}}}{+}{{\mathrm{α}}}_{{4}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}{+}\frac{{{\mathrm{α}}}_{{5}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{{\mathrm{α}}}_{{6}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}}{\sqrt{{T}}}\right){}\sqrt{{T}}}\right)\right){-}{1.00000}{}{{ⅇ}}^{{-}{0.05320}{}{T}}{}{K}{}\left({0.50000}{+}{0.50000}{}{\mathrm{erf}}{}\left(\frac{{0.70711}{}\left({\mathrm{ln}}{}\left(\frac{{1377.00000}}{{K}}\right){+}{0.03500}{}{T}{+}{0.50000}{}{\left({{\mathrm{α}}}_{{1}}{+}{{\mathrm{α}}}_{{2}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right){+}\frac{{{\mathrm{α}}}_{{3}}}{\sqrt{{T}}}{+}{{\mathrm{α}}}_{{4}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}{+}\frac{{{\mathrm{α}}}_{{5}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{{\mathrm{α}}}_{{6}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}}{\sqrt{{T}}}\right)}^{{2}}{}{T}\right)}{\left({{\mathrm{α}}}_{{1}}{+}{{\mathrm{α}}}_{{2}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right){+}\frac{{{\mathrm{α}}}_{{3}}}{\sqrt{{T}}}{+}{{\mathrm{α}}}_{{4}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}{+}\frac{{{\mathrm{α}}}_{{5}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{{\mathrm{α}}}_{{6}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}}{\sqrt{{T}}}\right){}\sqrt{{T}}}{-}{0.70711}{}\left({{\mathrm{α}}}_{{1}}{+}{{\mathrm{α}}}_{{2}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right){+}\frac{{{\mathrm{α}}}_{{3}}}{\sqrt{{T}}}{+}{{\mathrm{α}}}_{{4}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}{+}\frac{{{\mathrm{α}}}_{{5}}{}{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{{\mathrm{α}}}_{{6}}{}{{\mathrm{ln}}{}\left({0.00073}{}{K}\right)}^{{2}}}{\sqrt{{T}}}\right){}\sqrt{{T}}\right)\right)$ (1.14)

We can use non-linear fitting routines from the statistics data to find the values of that best fit our data. Construct a matrix of parameters and a vector of the corresponding value of the objective function.

 > $B:=⟨⟨\mathrm{Times}⟩|\mathrm{convert}\left(\mathrm{DF}\left["Strike"\right],\mathrm{Matrix}\right)⟩$
 ${B}{≔}\left[\begin{array}{c}{\mathrm{308 x 2}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (1.15)
 > $V≔\mathrm{convert}\left(\mathrm{DF}\left["Ask"\right],\mathrm{Vector}\left[\mathrm{column}\right]\right)$
 ${V}{≔}\left[\begin{array}{c}{\mathrm{1 .. 308}}{{\mathrm{Vector}}}_{{\mathrm{column}}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (1.16)
 >
 ${\mathrm{β1}}{≔}\left[\begin{array}{c}{0.14393}\\ {0.12599}\\ {-}{0.01069}\\ {-}{0.03301}\\ {-}{0.30394}\\ {0.40480}\end{array}\right]$ (1.17)

Here is the corresponding implied volatility function.

 > $\mathrm{F1}:=\genfrac{}{}{0}{}{\mathrm{Σ}\left(\mathrm{S0},T,K\right)}{\phantom{\mathrm{α}=\mathrm{β1}}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{\mathrm{Σ}\left(\mathrm{S0},T,K\right)}}{\mathrm{α}=\mathrm{β1}}$
 ${\mathrm{F1}}{≔}{0.14393}{+}{0.12599}{}{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right){-}\frac{{0.01069}}{\sqrt{{T}}}{-}{0.03301}{}{{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right)}^{{2}}{-}\frac{{0.30394}{}{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{0.40480}{}{{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right)}^{{2}}}{\sqrt{{T}}}$ (1.18)
 > $\mathrm{plot3d}\left(\mathrm{F1},T=0..2.5,K=600..1500\right)$
 >

Here is another way to estimate these parameters.

 > $U:=\mathrm{Vector}\left(1..M\right)$
 ${U}{≔}\left[\begin{array}{c}{\mathrm{1 .. 308}}{{\mathrm{Vector}}}_{{\mathrm{column}}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (1.19)
 >
 > $\mathrm{β2}≔\mathrm{NonlinearFit}\left(\mathrm{Σ}\left(\mathrm{S0},T,K\right),B,U,\left[T,K\right],\mathrm{parameternames}=\left[{\mathrm{α}}_{1},{\mathrm{α}}_{2},{\mathrm{α}}_{3},{\mathrm{α}}_{4},{\mathrm{α}}_{5},{\mathrm{α}}_{6}\right],\mathrm{output}=\mathrm{parametervector}\right)$
 ${\mathrm{β2}}{≔}\left[\begin{array}{c}{0.13011}\\ {0.17037}\\ {0.00115}\\ {-}{0.26597}\\ {-}{0.36133}\\ {0.28070}\end{array}\right]$ (1.20)

Here is the corresponding implied volatility function.

 > $\mathrm{F2}:=\genfrac{}{}{0}{}{\mathrm{Σ}\left(\mathrm{S0},T,K\right)}{\phantom{\mathrm{α}=\mathrm{β2}}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{\mathrm{Σ}\left(\mathrm{S0},T,K\right)}}{\mathrm{α}=\mathrm{β2}}$
 ${\mathrm{F2}}{≔}{0.13011}{+}{0.17037}{}{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right){+}\frac{{0.00115}}{\sqrt{{T}}}{-}{0.26597}{}{{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right)}^{{2}}{-}\frac{{0.36133}{}{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{0.28070}{}{{\mathrm{ln}}{}\left(\frac{{1}}{{1377}}{}{K}\right)}^{{2}}}{\sqrt{{T}}}$ (1.21)
 > $\mathrm{plot3d}\left(\mathrm{F2},T=0..2.5,K=600..1500\right)$
 >

We can compare both fits with the actual implied volatilities.

 > $P≔\mathrm{dataplot}\left(\mathrm{L1}\left[..,1\right],\mathrm{L1}\left[..,2\right]\right):$
 > $\mathrm{P1}:=\mathrm{plot}\left(\genfrac{}{}{0}{}{\mathrm{F1}}{\phantom{T={\mathrm{Time}}_{1}}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{\mathrm{F1}}}{T={\mathrm{Time}}_{1}},K=800..1500,\mathrm{thickness}=3,\mathrm{color}=\mathrm{red}\right):$
 > $\mathrm{P2}:=\mathrm{plot}\left(\genfrac{}{}{0}{}{\mathrm{F2}}{\phantom{T={\mathrm{Time}}_{1}}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{\mathrm{F2}}}{T={\mathrm{Time}}_{1}},K=800..1500,\mathrm{thickness}=3,\mathrm{color}=\mathrm{green}\right):$
 > $\mathrm{display}\left(P,\mathrm{P1},\mathrm{P2},\mathrm{axes}=\mathrm{boxed},\mathrm{gridlines},\mathrm{size}=\left[600,"golden"\right]\right)$
 > $P≔\mathrm{dataplot}\left(\mathrm{L2}\left[..,1\right],\mathrm{L2}\left[..,2\right]\right):$
 > $\mathrm{P1}:=\mathrm{plot}\left(\genfrac{}{}{0}{}{\mathrm{F1}}{\phantom{T={\mathrm{Time}}_{2}}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{\mathrm{F1}}}{T={\mathrm{Time}}_{2}},K=1000..1500,\mathrm{thickness}=3,\mathrm{color}=\mathrm{red}\right):$
 > $\mathrm{P2}:=\mathrm{plot}\left(\genfrac{}{}{0}{}{\mathrm{F2}}{\phantom{T={\mathrm{Time}}_{2}}}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}|\phantom{\rule[-0.0ex]{0.1em}{0.0ex}}\genfrac{}{}{0}{}{\phantom{\mathrm{F2}}}{T={\mathrm{Time}}_{2}},K=1000..1500,\mathrm{thickness}=3,\mathrm{color}=\mathrm{green}\right):$
 > $\mathrm{display}\left(P,\mathrm{P1},\mathrm{P2},\mathrm{gridlines},\mathrm{axes}=\mathrm{boxed},\mathrm{size}=\left[600,"golden"\right]\right)$

Modeling with Local Volatility

We will consider the same model for the local volatility except that in this case we will use parameters that were fit to some market data.

 > $\mathrm{restart}:$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{with}\left(\mathrm{Finance}\right):$
 > $\mathrm{Σ}:={\mathrm{α}}_{1}+{\mathrm{α}}_{2}\mathrm{ln}\left(\frac{K}{\mathrm{S0}}\right)+\frac{{\mathrm{α}}_{3}\cdot 1}{\sqrt{T}}+{\mathrm{α}}_{4}{\mathrm{ln}\left(\frac{K}{\mathrm{S0}}\right)}^{2}+\frac{{\mathrm{α}}_{5}\mathrm{ln}\left(\frac{K}{\mathrm{S0}}\right)}{\sqrt{T}}+\frac{{\mathrm{α}}_{6}{\mathrm{ln}\left(\frac{K}{\mathrm{S0}}\right)}^{2}}{\sqrt{T}}$
 ${\mathrm{Σ}}{≔}{{\mathrm{α}}}_{{1}}{+}{{\mathrm{α}}}_{{2}}{}{\mathrm{ln}}{}\left(\frac{{K}}{{\mathrm{S0}}}\right){+}\frac{{{\mathrm{α}}}_{{3}}}{\sqrt{{T}}}{+}{{\mathrm{α}}}_{{4}}{}{{\mathrm{ln}}{}\left(\frac{{K}}{{\mathrm{S0}}}\right)}^{{2}}{+}\frac{{{\mathrm{α}}}_{{5}}{}{\mathrm{ln}}{}\left(\frac{{K}}{{\mathrm{S0}}}\right)}{\sqrt{{T}}}{+}\frac{{{\mathrm{α}}}_{{6}}{}{{\mathrm{ln}}{}\left(\frac{{K}}{{\mathrm{S0}}}\right)}^{{2}}}{\sqrt{{T}}}$ (2.1)
 > ${\mathrm{α}}_{1}≔0.1581:$${\mathrm{α}}_{2}:=-0.2777:$${\mathrm{α}}_{3}:=-0.0050:$${\mathrm{α}}_{4}:=-0.5011:$${\mathrm{α}}_{5}≔0.1103:$${\mathrm{α}}_{6}:=0.5909:$
 > $\mathrm{S0}:=100$
 ${\mathrm{S0}}{≔}{100}$ (2.2)
 > $\mathrm{Σ}$
 ${0.15810}{-}{0.27770}{}{\mathrm{ln}}{}\left(\frac{{1}}{{100}}{}{K}\right){-}\frac{{0.00500}}{\sqrt{{T}}}{-}{0.50110}{}{{\mathrm{ln}}{}\left(\frac{{1}}{{100}}{}{K}\right)}^{{2}}{+}\frac{{0.11030}{}{\mathrm{ln}}{}\left(\frac{{1}}{{100}}{}{K}\right)}{\sqrt{{T}}}{+}\frac{{0.59090}{}{{\mathrm{ln}}{}\left(\frac{{1}}{{100}}{}{K}\right)}^{{2}}}{\sqrt{{T}}}$ (2.3)
 > $\mathrm{plot3d}\left(\mathrm{Σ},K=50..150,T=0.1..2\right)$

Consider two functions. The first one returns the Black-Scholes price of a European call option for our model. The second one returns the Black-Scholes price of a European put option for our model. We will assume that these functions are given two us (e.g. obtained by interpolating the market data) and will try to determine the corresponding local volatility term structure.

 > $C:=\mathrm{BlackScholesPrice}\left(\mathrm{S0},K,T,\mathrm{Σ},0.05,0.01,'\mathrm{call}'\right):$
 > $P:=\mathrm{BlackScholesPrice}\left(\mathrm{S0},K,T,\mathrm{Σ},0.05,0.01,'\mathrm{put}'\right):$

Construct the corresponding local volatility surface.

 > $V:=\mathrm{LocalVolatilitySurface}\left(0.05,0.01,C,K,T\right):$
 > $\mathrm{S1}:=⟨\mathrm{seq}\left(i,i=50..200\right)⟩$
 ${\mathrm{S1}}{≔}\left[\begin{array}{c}{\mathrm{1 .. 151}}{{\mathrm{Vector}}}_{{\mathrm{column}}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (2.4)
 > $\mathrm{T1}:=⟨\mathrm{seq}\left(0.02i,i=1..50\right)⟩$
 ${\mathrm{T1}}{≔}\left[\begin{array}{c}{\mathrm{1 .. 50}}{{\mathrm{Vector}}}_{{\mathrm{column}}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (2.5)
 > $V:=\mathrm{LocalVolatility}\left(C,\mathrm{S1},\mathrm{T1},0.05,0.01,T,K\right)$
 ${V}{≔}\left[\begin{array}{c}{\mathrm{151 x 50}}{\mathrm{Matrix}}\\ {\mathrm{Data Type:}}{\mathrm{anything}}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (2.6)
 > $\mathrm{LV}:=\mathrm{LocalVolatility}\left(C,S,T,0.05,0.01,T,K\right):$
 >
 >
 > ${\mathrm{plots}}_{\mathrm{display}}\left(\mathrm{P1},\mathrm{P2}\right)$

We can construct the corresponding local volatility surface and implied volatility surface.

 > $\mathrm{LVS}:=\mathrm{LocalVolatilitySurface}\left(\mathrm{T1},\mathrm{S1},V\right):$
 > $\mathrm{IVS}:=\mathrm{ImpliedVolatilitySurface}\left(\mathrm{Σ},T,K\right):$

We can now construct a Black-Scholes process which has the volatility structure we just obtained.

 > $X:=\mathrm{BlackScholesProcess}\left(100,\mathrm{LVS},0.05,0.01\right)$
 ${X}{≔}{\mathrm{_X}}$ (2.7)
 >
 > $\mathrm{P1}:=\mathrm{subs}\left(K=\mathrm{S0}x,\frac{1P}{\mathrm{S0}}\right):$
 > $\mathrm{C1}:=\mathrm{subs}\left(K=\mathrm{S0}x,\frac{1C}{\mathrm{S0}}\right):$
 > $T:='T'$
 ${T}{≔}{T}$ (2.8)

As an alternative, we can use the implied volatility surface to construct an implied trinomial tree.

 > $\mathrm{TT}:=\mathrm{ImpliedTrinomialTree}\left(100,0.05,0.01,\mathrm{IVS},1,100\right):$
 > $\mathrm{GetLocalVolatility}\left(\mathrm{TT},99,99,0.05\right)$
 ${0.15319}$ (2.9)
 > $\mathrm{IVS}\left(1.0,100\right)$
 ${0.15310}$ (2.10)

Compute some option prices. We can use Monte-Carlo simulation to price European-style options and lattice methods to price American-style options.

 > $\mathrm{ExpectedValue}\left(\mathrm{max}\left(X\left(1\right)-100,0\right),\mathrm{timesteps}=100,\mathrm{replications}={10}^{4}\right)$
 $\left[{\mathrm{value}}{=}{8.55963}{,}{\mathrm{standarderror}}{=}{0.09592}\right]$ (2.11)
 > $E:=\mathrm{EuropeanOption}\left(t→\mathrm{max}\left(t-100,0\right),1.0\right)$
 ${E}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (2.12)
 > $\mathrm{LatticePrice}\left(E,\mathrm{TT},0.0\right)$
 ${8.47775}$ (2.13)

Here is an example of an Asian-type option with European exercise.

 > $\mathrm{ExpectedValue}\left(\mathrm{max}\left({{∫}}_{0}^{1}X\left(u\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}u-100,0\right),\mathrm{timesteps}=100,\mathrm{replications}={10}^{3}\right)$
 $\left[{\mathrm{value}}{=}{4.45120}{,}{\mathrm{standarderror}}{=}{0.18170}\right]$ (2.14)