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_pde_1d_blackscholes_means (d03ne)

## Purpose

nag_pde_1d_blackscholes_means (d03ne) computes average values of a continuous function of time over the remaining life of an option. It is used together with nag_pde_1d_blackscholes_closed (d03nd) to value options with time-dependent arguments.

## Syntax

[phiav, ifail] = d03ne(t0, tmat, td, phid, 'ntd', ntd)
[phiav, ifail] = nag_pde_1d_blackscholes_means(t0, tmat, td, phid, 'ntd', ntd)

## Description

nag_pde_1d_blackscholes_means (d03ne) computes the quantities
 $ϕt0, ϕ^=1T-t0 ∫t0Tϕζdζ, ϕ-= 1T-t0 ∫t0Tϕ2ζdζ 1/2$
from a given set of values phid of a continuous time-dependent function $\varphi \left(t\right)$ at a set of discrete points td, where ${t}_{0}$ is the current time and $T$ is the maturity time. Thus $\stackrel{^}{\varphi }$ and $\stackrel{-}{\varphi }$ are first and second order averages of $\varphi$ over the remaining life of an option.
The function may be used in conjunction with nag_pde_1d_blackscholes_closed (d03nd) in order to value an option in the case where the risk-free interest rate $r$, the continuous dividend $q$, or the stock volatility $\sigma$ is time-dependent and is described by values at a set of discrete times (see Use with ). This is illustrated in Example.

None.

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{t0}$ – double scalar
The current time ${t}_{0}$.
Constraint: ${\mathbf{td}}\left(1\right)\le {\mathbf{t0}}\le {\mathbf{td}}\left({\mathbf{ntd}}\right)$.
2:     $\mathrm{tmat}$ – double scalar
The maturity time $T$.
Constraint: ${\mathbf{td}}\left(1\right)\le {\mathbf{tmat}}\le {\mathbf{td}}\left({\mathbf{ntd}}\right)$.
3:     $\mathrm{td}\left({\mathbf{ntd}}\right)$ – double array
The discrete times at which $\varphi$ is specified.
Constraint: ${\mathbf{td}}\left(1\right)<{\mathbf{td}}\left(2\right)<\cdots <{\mathbf{td}}\left({\mathbf{ntd}}\right)$.
4:     $\mathrm{phid}\left({\mathbf{ntd}}\right)$ – double array
${\mathbf{phid}}\left(\mathit{i}\right)$ must contain the value of $\varphi$ at time ${\mathbf{td}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{ntd}}$.

### Optional Input Parameters

1:     $\mathrm{ntd}$int64int32nag_int scalar
Default: the dimension of the arrays td, phid. (An error is raised if these dimensions are not equal.)
The number of discrete times at which $\varphi$ is given.
Constraint: ${\mathbf{ntd}}\ge 2$.

### Output Parameters

1:     $\mathrm{phiav}\left(3\right)$ – double array
${\mathbf{phiav}}\left(1\right)$ contains the value of $\varphi$ interpolated to ${t}_{0}$, ${\mathbf{phiav}}\left(2\right)$ contains the first-order average $\stackrel{^}{\varphi }$ and ${\mathbf{phiav}}\left(3\right)$ contains the second-order average $\stackrel{-}{\varphi }$, where:
 $ϕ^=1T-t0 ∫t0Tϕζdζ , ϕ-= 1T-t0 ∫t0Tϕ2ζdζ 1/2 .$
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, t0 lies outside the range [${\mathbf{td}}\left(1\right),{\mathbf{td}}\left({\mathbf{ntd}}\right)$], or tmat lies outside the range [${\mathbf{td}}\left(1\right),{\mathbf{td}}\left({\mathbf{ntd}}\right)$], or ${\mathbf{ntd}}<2$, or td badly ordered, or $\mathit{lwork}<9×{\mathbf{ntd}}+24$.
${\mathbf{ifail}}=2$
Unexpected failure in internal call to nag_interp_1d_spline (e01ba) or nag_fit_1dspline_eval (e02bb).
${\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

If $\varphi \in {C}^{4}\left[{t}_{0},T\right]$ then the error in the approximation of $\varphi \left({t}_{0}\right)$ and $\stackrel{^}{\varphi }$ is $\mathit{O}\left({H}^{4}\right)$, where $H=\underset{i}{\mathrm{max}}\phantom{\rule{0.25em}{0ex}}\left(T\left(i+1\right)-T\left(i\right)\right)$, for $i=1,2,\dots ,{\mathbf{ntd}}-1$. The approximation is exact for polynomials of degree up to $3$.
The third quantity $\stackrel{-}{\varphi }$ is $\mathit{O}\left({H}^{2}\right)$, and exact for linear functions.

### Timing

The time taken is proportional to ntd.

### Use with nag_pde_1d_blackscholes_closed (d03nd)

Suppose you wish to evaluate the analytic solution of the Black–Scholes equation in the case when the risk-free interest rate $r$ is a known function of time, and is represented as a set of values at discrete times. A call to nag_pde_1d_blackscholes_means (d03ne) providing these values in phid produces an output array phiav suitable for use as the argument r in a subsequent call to nag_pde_1d_blackscholes_closed (d03nd).
Time-dependent values of the continuous dividend $Q$ and the volatility $\sigma$ may be handled in the same way.

### Algorithmic Details

The ntd data points are fitted with a cubic B-spline using the function nag_interp_1d_spline (e01ba). Evaluation is then performed using nag_fit_1dspline_eval (e02bb), and the definite integrals are computed using direct integration of the cubic splines in each interval. The special case of $T={t}_{o}$ is handled by interpolating $\varphi$ at that point.

## Example

This example demonstrates the use of the function in conjunction with nag_pde_1d_blackscholes_closed (d03nd) to solve the Black–Scholes equation for valuation of a $5$-month American call option on a non-dividend-paying stock with an exercise price of \$50. The risk-free interest rate varies linearly with time and the stock volatility has a quadratic variation. Since these functions are integrated exactly by nag_pde_1d_blackscholes_means (d03ne) the solution of the Black–Scholes equation by nag_pde_1d_blackscholes_closed (d03nd) is also exact.
The option is valued at a range of times and stock prices.
```function d03ne_example

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

tmat = 0.4166667;
td   = [0:0.1:0.5];
rd   = [0.1:0.01:0.15];
sigd = [0.3 0.46 0.54 0.54 0.36 0.3];

% American 5-month call option, exercise price 50

kopt = int64(2);
x = 50;
ns    = 21;  nt    = 4;
s_beg = 0;   t_beg = 0;
s_end = 100; t_end = 0.125;
tmat  = 0.4166667;
tdpar = [true; false; true];
q     = [0];

% Discretize s and t
ds = (s_end-s_beg)/(ns-1);
dt = (t_end-t_beg)/(nt-1);
s = [s_beg:ds:s_end];
t = [t_beg:dt:t_end];

f = zeros(ns,nt);
theta = f; delta = f; gamma = f; lambda = f; rho = f;

% Loop over times and prices
for j = 1:nt

% Find average values of r and sigma
[ra, ifail] = d03ne( ...
t(j), tmat, td, rd);
[siga, ifail] = d03ne( ...
t(j), tmat, td, sigd);

for i = 1:ns
[f(i,j),theta(i,j),delta(i,j),gamma(i,j),lambda(i,j),rho(i,j),ifail] = ...
d03nd( ...
kopt, x, s(i), t(j), tmat, tdpar, ra, q, siga);
end
end

% Tabulate option values only
print_greek(ns,nt,tmat,s,t,'Option Values',f);
% print_greek(ns,nt,tmat,s,t,'Theta',theta);
% print_greek(ns,nt,tmat,s,t,'Delta',delta);
% print_greek(ns,nt,tmat,s,t,'Gamma',gamma);
% print_greek(ns,nt,tmat,s,t,'Lambda',lambda);
% print_greek(ns,nt,tmat,s,t,'Rho',rho);

% plot initial and final option values and greeks
fig1 = figure;
plot(s,f(:,1),s,theta(:,1),s,delta(:,1),s,gamma(:,1),s,lambda(:,1),s,rho(:,1));
legend('value','theta','delta','gamma','lambda','rho','Location','NorthWest');
title('Option values and greeks at 5 months to maturity');
xlabel('stock price');
ylabel('values and derivatives');
fig2 = figure;
plot(s,f(:,4),s,theta(:,4),s,delta(:,4),s,gamma(:,4),s,lambda(:,4),s,rho(:,4));
legend('value','theta','delta','gamma','lambda','rho','Location','NorthWest');
title('Option values and greeks at 3.5 months to maturity');
xlabel('stock price');
ylabel('values and derivatives');

function print_greek(ns,nt,tmat,s,t,grname,greek)

fprintf('\n%s\n\n',grname);
fprintf('  Stock Price  |   Time to Maturity (months)\n');
fprintf('%16s %12.4e%12.4e%12.4e%12.4e\n', '|', 12*(tmat-t));
fprintf('%15s+%48s\n', '---------------', ...
'-------------------------------------------------');
for i = 1:ns
fprintf('%12.4e%4s %12.4e%12.4e%12.4e%12.4e\n', s(i), '|', greek(i,:));
end
```
```d03ne example results

Option Values

Stock Price  |   Time to Maturity (months)
|   5.0000e+00  4.5000e+00  4.0000e+00  3.5000e+00
---------------+-------------------------------------------------
0.0000e+00   |   0.0000e+00  0.0000e+00  0.0000e+00  0.0000e+00
5.0000e+00   |   8.3570e-14  1.6081e-14  1.1216e-15  1.9522e-17
1.0000e+01   |   2.5775e-07  1.0991e-07  2.8081e-08  3.5715e-09
1.5000e+01   |   1.7581e-04  1.0511e-04  4.6513e-05  1.3698e-05
2.0000e+01   |   6.9193e-03  4.9696e-03  2.9591e-03  1.3701e-03
2.5000e+01   |   7.0752e-02  5.6767e-02  4.0397e-02  2.4520e-02
3.0000e+01   |   3.4255e-01  2.9499e-01  2.3506e-01  1.6927e-01
3.5000e+01   |   1.0512e+00  9.4849e-01  8.1382e-01  6.5475e-01
4.0000e+01   |   2.3997e+00  2.2341e+00  2.0134e+00  1.7424e+00
4.5000e+01   |   4.4829e+00  4.2630e+00  3.9702e+00  3.6055e+00
5.0000e+01   |   7.2786e+00  7.0226e+00  6.6859e+00  6.2677e+00
5.5000e+01   |   1.0687e+01  1.0414e+01  1.0063e+01  9.6324e+00
6.0000e+01   |   1.4580e+01  1.4305e+01  1.3959e+01  1.3546e+01
6.5000e+01   |   1.8832e+01  1.8563e+01  1.8236e+01  1.7855e+01
7.0000e+01   |   2.3337e+01  2.3079e+01  2.2774e+01  2.2429e+01
7.5000e+01   |   2.8016e+01  2.7768e+01  2.7485e+01  2.7173e+01
8.0000e+01   |   3.2809e+01  3.2573e+01  3.2308e+01  3.2022e+01
8.5000e+01   |   3.7678e+01  3.7450e+01  3.7201e+01  3.6935e+01
9.0000e+01   |   4.2595e+01  4.2374e+01  4.2136e+01  4.1885e+01
9.5000e+01   |   4.7543e+01  4.7327e+01  4.7097e+01  4.6856e+01
1.0000e+02   |   5.2510e+01  5.2298e+01  5.2074e+01  5.1840e+01
```