NFCP_Kalman_filter {NFCP} | R Documentation |
Filter an N-factor commodity pricing model though the Kalman filter
Description
Given a set of parameters of the N-factor model, filter term structure data using the Kalman filter.
Usage
NFCP_Kalman_filter(
parameter_values,
parameter_names,
log_futures,
dt,
futures_TTM,
ME_TTM = NULL,
verbose = FALSE,
debugging = FALSE,
seasonal_trend = NULL
)
Arguments
parameter_values |
|
parameter_names |
|
log_futures |
|
dt |
|
futures_TTM |
|
ME_TTM |
|
verbose |
|
debugging |
|
seasonal_trend |
|
Details
NFCP_Kalman_filter
applies the Kalman filter algorithm for observable log_futures
prices against the input parameters of an N-factor model
provided through the parameter_values
and parameter_names
input vectors.
The NFCP_Kalman_filter
function is
designed for subsequent input into optimization functions and is called within the N-factor parameter estimation function NFCP_MLE
. The first input to the
NFCP_Kalman_filter
function is a vector of parameters of an
N-factor model, with elements of this vector corresponding to the parameter names within the elements of input vector parameter_names
.
When logical
input verbose = F
, the NFCP_Kalman_filter
function calls the fkf_SP
function of the FKF_SP
package, which itself is a wrapper
of a routine of the Kalman filter written in C utilizing Sequential Processing for maximum computational efficiency (see fkf_SP
for more details). When verbose = T
,
the NFCP_Kalman_filter
instead applies a Kalman filter algorithm written in base R
and outputs several other list objects
, including filtered values and
measures for model fit and robustness (see Returns)
The N-factor model The N-factor framework was first presented in the work of Cortazar and Naranjo (2006, equations 1-3). It is a risk-premium class of commodity pricing model, in which futures prices are given by discounted expected future spot prices, where these spot prices are discounted at a given level of risk-premium, known as the cost-of-carry.
The N-factor framework describes the spot price process of a commodity as the correlated sum of \(N\) state variables \(x_t\). The 'NFCP' package also allows for a deterministic, cyclical seasonal function \(season(t)\) to be considered.
When GBM = TRUE
:
\[log(S_{t}) = season(t) + \sum_{i=1}^N x_{i,t}\]
When GBM = FALSE
:
\[log(S_{t}) = E + season(t) + \sum_{i=1}^N x_{i,t}\]
Where GBM
determines whether the first factor follows a Brownian Motion or Ornstein-Uhlenbeck process to induce a unit root in the spot price process.
When GBM = TRUE
, the first factor corresponds to the spot price, and additional N-1 factors model the cost-of-carry.
When GBM = FALSE
, the commodity model assumes that there is a long-term equilibrium the commodity price will tend towards over time, with model volatility a decreasing function of time. This is not the standard approach made in the commodity pricing literature (Cortazar and Naranjo, 2006).
State variables are thus assumed to follow the following processes:
When GBM = TRUE
:
\[dx_{1,t} = \mu^*dt + \sigma_{1} dw_{1}t\]
When GBM = FALSE
:
\[dx_{1,t} = - (\lambda_{1} + \kappa_{1}x_{1,t})dt + \sigma_{1} dw_{1}t\]
And: \[dx_{i,t} =_{i\neq1} - (\lambda_{i} + \kappa_{i}x_{i,t})dt + \sigma_{i} dw_{i}t\]
where: \[E(w_{i})E(w_{j}) = \rho_{i,j}\]
Additionally, the deterministic seasonal function (if specified) is given by:
\[season(t) = \sum_{i=1} ( season_{i,1} cos(2i\pi) + season_{i,2} sin(2i\pi)\]The addition of deterministic, cyclical seasonality as a function of trigonometric variables was first suggested by Hannan, Terrell, and Tuckwell (1970) and first applied to model commodities by Sørensen (2002).
The following constant parameters are defined as:
var
\(\mu\): long-term growth rate of the Brownian Motion process.
var
\(E\): Constant equilibrium level.
var
\(\mu^*=\mu-\lambda_1\): Long-term risk-neutral growth rate
var
\(\lambda_{i}\): Risk premium of state variable \(i\).
var
\(\kappa_{i}\): Reversion rate of state variable \(i\).
var
\(\sigma_{i}\): Instantaneous volatility of state variable \(i\).
var
\(\rho_{i,j} \in [-1,1]\): Instantaneous correlation between state variables \(i\) and \(j\).
Including additional factors within the spot-price process allow for additional flexibility (and possibly fit) to the term structure of a commodity. The N-factor model nests simpler models within its framework, allowing for the fit of different N-factor models (applied to the same term structure data), represented by the log-likelihood, to be directly compared with statistical testing possible through a chi-squared test.
Disturbances - Measurement Error:
The Kalman filtering algorithm assumes a given measure of measurement error or disturbance in the measurement equation (ie. matrix \(H\)). Measurement errors can be interpreted as error in the model's fit to observed prices, or as errors in the reporting of prices (Schwartz and Smith, 2000). These disturbances are typically assumed independent.
var
\(ME_i\) measurement error of contract \(i\).
where the measurement error of futures contracts \(ME_i\) is equal to 'ME_'
[i] (i.e. 'ME_1'
, 'ME_2'
, ...) specified in arguments parameter_values
and parameter_names
.
There are three particular cases on how the measurement error of observations can be treated in the NFCP_Kalman_filter
function:
Case 1: Only one ME is specified. The Kalman filter assumes that the measurement error of observations are independent and identical.
Case 2: One ME is specified for every observed futures contract. The Kalman filter assumes that the measurement error of observations are independent and unique.
Case 3: A series of ME's are specified for a given grouping of maturities of futures contracts. The Kalman filter assumes that the measurement error of observations are independent and unique to their respective time-to-maturity.
Grouping of maturities for case 3 is specified through the ME_TTM
argument. This is a vector that specifies the maximum maturity to consider for each respective ME parameter argument.
in other words, ME_1 is considered for observations with TTM less than ME_TTM[1], ME_2 is considered for observations with TTM less than ME_TTM[2], ..., etc.
The first case is clearly the simplest to estimate, but can be a restrictive assumption. The second case is clearly the most difficult to estimate, but can be an infeasible assumption when considering all available futures contracts that make up the term structure of a commodity.
Case 3 thus serves to ease the restriction of case 1, and allow the user to make the modeling of measurement error as simple or complex as desired for a given set of maturities.
Kalman Filtering
The following section describes the Kalman filter equations used to filter the N-factor model.
The Kalman filter iteration is characterised by a transition and measurement equation. The transition equation develops the vector of state variables between discretised time steps (whilst considering a given level of covariance between state variables over time). The measurement equation relates the unobservable state vector to a vector of observable measurements (whilst also considering a given level of measurement error). The typical Kalman filter algorithm is a Gaussian process state space model.
Transition Equation: \[\hat x_{t|t-1} = c_t + G_t \hat x_{t-1} + Q_t \eta_t \] Measurement Equation: \[\hat y_t = d_t + Z_t \hat x_{t|t-1} + H_t \epsilon_t\]
\[t = 1, \cdots, n \]Where \(\eta_t\) and \(\epsilon_t\) are IID \(N(0,I(m))\) and iid \(N(0,I(d))\) respectively.
The state vector follows a normal distribution, \(x_1 \sim N(a_1, P_1)\), with \(a_1\) and \(P_1\) as the mean vector and variance matrix of the initial state vector \(x_1\), respectively.
The Kalman filter can be used for parameter estimation through the maximization of the Log-Likelihood value. See NFCP_MLE
.
Filtering the N-factor model
let \(m\) represent the number of observations at time \(t\)
let \(n\) represent the number of factors in the N-factor model
observable futures prices: \(y_t = [ln(F(t,T_1)), ln(F(t,T_2)), \cdots, ln(F(t,T_m))]'\)
State vector: \(x_t=[x_1t,x_2t,\cdots,x_nt ]'\)
Measurement error: \(diag(H) = [ME_{1}^2, ME_{2}^2, \cdots, ME_{n}^2]\)
When the number of specified ME terms is one, \(s_1 = s_2 = \cdots = s_n = \) \(ME_1^2\)
var
\(Z\) is an \(m \times n\) matrix, where each element \([i,j]\) is equal to:
var
\(d_t\) is an \(m \times 1\) vector:
Under the assumption that Factor 1 follows a Brownian Motion, A(T) is given by: \[A(T) = \mu^*T-\sum_{i=1}^N - \frac{1-e^{-\kappa_i T}\lambda_i}{\kappa_i}+\frac{1}{2}(\sigma_1^2T + \sum_{i.j\neq 1} \sigma_i \sigma_j \rho_{i,j} \frac{1-e^{-(\kappa_i+\kappa_j)T}}{\kappa_i+\kappa_j})\]
var
\(v_t\) is a \(n \times 1\) vector of serially uncorrelated Guassian disturbances with
\(E(V_t) = 0\) and \(cov(v_t)=R^2\)
Where:
\(diag(G_t) = [e^{-\kappa_1 \tau}, e^{-\kappa_2 \tau}, \cdots, e^{-\kappa_n \tau}]\)
Where \( \tau =T-t\)
var
\(w_t\) is an \(n \times 1\) vector of serially uncorrelated Guassian disturbances where:
\[E(w_t) = 0\] and \(cov(w_t) = Q_t\)
var
\(c_t=[\mu \Delta t,0,\cdots,0]'\) is an \(N \times 1\) vector of the intercept of the transition equation.
var
\(Q_t\) is equal to the covariance function, given by:
(see also cov_func
)
Penalising poorly specified models
The Kalman filter returns non-real log-likelihood scores when the prediction error variance matrix becomes singular or its determinant becomes negative. This generally occurs when a poorly specified parameter set is input, such as when measurement error is zero.
Non-real log-likelihood scores can break optimization and gradients algorithms and functions. To circumvent this, the NFCP_Kalman_filter
returns a heavily penalized log-likelihood score when verbose = F
. Penalized log-likelihood scores are calculated by:
stats::runif(1, -2e6, -1e6)
Diffuse Kalman filtering
If the initial values of the state vector are not supplied within the parameter_names
and parameter_values
vectors, a 'diffuse' assumption is used within the Kalman filtering algorithm.
Initial states of factors that follow an Ornstein-Uhlenbeck are assumed to equal zero.
The initial state of the first factor, given that it follows a Brownian motion, is assumed equal to the first element of log_futures
. This is an
assumption that the initial estimate of the spot price is equal to the closest to maturity observed futures price.
The initial states of factors that follow an Ornstein-Uhlenbeck have a transient effect on future observations. This makes the diffuse assumption reasonable and further means that initial states cannot generally be accurately estimated.
Value
NFCP_Kalman_filter
returns a numeric
object when verbose = F
, which corresponds to the log-likelihood of observations.
When verbose = T
, the NFCP_Kalman_filter
function returns a list
object of length seven with the following objects:
Log-Likelihood | Log-Likelihood of observations. |
Information Criteria | vector . The Akaikie and Bayesian Information Criterion. |
X_t | vector . The final observation of the state vector. |
X | matrix . Optimal one-step-ahead state vector. |
Y | matrix . Estimated futures prices. |
V | matrix . Estimation error. |
Filtered Error | matrix . positive mean error (high bias), negative mean error (low bias),
mean error (bias) and root mean squared error (RMSE)
of the filtered values to observed futures prices. |
Term Structure Fit | matrix . The mean error (Bias), mean absolute error, standard deviation of error
and root mean squared error (RMSE) of each observed futures contract. |
Term Structure Volatility Fit | matrix . Theoretical and empirical volatility of observed futures contract returns. |
When debugging = T
, 9 objects are returned in addition to those returned when verbose = T
:
P_t | array . Covariance matrix of state variables, with the third dimension indexing across time |
F_t | vector . Prediction error variance matrix, with the third dimension indexing across time |
K_t | matrix . Kalman Gain, with the third dimension indexing across time |
d | matrix . \(d_t\) (see details) |
Z | matrix . \(Z_t\) (see details) |
G_t | matrix . \(G_t\) (see details) |
c_t | vector . \(C_t\) (see details) |
Q_t | matrix . \(Q_t\) (see details) |
H | matrix . \(H\) (see details) |
References
Hannan, E. J., et al. (1970). "The seasonal adjustment of economic time series." International economic review, 11(1): 24-52.
Anderson, B. D. O. and J. B. Moore, (1979). Optimal filtering Englewood Cliffs: Prentice-Hall.
Fahrmeir, L. and G. tutz,(1994) Multivariate Statistical Modelling Based on Generalized Linear Models. Berlin: Springer.
Schwartz, E. S., and J. E. Smith, (2000). Short-Term Variations and Long-Term Dynamics in Commodity Prices. Manage. Sci., 46, 893-911.
Sørensen, C. (2002). "Modeling seasonality in agricultural commodity futures." Journal of Futures Markets: Futures, Options, and Other Derivative Products 22(5): 393-426.
Cortazar, G., and L. Naranjo, (2006). An N-factor Gaussian model of oil futures prices. Journal of Futures Markets: Futures, Options, and Other Derivative Products, 26(3), 243-268.
Durbin, J., and S. J. Koopman, (2012). Time series analysis by state space methods. Oxford university press.
Examples
##Example 1 - complete, stitched data.
##Replicating the Schwartz and Smith (2000)
##Two-Factor commodity pricing model applied to crude oil:
SS_stitched_filtered <- NFCP_Kalman_filter(
parameter_values = SS_oil$two_factor,
parameter_names = names(SS_oil$two_factor),
log_futures = log(SS_oil$stitched_futures),
futures_TTM = SS_oil$stitched_TTM,
## maturity groupings need not be considered here:
ME_TTM = NULL,
dt = SS_oil$dt,
verbose = FALSE)
##Example 2 - incomplete, contract data.
##Replicating the Schwartz and Smith (2000)
##Two-Factor commodity pricing model applied to all available
##crude oil contracts:
SS_2F <- SS_oil$two_factor
##omit stitched contract white noise
SS_2F <- SS_2F[!grepl("ME",
names(SS_2F))]
# Evaluate two different measurement errors
SS_2F[c("ME_1", "ME_2")] <- c(0.01, 0.04)
## Separate measurement error into two different maturity groupings
SS_ME_TTM <- c(1,3)
## ME_1 is applied for observed contracts with less than one year
## maturity, whilst ME_2 considers contracts with maturity greater
## than one year, and less than three years
#Kalman filter
SS_contract_filtered <- NFCP_Kalman_filter(
parameter_values = SS_2F,
parameter_names = names(SS_2F),
## All available contracts are considered
log_futures = log(SS_oil$contracts),
## Respective 'futures_TTM' of these contracts are input:
futures_TTM = SS_oil$contract_maturities,
ME_TTM = SS_ME_TTM,
dt = SS_oil$dt,
verbose = FALSE)