# NAG FL Interfaces30ncf (opt_​heston_​term)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

s30ncf computes the European option price given by Heston's stochastic volatility model with term structure.

## 2Specification

Fortran Interface
 Subroutine s30ncf ( m, x, fwd, disc, ts, t, corr, var0, p,
 Integer, Intent (In) :: m, numts Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(m), fwd, disc, ts(numts), t, alpha(numts), lambda(numts), corr(numts), sigmat(numts), var0 Real (Kind=nag_wp), Intent (Out) :: p(m) Character (1), Intent (In) :: calput
#include <nag.h>
 void s30ncf_ (const char *calput, const Integer *m, const Integer *numts, const double x[], const double *fwd, const double *disc, const double ts[], const double *t, const double alpha[], const double lambda[], const double corr[], const double sigmat[], const double *var0, double p[], Integer *ifail, const Charlen length_calput)
The routine may be called by the names s30ncf or nagf_specfun_opt_heston_term.

## 3Description

s30ncf 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 Wt(1) ,$ (1)
 $d νt = λt (1-νt) d t+ αt νt d Wt(2) ,$ (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 μsds) .$ (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) can be recovered by setting $\kappa ={\lambda }_{t}$, $\eta ={\sigma }_{t}^{2}$, ${\sigma }_{v}={\sigma }_{t}{\alpha }_{t}$ and ${V}_{0}={\sigma }_{t}^{2}{V}_{0}$.
Conversely, given the Heston form of the SDE pair, to get the term structure form set ${\lambda }_{t}=\kappa$, ${\sigma }_{t}=\sqrt{\eta }$, ${\alpha }_{t}=\frac{{\sigma }_{v}}{\sqrt{\eta }}$ and ${V}_{0}=\frac{{V}_{0}}{\eta }$.
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

## 5Arguments

1: $\mathbf{calput}$Character(1) Input
On entry: 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: $\mathbf{m}$Integer Input
On entry: $m$, the number of strike prices to be used.
Constraint: ${\mathbf{m}}\ge 1$.
3: $\mathbf{numts}$Integer Input
On entry: ${n}_{s}$, the number of subintervals into which the time to expiry, $T$, is divided.
Constraint: ${\mathbf{numts}}\ge 1$.
4: $\mathbf{x}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\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{x02amf}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}$.
5: $\mathbf{fwd}$Real (Kind=nag_wp) Input
On entry: the forward price of the asset.
Constraint: ${\mathbf{fwd}}\ge z$ and ${\mathbf{fwd}}\le 1/z$, where $z={\mathbf{x02amf}}\left(\right)$, the safe range parameter.
6: $\mathbf{disc}$Real (Kind=nag_wp) Input
On entry: 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{x02amf}}\left(\right)$, the safe range parameter.
7: $\mathbf{ts}\left({\mathbf{numts}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\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{x02amf}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
8: $\mathbf{t}$Real (Kind=nag_wp) Input
On entry: 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{x02amf}}\left(\right)$, the safe range parameter.
9: $\mathbf{alpha}\left({\mathbf{numts}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\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{x02amf}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
10: $\mathbf{lambda}\left({\mathbf{numts}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\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{x02amf}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
11: $\mathbf{corr}\left({\mathbf{numts}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\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}}$.
12: $\mathbf{sigmat}\left({\mathbf{numts}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\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{x02amf}}\left(\right)$, the safe range parameter, for $\mathit{i}=1,2,\dots ,{\mathbf{numts}}$.
13: $\mathbf{var0}$Real (Kind=nag_wp) Input
On entry: ${\nu }_{0}$, the initial scaled variance.
Constraint: ${\mathbf{var0}}\ge 0.0$.
14: $\mathbf{p}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Output
On exit: ${\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}}$.
15: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-1$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $-1$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{calput}}=⟨\mathit{\text{value}}⟩$ was an illegal value.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{numts}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{numts}}\ge 1$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{x}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: $⟨\mathit{\text{value}}⟩\le {\mathbf{x}}\left(i\right)\le ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{fwd}}=⟨\mathit{\text{value}}⟩$.
Constraint: $⟨\mathit{\text{value}}⟩\le {\mathbf{fwd}}\le ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=6$
On entry, ${\mathbf{disc}}=⟨\mathit{\text{value}}⟩$.
Constraint: $⟨\mathit{\text{value}}⟩\le {\mathbf{disc}}\le ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{ts}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: $⟨\mathit{\text{value}}⟩\le {\mathbf{ts}}\left(i\right)\le ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=8$
On entry, ${\mathbf{t}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{t}}\ge ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=9$
On entry, ${\mathbf{alpha}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: $⟨\mathit{\text{value}}⟩\le {\mathbf{alpha}}\left(i\right)\le ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=10$
On entry, ${\mathbf{lambda}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: $⟨\mathit{\text{value}}⟩\le {\mathbf{lambda}}\left(i\right)\le ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=11$
On entry, ${\mathbf{corr}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: $|{\mathbf{corr}}\left(i\right)|\le 1.0$.
${\mathbf{ifail}}=12$
On entry, ${\mathbf{sigmat}}\left(⟨\mathit{\text{value}}⟩\right)=⟨\mathit{\text{value}}⟩$.
Constraint: $⟨\mathit{\text{value}}⟩\le {\mathbf{sigmat}}\left(i\right)\le ⟨\mathit{\text{value}}⟩$.
${\mathbf{ifail}}=13$
On entry, ${\mathbf{var0}}=⟨\mathit{\text{value}}⟩$.
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$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

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.

## 8Parallelism and Performance

s30ncf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

None.

## 10Example

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

### 10.1Program Text

Program Text (s30ncfe.f90)

### 10.2Program Data

Program Data (s30ncfe.d)

### 10.3Program Results

Program Results (s30ncfe.r)