# For an ideal Kalman filter, I have that the NEES test passes but NIS test does not?

Sorry if this is more of a debugging question, but I have been stuck on this supposedly simple NIS test for a very long while. If anyone knows any sources which cover the theory or implementation of the NIS test, I would definitely appreciate any links.

So, I am just running a very straight forward Kalman filter on a toy problem to check my understanding. I have the LTI discrete system

$$x_{k+1}=F x_k+w_k$$

$$y_k=Hx_k+v_k$$

where $$w_k \sim N(0,\sigma_w^2)$$ and $$v_k \sim N(0,\sigma_v^2)$$ are the white process and measurement noise respectively with covariances $$Q$$ and $$R$$. These are used to generate the true state $$x_k$$ and the obtained measurement $$y_k$$ (I am just using mvnrnd in MATLAB to obtain the white noise samples).

Then, I am also running the straightforward Kalman filter equations. With an initial state error covariance $$\hat{P}_0$$ and an initial corrected state vector $$\hat{x}_0$$ I then can integrate these forward using

$$\tilde P_k=\hat{P}_{k-1}$$

$$\tilde x_k=F \hat{x}_{k-1}$$

$$K_k=\tilde P_k H^T (H \tilde P_k H^T +R )^{-1}$$

$$\hat{P}_k=(1-K_k H) \tilde P_k (1-K_k H)^T + K_k R K_k^T$$

$$\hat{x}_k=\tilde{x}_k+K_k(y_k-H \tilde x_k)$$

Then, I perform a NIS test to ascertain its consistency following section 2.2.3 in the lecture notes here: https://www.robots.ox.ac.uk/~ian/Teaching/Estimation/LectureNotes2.pdf

To do this, I formulate the innovation covariance

$$S_k=H \hat{P}_k H^T + R$$

from which I formulate the normalized innovation error

$$e_k=[H(x_k-\hat{x}_k)]^T S_k^{-1} [H(x_k-\hat{x}_k)]$$.

Doing an $$N$$ trial Monte Carlo run and with the measurement having $$m$$ elements, I can get for every $$k^{th}$$ timestep the average normalized innovation error across all of the Monte Carlo runs as $$\bar{e}_k$$, and from the sources I saw we should have that $$N \bar{e}_k$$ follows a Chi squared distribution with $$N*m$$ degrees of freedom.

So, apparently with $$\alpha=0.05$$ I should have around 95% of $$\bar{e}_k$$ lie in the bounds $$[r_1,r_2]$$ obtained using the MATLAB commands

$$r_1=chi2inv(alpha/2,N*m)/N$$

$$r_2=chi2inv(1-alpha/2,N*m)/N$$

However, I always have $$\bar{e}_k$$ being way below the computed error bounds. I did the very similar NEES test and those pass without issue, and I double checked the lecture notes and other sources, made sure my computed Chi squared bounds matched the ones in the lecture notes, and I even tried multiple different expressions for $$P$$ and $$S$$, but so far nothing has worked for the NIS test. I was wondering is the above correct, and does anyone have any sources for the NIS test? Sorry again for the rambling debugging question.

$$e_k=[y_k-H \tilde x_k]^T S_k^{-1} [y_k-H \tilde x_k]$$