Financial statistics: Lab 4

Hidden states from noisy data: a Kalman filter that recovers the CIR short rate from six bond yields, then stochastic volatility, where a particle filter does better and shows why it has to resample.

PythonNumPySciPystatsmodelsMatplotlibJupyter 2026

The fourth and last MASM18 lab is about quantities that are never observed. A short rate hides behind bond yields, and a volatility hides behind returns. Both are written as state space models, with one equation for how the hidden state moves and one for how the data depends on it, and both are estimated with filters: the Kalman filter throughout, and a particle filter where the Kalman filter's assumptions give way.

A short rate behind six yields

The Cox-Ingersoll-Ross model of the previous lab,

$$dr_t = a(b - r_t)\,dt + \sigma\sqrt{r_t}\,dW_t,$$

is an affine model, which means the price of a zero-coupon bond is $\mathbf{A}(\tau)e^{-\mathbf{B}(\tau)r_t}$ for known functions $\mathbf{A}$ and $\mathbf{B}$ of the time to maturity $\tau$. Taking logarithms makes every yield a straight line in the short rate,

$$Y_t^{\tau} = \frac{\mathbf{B}(\tau)}{\tau}\,r_t - \frac{\log\mathbf{A}(\tau)}{\tau} + \varepsilon_t^{\tau},$$

so six yields, with maturities from three months to five years, are six noisy linear measurements of one number. The slopes run from 0.98 at three months to 0.72 at five years: every maturity carries information about $r_t$.

The state equation is the Euler step of the SDE. Its noise variance, $\sigma^2 r_t\Delta$, depends on the state itself, which a Kalman filter does not allow, so it is evaluated at the latest filtered value, as an extended Kalman filter would do. The measurement equation needs no approximation at all.

On 500 simulated months with known parameters, the one thing missing is the size of the measurement noise. The filter supplies the likelihood of the data as a by-product, and maximising it puts the noise at 2.0 percentage points per yield. That is a lot: the short rate itself has a standard deviation of 4.2 percentage points.

Estimate of the short rate Root mean square error, percentage points
The 3-month yield, taken as it is 1.99
Kalman filter 0.71

The filter cuts the error to about a third. It gains twice, by pooling six yields at each date and by using the dynamics of the rate to average over time. It also knows how uncertain it is. Its own standard deviation is 0.71 percentage points, the same as its actual error, and the true rate lies inside its 95 percent band 93.8 percent of the time.

The same filter on Treasury yields

The real data is US Treasury yields at the same six maturities, monthly from January 2009 to October 2022. Now nothing is known, so the likelihood is maximised over all four parameters at once.

Parameter Estimate Standard error
Speed of mean reversion, $a$ 0.045 0.012
Long-run level, $b$ 0.105 0.026
Volatility, $\sigma$ 0.089 0.007
Measurement noise, $\xi$ 0.0027 0.00006

Three different starting points end at these values, to within a quarter of a standard error. The first two rows are the uncertain ones. A mean reversion of 0.045 a year is a half-life of about 15 years, longer than the 14 years of data, so the sample says little about where the rate is heading in the long run.

The level it does report, 10.5 percent, looks wrong for a period when yields never passed 4.5 percent. It is the model's way of producing an upward-sloping yield curve. Bond prices depend on the parameters under the pricing measure, and a curve that rises with maturity needs a high long-run level there. The lab uses the same parameters for the dynamics of the rate, so the same number has to serve both. A market price of risk would let the two differ.

The filtered rate follows the 3-month yield: near zero from 2009 to 2015 and again in 2020 and 2021, rising in between and in 2022. In 17 of the 166 months it dips just below zero, which a CIR rate cannot do but a Gaussian filter can.

The fit shows the limit of the model. The typical misfit is 0.16 to 0.25 percentage points up to three years, and 0.45 at five. One factor moves the whole curve up and down. It cannot also change its slope.

Volatility as a hidden state

The second problem is a stochastic volatility model,

$$\log\sigma_t^2 = \kappa_0 + \kappa_1\log\sigma_{t-1}^2 + \nu_{t-1}, \qquad z_t = \sigma_t\epsilon_t,$$

with 1000 simulated observations. The state equation is linear, but the measurement is a product. Squaring and taking logarithms turns it into a sum,

$$\log z_t^2 = \log\sigma_t^2 + \log\epsilon_t^2,$$

