image thumbnail

Hierarchical Kalman Filter for clinical time series prediction

by

 

It is an implementation of hierarchical (a.k.a. multi-scale) Kalman filter using belief propagation.

Demo_KalmanFilter_BP_multiScale.m
clear;
useTrueParameter = true;
ts_data = timeSeriesDataGeneration(useTrueParameter);

tic; 
numLevels = 2; % either 1 or 2. define how many levels will be used in kalman filter and smoother.
max_EM_iter = 21;
for em_iter = 1:max_EM_iter
    [msg, varNode, factorNode, belief] = initializeMessage(ts_data);
    [msg, belief, ts_data] = kalmanFilter_MultiScale(msg, varNode, factorNode, belief, ts_data, numLevels);
    [msg, belief, ts_data] = kalmanSmoother_MultiScale(msg, varNode, factorNode, belief, ts_data, numLevels);
    ts_data = EMConvergence(msg, varNode, ts_data, numLevels);
    ts_data = EMForMultiScaleKalmanFilter(msg, varNode, belief, ts_data, numLevels);    
    fprintf('Loglikelihood: %f, iter %d\n', ts_data.newLoglik, em_iter);
end

subplot(1,2,1),
plot(ts_data.mu_kf{ts_data.Lv1}(1, :), ts_data.mu_kf{ts_data.Lv1}(2, :), 'ro'); hold on;
plot(ts_data.mu_sm{ts_data.Lv1}(1, :), ts_data.mu_sm{ts_data.Lv1}(2, :), 'gd'); hold on;
h = legend('hidden state', 'observation', 'Kalman filter', 'Kalman SM');
set(h, 'box','off', 'location', 'best');
subplot(1,2,2)
plot(ts_data.mu_kf{ts_data.Lv2}(1, :), ts_data.mu_kf{ts_data.Lv2}(2, :), 'ro'); hold on;
plot(ts_data.mu_sm{ts_data.Lv2}(1, :), ts_data.mu_sm{ts_data.Lv2}(2, :), 'gd'); hold on;
h = legend('hidden state', 'observation', 'Kalman filter', 'Kalman SM');
set(h, 'box','off', 'location', 'best');

fprintf('MAD_KF at level 1: %f\n', mean(abs(ts_data.mu_kf{ts_data.Lv1}(:) - ts_data.x{ts_data.Lv1}(:))));
fprintf('MAD_SM at level 1: %f\n', mean(abs(ts_data.mu_sm{ts_data.Lv1}(:) - ts_data.x{ts_data.Lv1}(:))));

fprintf('MAD_KF at level 2: %f\n', mean(abs(ts_data.mu_kf{ts_data.Lv2}(:) - ts_data.x{ts_data.Lv2}(:))));
fprintf('MAD_SM at level 2: %f\n', mean(abs(ts_data.mu_sm{ts_data.Lv2}(:) - ts_data.x{ts_data.Lv2}(:))));

Contact us