Scalar static-state Kalman update (source code)

= Scalar static-state Kalman update

Suppose the predicted state has <normal distribution> $x\mid\mathcal F\sim N(\widehat x,V)$ and the new observation is $y=x+\eta$, where the <measurement noise> $\eta\sim N(0,R)$ is <independent> of the state and previous observations. <Gaussian conditional expectation> gives
$$
\mathbb E[x\mid\mathcal F,y]
=\widehat x+\frac{V}{V+R}(y-\widehat x),
\qquad
\operatorname{Var}(x\mid\mathcal F,y)
=\frac{VR}{V+R}.
$$
The coefficient $V/(V+R)$ is the scalar <Kalman gain>. The <posterior mean> is a <weighted mean> of the predicted state and the new observation, with the more precise quantity receiving more weight. For $V,R>0$, the same equations can be expressed in <precision parameters>
$$
\frac1{P_{\mathrm{post}}}=\frac1V+\frac1R,\qquad
m_{\mathrm{post}}=P_{\mathrm{post}}\left(\frac{\widehat x}{V}+\frac{y}{R}\right).
$$
The two <precision parameters> add because the <measurement noise> is <independent> of the state and past observations. <Completing the square> in the product of the <prior density> and the <likelihood function> gives this form directly. This is a <Gaussian conditional expectation> for a fixed unknown state; a moving state additionally requires a <Kalman prediction step>.