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_term (s30nc)

## Purpose

nag_specfun_opt_heston_term (s30nc) computes the European option price given by Heston's stochastic volatility model with term structure.

## Syntax

[p, ifail] = s30nc(calput, x, fwd, disc, ts, t, alpha, lambda, corr, sigmat, var0, 'm', m, 'numts', numts)
[p, ifail] = nag_specfun_opt_heston_term(calput, x, fwd, disc, ts, t, alpha, lambda, corr, sigmat, var0, 'm', m, 'numts', numts)

## Description

nag_specfun_opt_heston_term (s30nc) computes the price of a European option for Heston's stochastic volatility model with time-dependent parameters which are piecewise constant. Starting from the stochastic volatility model given by the Stochastic Differential Equation (SDE) system defined by Heston (1993), a scaling of the variance process is introduced, together with a normalization, setting the long run variance, $\eta$, equal to $1$. This leads to
 $d St St = μt d t+σt νt d Wt1 ,$ (1)
 $d νt = λt 1-νt d t+ αt νt d Wt2 ,$ (2)
 $Cov d W t 1 , d W t 2 = ρt d t ,$ (3)
where ${\mu }_{t}={r}_{t}-{q}_{t}$ is the drift term representing the contribution of interest rates, ${r}_{t}$, and dividends, ${q}_{t}$, while ${\sigma }_{t}$ is the scaling parameter, ${\nu }_{t}$ is the scaled variance, ${\lambda }_{t}$ is the mean reversion rate and ${\alpha }_{t}$ is the volatility of the scaled volatility, $\sqrt{{\nu }_{t}}$. Then, ${W}_{t}^{\left(\mathit{i}\right)}$, for $\mathit{i}=1,2$, are two standard Brownian motions with correlation parameter ${\rho }_{t}$. Without loss of generality, the drift term, ${\mu }_{t}$, is eliminated by modelling the forward price, ${F}_{t}$, directly, instead of the spot price, ${S}_{t}$, with
 $Ft = S0 exp ∫ 0 t μs d s .$ (4)
If required, the spot can be expressed as, ${S}_{0}=D{F}_{t}$, where $D$ is the discount factor.
The option price is computed by dividing the time to expiry, $T$, into ${n}_{s}$ subintervals $\left[{t}_{0},{t}_{1}\right],\dots ,\left[{t}_{i-1},{t}_{i}\right],\dots ,\left[{t}_{{n}_{s}-1},T\right]$ and applying the method of characteristic functions to each subinterval, with appropriate initial conditions. Thus, a pair of ordinary differential equations (one of which is a Riccati equation) is solved on each subinterval as outlined in Elices (2008) and Mikhailov and Nögel (2003). Reversing time by taking $\tau =T-t$, the characteristic function solution for the first time subinterval, starting at $\tau =0$, is given by Heston (1993), while the solution on each following subinterval uses the solution of the preceding subinterval as initial condition to compute the value of the characteristic function.
In the case of a ‘flat’ term structure, i.e., the parameters are constant over the time of the option, $T$, the form of the SDE system given by Heston (1993)
 $d St St = μt d t+ Vt d W t 1 ,$ (5)
 $d Vt = κ η-Vt d t + σv Vt d W t 2 ,$ (6)
can be recovered by setting ${V}_{0}=\eta ={\sigma }_{t}^{2}$, $\kappa ={\lambda }_{t}$, ${\sigma }_{v}={\sigma }_{t}{\alpha }_{t}$.
Conversely, given the Heston form of the SDE pair, to get the term structure form set $\sigma =\sqrt{\eta }$, ${\alpha }_{t}={\sigma }_{v}/\sqrt{\eta }$, ${\lambda }_{t}=\kappa$.

## References

