Kalman filter is, in certain sense, a way to give the moving average of a time series. It keeps track on a vector of state variables with its corresponding covariance matrix. The Kalman algorithm usually give a converging covariance matrix after several iterations of prediction-update. A common use is to give educated guess of the current state, and prediction (in range of value) of a future state with level of confidence. In the case of value-at-risk of a portfolio, we will need the covariance matrix of return of the assets in the portfolio. Therefore we might be able to make use of Kalman filter to find the VaR.

Below are some related papers that I found. The symbols we used are standardized at the bottom (adapted from the Kalman cheatsheet).

## Arnold et al (2008) A simplified approach to understanding the Kalman filter technique

@article{
author = {Tom Arnold and Mark J. Bertus and Jonathan Godbey},
title = {A simplified approach to understanding the Kalman filter technique},
journal = {The Engineering Journal},
volume = 53,
pages = {140--155},
year = 2008,
issn = {0013-791X},
doi = {10.1080/00137910802058574},
url = {https://scholarship.richmond.edu/finance-faculty-publications/8/},
}


This paper is not about VaR but this is an interesting paper to explain how to use Kalman filter for price prediction, without matrix notation and use scalar instead, and with algorithm examples in Excel.

The paper covers only the single variable time series, with the equations:

• State transition: $x_{t+1\mid t} = a_t x_{t\mid t} + u_t + w_t$
• Prediction: $z_{t+1\mid t} = h_t x_{t+1\mid t} + b_{t+1} + v_{t+1}$
• Measurement error: $y_{t+1} = z_{t+1} - z_{t+1\mid t}$
• Update: $x_{t+1\mid t+1} = x_{t+1\mid t} + k_{t+1}y_{t+1}$
• Kalman gain: $k_{t+1} = \dfrac{\mathrm{Cov}[x_{t+1\mid t}, z_{t+1\mid t}]}{\mathrm{Var}[z_{t+1\mid t}]}$

In the paper, we assumed $h_t$ is known, and $u_t, a_t$ are constant over time, $w_t, v_t$ are drawn from normal distribution with zero mean and constant variance. The best result is when the predicted measurement fits the observation. Very often we do not know how to set these model parameters. But we can figure out the best fit parameters by maximum likelihood. Over $T$ periods, the probability of seeing $z_t$ given we assumed $z_{t\mid t-1} \sim N(\mu_t, \sigma^2_t)$ (normal distribution) is:

and taking log will become

By maximizing (expectation maximization (EM) algorithm) the above quantity over model variables $a, \sigma^2_w, \sigma^2_v$, we obtain the optimal filter result.

The paper gives an example and implemented the Kalman algorithm in Excel. Assume in oil market, we have spot market which the price is not observable but oil futures are actively traded on organized exchange with observable price. Assume spot price $S_t$ as state variable and future price is the future value of spot price with interest rate $r$ and maturity $\tau$:

The paper generate a random time series with the gBm on $S_t$ and computed the corresponding $F_t$ in Excel. Then assume we do not know the $S_t$ but only $F_t$ is observable and operate on the logarithm domain, $x_t=\log S_t$ and $z_t=\log F_t$. Then the Kalman algorithm is provided by:

And in Excel, for convenience of composing the equation, two MLE metrics are computed as well:

which are the two summation terms in the log-likelihood equation above. The paper suggest to use the solver add-on in Excel to optimize for the maximum likelihood and find the best-fit parameters for the model (fit only to $F_t$ as $S_t$ is assumed not available).

## Xu & Zhang (2015) Application of Kalman filter in prediction of stock price

@inproceedings{
author = {Yan Xu and Guosheng Zhang},
title = {Application of Kalman Filter in the Prediction of Stock Price},
booktitle = {Proc International Symposium on Knowledge Acquisition and Modeling (KAM)},
year = 2015,
}


This is a 2-page paper giving an easy-to-understand example of using Kalman filter to predict stock price in short future. Here we have only one time series and the state variable is observable up to present ($z_t=x_t$). Represent the price as $x_t$, the system is modelled as:

or in matrix form, $\mathbf{x}_{t+1} = \mathbf{Fx}_t + w_t \mathbf{\Gamma}$ and $z_t = \mathbf{Hx}_t + v_t$. $T=1$ is used for simplicity. And the Kalman equations:
(initialization)

(prediction)

(Kalman gain)

(update)

where $\mathbf{Q}$ and $\mathbf{R}$ are the variance “matrices” (scalar in this case) of noise terms $w_t$ and $v_t$. And we define $\dot{x}_t=\dfrac{x_t - x_{t-1}}{T}$. It is common to have both $x_t$ and $\dot{x}_t$ in the state variable in this sort of use case.

## Bassett et al (1991) Kalman filter estimation for valuing nontrading securities

@article{
author = {Gilbert W. Bassett, Jr and Virginia G. France and Stanley R. Pliska},
title = {Kalman Filter Estimation for Valuing Nontrading Securities, with Applications to the MMI Cash-Future Spread on October 19 and 20, 1987},
journal = {Review of Quantitative Finance and Accounting},
volume = 1,
pages = 135--151,
year = 1991,
url = {https://gib.people.uic.edu/Kalman%20Filter.pdf},
}


Another example of using Kalman filter for prediction. It uses a 0-1 matrix as measurement matrix $H$ (direct observation to price but only some of them) and use the predicted price in place of non-observed prices. The state variable $X$ is the true price, measured every minute, and the measurement $Z$ is the traded subset of $X$, i.e., the trade price.

This paper suggested some ways to set up the parameters: The covariance matrix $Q$ of the state transition noise is the empirical covariance from the whole last day. The covariance matrix $R$ of the measurement noise is chosen based on the bid-ask spread of 1/8 tick size, $R=0.005I$. The initial state $X_0$ is chosen to be the last closing price. The tricky part is the initial variance $P_0$. It is evaluated from previous data on non-trading minutes. It answers the question of what will best fit the dispersion on state variable $X$ if there is no measurement for $n$ periods (minutes). By measuring the dispersion over past non-trading minutes, variance $P_0$ is computed.

## Cortazar et al (2003) Term structure estimation in low-frequency transaction markets

@techreport{
author = {Gonzalo Cortazar and Eduardo S. Schwartz and Lorenzo Naranjo},
title = {Term Structure Estimation in Low-Frequency Transaction Markets: A Kalman Filter Approach with Incomplete Panel-Data},
institution = {EFA 2004 Maastricht Meetings Paper},
number = 3102,
month = March,
year = 2004,
url = {https://papers.ssrn.com/sol3/papers.cfm?abstract_id=567090},
url = {https://pdfs.semanticscholar.org/f164/1f1f36b36a8e4df549091afb1655421b8228.pdf},
}


This paper focus on thin (illiquid) markets. It uses Kalman filter to estimate the missing observations. Then use historical price data and a dynamic model to estimate the current term structure.

As the paper concerns bond but not stock price, it uses Vasicek (1977) model for interest rates, which is an Ornstein-Uhlenbeck process governed by the SDE

for some constant drift $\mu$, and parameters $\theta,\sigma>0$. Generalized into matrix form for $\mathbf{x}_t$ to become a vector of factors:

where $\mathbf{K}$ is an $n\times n$ diagonal matrix with strictly positive diagonal terms. $\mathbf{x}_t, d\mathbf{w}_t$ are $n$-vector such that $(d\mathbf{w}_t)^T(d\mathbf{w}_t)=\mathbf{Q}dt$. $\mathbf{u}$ is a vector of the market price of risk.

This equation models the mean reversion, which the reversion rate is given by $\mathbf{K}$, and instantaneous interest rate depends on the factors (state variables) $\mathbf{x}_t$:

The mean reversion model means that every perturbation is reduced by half in $\dfrac{\log 2}{k_i}$ units of time, for $k_i$ the corresponding diagonal term on matrix $\mathbf{K}$.

Given the interest rate model, we can model the price of a bond (pure discount or coupon), and its yield to maturity. Derivation is in the paper.

To the issue of thin market, once we modelled the price movement in a state space (prediction equations in Kalman algorithm), the problem is that the measurement is always incomplete: Only some of the state is observable at a time due to illiquid market. Depends on the subset of state variables $\mathbf{z}$ observed, we adjust the matrix $\mathbf{H}$ accordingly. Indeed, we skip the update step in Kalman algorithm in case of missing data.

## Nkomo & Kabundi (2013) Kalman filtering and online learning algorithms for portfolio selection

@techreport{
title = {Kalman filtering and online learning algorithms for portfolio selection},
author = {Raphael Nkomo and Alain Kabundi},
year = 2013,
month = Nov,
institution = {Economic Research Southern Africa},
number = {working paper 394},
}


This paper uses Kalman filter as a replacement for moving average for dynamic portfolio selection.

In a market of $m$ assets with closing price of asset $i$ at time $t$ denoted by $p_{t,i}$, price relative is defined as $x_{t,i} = \dfrac{p_{t,i}}{p_{t-1,i}}$. A portfolio is defined as a $m$-vector $% $, which $\sum_i b_i = 1$ and $b_i\ge 0$ for all $i$.

Assume initial capital of $S_0$, the wealth at end of time $t$ is

If the portfolio is time-varying, we denote $\mathbf{b}_{t-1}$ the portfolio vector at trading period $t$, then the wealth will be

which the period return is $\mathbf{b}_{t-1}^T\mathbf{x}_t$ and $W_T(B) = \frac{1}{T}\sum_{t=1}^T \log(\mathbf{b}_{t-1}^T\mathbf{x}_t)$ is the log-utility function. Maximizing $W_T(B)$ maximizes $S_T$.

As a baseline, a buy-and-hold (BAH) strategy will be $\mathbf{b}_0 = \mathbf{b}$ fixed and never rebalance the portfolio. This results in the portfolio vector given by:

where $\odot$ denotes elementwise multiplication between vectors $\mathbf{b}_{t-1}$ and $\mathbf{x}_t$. BAH assumes stock market to grow over time and will perform badly in case of severe market correction.

Another strategy is constant rebalancing (CBAL), which maintains a fixed portfolio weight $\mathbf{b}_t = \mathbf{b}$ for all trading period $t$. It takes advantage of market fluctuations but subject to transaction cost.

The portfolio selection algorithm in this paper is to find the optimal $\mathbf{b}_t$ based on past data. The approach is named cyclically adjusted price relative (CAPR): If we remove the noise from the price relative time series $x_t = p_t/p_{t-1}$, we can capture both the price momentum and price reversal and make use of that to adjust the portfolio.

Common way to remove noise from a time series is the moving average (or EWMA) but the moving average will change the statistical properties of the time series. For example, it will fail to capture large deviation from average as a sign of price reversal but contribute that to momentum. In CAPR, we maintain a Kalman filtered price $p_t^K$ and keep track on the ratio $p_t/p_t^K$, which offset from 1 represents how much over sold or over bought of the stock. The paper derived that this ratio is Gaussian distributed.

The Kalman model for $p_t^K$ is as follows: It starts with initial estimate $p_0^K$ with mean $\mu_0$ and variance $P$, then the prediction:

and update:

With this, the paper reuses the Anticor algorithm from Borodin (2006). This algorithm is based on equal size periods (windows) and trading is on the end of window, to transfer wealth from recently high-performing assets to anti-correlated low-performing assets. Precisely, for stocks $i,j$ if $\mu_i > \mu_j$ in the last window and $\mathrm{corr}_{i,j} > 0$, then transfer wealth from $i$ to $j$. The Anticor algorithm is as follows:

For $m$ stocks and window of length $w$, at each time $t$ we have a $m$-vector of $x_t$ and define two consecutive window as $w\times n$ matrices:

Let $LX_k(j)$ be the $j$-th column of $LX_k$ and $\mu_k(j), \sigma_k^2(j)$ be the mean and variance of $LX_k(j)$. The cross-covariance matrix between columns of $LX_k$ is

and the cross-correlation matrix:

and the amount to move from $i$ to $j$ is

## Berardi et al (2002) Estimating value at risk with the Kalman filter

@techreport{
author = {Andrea Berardi and Stefano Corradin and Cristina Sommacamagna},
title = {Estimating Value at Risk with the Kalman Filter},
month = December,
year = 2002,
}


Value-at-risk in percentage terms of a portfolio is given by VaR $= z_{\alpha}\sqrt{\mathbf{w}'\mathbf{\Sigma w}}\sqrt{\Delta t}$ which the variance of portfolio return is $\sigma_P^2 = \mathbf{w}'\mathbf{\Sigma w}$, $\mathbf{w}$ is a vector of weights, $z_{\alpha}$ the confidence level, $\Delta t$ is the time horizon and $\mathbf{\Sigma}$ the covariance matrix of asset returns. If we anchor to the market return $R_m$, we can use the CAPM to express the return of each asset in a portfolio as $R_i = \alpha_i + \beta_i R_m + \epsilon_i$. If we use a vector $\mathbf{\beta}$ for all assets, the VaR can be redefined as $z_{\alpha}\sqrt{\mathbf{w}'\mathbf{\beta\beta}'\mathbf{w}\sigma_m^2}\sqrt{\Delta t}$, with the estimated market index variance $\sigma_m^2$.

This paper is to use Kalman filter to estimate vector $\mathbf{\beta}$ and thus the VaR can be found using the above formula. The system equation for a single asset is

In vector form, defining

and $\mathbf{z}$ the return of stock, and $\mathbf{x}$ the state variable, then we will have the Kalman equations as in the summary at the bottom of this page. The matrices and the variances of noises are to be estimated by expectation-maximization.

## Date & Bustreo (2016) Value-at-risk for fixed-income portfolios

@article{
author = {P. Date and R. Bustreo},
title = {Value-at-Risk for fixed-income portfolios: a Kalman filtering approach},
journal = {IMA Journal of Management Mathematics},
volume = 27,
number = 4,
pages = 557--573,
month = Oct,
year = 2016,
doi = {https://doi.org/10.1093/imaman/dpv016},
url = {https://bura.brunel.ac.uk/bitstream/2438/12202/1/Fulltext.pdf},
}


Another example of VaR calculation, but this one is based on bonds.

This paper mentions a test (Kupiec, 1995) for the VaR by chi-squared statistics. We introduce indicator variable $I_n$ for 1 if the actual loss of portfolio is greater than VaR and 0 otherwise. We expect $I_n$ follows a binomial distribution

which $p=1-\alpha$ for $\alpha$ the level chosen for VaR. Then the null hypothesis $H_0$ is $p=n/N$ and

which for 1 DoF, 95% CI, we reject null hypothesis if $LR>3.841$.

## Idea of using Kalman filter for VaR calculation

The literature above should give a good insight on how VaR is calculated. In case of a portfolio, VaR is $z_\alpha\sqrt{\mathbf{w}'\mathbf{\Sigma w}}$ with $\mathbf{\Sigma}$ the covariance matrix of returns of assets in the portfolio. $z_\alpha$ is a parameter of choice and $\mathbf{w}$ corresponds to the portfolio. The only unknown is covariance matrix $\mathbf{\Sigma}$.

Kalman filter equations also involves covariance matrix $\mathbf{P}$ in the state transition. We may run the history of asset returns as state variables $\mathbf{x}$ and determine the matrix $P$. This approach can incorporate illiquid assets in the portfolio as the measurement of Kalman algorithm can handle missing (unobserved) data. The Kalman parameters: state gain $\mathbf{F}$, and output gain $\mathbf{H}$ are easy to decide. However, we also introduced two unknown noise covariance matrices $\mathbf{Q}$ and $\mathbf{R}$, which their value affects the quality of the result.

## Brief summary of Kalman filter

Variables:

usage dimension symbols
State $n\times 1$ $\mathbf{x}_j$, approx by $\hat{\mathbf{x}}_{j\mid j}$ and $\hat{\mathbf{x}}_{j\mid j-1}$
Control input $p\times 1$ $\mathbf{u}_j$
Measurement output $m\times 1$ $\mathbf{z}_j$
State gain $n\times n$ $\mathbf{F}$
Input gain $n\times p$ $\mathbf{B}$
Output gain $m\times n$ $\mathbf{H}_j$
Process noise $n\times 1$ $\mathbf{w}_j$
Process noise covariance $n\times n$ $\mathbf{Q}$
Measurement noise $m\times 1$ $\mathbf{v}_j$
Measurement noise covariance $m\times m$ $\mathbf{R}$
A priori covariance $n\times n$ $\mathbf{P}_{j\mid j-1}$
A posteriori covariance $n\times n$ $\mathbf{P}_{j\mid j}$
Kalman gain $n\times m$ $\mathbf{K}_j$

Discrete time system: State transition and measurement

Prediction (a priori):

Pre-fit residual:

Kalman gain:

Update (a posteriori):

Post-fit residual: