3
$\begingroup$

I read that Kalman filter can be applied to perform regression with a dynamic beta, calculated on the fly. Can someone please break this down for me, with some simple example of single-variable regression? What are the assumptions, what are the inputs, what are the equations? By inputs I also mean user-set parameters(configs), such as half-life in the context of ema.

$\endgroup$
8
$\begingroup$

The standard Kalman filter model is given by: \begin{align*} y_t &= \mathbf{F}_t' \boldsymbol{\theta}_t + \nu_t, \qquad \nu_t \sim \mathcal{N}(0, v_t)\\ \boldsymbol{\theta}_t &= \mathbf{G}_t \boldsymbol{\theta}_{t-1} + \boldsymbol{\omega}_t, \qquad \boldsymbol{\omega}_t \sim \mathcal{N}(0, \mathbf{W}_t) \end{align*}

Say you have a pair of random variables $y_t$ and $\mathbf{F}_t$ - for example, the price of a stock and a set of covariates including the time of the year, prices of other stocks, etc. The Kalman filter assumes that the relationship between $y_t$ and $\mathbf{F}_t$ varies as a function of time. So, while today, the two might be highly correlated, tomorrow, they may not be at all (usually the dynamics are much more gradual).

To fit a Kalman filter, you use a forward filtering, backward smoothing approach. Essentially, you are assuming a prior distribution on your parameter, and based on the discrepancy between your prediction and of $y_t$ and the true value, the prior is updated.

$v_t$ controls the scale of the $y_t$, and $\mathbf{W}_t$ controls the scale of $\boldsymbol{\theta}_t$. This means that there is an inherent identifiability problem; if we don't care about the identifiability or don't know the scale of one of the variables, we can leave it. It won't affect the model fit. If we have a ballpark estimate of one or the other, we can place a prior distribution on it. If the scale is known exactly, we can fix one of the two, and leave the other to be inferred. Note that if the scale is too big, our predictions will be more or less flat. If the scale is too small, the predictions will be very jittery.

Since you're interested in doing regression "on the fly," I'll derive the forward filtering steps here. (Backward smoothing is used when you have observed all the data and want to correct parameter estimates given future data.) $v_t$, $\mathbf{W}_t$, and $\mathbf{G}_t$ must be set by the user. (The first two can be inferred but it makes the update equations a bit more complicated - see Prado and West (2010). $\mathbf{G}_t$ is usually set by knowledge of the process.) There are three distributions: prior state, prior observation (i.e. forecast), posterior state. Derivations of these are given as follows, where $\mathcal{D}_t$ is all observed data ($y_t$ and $\mathbf{F}_t)$ up to and including time $t$.

Prior state \begin{align*} \boldsymbol{\theta}_t | \mathcal{D}_{t-1} &\sim \mathcal{N}(\mathbf{a}_t, \mathbf{R}_t)\\ \mathbf{a}_t &= \mathbb{E}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}]\\ &= \mathbb{E}[\mathbf{G}_t \boldsymbol{\theta}_{t-1} + \boldsymbol{\omega}_t | \mathcal{D}_{t-1}]\\ &= \mathbf{G}_t \mathbb{E}[\boldsymbol{\theta}_{t-1} | \mathcal{D}_{t-1}]\\ & = \boxed{\mathbf{G}_t \mathbf{m}_{t-1}}\\ \mathbf{R}_t &= \mbox{Var}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}]\\ &= \mbox{Var}[\mathbf{G}_t \boldsymbol{\theta}_{t-1} + \boldsymbol{\omega}_t | \mathcal{D}_{t-1}]\\ &= \mathbf{G}_t \mbox{Var}[\boldsymbol{\theta}_{t-1} | \mathcal{D}_{t-1}] \mathbf{G}_t' + \mbox{Var}[\boldsymbol{\omega}_t | \mathcal{D}_{t-1}]\\ &= \boxed{\mathbf{G}_t \mathbf{C}_{t-1} \mathbf{G}_t' + \mathbf{W}_t} \end{align*}

