-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsample_lds.m
34 lines (29 loc) · 1021 Bytes
/
sample_lds.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [x,y] = sample_lds(A, C, Q, R, init_state, T, G, u)
% SAMPLE_LDS Simulate a run of a (switching) stochastic linear dynamical system.
% [x,y] = switching_lds_draw(A, C, Q, R, init_state, models, G, u)
%
% x(t+1) = A*x(t) + w(t), w ~ N(0, Q), x(0) = init_state
% y(t) = C*x(t) + v(t), v ~ N(0, R)
%
% Input:
% A(:,:) - the transition matrix
% C(:,:) - the observation matrix
% Q(:,:) - the transition covariance
% R(:,:) - the observation covariance
% init_state(:) - the initial mean
% T - the num. time steps to run for
%
% Output:
% x(:,t) - the hidden state vector at time t.
% y(:,t) - the observation vector at time t.
[os ss] = size(C);
state_noise_samples = sample_gaussian(zeros(length(Q),1), Q, T)';
obs_noise_samples = sample_gaussian(zeros(length(R),1), R, T)';
x = zeros(ss, T);
y = zeros(os, T);
x(:,1) = init_state(:);
y(:,1) = C*x(:,1) + obs_noise_samples(:,1);
for t=2:T
x(:,t) = A*x(:,t-1)+ state_noise_samples(:,t);
y(:,t) = C*x(:,t) + obs_noise_samples(:,t);
end