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

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [ypred, ll, mse] = eval_AR_perf(coef, C, y, model)
wolffd@0 2 % Evaluate the performance of an AR model.
wolffd@0 3 %
wolffd@0 4 % Inputs
wolffd@0 5 % coef(:,:,k,m) - coef. matrix to use for k steps back, model m
wolffd@0 6 % C(:,:,m) - cov. matrix for model m
wolffd@0 7 % y(:,t) - observation at time t
wolffd@0 8 % model(t) - which model to use at time t (defaults to 1 if not specified)
wolffd@0 9 %
wolffd@0 10 % Outputs
wolffd@0 11 % ypred(:,t) - the predicted value of y at t based on the evidence thru t-1.
wolffd@0 12 % ll - log likelihood
wolffd@0 13 % mse - mean squared error = sum_t d_t . d_t, where d_t = pred(y_t) - y(t)
wolffd@0 14
wolffd@0 15 [s T] = size(y);
wolffd@0 16 k = size(coef, 3);
wolffd@0 17 M = size(coef, 4);
wolffd@0 18
wolffd@0 19 if nargin<4, model = ones(1, T); end
wolffd@0 20
wolffd@0 21 ypred = zeros(s, T);
wolffd@0 22 ypred(:, 1:k) = y(:, 1:k);
wolffd@0 23 mse = 0;
wolffd@0 24 ll = 0;
wolffd@0 25 for j=1:M
wolffd@0 26 c(j) = log(normal_coef(C(:,:,j)));
wolffd@0 27 invC(:,:,j) = inv(C(:,:,j));
wolffd@0 28 end
wolffd@0 29 coef = reshape(coef, [s s*k M]);
wolffd@0 30
wolffd@0 31 for t=k+1:T
wolffd@0 32 m = model(t-k);
wolffd@0 33 past = y(:,t-1:-1:t-k);
wolffd@0 34 ypred(:,t) = coef(:, :, m) * past(:);
wolffd@0 35 d = ypred(:,t) - y(:,t);
wolffd@0 36 mse = mse + d' * d;
wolffd@0 37 ll = ll + c(m) - 0.5*(d' * invC(:,:,m) * d);
wolffd@0 38 end
wolffd@0 39 mse = mse / (T-k+1);
wolffd@0 40