annotate toolboxes/FullBNT-1.0.7/Kalman/eval_AR_perf.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [ypred, ll, mse] = eval_AR_perf(coef, C, y, model)
Daniel@0 2 % Evaluate the performance of an AR model.
Daniel@0 3 %
Daniel@0 4 % Inputs
Daniel@0 5 % coef(:,:,k,m) - coef. matrix to use for k steps back, model m
Daniel@0 6 % C(:,:,m) - cov. matrix for model m
Daniel@0 7 % y(:,t) - observation at time t
Daniel@0 8 % model(t) - which model to use at time t (defaults to 1 if not specified)
Daniel@0 9 %
Daniel@0 10 % Outputs
Daniel@0 11 % ypred(:,t) - the predicted value of y at t based on the evidence thru t-1.
Daniel@0 12 % ll - log likelihood
Daniel@0 13 % mse - mean squared error = sum_t d_t . d_t, where d_t = pred(y_t) - y(t)
Daniel@0 14
Daniel@0 15 [s T] = size(y);
Daniel@0 16 k = size(coef, 3);
Daniel@0 17 M = size(coef, 4);
Daniel@0 18
Daniel@0 19 if nargin<4, model = ones(1, T); end
Daniel@0 20
Daniel@0 21 ypred = zeros(s, T);
Daniel@0 22 ypred(:, 1:k) = y(:, 1:k);
Daniel@0 23 mse = 0;
Daniel@0 24 ll = 0;
Daniel@0 25 for j=1:M
Daniel@0 26 c(j) = log(normal_coef(C(:,:,j)));
Daniel@0 27 invC(:,:,j) = inv(C(:,:,j));
Daniel@0 28 end
Daniel@0 29 coef = reshape(coef, [s s*k M]);
Daniel@0 30
Daniel@0 31 for t=k+1:T
Daniel@0 32 m = model(t-k);
Daniel@0 33 past = y(:,t-1:-1:t-k);
Daniel@0 34 ypred(:,t) = coef(:, :, m) * past(:);
Daniel@0 35 d = ypred(:,t) - y(:,t);
Daniel@0 36 mse = mse + d' * d;
Daniel@0 37 ll = ll + c(m) - 0.5*(d' * invC(:,:,m) * d);
Daniel@0 38 end
Daniel@0 39 mse = mse / (T-k+1);
Daniel@0 40