and the Kalman filter applies once the noise $\log\epsilon_t^2$ is treated as Gaussian, with its true mean of −1.27 and variance of $\pi^2/2$.

The filter follows the volatility, with a correlation of 0.62 to the true series, but it stays unsure. Its standard deviation settles at 1.10, when the log variance itself only has a standard deviation of 1.40. Each observation is weak evidence: the measurement noise has variance 4.9, against 1 for the noise that drives the state.

The Gaussian assumption is where it gives. The true density of $\log\epsilon_t^2$ is skewed, with a long tail to the left, because a return that happens to land near zero has a hugely negative logarithm. The filter reads such a return as a collapse in volatility. In the 5 percent of time steps with the most negative noise, its estimate is on average 1.35 too low, although over all time steps the average error is 0.02.

A particle filter

A particle filter does not need the transformation. It carries 5000 guesses of the state, moves each one forward with the state equation, and weights it by the exact density of the observed $z_t$ given that guess. The weighted average is the estimate.

Estimate of the log variance Root mean square error
Constant, the long-run mean 1.43
Kalman filter 1.12
Particle filter 1.04

The gain is where the Kalman filter was weak. In the same 5 percent of time steps the particle filter is 0.65 too low on average, half as much.

The step that makes it work is resampling: after each observation the particles are redrawn in proportion to their weights, so unlikely ones die and likely ones multiply. Without it the weights pile onto fewer and fewer particles. The effective number falls from 5000 to 274 after 10 steps, to about 7 after 50, and to a single particle by step 200. The error is then 1.64, worse than not filtering at all. With resampling the effective number never drops below 53 and is usually around four thousand.

The other advantage is how little has to change for a different model. If the returns have Student $t$ noise, only the weighting line is replaced, by the density

$$p(z_t \mid \sigma_t) = \frac{1}{\sigma_t}\,f_{\nu}\!\left(\frac{z_t}{\sigma_t}\right).$$

On data simulated with three degrees of freedom, the filter with these weights has an error of 1.04, against 1.12 for the same filter with Gaussian weights, which takes every large return for a jump in volatility.

What this involved

One Kalman filter, written for the general linear state space form, served both problems: it accepts a state noise that depends on the state, and returns the log-likelihood along with the filtered means and variances. The parameters were estimated by maximising that likelihood over their logarithms, with standard errors from a numerical Hessian. The particle filter works with log-weights, so that a very unlikely observation cannot turn every weight into zero. One data point was left out: the first return in the volatility file implies a shock of 14 standard deviations and is evidently a start value. The preparation also covered the derivation of the affine bond price equations, and the Euler and Milstein schemes for the CIR model. The course supplies its lab code in MATLAB.


The first 20 of the 41 simulated years. The 3-month yield, in grey, is the short rate plus noise of about 2 percentage points. The filter, in blue, stays on the true rate, in red.
The first 20 of the 41 simulated years. The 3-month yield, in grey, is the short rate plus noise of about 2 percentage points. The filter, in blue, stays on the true rate, in red.
Standard deviation of observed minus model yield on the Treasury data, by maturity. Up to three years the one-factor CIR model is within a quarter of a percentage point; at five years the misfit is nearly twice that.
Standard deviation of observed minus model yield on the Treasury data, by maturity. Up to three years the one-factor CIR model is within a quarter of a percentage point; at five years the misfit is nearly twice that.
Density of the noise after the log transform, with the Gaussian of the same mean and variance. Returns close to zero give very negative values, which the Gaussian treats as almost impossible.
Density of the noise after the log transform, with the Gaussian of the same mean and variance. Returns close to zero give very negative values, which the Gaussian treats as almost impossible.
Error of three estimates of the hidden log variance over 999 time steps. Both filters beat a constant guess; the particle filter, which weights with the exact density of the return, does best.
Error of three estimates of the hidden log variance over 999 time steps. Both filters beat a constant guess; the particle filter, which weights with the exact density of the return, does best.
Effective number of particles, $1/\sum W^2$, on a log scale. Without resampling the weights concentrate on one particle within a few dozen steps. With resampling the filter keeps thousands in play.
Effective number of particles, $1/\sum W^2$, on a log scale. Without resampling the weights concentrate on one particle within a few dozen steps. With resampling the filter keeps thousands in play.