annotate toolboxes/FullBNT-1.0.7/Kalman/sample_lds.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 [x,y] = sample_lds(F, H, Q, R, init_state, T, models, G, u)
Daniel@0 2 % SAMPLE_LDS Simulate a run of a (switching) stochastic linear dynamical system.
Daniel@0 3 % [x,y] = switching_lds_draw(F, H, Q, R, init_state, models, G, u)
Daniel@0 4 %
Daniel@0 5 % x(t+1) = F*x(t) + G*u(t) + w(t), w ~ N(0, Q), x(0) = init_state
Daniel@0 6 % y(t) = H*x(t) + v(t), v ~ N(0, R)
Daniel@0 7 %
Daniel@0 8 % Input:
Daniel@0 9 % F(:,:,i) - the transition matrix for the i'th model
Daniel@0 10 % H(:,:,i) - the observation matrix for the i'th model
Daniel@0 11 % Q(:,:,i) - the transition covariance for the i'th model
Daniel@0 12 % R(:,:,i) - the observation covariance for the i'th model
Daniel@0 13 % init_state(:,i) - the initial mean for the i'th model
Daniel@0 14 % T - the num. time steps to run for
Daniel@0 15 %
Daniel@0 16 % Optional inputs:
Daniel@0 17 % models(t) - which model to use at time t. Default = ones(1,T)
Daniel@0 18 % G(:,:,i) - the input matrix for the i'th model. Default = 0.
Daniel@0 19 % u(:,t) - the input vector at time t. Default = zeros(1,T)
Daniel@0 20 %
Daniel@0 21 % Output:
Daniel@0 22 % x(:,t) - the hidden state vector at time t.
Daniel@0 23 % y(:,t) - the observation vector at time t.
Daniel@0 24
Daniel@0 25
Daniel@0 26 if ~iscell(F)
Daniel@0 27 F = num2cell(F, [1 2]);
Daniel@0 28 H = num2cell(H, [1 2]);
Daniel@0 29 Q = num2cell(Q, [1 2]);
Daniel@0 30 R = num2cell(R, [1 2]);
Daniel@0 31 end
Daniel@0 32
Daniel@0 33 M = length(F);
Daniel@0 34 %T = length(models);
Daniel@0 35
Daniel@0 36 if nargin < 7,
Daniel@0 37 models = ones(1,T);
Daniel@0 38 end
Daniel@0 39 if nargin < 8,
Daniel@0 40 G = num2cell(repmat(0, [1 1 M]));
Daniel@0 41 u = zeros(1,T);
Daniel@0 42 end
Daniel@0 43
Daniel@0 44 [os ss] = size(H{1});
Daniel@0 45 state_noise_samples = cell(1,M);
Daniel@0 46 obs_noise_samples = cell(1,M);
Daniel@0 47 for i=1:M
Daniel@0 48 state_noise_samples{i} = sample_gaussian(zeros(length(Q{i}),1), Q{i}, T)';
Daniel@0 49 obs_noise_samples{i} = sample_gaussian(zeros(length(R{i}),1), R{i}, T)';
Daniel@0 50 end
Daniel@0 51
Daniel@0 52 x = zeros(ss, T);
Daniel@0 53 y = zeros(os, T);
Daniel@0 54
Daniel@0 55 m = models(1);
Daniel@0 56 x(:,1) = init_state(:,m);
Daniel@0 57 y(:,1) = H{m}*x(:,1) + obs_noise_samples{m}(:,1);
Daniel@0 58
Daniel@0 59 for t=2:T
Daniel@0 60 m = models(t);
Daniel@0 61 x(:,t) = F{m}*x(:,t-1) + G{m}*u(:,t-1) + state_noise_samples{m}(:,t);
Daniel@0 62 y(:,t) = H{m}*x(:,t) + obs_noise_samples{m}(:,t);
Daniel@0 63 end
Daniel@0 64
Daniel@0 65