### Introduction

The following is an example of implementing the `kalmanFilter`

GAUSS procedure. This follows the example in section 2.2.5 from Durbin, James, and Siem Jan Koopman. 2012. *Time Series Analysis by State Space Methods: Second Edition.* Oxford University Press.

The data contains readings of the river Nile annual flow volume at Aswan from 1871 to 1970 and is stored in the GAUSS dataset file "nile.dat".

## Load data

This example loads the data using the GAUSS function `loadd`

.

```
new;
library tsmt;
// Load all variables from 'nile.dat'
dataset = getGAUSSHome() $+ "pkgs/tsmt/examples/nile.dat";
y = loadd(dataset);
```

## Set up the estimation

The GAUSS procedure `kalmanFilter`

requires that models take a state space representation. In addition to the data, `kalmanFilter`

requires the following inputs

Name | Description |
---|---|

Z | Matrix, pxM, design matrix |

d | Matrix, px1, observation intercept |

H | Matrix, pxp, observation disturbance covariance matrix |

T | Matrix, MxM, transition matrix |

c | Matrix, mx1, state intercept |

R | Matrix, mxr, selection matrix |

Q | Matrix, rxr, state disturbance covariance matrix |

a_0 | Matrix, mx1, initial prior state mean |

p_0 | Matrix, mxm, initial prior state covariance |

```
sigma_e = 15099;
sigma_n = 1469.1;
Z = 1;
H = sigma_e;
T = 1;
Q = sigma_n;
R = 1;
d = 0;
c = 0;
a_0 = 0;
p_0 = 10e7;
```

## Estimate The Model

The GAUSS function `kalmanFilter`

implements the GAUSS Kalman filter.

```
// Declare 'rslt' to be a 'kalmanResult' structure
struct kalmanResult rslt;
// Perform estimation and fill 'rslt' with estimation results
rslt = kalmanFilter(y', Z, d, H, T, c, R, Q, a_0, p_0);
```

## Output

Code for generating the plots below of the filtered data and state variance can be found here.