-
Notifications
You must be signed in to change notification settings - Fork 0
/
J4_troubleshoot.m
executable file
·169 lines (148 loc) · 5.15 KB
/
J4_troubleshoot.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
% Trouble shooting J function with fitting for sigma
% this code is meant to go through each piece of the negative LL function
% and trouble shoot it, using the neg_LL as a function of time for the true
% parameters, guessed parameters, and incorrect but converged parameters.
% Run simultaneously with moment_approach_fitting.m
% current J function
J4_t= @(phat)((0.5*((log(2*pi*var_in_mean4(pbxform4(phat)))) + (((yfxform(modelfun_mu4(pbxform4(phat))) - yfxform(mudata))./sqrt(var_in_mean4(pbxform4(phat)))).^2))) +...
(0.5*((log(2*pi*var_in_var4(pbxform4(phat)))) + (((yfxform(modelfun_V4(pbxform4(phat))) - yfxform(vardata))./sqrt(var_in_var4(pbxform4(phat)))).^2))));
%%
guess = [];
true = [];
fit = [];
% Look only at mean, model - data
J1_4 = @(phat)(((yfxform(modelfun_mu4(pbxform4(phat))) - yfxform(mudata)).^2)./(var_in_mean4(pbxform4(phat))));
true(:,1) = J1_4(pfxform4(p4));
guess(:,1) = J1_4(pfxform4(theta4));
fit(:,1) = J1_4(phatbest_J4);
figure;
plot(t, mudata, 'LineWidth',2)
hold on
plot(t,modelfun_mu4(p4), 'LineWidth',2)
plot(t, modelfun_mu4(pbxform4(phatbest_J4)), 'LineWidth',2)
legend('data', 'true params', 'fit params')
figure;
%plot(t, true(:,1), 'LineWidth',2)
hold on
plot(t,guess(:,1),'*','LineWidth', 2)
plot(t,fit(:,1), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('component 1 of neg LL')
title('squared error/var in mean')
%%
J2_4 = @(phat)((log(2*pi*(var_in_mean4(pbxform4(phat)))))+ ((yfxform(modelfun_mu4(pbxform4(phat))) - yfxform(mudata)).^2)./(var_in_mean4(pbxform4(phat))));
true(:,2) = J2_4(pfxform4(p4));
guess(:,2) = J2_4(pfxform4(theta4));
fit(:,2) = J2_4(phatbest_J4);
figure;
plot(t, true(:,2), 'LineWidth',2)
hold on
plot(t,guess(:,2),'*','LineWidth', 2)
plot(t,fit(:,2), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('component 2 of neg LL')
title('log 2pi*var in mean + squared error/var in mean')
J3_4 = @(phat)((log(2*pi*(var_in_mean4(pbxform4(phat))))));
true(:,3) = J3_4(pfxform4(p4));
guess(:,3) = J3_4(pfxform4(theta4));
fit(:,3) = J3_4(phatbest_J4);
figure;
plot(t, true(:,3), 'LineWidth',2)
hold on
plot(t,guess(:,3),'*','LineWidth', 2)
plot(t,fit(:,3), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('component 2 of neg LL')
title('log 2pi*var in mean')
%% Next focus on variance terms
J4_4 = @(phat) ((yfxform(modelfun_V4(pbxform4(phat))) - yfxform(vardata)).^2)./(var_in_var4(pbxform4(phat)));
true(:,4) = J4_4(pfxform4(p4));
guess(:,4) = J4_4(pfxform4(theta4));
fit(:,4) = J4_4(phatbest_J4);
figure;
plot(t, true(:,4), 'LineWidth',2)
hold on
plot(t,guess(:,4),'*','LineWidth', 2)
plot(t,fit(:,4), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('component of neg LL')
xlim( [0 100])
title('squared error in variance/var in var')
%%
J5_4 = @(phat) (log(2*pi*(var_in_var4(pbxform4(phat))))+(yfxform(modelfun_V4(pbxform4(phat))) - yfxform(vardata)).^2./(var_in_var4(pbxform4(phat))));
true(:,5) = J5_4(pfxform4(p4));
guess(:,5) = J5_4(pfxform4(theta4));
fit(:,5) = J5_4(phatbest_J4);
figure;
plot(t, true(:,5), 'LineWidth',2)
hold on
plot(t,guess(:,5),'*','LineWidth', 2)
plot(t,fit(:,5), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('component of neg LL')
title(' log 2pi var in var + squared error in variance/var in var')
J7_4 = @(phat) (log(2*pi*(var_in_var4(pbxform4(phat)))));
true(:,7) = J7_4(pfxform4(p4));
guess(:,7) = J7_4(pfxform4(theta4));
fit(:,7) = J7_4(phatbest_J4);
figure;
plot(t, true(:,7), 'LineWidth',2)
hold on
plot(t,guess(:,7),'*','LineWidth', 2)
plot(t,fit(:,7), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('component of neg LL')
title(' log 2pi var in var ')
%%
J6_4 = @(phat) ((yfxform(modelfun_V4(pbxform4(phat))) - yfxform(vardata)).^2);
true(:,6) = J6_4(pfxform4(p4));
guess(:,6) = J6_4(pfxform4(theta4));
fit(:,6) = J6_4(phatbest_J4);
figure;
plot(t, true(:,6), 'LineWidth',2)
hold on
plot(t,guess(:,6),'*','LineWidth', 2)
plot(t,fit(:,6), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('component 2 of neg LL')
title('squared error in variance')
%%
true(:,7) = var_in_var(p);
guess(:,7) = var_in_var(theta);
fit(:,7) = var_in_var(params_best_J4);
figure;
plot(t, true(:,7), 'LineWidth', 2)
hold on
plot(t, var_in_var_data, 'LineWidth',2)
legend('var in var fxn', 'var in var data')
xlabel('time')
ylabel('var in var')
title('Comparison data to function')
%%
figure;
plot(t, true(:,7), 'LineWidth',2)
hold on
plot(t,guess(:,7),'*','LineWidth', 2)
%plot(t,fit(:,7), 'LineWidth', 2)
plot(t, var_in_var_data, 'o','LineWidth', 2)
legend('true params', 'initial guess', 'var in var data')
xlabel('time')
ylabel('var in var')
title('var in var')
%%
figure;
plot(t, 1/true(:,7), 'LineWidth',2)
hold on
plot(t,1/guess(:,7),'*','LineWidth', 2)
plot(t,1/fit(:,7), 'LineWidth', 2)
legend('true params', 'initial guess', 'fit params')
xlabel('time')
ylabel('1/var in var')
title('1/var in var')