Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_specfun_opt_heston_greeks (s30nb)

## Purpose

nag_specfun_opt_heston_greeks (s30nb) computes the European option price given by Heston's stochastic volatility model together with its sensitivities (Greeks).

## Syntax

[p, delta, gamma, vega, theta, rho, vanna, charm, speed, zomma, vomma, ifail] = s30nb(calput, x, s, t, sigmav, kappa, corr, var0, eta, grisk, r, q, 'm', m, 'n', n)
[p, delta, gamma, vega, theta, rho, vanna, charm, speed, zomma, vomma, ifail] = nag_specfun_opt_heston_greeks(calput, x, s, t, sigmav, kappa, corr, var0, eta, grisk, r, q, 'm', m, 'n', n)

## Description

nag_specfun_opt_heston_greeks (s30nb) computes the price and sensitivities of a European option using Heston's stochastic volatility model. The return on the asset price, S$S$, is
 (dS)/S = (r − q) dt + sqrt(vt) d Wt(1) $dS S = (r-q) dt + vt d W t (1)$
and the instantaneous variance, vt${v}_{t}$, is defined by a mean-reverting square root stochastic process,
 dvt = κ (η − vt) dt + σv sqrt(vt) d Wt(2) , $dvt = κ ( η-vt ) dt + σv vt d W t (2) ,$
where r$r$ is the risk free annual interest rate; q$q$ is the annual dividend rate; vt${v}_{t}$ is the variance of the asset price; σv${\sigma }_{v}$ is the volatility of the volatility, sqrt(vt)$\sqrt{{v}_{t}}$; κ$\kappa$ is the mean reversion rate; η$\eta$ is the long term variance. dWt(i)$d{W}_{t}^{\left(\mathit{i}\right)}$, for i = 1,2$\mathit{i}=1,2$, denotes two correlated standard Brownian motions with
 ℂov [ d Wt(1) , d Wt(2) ] = ρ d t . $ℂov [ d W t (1) , d W t (2) ] = ρ d t .$
The option price is computed by evaluating the integral transform given by Lewis (2000) using the form of the characteristic function discussed by Albrecher et al. (2007), see also Kilin (2006).
 Pcall = S e − qT − X e − rT 1/π Re[∫0 + i / 2∞ + i / 2e − ikX( Ĥ (k,v,T) )/( k2 − ik )dk] , $Pcall = S e-qT - X e-rT 1π Re[ ∫ 0+i/2 ∞+i/2 e-ikX- H^ (k,v,T) k2 - ik d k ] ,$ (1)
where X = ln(S / X) + (rq) T $\stackrel{-}{X}=\mathrm{ln}\left(S/X\right)+\left(r-q\right)T$ and
 Ĥ (k,v,T) = exp((2κη)/(σv2)[tg − ln((1 − he − ξt)/(1 − h))] + vtg[(1 − e − ξt)/(1 − he − ξt)]) , $H^ (k,v,T) = exp( 2κη σv2 [ tg - ln( 1-he-ξt 1-h ) ] + vt g [ 1-e-ξt 1-he-ξt ] ) ,$
 g = (1/2) (b − ξ) ,   h = (b − ξ)/(b + ξ) ,   t = σv2 T / 2 , $g = 12 (b-ξ) , h = b-ξ b+ξ , t = σv2 T/2 ,$
 ξ = [b2 + 4(k2 − ik)/(σv2)](1/2) , $ξ = [ b2 + 4 k2-ik σv2 ] 12 ,$
 b = 2/(σv2) [(1 − γ + ik)ρσv + sqrt( κ2 − γ(1 − γ) σv2 )] $b = 2 σv2 [ ( 1-γ+ik ) ρσv + κ2 - γ(1-γ) σv2 ]$
with t = σv2 T / 2 $t={\sigma }_{v}^{2}T/2$. Here γ$\gamma$ is the risk aversion parameter of the representative agent with 0γ1$0\le \gamma \le 1$ and γ(1γ) σv2 κ2 $\gamma \left(1-\gamma \right){\sigma }_{v}^{2}\le {\kappa }^{2}$. The value γ = 1 $\gamma =1$ corresponds to λ = 0$\lambda =0$, where λ$\lambda$ is the market price of risk in Heston (1993) (see Lewis (2000) and Rouah and Vainberg (2007)).
The price of a put option is obtained by put-call parity.
 Pput = Pcall + Xe − rT − S e − qT . $Pput = Pcall + Xe-rT - S e-qT .$
