-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem1.m
108 lines (80 loc) · 2.53 KB
/
problem1.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
% Problem 1
% Implementing the Bayesian Online Changepoint Detection algorithm
%% Initialization and loading data
clear;
load('NMRlogWell.mat');
N = length(y);
xx = 1:N;
%% Step 1: Setting intial conditions
lambda_CP = 250;
hazard = 1/lambda_CP;
mu_0 = 1.15;
k = 0.01;
alpha = 20;
beta = 2;
chi = zeros(4, 1);
chi(:, 1) = [mu_0; k; alpha; beta];
log_message(1) = 0;
log_R = -Inf(N+1, N+1);
log_R(1, 1) = 0;
mean_cap = zeros(1, N+1);
mean_cap(1) = mu_0;
lambda_cap = zeros(1, N+1);
lambda_cap(1) = k;
%% Starting the main loop
for i = 1:N
% Step 2: Observing datum
x = y(i);
% Step 3: Calculating UPM predictive
log_UPM_predictive = log(pdf('tLocationScale', x, chi(1, 1:i), ...
sqrt((chi(4, 1:i) .* (chi(2, 1:i) + 1)) ./ ...
(chi(3, 1:i) .* chi(2, 1:i))), 2 .* chi(3, 1:i)));
% Step 4: Calculating growth probabilities
log_growth_prob = log_UPM_predictive + log_message + log(1 - hazard);
% Step 5: Calculating changepoint probabilities
log_cp_prob = log(sum(exp(log_UPM_predictive) .* exp(log_message) ...
* hazard));
% Step 6: Computing joint and evidence, and normalizing joint
log_new_joint = [log_cp_prob log_growth_prob];
log_new_joint = log_new_joint - log(sum(exp(log_new_joint)));
% Step 7: Compute RL posterior
log_R(i+1, :) = [log_new_joint -Inf(1, N - i)];
% Step 8: Update statistics and pass message
chi = update_statistics(i, y, mu_0, beta, chi);
log_message = log_new_joint;
% Step 9: Perform prediction
mean_cap(i+1) = sum(exp(log_R(i+1, 1:i+1)) .* chi(1, :));
lambda_cap(i+1) = sum(exp(log_R(i+1, 1:i+1)) .* chi(2, :));
end
%% Plotting run length vs time
figure (1);
[~, run_length] = max(log_R, [], 2);
plot(run_length);
hold on;
title('Run length vs time');
xlabel('Time');
ylabel('Run length');
hold off;
%% Plotting given data and predictive mean vs time
figure (2);
plot(xx, y, xx, mean_cap(2:N+1));
hold on;
legend('Given data', 'Predictive mean');
title('Given data and predictor vs time');
xlabel('Time');
ylabel('Value');
hold off;
function z = update_statistics(i, y, mu_0, beta, chi)
z = [chi(1, :) 0;
chi(2, :) 0;
chi(3, :) 0;
zeros(1, length(chi(4, :)) + 1)];
x = y(i);
temp = (chi(1, :) .* chi(2, :) + x) ./ (chi(2, :) + 1);
z(1, :) = [mu_0 temp];
z(2, i+1) = z(2, i) + 1;
z(3, i+1) = z(3, i) + 0.5;
temp = chi(4, :) + (chi(2, :) .* (x - chi(1, :)) .^ 2) ...
./ (2 .* (chi(2, :) + 1));
z(4, :) = [beta temp];
end