Advertisement
Guest User

Lab 3

a guest
Dec 15th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. % Lab 3
  2.  
  3.  
  4. %% 1.1
  5. P11 = 7/8
  6. P22 = P11
  7. P = [P11 (1-P11); (1-P22) P22]
  8. mc = dtmc(P)
  9. numSteps = 999
  10. u = simulate(mc, numSteps)
  11.  
  12.  
  13. %% 1.2
  14.  
  15. % Example of Kalman filter
  16. % Simulate process
  17.  
  18. % Example of Kalmanfilter
  19. % Simulate process
  20.  
  21. N = 200;
  22. A = [1 .5 0.8];
  23. sigma2 = 1.5;
  24. e = sqrt(sigma2)*randn(N,1);
  25. y = filter(1,A,e);
  26.  
  27.  
  28.  
  29. % Data length
  30. N = length(y);
  31.  
  32. % Define the state space equations
  33.  
  34. Sigma2_e = 1.5;
  35. sigma2_w = 1.5;
  36.  
  37. A = eye(2); %???
  38. Re = zeros(2); % Hidden state noise covariance matrix %IS THIS RIGHT???
  39. Rw = sigma2_w; % Observation variance % Initital guess here is 1.
  40.  
  41. % usually C should be set here to,
  42. % but in this case C is a function of time.
  43.  
  44. % Set some initial values
  45. Rxx_1 = (1) * eye(2); % Initial variance
  46. xtt_1 = A*[0 0]'; % Initial state . ????
  47.  
  48. % Vector to store values in
  49. xsave = zeros(2, N);
  50. % Kalman filter. Start from k=3 ,
  51. % since we need old values of y.
  52. for k=3:N,
  53. % C is, in our case, a function of time.
  54. C = [-y(k-1) -y(k-2)];
  55. % Update
  56. Ryy = C*Rxx_1*C' + Rw;
  57. Kt = Rxx_1*C'*inv(Ryy);
  58. xtt = xtt_1 + Kt* (y(k) - C*xtt_1);
  59. Rxx = (eye(2) - Kt * C)*Rxx_1;
  60. % Save
  61. xsave(:, k) = xtt;
  62. % Predict
  63. Rxx_1 = A*Rxx*A' + Re;
  64. xtt_1 = A*xtt;
  65. end;
  66.  
  67. xtt
  68.  
  69. %% -------------------------- SECTION 3 --------------------------------
  70. subplot(211)
  71. plot(tar2)
  72. title('tar2')
  73. subplot(212)
  74. plot(thx)
  75. title('thx')
  76. %%
  77. na = 2;
  78. nb = 0;
  79. nk = 0;
  80. model = [na];
  81. lambda = 0.95;
  82. [Aest, yhat, covAest, yprev] = rarx(tar2, model, 'ff', lambda);
  83.  
  84. %%
  85. plot(Aest)
  86. hold on
  87. plot(thx)
  88. % QUESTION 1, the lower the lambda, the faster and noiser the parameter
  89. % estimates become
  90. %%
  91. n = 100;
  92. lambda_line = linspace(0.85, 1, n);
  93. ls2 = zeros(n, 1);
  94. for i =1:length(lambda_line)
  95. [Aest, yhat, CovAest, trash] = ...
  96. rarx(tar2, [2], 'ff', lambda_line(i));
  97. ls2(i) = sum((tar2 - yhat).^2);
  98. end
  99. plot(lambda_line, ls2)
  100. %find(ls2 == min(ls2(:)))
  101. % QUESTION 2, the square sum of the prediction errors.
  102. % QUESTION 3, Re = [sigma2_e 0; 0 0] as e = [sigma_e 0]
  103. %%
  104. sigma2_e = 10^(-3);
  105. Re = sigma2_e*[1 0; 0 0];
  106. Rw = 1;
  107. V0 = 10*eye(2)
  108. m0 = [0 0]'
  109. param = kalman_param(tar2, 2, Re, Rw, V0, m0)
  110. zsave1 = param(1,:)'
  111. param2 = param(2,:)'
  112. param = [zsave1 param2]
  113. plot(param)
  114. hold on
  115. plot(thx)
  116. % QUESTION 4.1, Increasing Re and decreasing Rw increases the speed and varaince. Also it
  117. % is the relationship between Re and Rw that is important not the absolute
  118. % terms.
  119. % QUESTION 4.2, Yes we got alot better results than RLS. Kalman might be less
  120. % computionally efficient and we have to check that our poles are inside of our unit circle
  121.  
  122. %% ----------------------- SECTION 3.3 -----------------------
  123. clear y e N
  124. sigma2_e = 1;
  125. sigma2_v = 4;
  126. b = 20;
  127. N = 1000;
  128.  
  129. RW = [1 -1];
  130. e = sqrt(sigma2_e) * randn(N,1);
  131. v = sqrt(sigma2_v) * randn(N,1);
  132. x = filter(1, RW, e);
  133.  
  134. x = x(100:end);
  135. e = e(100:end);
  136. v = v(100:end);
  137. u = u(100:end);
  138.  
  139.  
  140. for k = 1:length(u)
  141. y(k) = x(k) + b * u(k) + v(k);
  142. end
  143. y = y';
  144. %%
  145.  
  146. A = [1 0; 0 1];
  147.  
  148. sigma2_e = 1;
  149. sigma2_v = 4;
  150.  
  151. Re = sigma2_e * [1 0; 0 0];
  152. Rw = sigma2_v;
  153.  
  154. ztt_1 = [0;0];
  155. Rxx_1 = 100*eye(2);
  156.  
  157. clear zsave
  158. for k = 1:length(y)
  159. ztt = ztt_1;
  160. C = [1 u(k)];
  161. Kt = (Rxx_1*C')*((C*Rxx_1*C' + Rw)^(-1));
  162. ztt = ztt_1 + Kt * (y(k)-C*ztt_1);
  163. Rxx = Rxx_1 - Kt*C*Rxx_1;
  164. zsave(:,k) = ztt;
  165. ztt_1 = ztt;
  166. Rxx_1 = Rxx + Re; % ignored A here since it is the identity matrix.
  167. end
  168. zsave = [zsave(1,:)' zsave(2,:)'];
  169. plot(zsave)
  170. hold on
  171. plot(x, '--')
  172. hold on
  173. plot(repmat(20, [length(x) 1]))
  174.  
  175.  
  176. % QUESTION 5.2, the state is the param b
  177. % 5.3 Re = [sigma2_e 0; 0 0] and Rw = sigma2_v
  178.  
  179.  
  180. %% --------------------- SECTION 3.4 -------------------
  181. plot(svedala94)
  182. s = 6
  183. y_diff = filter([1 zeros(1, s-1) -1], 1, svedala94)
  184.  
  185. %%
  186. subplot(211)
  187. plot(y_diff)
  188. subplot(212)
  189. plot(svedala94)
  190. %%
  191. subplot(211)
  192. T = linspace(datenum(1994,1,1), datenum(1994, 12, 31),...
  193. length(svedala94));
  194. plot(T,svedala94);
  195. datetick('x');
  196. subplot(212)
  197. T = linspace(datenum(1994,1,1), datenum(1994, 12, 31),...
  198. length(svedala94));
  199. plot(T,y_diff);
  200. datetick('x');
  201.  
  202. %%
  203. th = armax(y_diff, [2 2]);
  204. th_winter = armax(y_diff(1:540), [2 2]);
  205. th_summer = armax(y_diff(907:1458), [2 2]);
  206. %% i: ALL the data
  207. present(th)
  208. % FPE = 2.603, MSE = 2.593, All params are significant
  209. th.A % [1 -1.6235 0.6551]
  210. th.c % [1 -0.8313 -0.1361]
  211. %% ii: Jan - Mar
  212. present(th_winter)
  213. % FPE = 1.6, MSE = 1.576, All params are significant
  214. th_winter.A % [1 -1.6662 0.7064]
  215. th_winter.C % [1 -0.8331 -0.1078]
  216.  
  217. %% iii: Jun - Aug
  218. present(th_summer)
  219. % FPE = 3.515, MSE = 3.464, All params are not significant
  220. th_summer.A % [1 0.2545 -0.4598]
  221. th_summer.C % [1 1.0326 0.0427]
  222. % QUESTION 6, Yes it is likley that th_winter and th_summer are different
  223. % processes given our param estimates
  224.  
  225. %%
  226. th0 = [th_winter.A(2:end) th_winter.C(2:end)];
  227. [thr, yhat] = rarmax(y_diff, [2 2], 'ff', 0.99, th0);
  228. subplot(311);
  229. plot(T, svedala94);
  230. datetick('x');
  231.  
  232. subplot(312);
  233. plot(thr(:,1:2));
  234. hold on
  235. plot(repmat(th_winter.A(2:end), [length(thr) 1]), 'b');
  236. plot(repmat(th_summer.A(2:end), [length(thr) 1]), 'r');
  237. axis tight
  238. hold off
  239.  
  240. subplot(313)
  241. plot(thr(:,3:end))
  242. hold on
  243. plot(repmat(th_winter.C(2:end), [length(thr) 1]), 'b');
  244. plot(repmat(th_summer.C(2:end), [length(thr) 1]), 'r');
  245. axis tight
  246. hold off
  247. % QUESTION 7, ??
  248.  
  249. %% ------------------ SECTION 3.5 -------------------------------
  250.  
  251. y = svedala94(850:1100)
  252. y_mean = mean(y)
  253.  
  254. %%
  255. s = 6;
  256. t = (1:length(y))';
  257. U = [sin(2*pi*t/s) cos(2*pi*t/s)];
  258. Z = iddata(y ,U);
  259. model = [3 [1 1] 4 [0 0]];
  260. %[na [nb_1 nb_2] nc [nk_1 nk_2]];
  261.  
  262.  
  263. thx = armax(Z,model);
  264. thx.b;
  265.  
  266. subplot(211)
  267. plot(U * cell2mat(thx.b)')
  268. subplot(212)
  269.  
  270. plot(y)
  271.  
  272. % QUESTION 7.1, increasing the length of the season decreases the frequency
  273. % of U*thx.b ??
  274. % QUESTION 7.2, we might need to take a trend into acount
  275.  
  276. %%
  277. U = [sin(2*pi*t/6) cos(2*pi*t/6) ones(size(t))];
  278. Z = iddata(y,U);
  279. m0 = [thx.A(2:end) cell2mat(thx.B) 0 thx.C(2:end)]; %% thx.B
  280. Re = diag([0 0 0 0 0 1 0 0 0 0]);
  281. model = [3 [1 1 1] 4 0 [0 0 0] [1 1 1]];
  282. [thr, yhat] = rpem(Z, model, 'kf', Re ,m0)
  283.  
  284. plot(yhat)
  285. hold on
  286. plot(y, 'black')
  287. %%
  288.  
  289. m = thr(:,6);
  290. a = thr(end, 4);
  291. b = thr(end, 5);
  292. y_mean = m + a*U(:, 1)+ b*U(:, 2);
  293. y_mean = [0;y_mean(1:end - 1)];
  294.  
  295. %%
  296. plot(y)
  297. hold on
  298. plot(y_mean, '--')
  299.  
  300. % QUESTION 9, They seem rahter similar except that the true y values have
  301. % an added constant. Moreover we also have some peaks in our y_mean that
  302. % are not present in the true values. WHY?
  303.  
  304. %%
  305.  
  306. y = svedala94;
  307. y = y-y(1);
  308. %%
  309. s = 6;
  310. t = (1:length(y))';
  311. U = [sin(2*pi*t/s) cos(2*pi*t/s)];
  312. Z = iddata(y ,U);
  313. model = [3 [1 1] 4 [0 0]];
  314. %[na [nb_1 nb_2] nc [nk_1 nk_2]];
  315.  
  316.  
  317. thx = armax(Z,model);
  318. thx.b;
  319.  
  320.  
  321. U = [sin(2*pi*t/6) cos(2*pi*t/6) ones(size(t))];
  322. Z = iddata(y,U);
  323. m0 = [thx.A(2:end) cell2mat(thx.B) 0 thx.C(2:end)]; %% thx.B
  324. Re = diag([0 0 0 0 0 1 0 0 0 0]);
  325. model = [3 [1 1 1] 4 0 [0 0 0] [1 1 1]];
  326. [thr, yhat] = rpem(Z, model, 'kf', Re ,m0);
  327.  
  328. %%
  329. y_tilde = (y - yhat)
  330. %acfpacf(y_tilde)
  331. whitenessTest(y_tilde)
  332. %
  333. %% Due to that the processes for the different season vary quite alot we could redo the analysis above for the different seasons
  334. % SUMMER
  335. y = svedala94(850:1100);
  336. y = y-y(1);
  337. s = 6;
  338. t = (1:length(y))';
  339. U = [sin(2*pi*t/s) cos(2*pi*t/s)];
  340. Z = iddata(y ,U);
  341. model = [3 [1 1] 4 [0 0]];
  342. %[na [nb_1 nb_2] nc [nk_1 nk_2]];
  343.  
  344.  
  345. thx = armax(Z,model);
  346. thx.b;
  347.  
  348. U = [sin(2*pi*t/6) cos(2*pi*t/6) ones(size(t))];
  349. Z = iddata(y,U);
  350. m0 = [thx.A(2:end) cell2mat(thx.B) 0 thx.C(2:end)]; %% thx.B
  351. Re = diag([0 0 0 0 0 1 0 0 0 0]);
  352. model = [3 [1 1 1] 4 0 [0 0 0] [1 1 1]];
  353. [thr, yhat] = rpem(Z, model, 'kf', Re ,m0);
  354.  
  355. y_tilde = (y - yhat)
  356. whitenessTest(y_tilde)
  357. % Remove the outlier in observation 8 perhaps by interpolation or something
  358. % var(y_tilde) = 9.7612
  359. %%
  360. % WINTER
  361. y = svedala94(1:450);
  362. y = y-y(1);
  363. s = 6;
  364. t = (1:length(y))';
  365. U = [sin(2*pi*t/s) cos(2*pi*t/s)];
  366. Z = iddata(y ,U);
  367. model = [3 [1 1] 4 [0 0]];
  368. %[na [nb_1 nb_2] nc [nk_1 nk_2]];
  369.  
  370.  
  371. thx = armax(Z,model);
  372. thx.b;
  373.  
  374. U = [sin(2*pi*t/6) cos(2*pi*t/6) ones(size(t))];
  375. Z = iddata(y,U);
  376. m0 = [thx.A(2:end) cell2mat(thx.B) 0 thx.C(2:end)]; %% thx.B
  377. Re = diag([0 0 0 0 0 1 0 0 0 0]);
  378. model = [3 [1 1 1] 4 0 [0 0 0] [1 1 1]];
  379. [thr, yhat] = rpem(Z, model, 'kf', Re ,m0);
  380.  
  381. y_tilde = (y - yhat)
  382. whitenessTest(y_tilde)
  383.  
  384. % Same as above!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement