d03 Chapter Contents
d03 Chapter Introduction
NAG C Library Manual

# NAG Library Function Documentnag_pde_bs_1d_means (d03nec)

## 1  Purpose

nag_pde_bs_1d_means (d03nec) computes average values of a continuous function of time over the remaining life of an option. It is used together with nag_pde_bs_1d_analytic (d03ndc) to value options with time-dependent arguments.

## 2  Specification

 #include #include
 void nag_pde_bs_1d_means (double t0, double tmat, Integer ntd, const double td[], const double phid[], double phiav[], NagError *fail)

## 3  Description

nag_pde_bs_1d_means (d03nec) 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_bs_1d_analytic (d03ndc) 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 Section 8.2). This is illustrated in Section 9.

None.

## 5  Arguments

1:     t0doubleInput
On entry: the current time ${t}_{0}$.
Constraint: ${\mathbf{td}}\left[0\right]\le {\mathbf{t0}}\le {\mathbf{td}}\left[{\mathbf{ntd}}-1\right]$.
2:     tmatdoubleInput
On entry: the maturity time $T$.
Constraint: ${\mathbf{td}}\left[0\right]\le {\mathbf{tmat}}\le {\mathbf{td}}\left[{\mathbf{ntd}}-1\right]$.
3:     ntdIntegerInput
On entry: the number of discrete times at which $\varphi$ is given.
Constraint: ${\mathbf{ntd}}\ge 2$.
4:     td[ntd]const doubleInput
On entry: the discrete times at which $\varphi$ is specified.
Constraint: ${\mathbf{td}}\left[0\right]<{\mathbf{td}}\left[1\right]<\cdots <{\mathbf{td}}\left[{\mathbf{ntd}}-1\right]$.
5:     phid[ntd]const doubleInput
On entry: ${\mathbf{phid}}\left[\mathit{i}-1\right]$ must contain the value of $\varphi$ at time ${\mathbf{td}}\left[\mathit{i}-1\right]$, for $\mathit{i}=1,2,\dots ,{\mathbf{ntd}}$.
6:     phiav[$3$]doubleOutput
On exit: ${\mathbf{phiav}}\left[0\right]$ contains the value of $\varphi$ interpolated to ${t}_{0}$, ${\mathbf{phiav}}\left[1\right]$ contains the first-order average $\stackrel{^}{\varphi }$ and ${\mathbf{phiav}}\left[2\right]$ contains the second-order average $\stackrel{-}{\varphi }$, where:
 $ϕ^=1T-t0 ∫t0Tϕζdζ , ϕ-= 1T-t0 ∫t0Tϕ2ζdζ 1/2 .$
7:     failNagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{ntd}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ntd}}\ge 2$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
Unexpected failure in internal call to nag_1d_spline_interpolant (e01bac) or nag_1d_spline_evaluate (e02bbc).
NE_NOT_STRICTLY_INCREASING
On entry, ${\mathbf{td}}\left[i\right]\le {\mathbf{td}}\left[i-1\right]$, for $i=〈\mathit{\text{value}}〉$.
NE_REAL_3
On entry, t0 lies outside the range $\left[{\mathbf{td}}\left[0\right],{\mathbf{td}}\left[{\mathbf{ntd}}-1\right]\right]$: ${\mathbf{t0}}=〈\mathit{\text{value}}〉$, ${\mathbf{td}}\left[0\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{td}}\left[{\mathbf{ntd}}-1\right]=〈\mathit{\text{value}}〉$.
On entry, tmat lies outside the range $\left[{\mathbf{td}}\left[0\right],{\mathbf{td}}\left[{\mathbf{ntd}}-1\right]\right]$: ${\mathbf{tmat}}=〈\mathit{\text{value}}〉$, ${\mathbf{td}}\left[0\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{td}}\left[{\mathbf{ntd}}-1\right]=〈\mathit{\text{value}}〉$.

## 7  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.

## 8  Further Comments

### 8.1  Timing

The time taken is proportional to ntd.

### 8.2  Use with nag_pde_bs_1d_analytic (d03ndc)

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_bs_1d_means (d03nec) providing these values in phid produces an output array phiav suitable for use as the argument r in a subsequent call to nag_pde_bs_1d_analytic (d03ndc).
Time-dependent values of the continuous dividend $Q$ and the volatility $\sigma$ may be handled in the same way.

### 8.3  Algorithmic Details

The ntd data points are fitted with a cubic B-spline using the function nag_1d_spline_interpolant (e01bac). Evaluation is then performed using nag_1d_spline_evaluate (e02bbc), 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.

## 9  Example

This example demonstrates the use of the function in conjunction with nag_pde_bs_1d_analytic (d03ndc) 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_bs_1d_means (d03nec) the solution of the Black–Scholes equation by nag_pde_bs_1d_analytic (d03ndc) is also exact.
The option is valued at a range of times and stock prices.

### 9.1  Program Text

Program Text (d03nece.c)

### 9.2  Program Data

Program Data (d03nece.d)

### 9.3  Program Results

Program Results (d03nece.r)