# Oil price model calibration with Kalman Filter and MLE in python

I am trying to calibrate a one-factor mean-reverting process in python 3. The process is defined as:

$$$$dX = k(\alpha - X)dt + \sigma dW ,$$$$

where $$\alpha = \mu - \frac{\sigma^2}{2k}$$ is the long-run mean log price and $$k$$ is the speed of adjustment. Under the risk neutral probability $$Q$$ we write the previous equation as

$$$$dX = k(\alpha^* - X)dt + \sigma dW^*.$$$$

Here $$\alpha^*= \alpha - \lambda$$, where $$\lambda$$ is the market price of risk. Based on Girsanov's Theorem $$dW^*$$ is a Brownian Motion under the martingale measure $$Q$$.

My measurement and transition equations are:

• The measurement equation relates the time series of observable variables, in my case futures prices for different maturities, to the unobservable state variable, the spot price:

$$$$y_{t} = Z_{t}X_{t} + d_{t} + \epsilon_{t}, \qquad t = 1, ..., NT$$$$

where

$$y_{t}=\left[\ln F\left(T_{i}\right)\right]$$, $$i=1,...,N$$, $$N\times 1$$ vector of observables,

$$d_{t}=\left[\left(1-e^{-\kappa T_{1}}\right) \alpha^{*}+\frac{\sigma^{2}}{4 \kappa}\left(1-e^{-2 k T_{1}}\right)\right], \quad i=1, \ldots, N, \quad N \times 1$$ vector,

$$Z_{t}=\left[e^{-\kappa T_{i}}\right], \quad i=1, \ldots, N, \quad N \times 1$$ vector,

$$\epsilon_{t}, \quad N\times 1$$ vector of serially uncorrelated disturbances with $$\mathbb{E}(\epsilon_{t}) = 0$$ and $$Var(\epsilon_{t}) = H$$.

• The transition equation is a discrete-time version of the O-U oil price stochastic process:

$$$$X_{t}=c_{t}+Q_{t} X_{t-1}+\eta_{t}, \quad t=1, \ldots, N T \label{eq:38},$$$$

where

$$c_{t}=\kappa \alpha \Delta t$$

$$Q_{t}=1-\kappa \Delta t$$

$$\eta_{t}$$, serially uncorrelated disturbances with $$\mathbb{E}(\eta_{t}) = 0$$ and $$Var(\eta_{t}) = \sigma^2 \Delta t$$.

The aim is to calibrate the model parameters using the time series of different maturities Futures prices via MLE. Any help with the coding part (in python 3) would be much appreciated!

• What do you have so far? Jul 15 '20 at 15:32
• Maths is done, but don't really know where to start with the code.
– gte
Jul 15 '20 at 16:43

Expanding on the answer by @ir7, here is some pykalman code/psuedocode to help get you started. This can be adjusted in many ways but I have left in some parameters to give you an idea. I left a documentation link at the bottom as well. The functions will setup Kalman Filters that are applied to your data and subsequently that data is fed to a regression that is subsequently called in a backtest function:

import pandas as pd
import numpy as np
from pykalman import KalmanFilter

def your_function_name(x):
your_filter_name = KalmanFilter(transition_matrices = [1],
observation_matrices = [1],
observation_covariance=1,
transition_covariance=.01,
initial_state_mean = 0,
initial_state_covariance = 1)

state_means, _ = kf.filter(x.values)
state_means = pd.Series(state_means.flatten(), index=x.index)
return state_means

def your_regression_filter(x, y):
delta = 1e-3
trans_cov = delta / (1 - delta) * np.eye(2)  #random walk wiggle
obs_mat = np.expand_dims(np.vstack([[x], [np.ones(len(x))]]).T, axis=1)
kf = KalmanFilter(n_dim_obs=1,
n_dim_state=2,
initial_state_mean=[0,0],
initial_state_covariance=np.ones((2, 2)),
transition_matrices=np.eye(2),
observation_matrices=obs_mat,
observation_covariance=2,
transition_covariance=trans_cov)

# Use the observations y to get running estimates and errors for the state parameters
state_means, state_covs = kf.filter(y.values)
return state_means


With these 2 functions your would define a backtest function in which you would pull state_means by calling:

state_means = your_regression_filter(your_function_name(x), your_function_name(y))

How you use state_means from here depends on you.