Prior observation (i.e. forecast) \begin{align*} y_t | \mathcal{D}_{t-1} &\sim \mathcal{N}(f_t, Q_t)\\ f_t &= \mathbb{E}[y_t | \mathcal{D}_{t-1}]\\ &= \mathbb{E}[\mathbf{F}_t' \boldsymbol{\theta}_t + \nu_t | \mathcal{D}_{t-1}]\\ &= \boxed{\mathbf{F}_t'\mathbf{a}_t}\\ Q_t &= \mbox{Var}[y_t | \mathcal{D}_{t-1}]\\ &= \mbox{Var}[\mathbf{F}_t' \boldsymbol{\theta}_t + \nu_t | \mathcal{D}_{t-1}]\\ &= \mathbf{F}_t' \mbox{Var}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}] \mathbf{F}_t + \mbox{Var}[\nu_t | \mathcal{D}_{t-1}]\\ &= \boxed{\mathbf{F}_t' \mathbf{R}_t \mathbf{F}_t + v_t} \end{align*}

Posterior state

There are two ways to get the posterior state. The first is by conditioning on the joint distribution of $(\boldsymbol{\theta}_t, y_t | \mathcal{D}_{t-1})$. We need to get the covariance between $\boldsymbol{\theta}_t$ and $y_t$ to establish the joint distribution of $(\boldsymbol{\theta}_t, y_t | \mathcal{D}_{t-1})$. By definition, the covariance between two random quantities $\mathbf{X}$ and $\mathbf{Y}$ is $\mbox{Cov}[\mathbf{X}, \mathbf{Y}] = \mathbb{E}[\mathbf{X}\mathbf{Y}'] - \mathbb{E}[\mathbf{X}]\mathbb{E}[\mathbf{Y}]'$. Thus: \begin{align*} \mbox{Cov}[\boldsymbol{\theta}_t, y_t | \mathcal{D}_{t-1}] &= \mbox{Cov}[\boldsymbol{\theta}_t, \mathbf{F}_t' \boldsymbol{\theta}_t + \nu_t | \mathcal{D}_{t-1}]\\ &= \mathbb{E}[\boldsymbol{\theta}_t (\mathbf{F}_t' \boldsymbol{\theta}_t + \nu_t)' | \mathcal{D}_{t-1}] - \mathbb{E}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}] \mathbb{E}[\mathbf{F}_t' \boldsymbol{\theta}_t + \nu_t' | \mathcal{D}_{t-1}]\\ &= \mathbb{E}[\boldsymbol{\theta}_t \nu_t' + \boldsymbol{\theta}_t \boldsymbol{\theta}_t' \mathbf{F}_t| \mathcal{D}_{t-1}] - \mathbb{E}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}] \mathbb{E}[\boldsymbol{\theta}_t' | \mathcal{D}_{t-1}] \mathbf{F}_t \\ &= \mathbb{E}[\boldsymbol{\theta}_t \nu_t' | \mathcal{D}_{t-1}] + \mathbb{E}[\boldsymbol{\theta}_t \boldsymbol{\theta}_t' | \mathcal{D}_{t-1}]\mathbf{F}_t - \mathbb{E}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}] \mathbb{E}[\boldsymbol{\theta}_t' | \mathcal{D}_{t-1}] \mathbf{F}_t \\ &= \mathbf{0} + (\mathbb{E}[\boldsymbol{\theta}_t \boldsymbol{\theta}_t' | \mathcal{D}_{t-1}] - \mathbb{E}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}] \mathbb{E}[\boldsymbol{\theta}_t' | \mathcal{D}_{t-1}]) \mathbf{F}_t \\ &= \mbox{Var}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}]\mathbf{F}_t\\ &= \mathbf{R}_t \mathbf{F}_t\\ &= \mathbf{a}_t Q_t \end{align*}

Thus, the joint distribution of $(\boldsymbol{\theta}_t, y_t | \mathcal{D}_{t-1})$ is given by: \begin{align*} \begin{pmatrix} \boldsymbol{\theta}_t\\ y_t \end{pmatrix} \sim \mathcal{N}\left( \begin{pmatrix} \mathbf{a}_t\\ f_t \end{pmatrix} , \begin{pmatrix} \mathbf{R}_t & \mathbf{a}_t Q_t\\ \mathbf{a}_t' Q_t & Q_t \end{pmatrix} \right) \end{align*}

Then, conditioning on $y_t$, we get: \begin{align*} \boldsymbol{\theta}_t | y_t, \mathcal{D}_{t-1} &\sim \mathcal{N}(\mathbf{m}_t, \mathbf{C}_t)\\ \mathbf{m}_t &= \mathbf{a}_t + \mathbf{a}_t Q_t Q_t^{-1}(y_t - f_t)\\ &= \boxed{\mathbf{a}_t + \mathbf{a}_t e_t}\\ \mathbf{C}_t &= \mathbf{R}_t - (\mathbf{a}_t Q_t) Q_t^{-1} (Q_t \mathbf{a}_t')\\ &= \boxed{\mathbf{R}_t - \mathbf{a}_t Q_t \mathbf{a}_t'} \end{align*}

We can also find the posterior state using Bayes' theorem. Take $\mathbf{G}_t = \mathbf{I}$ for simplicity. Then, \begin{align*} p(\boldsymbol{\theta}_t | y_t, \mathbf{F}_t) &\propto p(\mathbf{F}_t, y_t | \boldsymbol{\theta}_t) p(\boldsymbol{\theta}_t)\\ &= p(y_t | \mathbf{F}_t, \boldsymbol{\theta}_t) p(\mathbf{F}_t | \boldsymbol{\theta}_t) p(\boldsymbol{\theta}_t)\\ \end{align*} Note that $\mathbf{F}_t$ is observed and deterministic while $y_t$ is observed and random. Thus, we can get rid of the $p(\mathbf{F}_t | \boldsymbol{\theta}_t)$. \begin{align*} &= \mathcal{N}(y_t | \mathbf{F}_t' \boldsymbol{\theta}_t, v_t) \mathcal{N}(\boldsymbol{\theta}_t | \mathbf{m}_{t-1}, \mathbf{R}_t)\\ \end{align*} We use $\boldsymbol{\theta}_t$ rather than $\mathbf{m}_{t-1}$ in the expression for the mean of $\mathbf{y}_t$ because we are given $\boldsymbol{\theta}_t$ in the distribution of $y_t$. We are not given $\boldsymbol{\theta}_{t-1}$ in the distribution for $\boldsymbol{\theta}_t$, however, (this is a marginal distribution) so the expected value of $\boldsymbol{\theta}_t$ is $\mathbf{m}_{t-1}$, not $\boldsymbol{\theta}_{t-1}$. \begin{align*} &= \left[(2\pi v_t)^{-1/2}\exp\left\{-\frac{1}{2v_t}(y_t - \mathbf{F}_t'\boldsymbol{\theta}_t)^2\right\}\right]\left[(2\pi)^{-p/2}|\mathbf{R}_t|^{-1/2}\exp\left\{-\frac{1}{2}(\boldsymbol{\theta}_t - \mathbf{m}_{t-1})'\mathbf{R}_t^{-1}(\boldsymbol{\theta}_t - \mathbf{m}_{t-1})\right\}\right]\\ &\propto \exp \left\{-\frac{1}{2v_t}(y_t^2 - 2y_t\boldsymbol{\theta}'\mathbf{F}_t + \boldsymbol{\theta}_t'\mathbf{F}_t\mathbf{F}_t'\boldsymbol{\theta}_t) - \frac{1}{2}(\boldsymbol{\theta}_t'\mathbf{R}_t^{-1}\boldsymbol{\theta}_t - 2\boldsymbol{\theta}_t\mathbf{R}_t^{-1}\mathbf{m}_{t-1} + \mathbf{m}_{t-1}'\mathbf{R}_t^{-1}\mathbf{m}_{t-1})\right\}\\ &\propto \exp \left\{-\frac{1}{2}\left[\frac{1}{v_t}\boldsymbol{\theta}_t'\mathbf{F}_t\mathbf{F}_t'\boldsymbol{\theta}_t - \frac{2}{v_t}y_t\boldsymbol{\theta}_t'\mathbf{F}_t + \boldsymbol{\theta}_t'\mathbf{R}_t^{-1}\boldsymbol{\theta}_t - 2\boldsymbol{\theta}_t'\mathbf{R}_t^{-1}\mathbf{m}_{t-1}\right]\right\}\\ &= \exp \left\{ -\frac{1}{2}\left[\boldsymbol{\theta}_t'\left(\frac{1}{v_t}\mathbf{F}_t\mathbf{F}_t' + \mathbf{R}_t^{-1}\right)\boldsymbol{\theta}_t - 2\boldsymbol{\theta}_t'\left(\frac{1}{v_t}y_t\mathbf{F}_t + \mathbf{R}_t^{-1}\mathbf{m}_{t-1}\right)\right]\right\}\\ &\propto \mathcal{N}(\mathbf{m}_t, \mathbf{C}_t)\\ \end{align*} where \begin{align*} \mathbf{a}_t &= \mathbb{E}[\boldsymbol{\theta}_t | \mathcal{D}_{t-1}] = \mathbf{m}_{t-1}\\ \mathbf{m}_t &= \boxed{\mathbf{C}_t\left[\frac{1}{v_t}y_t\mathbf{F}_t + \mathbf{R}_t^{-1}\mathbf{a}_{t}\right]}\\ \mathbf{C}_t &= \boxed{\left[\frac{1}{v_t}\mathbf{F}_t\mathbf{F}_t' + \mathbf{R}_t^{-1}\right]^{-1}} \end{align*}

Although the expressions for $\mathbf{m}_t$ and $\mathbf{C}_t$ look very different when derived by conditioning on the joint distribution vs. using Bayes' theorem, they are mathematically identical.

Hope this helps!

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Just stumbled across this in 2019 - thanks for such a comprehensive answer. Would you happen to have a source that talks more about this method? $\endgroup$ – swmfg Mar 10 '19 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.