comparison toolboxes/FullBNT-1.0.7/Kalman/sample_lds.m @ 0:e9a9cd732c1e tip

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