Writing the expression for the price of a call option as
 Pcall = Se − qT − Xe − rT 1/π Re[∫0 + i / 2∞ + i / 2I(k,r,S,T,v)dk] $Pcall = Se-qT - Xe-rT 1π Re[ ∫ 0+i/2 ∞+i/2 I (k,r,S,T,v) d k ]$
then the sensitivities or Greeks can be obtained in the following manner,
Delta
 ( ∂ Pcall )/(∂S) = e − qT + (Xe − rT)/S 1/π Re[∫0 + i / 2∞ + i / 2(ik)I(k,r,S,T,v)dk] , $∂ Pcall ∂S = e-qT + Xe-rT S 1π Re[ ∫ 0+i/2 ∞+i/2 ( ik ) I (k,r,S,T,v) dk ] ,$
Vega
 (∂P)/(∂v) = − X e − rT 1/π Re[∫0 − i / 20 + i / 2f2I(k,r,j,S,T,v)dk] ,  where ​ f2 = g [( 1 − e − ξt )/( 1 − h e − ξt )] , $∂P ∂v = - X e-rT 1π Re[ ∫ 0-i/2 0+i/2 f2 I (k,r,j,S,T,v) dk ] , where ​ f2 = g [ 1 - e-ξt 1 - h e-ξt ] ,$
Rho
 ( ∂Pcall )/(∂r) = T X e − rT 1/π Re[∫0 + i / 2∞ + i / 2(1 + ik)I(k,r,S,T,v)dk] . $∂Pcall ∂r = T X e-rT 1π Re[ ∫ 0+i/2 ∞+i/2 ( 1+ik ) I (k,r,S,T,v) dk ] .$

## References

Albrecher H, Mayer P, Schoutens W and Tistaert J (2007) The little Heston trap Wilmott Magazine January 2007 83–92
Heston S (1993) A closed-form solution for options with stochastic volatility with applications to bond and currency options 6 347–343 Review of Financial Studies
Kilin F (2006) Accelerating the calibration of stochastic volatility models MPRA Paper No. 2975 http://mpra.ub.uni-muenchen.de/2975/
Lewis A L (2000) Option valuation under stochastic volatility Finance Press, USA
Rouah F D and Vainberg G (2007) Option Pricing Models and Volatility using Excel-VBA John Wiley and Sons, Inc

## Parameters

### Compulsory Input Parameters

