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.

kalmanSmoother_MultiScale(msg, varNode, factorNode, belief, ts_data, numLevels)
function [msg, belief, ts_data] = kalmanSmoother_MultiScale(msg, varNode, factorNode, belief, ts_data, numLevels)
    Lv1 = ts_data.Lv1;
    Lv2 = ts_data.Lv2;
    %% backword message for kalman smoother
    for i = ts_data.T(Lv1):-1:2
        %% factor node update
        msg{factorNode.x{Lv1}{i}.backwardNeighborMsgID}.toVarNode.mu ...
            = ts_data.A{Lv1}\msg{factorNode.x{Lv1}{i}.forwardNeighborMsgID}.toFactorNode.mu;
        if(~isempty(msg{factorNode.x{Lv1}{i}.forwardNeighborMsgID}.toFactorNode.V))
            msg{factorNode.x{Lv1}{i}.backwardNeighborMsgID}.toVarNode.iV = [];
            msg{factorNode.x{Lv1}{i}.backwardNeighborMsgID}.toVarNode.V ...
                = ts_data.A{Lv1}\(msg{factorNode.x{Lv1}{i}.forwardNeighborMsgID}.toFactorNode.V + ts_data.V_Q{Lv1})/(ts_data.A{Lv1}');
        else
            tmp_iv = msg{factorNode.x{Lv1}{i}.forwardNeighborMsgID}.toFactorNode.iV;
            msg{factorNode.x{Lv1}{i}.backwardNeighborMsgID}.toVarNode.V = [];
            msg{factorNode.x{Lv1}{i}.backwardNeighborMsgID}.toVarNode.iV ...
                = (ts_data.A{Lv1}')*((eye(ts_data.dim_x(Lv1)) + tmp_iv*ts_data.V_Q{Lv1})\tmp_iv)*ts_data.A{Lv1};
        end
        %% variable node update
        msg{varNode.x{Lv1}{i-1}.backwardNeighborMsgID}.toFactorNode ...
            = GaussianMultiply(msg{varNode.x{Lv1}{i-1}.forwardNeighborMsgID}.toVarNode,...
            msg{varNode.x{Lv1}{i-1}.upperNeighborMsgID}.toVarNode);
        %% include level 2 information
        if(numLevels == 2)
            msg = updateMSGToLevel2Backward(msg, varNode, i-1, Lv1);
            msg = updateMSGInLevel2(msg, varNode, factorNode, i-1, Lv1, Lv2, ts_data);
        end
    end
    %% update belief for kalman smoother in level 1
    for i = 1:ts_data.T(Lv1)
        if(i < ts_data.T(Lv1))
            belief.x_sm{Lv1}{i} = GaussianMultiply(msg{varNode.x{Lv1}{i}.forwardNeighborMsgID}.toFactorNode,...
                msg{varNode.x{Lv1}{i}.forwardNeighborMsgID}.toVarNode);
        else
            belief.x_sm{Lv1}{i} = GaussianMultiply(msg{varNode.x{Lv1}{i}.backwardNeighborMsgID}.toFactorNode,...
                msg{varNode.x{Lv1}{i}.backwardNeighborMsgID}.toVarNode);
        end
        ts_data.mu_sm{Lv1}(:,i) = belief.x_sm{Lv1}{i}.mu;
    end
    %% update belief for kalman smoother in level 2
    for i = 1:ts_data.T(Lv2)
        belief.x_sm{Lv2}{i} = GaussianMultiply(msg{varNode.x{Lv2}{i}.upperNeighborMsgID}.toFactorNode,...
                msg{varNode.x{Lv2}{i}.upperNeighborMsgID}.toVarNode);
        ts_data.mu_sm{Lv2}(:,i) = belief.x_sm{Lv2}{i}.mu;
    end
end

Contact us