Kalman

Kalman — The Kalman filter

Synopsis

#include <gretl/libgretl.h>
#include <gretl/kalman.h>

typedef             kalman;
kalman *            kalman_new                          (const gretl_matrix *S,
                                                         const gretl_matrix *P,
                                                         const gretl_matrix *F,
                                                         const gretl_matrix *A,
                                                         const gretl_matrix *H,
                                                         const gretl_matrix *Q,
                                                         const gretl_matrix *R,
                                                         const gretl_matrix *y,
                                                         const gretl_matrix *x,
                                                         const gretl_matrix *m,
                                                         gretl_matrix *E,
                                                         int *err);
void                kalman_free                         (kalman *K);
int                 kalman_forecast                     (kalman *K,
                                                         PRN *prn);
double              kalman_get_loglik                   (const kalman *K);
double              kalman_get_arma_variance            (const kalman *K);
gretl_matrix *      kalman_arma_smooth                  (kalman *K,
                                                         int *err);
gretl_matrix *      kalman_smooth                       (kalman *K,
                                                         gretl_matrix **pP,
                                                         gretl_matrix **pU,
                                                         int *err);
int                 kalman_set_initial_state_vector     (kalman *K,
                                                         const gretl_matrix *S);
int                 kalman_set_initial_MSE_matrix       (kalman *K,
                                                         const gretl_matrix *P);
void                kalman_set_nonshift                 (kalman *K,
                                                         int n);
void                kalman_set_options                  (kalman *K,
                                                         int opts);
int                 kalman_get_options                  (kalman *K);
void                kalman_attach_data                  (kalman *K,
                                                         void *data);
void *              kalman_get_data                     (const kalman *K);
void                kalman_attach_printer               (kalman *K,
                                                         PRN *prn);
PRN *               kalman_get_printer                  (const kalman *K);

Description

Details

kalman

typedef struct kalman_ kalman;


kalman_new ()

kalman *            kalman_new                          (const gretl_matrix *S,
                                                         const gretl_matrix *P,
                                                         const gretl_matrix *F,
                                                         const gretl_matrix *A,
                                                         const gretl_matrix *H,
                                                         const gretl_matrix *Q,
                                                         const gretl_matrix *R,
                                                         const gretl_matrix *y,
                                                         const gretl_matrix *x,
                                                         const gretl_matrix *m,
                                                         gretl_matrix *E,
                                                         int *err);

Allocates and initializes a Kalman struct, which can subsequently be used for forecasting with kalman_forecast(). The nomenclature for the various required matrices is that in Hamilton's Time Series Analysis (1994, chapter 13), except that "S" is used in place of Hamilton's \xi for the state vector.

S :

r x 1 initial state vector.

P :

r x r initial precision matrix.

F :

r x r state transition matrix.

A :

n x k matrix of coefficients on exogenous variables in the observation equation.

H :

n x r matrix of coefficients on the state variables in the observation equation.

Q :

r x r contemporaneous covariance matrix for the errors in the state equation.

R :

n x n contemporaneous covariance matrix for the errors in the observation equation (or NULL if this is not applicable).

y :

T x n matrix of dependent variable(s).

x :

T x k matrix of exogenous variable(s). May be NULL if there are no exogenous variables, or if there's only a constant.

m :

r x 1 vector of constants in the state transition, or NULL.

E :

T x n matrix in which to record forecast errors (or NULL if this is not required).

err :

location to receive error code.

Returns :

pointer to allocated struct, or NULL on failure, in which case err will receive a non-zero code.

kalman_free ()

void                kalman_free                         (kalman *K);


kalman_forecast ()

int                 kalman_forecast                     (kalman *K,
                                                         PRN *prn);

Generates a series of one-step ahead forecasts for y, based on information entered initially using kalman_new(), and possibly modified using kalman_set_initial_state_vector() and/or kalman_set_initial_MSE_matrix(). The log-likelihood is calculated for the sequence of forecast errors on the assumption of normality: this can be accessed using kalman_get_loglik().

K :

pointer to Kalman struct: see kalman_new().

prn :

printing apparatus (or NULL).

Returns :

0 on success, non-zero on error.

kalman_get_loglik ()

double              kalman_get_loglik                   (const kalman *K);

Retrieves the log-likelhood calculated via a run of kalman_forecast().

K :

pointer to Kalman struct.

Returns :

ll value, or NADBL on failure.

kalman_get_arma_variance ()

double              kalman_get_arma_variance            (const kalman *K);

Retrieves the estimated variance for an ARMA model estimated using the Kalman filter.

K :

pointer to Kalman struct.

Returns :

sigma-squared value, or NADBL on failure.

kalman_arma_smooth ()

gretl_matrix *      kalman_arma_smooth                  (kalman *K,
                                                         int *err);

Runs a filtering pass followed by a smoothing pass.

K :

pointer to Kalman struct.

err :

location to receive error code.

Returns :

matrix containing the smoothed estimate of the dependent variable, or NULL on error.

kalman_smooth ()

gretl_matrix *      kalman_smooth                       (kalman *K,
                                                         gretl_matrix **pP,
                                                         gretl_matrix **pU,
                                                         int *err);

Runs a filtering pass followed by a backward, smoothing pass. At present the pU argument is experimental and a bodge: it will not actually do anything unless pP is left NULL.

K :

pointer to kalman struct.

pP :

pointer to matrix in which to retrieve the MSE of the smoothed state (or NULL if this is not required).

pU :

pointer to matrix in which to retrieve the smoothed disturbances (or NULL if this is not required).

err :

location to receive error code.

Returns :

matrix containing the smoothed estimate of the state, or NULL on error.

kalman_set_initial_state_vector ()

int                 kalman_set_initial_state_vector     (kalman *K,
                                                         const gretl_matrix *S);

Resets the initial value of the state vector in a Kalman struct, using the values from S. See also kalman_new().

K :

pointer to Kalman struct.

S :

matrix of values to set.

Returns :

0 on success, non-zero on error.

kalman_set_initial_MSE_matrix ()

int                 kalman_set_initial_MSE_matrix       (kalman *K,
                                                         const gretl_matrix *P);

Resets the initial value of the MSE matrix in a Kalman struct, using the values from P. See also kalman_new().

K :

pointer to Kalman struct.

P :

matrix of values to set.

Returns :

0 on success, non-zero on error.

kalman_set_nonshift ()

void                kalman_set_nonshift                 (kalman *K,
                                                         int n);


kalman_set_options ()

void                kalman_set_options                  (kalman *K,
                                                         int opts);


kalman_get_options ()

int                 kalman_get_options                  (kalman *K);


kalman_attach_data ()

void                kalman_attach_data                  (kalman *K,
                                                         void *data);


kalman_get_data ()

void *              kalman_get_data                     (const kalman *K);


kalman_attach_printer ()

void                kalman_attach_printer               (kalman *K,
                                                         PRN *prn);


kalman_get_printer ()

PRN *               kalman_get_printer                  (const kalman *K);