1:     calput – string (length ≥ 1)
Determines whether the option is a call or a put.
calput = 'C'${\mathbf{calput}}=\text{'C'}$
A call. The holder has a right to buy.
calput = 'P'${\mathbf{calput}}=\text{'P'}$
A put. The holder has a right to sell.
Constraint: calput = 'C'${\mathbf{calput}}=\text{'C'}$ or 'P'$\text{'P'}$.
2:     x(m) – double array
m, the dimension of the array, must satisfy the constraint m1${\mathbf{m}}\ge 1$.
x(i)${\mathbf{x}}\left(i\right)$ must contain Xi${X}_{\mathit{i}}$, the i$\mathit{i}$th strike price, for i = 1,2,,m$\mathit{i}=1,2,\dots ,{\mathbf{m}}$.
Constraint: x(i)z ​ and ​ x(i) 1 / z ${\mathbf{x}}\left(\mathit{i}\right)\ge z\text{​ and ​}{\mathbf{x}}\left(\mathit{i}\right)\le 1/z$, where z = x02am () $z=\mathbf{x02am}\left(\right)$, the safe range parameter, for i = 1,2,,m$\mathit{i}=1,2,\dots ,{\mathbf{m}}$.
3:     s – double scalar
S$S$, the price of the underlying asset.
Constraint: sz ​ and ​s1.0 / z${\mathbf{s}}\ge z\text{​ and ​}{\mathbf{s}}\le 1.0/z$, where z = x02am()$z=\mathbf{x02am}\left(\right)$, the safe range parameter.
4:     t(n) – double array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
t(i)${\mathbf{t}}\left(i\right)$ must contain Ti${T}_{\mathit{i}}$, the i$\mathit{i}$th time, in years, to expiry, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
Constraint: t(i)z${\mathbf{t}}\left(\mathit{i}\right)\ge z$, where z = x02am () $z=\mathbf{x02am}\left(\right)$, the safe range parameter, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
5:     sigmav – double scalar
The volatility, σv${\sigma }_{v}$, of the volatility process, sqrt(vt)$\sqrt{{v}_{t}}$. Note that a rate of 20% should be entered as 0.2$0.2$.
Constraint: sigmav > 0.0${\mathbf{sigmav}}>0.0$.
6:     kappa – double scalar
κ$\kappa$, the long term mean reversion rate of the volatility.
Constraint: kappa > 0.0${\mathbf{kappa}}>0.0$.
7:     corr – double scalar
The correlation between the two standard Brownian motions for the asset price and the volatility.
Constraint: 1.0corr1.0$-1.0\le {\mathbf{corr}}\le 1.0$.
8:     var0 – double scalar
The initial value of the variance, vt${v}_{t}$, of the asset price.
Constraint: var00.0${\mathbf{var0}}\ge 0.0$.
9:     eta – double scalar
η$\eta$, the long term mean of the variance of the asset price.
Constraint: eta > 0.0${\mathbf{eta}}>0.0$.
10:   grisk – double scalar
The risk aversion parameter, γ$\gamma$, of the representative agent.
Constraint: 0.0grisk1.0$0.0\le {\mathbf{grisk}}\le 1.0$ and grisk × (1grisk) × sigmav × sigmavkappa × kappa${\mathbf{grisk}}×\left(1-{\mathbf{grisk}}\right)×{\mathbf{sigmav}}×{\mathbf{sigmav}}\le {\mathbf{kappa}}×{\mathbf{kappa}}$.
11:   r – double scalar
r$r$, the annual risk-free interest rate, continuously compounded. Note that a rate of 5% should be entered as 0.05.
Constraint: r0.0${\mathbf{r}}\ge 0.0$.
12:   q – double scalar
q$q$, the annual continuous yield rate. Note that a rate of 8% should be entered as 0.08.
Constraint: q0.0${\mathbf{q}}\ge 0.0$.

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The dimension of the array x.
The number of strike prices to be used.
Constraint: m1${\mathbf{m}}\ge 1$.
2:     n – int64int32nag_int scalar
Default: The dimension of the array t.
The number of times to expiry to be used.
Constraint: n1${\mathbf{n}}\ge 1$.

ldp

### Output Parameters