Bain A (2011) Private communication
Elices A (2008) Models with time-dependent parameters using transform methods: application to Heston’s model arXiv:0708.2020v2
Heston S (1993) A closed-form solution for options with stochastic volatility with applications to bond and currency options Review of Financial Studies 6 327–343
Mikhailov S and Nögel U (2003) Heston’s Stochastic Volatility Model Implementation, Calibration and Some Extensions Wilmott Magazine July/August 74–79

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{calput}$ – string (length ≥ 1)
Determines whether the option is a call or a put.
${\mathbf{calput}}=\text{'C'}$
A call; the holder has a right to buy.
${\mathbf{calput}}=\text{'P'}$
A put; the holder has a right to sell.
Constraint: ${\mathbf{calput}}=\text{'C'}$ or $\text{'P'}$.
2:     $\mathrm{x}\left({\mathbf{m}}\right)$ – double array
${\mathbf{x}}\left(i\right)$ contains the $\mathit{i}$th strike price, for $\mathit{i}=1,2,\dots ,m$.
Constraint: ${\mathbf{x}}\left(\mathit{i}\right)\ge z\text{​ and ​}{\mathbf{x}}\left(\mathit{i}\right)\le 1/z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}$.
3:     $\mathrm{fwd}$ – double scalar
The forward price of the asset.
Constraint: ${\mathbf{fwd}}\ge z$ and ${\mathbf{fwd}}\le 1/z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter.
4:     $\mathrm{disc}$ – double scalar
The discount factor, where the current price of the underlying asset, ${S}_{0}$, is given as ${S}_{0}={\mathbf{disc}}×{\mathbf{fwd}}$.
Constraint: ${\mathbf{disc}}\ge z$ and ${\mathbf{disc}}\le 1/z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter.
5:     $\mathrm{ts}\left({\mathbf{numts}}\right)$ – double array
${\mathbf{ts}}\left(\mathit{i}\right)$ must contain the length of the time intervals for which the corresponding element of alpha, lambda, corr and sigmat apply. These should be ordered as they occur in time i.e., $\Delta {t}_{\mathit{i}}={t}_{\mathit{i}}-{t}_{\mathit{i}-1}$.
Constraint: ${\mathbf{ts}}\left(\mathit{i}\right)\ge z\text{​ and ​}{\mathbf{ts}}\left(\mathit{i}\right)\le 1/z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
6:     $\mathrm{t}$ – double scalar
t contains the time to expiry. If $T>\sum \Delta {t}_{\mathit{i}}$ then the parameters associated with the last time interval are extended to the expiry time. If $T<\sum \Delta {t}_{\mathit{i}}$ then the parameters specified are used up until the expiry time. The rest are ignored.
Constraint: ${\mathbf{t}}\ge z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter.
7:     $\mathrm{alpha}\left({\mathbf{numts}}\right)$ – double array
${\mathbf{alpha}}\left(i\right)$ must contain the value of ${\alpha }_{t}$, the value of the volatility of the scaled volatility, $\sqrt{\nu }$, over time subinterval $\Delta {t}_{i}$.
Constraint: ${\mathbf{alpha}}\left(\mathit{i}\right)\ge z\text{​ and ​}{\mathbf{alpha}}\left(\mathit{i}\right)\le 1/z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
8:     $\mathrm{lambda}\left({\mathbf{numts}}\right)$ – double array
${\mathbf{lambda}}\left(i\right)$ must contain the value, ${\lambda }_{t}$, of the mean reversion parameter over the time subinterval $\Delta {t}_{i}$.
Constraint: ${\mathbf{lambda}}\left(\mathit{i}\right)\ge z\text{​ and ​}{\mathbf{lambda}}\left(\mathit{i}\right)\le 1/z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
9:     $\mathrm{corr}\left({\mathbf{numts}}\right)$ – double array
${\mathbf{corr}}\left(i\right)$ must contain the value, ${\rho }_{t}$, of the correlation parameter over the time subinterval $\Delta {t}_{i}$.
Constraint: $-1.0\le {\mathbf{corr}}\left(\mathit{i}\right)\le 1.0$, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
10:   $\mathrm{sigmat}\left({\mathbf{numts}}\right)$ – double array
${\mathbf{sigmat}}\left(i\right)$ must contain the value, ${\sigma }_{t}$, of the variance scale factor over the time subinterval $\Delta {t}_{i}$.
Constraint: ${\mathbf{sigmat}}\left(\mathit{i}\right)\ge z\text{​ and ​}{\mathbf{sigmat}}\left(\mathit{i}\right)\le 1/z$, where $z={\mathbf{x02am}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
11:   $\mathrm{var0}$ – double scalar
${\nu }_{0}$, the initial scaled variance.
Constraint: ${\mathbf{var0}}\ge 0.0$.

### Optional Input Parameters

1:     $\mathrm{m}$int64int32nag_int scalar
Default: the dimension of the array x.
$m$, the number of strike prices to be used.
Constraint: ${\mathbf{m}}\ge 1$.
2:     $\mathrm{numts}$int64int32nag_int scalar
Default: the dimension of the arrays corr, lambda, sigmat, ts, alpha. (An error is raised if these dimensions are not equal.)
${n}_{s}$, the number of subintervals into which the time to expiry, $T$, is divided.
Constraint: ${\mathbf{numts}}\ge 1$.

### Output Parameters

1:     $\mathrm{p}\left({\mathbf{m}}\right)$ – double array
${\mathbf{p}}\left(\mathit{i}\right)$ contains the computed option price at the expiry time, $T$, corresponding to strike ${\mathbf{x}}\left(\mathit{i}\right)$ for the specified term structure, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}$.
2:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{calput}}=_$ was an illegal value.
${\mathbf{ifail}}=2$
Constraint: ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=3$
Constraint: ${\mathbf{numts}}\ge 1$.
${\mathbf{ifail}}=4$
Constraint: $_\le {\mathbf{x}}\left(i\right)\le _$.
${\mathbf{ifail}}=5$
Constraint: $_\le {\mathbf{fwd}}\le _$.
${\mathbf{ifail}}=6$
Constraint: $_\le {\mathbf{disc}}\le _$.
${\mathbf{ifail}}=7$
Constraint: $_\le {\mathbf{ts}}\left(i\right)\le _$.
${\mathbf{ifail}}=8$
t is too small.
${\mathbf{ifail}}=9$
Constraint: $_\le {\mathbf{alpha}}\left(i\right)\le _$.
${\mathbf{ifail}}=10$
Constraint: $_\le {\mathbf{lambda}}\left(i\right)\le _$.
${\mathbf{ifail}}=11$
Constraint: $\left|{\mathbf{corr}}\left(i\right)\right|\le 1.0$.
${\mathbf{ifail}}=12$
Constraint: $_\le {\mathbf{sigmat}}\left(i\right)\le _$.
${\mathbf{ifail}}=13$
Constraint: ${\mathbf{var0}}>0.0$.
${\mathbf{ifail}}=14$
Quadrature has not converged to the specified accuracy. However, the result should be a reasonable approximation.
${\mathbf{ifail}}=15$
Solution cannot be computed accurately. Check values of input arguments.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