1:     p(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array p contains the computed option prices.
2:     delta(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array delta contains the sensitivity, (P)/(S)$\frac{\partial P}{\partial S}$, of the option price to change in the price of the underlying asset.
3:     gamma(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array gamma contains the sensitivity, (2P)/(S2)$\frac{{\partial }^{2}P}{\partial {S}^{2}}$, of delta to change in the price of the underlying asset.
4:     vega(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array vega contains the sensitivity, (P)/(σ)$\frac{\partial P}{\partial \sigma }$, of the option price to change in the volatility of the underlying asset.
5:     theta(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array theta contains the sensitivity, (P)/(T)$-\frac{\partial P}{\partial T}$, of the option price to change in the time to expiry of the option.
6:     rho(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array rho contains the sensitivity, (P)/(r)$\frac{\partial P}{\partial r}$, of the option price to change in the annual risk-free interest rate.
7:     vanna(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array vanna contains the sensitivity, (2P)/(Sσ)$\frac{{\partial }^{2}P}{\partial S\partial \sigma }$, of vega to change in the price of the underlying asset or, equivalently, the sensitivity of delta to change in the volatility of the asset price.
8:     charm(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array charm contains the sensitivity, (2P)/(S T)$-\frac{{\partial }^{2}P}{\partial S\partial T}$, of delta to change in the time to expiry of the option.
9:     speed(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array speed contains the sensitivity, (3P)/(S3)$\frac{{\partial }^{3}P}{\partial {S}^{3}}$, of gamma to change in the price of the underlying asset.
10:   zomma(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array zomma contains the sensitivity, (3P)/(S2σ)$\frac{{\partial }^{3}P}{\partial {S}^{2}\partial \sigma }$, of gamma to change in the volatility of the underlying asset.
11:   vomma(ldp,n) – double array
ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
The leading m × n${\mathbf{m}}×{\mathbf{n}}$ part of the array vomma contains the sensitivity, (2P)/(σ2)$\frac{{\partial }^{2}P}{\partial {\sigma }^{2}}$, of vega to change in the volatility of the underlying asset.
12:   ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
Constraint: calput = 'C'${\mathbf{calput}}=\text{'C'}$ or 'P'$\text{'P'}$.
ifail = 2${\mathbf{ifail}}=2$
Constraint: m1${\mathbf{m}}\ge 1$.
ifail = 3${\mathbf{ifail}}=3$
Constraint: n1${\mathbf{n}}\ge 1$.
ifail = 4${\mathbf{ifail}}=4$
Constraint: x(i)_${\mathbf{x}}\left(i\right)\ge _$ and .
ifail = 5${\mathbf{ifail}}=5$
s is too small and .
ifail = 6${\mathbf{ifail}}=6$
Constraint: .
ifail = 7${\mathbf{ifail}}=7$
Constraint: sigmav > 0.0${\mathbf{sigmav}}>0.0$.
ifail = 8${\mathbf{ifail}}=8$
Constraint: kappa > 0.0${\mathbf{kappa}}>0.0$.
ifail = 9${\mathbf{ifail}}=9$
Constraint: |corr|1.0$|{\mathbf{corr}}|\le 1.0$.
ifail = 10${\mathbf{ifail}}=10$
Constraint: var00.0${\mathbf{var0}}\ge 0.0$.
ifail = 11${\mathbf{ifail}}=11$
Constraint: eta > 0.0${\mathbf{eta}}>0.0$.
ifail = 12${\mathbf{ifail}}=12$
Constraint: 0.0grisk1.0$0.0\le {\mathbf{grisk}}\le 1.0$ and grisk × (1.0grisk) × ${\mathbf{grisk}}×\left(1.0-{\mathbf{grisk}}\right)×{{\mathbf{sigmav}}}^{2}\le {{\mathbf{kappa}}}^{2}$.
ifail = 13${\mathbf{ifail}}=13$
Constraint: r0.0${\mathbf{r}}\ge 0.0$.
ifail = 14${\mathbf{ifail}}=14$
Constraint: q0.0${\mathbf{q}}\ge 0.0$.
ifail = 16${\mathbf{ifail}}=16$
Constraint: ldpm$\mathit{ldp}\ge {\mathbf{m}}$.
W ifail = 17${\mathbf{ifail}}=17$
Quadrature has not converged to the required accuracy. However, the result should be a reasonable approximation.
W ifail = 18${\mathbf{ifail}}=18$
Quadrature has not converged to the required accuracy. The values returned cannot be relied upon.

## Accuracy

The accuracy of the output is determined by the accuracy of the numerical quadrature used to evaluate the integral in (1). An adaptive method is used which evaluates the integral to within a tolerance of max (108, 1010 × |I| ) $\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({10}^{-8},{10}^{-10}×|I|\right)$, where |I|$|I|$ is the absolute value of the integral.

None.

## Example

```function nag_specfun_opt_heston_greeks_example
calput = 'C';
s = 100.0;
r = 0.025;
q = 0.0;
kappa = 1.5768;
eta = 0.0398;
var0 = 0.0175;
sigmav = 0.5751;
corr = -0.5711;
grisk = 1;
x = [100.0];
t = [1];

[p, delta, gamma, vega, theta, rho, vanna, charm, speed, zomma, vomma, ifail] = ...
nag_specfun_opt_heston_greeks(calput, x, s, t, sigmav, kappa, corr, var0, eta, grisk, r, q);

fprintf('\nHeston''s Stochastic Volatility Model\n');
if calput == 'C' || calput == 'c'
fprintf('European Call :\n');
else
fprintf('European Put :\n');
end
fprintf(' Spot                   =  %9.4f\n', s);
fprintf(' Volatility of vol      =  %9.4f\n', sigmav);
fprintf(' Mean reversion         =  %9.4f\n', kappa);
fprintf(' Correlation            =  %9.4f\n', corr);
fprintf(' Variance               =  %9.4f\n', var0);
fprintf(' Mean of variance       =  %9.4f\n', eta);
fprintf(' Risk aversion          =  %9.4f\n', grisk);
fprintf(' Rate                   =  %9.4f\n', r);
fprintf(' Dividend               =  %9.4f\n\n', q);

for j=1:1
fprintf(' Strike      Price     Delta     Gamma     Vega      Theta     Rho\n');
for i=1:1
fprintf('%9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n', x(i), p(i,j), ...
delta(i,j), gamma(i,j), vega(i,j), theta(i,j), rho(i,j));
end
fprintf(' Strike      Price     Vanna     Charm     Speed    Zomma     Vomma\n');
for i=1:1
fprintf('%9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n', x(i), p(i,j), ...
vanna(i,j), charm(i,j), speed(i,j), zomma(i,j), vomma(i,j));
end
end
```
```

Heston's Stochastic Volatility Model
European Call :
Spot                   =   100.0000
Volatility of vol      =     0.5751
Mean reversion         =     1.5768
Correlation            =    -0.5711
Variance               =     0.0175
Mean of variance       =     0.0398
Risk aversion          =     1.0000
Rate                   =     0.0250
Dividend               =     0.0000

Strike      Price     Delta     Gamma     Vega      Theta     Rho
100.0000    7.2743    0.6945    0.0251   52.5461   -4.9969   62.1735
Strike      Price     Vanna     Charm     Speed    Zomma     Vomma
100.0000    7.2743   -0.5643   -0.0321   -0.0023   -0.1976 -321.0780

```
```function s30nb_example
calput = 'C';
s = 100.0;
r = 0.025;
q = 0.0;
kappa = 1.5768;
eta = 0.0398;
var0 = 0.0175;
sigmav = 0.5751;
corr = -0.5711;
grisk = 1;
x = [100.0];
t = [1];

[p, delta, gamma, vega, theta, rho, vanna, charm, speed, zomma, vomma, ifail] = ...
s30nb(calput, x, s, t, sigmav, kappa, corr, var0, eta, grisk, r, q);

fprintf('\nHeston''s Stochastic Volatility Model\n');
if calput == 'C' || calput == 'c'
fprintf('European Call :\n');
else
fprintf('European Put :\n');
end
fprintf(' Spot                   =  %9.4f\n', s);
fprintf(' Volatility of vol      =  %9.4f\n', sigmav);
fprintf(' Mean reversion         =  %9.4f\n', kappa);
fprintf(' Correlation            =  %9.4f\n', corr);
fprintf(' Variance               =  %9.4f\n', var0);
fprintf(' Mean of variance       =  %9.4f\n', eta);
fprintf(' Risk aversion          =  %9.4f\n', grisk);
fprintf(' Rate                   =  %9.4f\n', r);
fprintf(' Dividend               =  %9.4f\n\n', q);

for j=1:1
fprintf(' Strike      Price     Delta     Gamma     Vega      Theta     Rho\n');
for i=1:1
fprintf('%9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n', x(i), p(i,j), ...
delta(i,j), gamma(i,j), vega(i,j), theta(i,j), rho(i,j));
end
fprintf(' Strike      Price     Vanna     Charm     Speed    Zomma     Vomma\n');
for i=1:1
fprintf('%9.4f %9.4f %9.4f %9.4f %9.4f %9.4f %9.4f\n', x(i), p(i,j), ...
vanna(i,j), charm(i,j), speed(i,j), zomma(i,j), vomma(i,j));
end
end
```
```

Heston's Stochastic Volatility Model
European Call :
Spot                   =   100.0000
Volatility of vol      =     0.5751
Mean reversion         =     1.5768
Correlation            =    -0.5711
Variance               =     0.0175
Mean of variance       =     0.0398
Risk aversion          =     1.0000
Rate                   =     0.0250
Dividend               =     0.0000

Strike      Price     Delta     Gamma     Vega      Theta     Rho
100.0000    7.2743    0.6945    0.0251   52.5461   -4.9969   62.1735
Strike      Price     Vanna     Charm     Speed    Zomma     Vomma
100.0000    7.2743   -0.5643   -0.0321   -0.0023   -0.1976 -321.0780

```