The solution is obtained by integrating the pair of ordinary differential equations over each subinterval in time. The accuracy is controlled by a relative tolerance over each time subinterval, which is set to ${10}^{-8}$. Over a number of subintervals in time the error may accumulate and so the overall error in the computation may be greater than this. A threshold of ${10}^{-10}$ is used and solutions smaller than this are not accurately evaluated.

None.

## Example

This example computes the price of a European call using Heston's stochastic volatility model with a term structure of interest rates.
```function s30nc_example

fprintf('s30nc example results\n\n');

% Price of a European call using Heston model with a term structure.

calput = 'C';
fwd    = 100;
disc   = 1;
var0   = 1;
fprintf('European Call :\n');
fprintf(' %-22s = %9.4f\n','Forward',fwd);
fprintf(' %-22s = %9.4f\n','Discount factor',disc);
fprintf(' %-22s = %9.4f\n','Variance',var0);

m      = 1;
x      = ;
n      = 2;
ts     = [ 0.35 0.65];
t      = 1;
alpha  = [ 2.25 1.5];
corr   = [-0.05 0.1];
lambda = [ 2.0  1.5];
sigmat = [ 0.04 0.13];
fprintf('\n%6s%13s%11s%8s%12s\n','ts','alpha','lambda','corr','sigmat');
for i = 1:n
fprintf('%10.4f%10.4f%10.4f%10.4f%10.4f\n',...
ts(i),alpha(i),lambda(i),corr(i),sigmat(i));
end

[p, ifail] = s30nc(...
calput, x, fwd, disc, ts, t, ...
alpha, lambda, corr, sigmat, var0);

fprintf('\n%10s%10s%10s\n','strike','expiry','price');
for i = 1:m
fprintf('%10.4f%10.4f%10.4f\n',x(i),t,p(i));
end

```
```s30nc example results

European Call :
Forward                =  100.0000
Discount factor        =    1.0000
Variance               =    1.0000

ts        alpha     lambda    corr      sigmat
0.3500    2.2500    2.0000   -0.0500    0.0400
0.6500    1.5000    1.5000    0.1000    0.1300

strike    expiry     price
100.0000    1.0000    4.0074
```