-
Notifications
You must be signed in to change notification settings - Fork 1
/
rigorous_statistics_TC_rhoC.m
458 lines (282 loc) · 8.91 KB
/
rigorous_statistics_TC_rhoC.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
function [ TC_min, TC_max, rhoC_min, rhoC_max ] = rigorous_statistics_TC_rhoC( T,pv,pL)
%This function requires the best fit parameters as input and it performs a
%rigorous determination of the confidence region.
% [TC_fit, pc_fit, A_fit, b_fit] = towhee_rectilinear(T,pv,pL,sv,sL);
[TC_fit, pc_fit, A_fit, b_fit, sy, sz] = towhee_error_model(T,pv,pL);
% sz = ((sv.^2 + sL.^2).^(1/2))/2;
% sy = sz;
% A_fit = 3.8858442*10^-4;
% b_fit = 0.0584264;
% pc_fit = 0.2424774;
% TC_fit = 617.2207397;
n = 2*length(T);
beta = 0.32;
p = 4;
y = (pv + pL)/2;
z = (pL - pv)/2;
SE_fit = ((y - (pc_fit + A_fit*(TC_fit-T)))./sy).^2 + ((z - (b_fit*(TC_fit-T).^beta))./sz).^2; % This includes uncertainties
SSE_fit = sum(SE_fit);
sigma = SSE_fit/(n-p);
alpha = 0.95;
RHS = sigma * (n + p * (finv(alpha,p,n-p)-1)); % No 0.95^p because I want the true 95% (though sometimes it will be 0.95^2 or 0.95^4)
% For the growth part we start with a pretty coarse grid. Once we have
% grown as much as we want, then we go to a finer grid.
Ab_spacing = 10; % For speed, only 10 values will be used for A and b
pcTC_spacing = 20; % pc and TC are more important
iteration = 0;
% Converged is for the finer grid portion, growing is for the coarse grid
converged = false;
convergence = [false, false, false, false, false, false, false, false];
growing = true;
growth = [true,true,true,true,true,true,true,true];
% Since we are going to grow our confidence region we start with a very
% small region. These will grow with the coarse grid and then contract
% back with the finer grid.
A_low = 0.99*A_fit;
A_high = 1.01*A_fit;
b_low = 0.995*b_fit;
b_high = 1.005*b_fit;
pc_low = 0.995*pc_fit;
pc_high = 1.005*pc_fit;
% pc_low = 0.2405;
% pc_high = 0.24445;
TC_low = 0.995*TC_fit;
TC_high = 1.005*TC_fit;
% TC_low = 616.75;
% TC_high = 617.735;
while converged ==false
A_spacing = (A_high-A_low)/(Ab_spacing-1);
b_spacing = (b_high-b_low)/(Ab_spacing-1);
pc_spacing = (pc_high-pc_low)/(pcTC_spacing-1);
TC_spacing = (TC_high-TC_low)/(pcTC_spacing-1);
A_range = A_low:A_spacing:A_high;
b_range = b_low:b_spacing:b_high;
pc_range = pc_low:pc_spacing:pc_high;
TC_range = TC_low:TC_spacing:TC_high;
Ext = zeros(1,(length(A_range)*length(b_range)*length(pc_range)*length(TC_range)));
A_ext = Ext;
b_ext = Ext;
pc_ext = Ext;
TC_ext = Ext;
clear Ext
s=1;
for g=1:length(A_range)
for h=1:length(b_range)
for i=1:length(pc_range)
for j=1:length(TC_range)
SE = ((y - (pc_range(i) + A_range(g)*(TC_range(j)-T)))./sy).^2 + ((z - (b_range(h)*(TC_range(j)-T).^beta))./sz).^2;
SSE = sum(SE);
if SSE < RHS
A_ext(s) = A_range(g);
b_ext(s) = b_range(h);
pc_ext(s) = pc_range(i);
TC_ext(s) = TC_range(j);
s=s+1;
if SSE < SSE_fit % This is to make sure that we actually have the global minimum. If not, rerun the analysis with the new optimum (after plugging in as a new Mathcad guess)
new_best_fit = [A_range(g) b_range(h) pc_range(i) TC_range(j)];
SSE_fit = SSE;
end
end
end
end
end
end
% This eliminates the superfluous zero elements
A_ext = A_ext(:,1:(s-1));
b_ext = b_ext(:,1:(s-1));
pc_ext = pc_ext(:,1:(s-1));
TC_ext = TC_ext(:,1:(s-1));
A_low_temp = min(A_ext);
A_high_temp = max(A_ext);
b_low_temp = min(b_ext);
b_high_temp = max(b_ext);
pc_low_temp = min(pc_ext);
pc_high_temp = max(pc_ext);
TC_low_temp = min(TC_ext);
TC_high_temp = max(TC_ext);
if growing % The first part is to grow the regions
if isempty(A_low_temp)
factor = 10^(-(6+iteration));
low_factor = 1-factor;
high_factor = 1+factor;
A_low = low_factor*A_fit;
A_high = high_factor*A_fit;
b_low = low_factor*b_fit;
b_high = high_factor*b_fit;
pc_low = low_factor*pc_fit;
pc_high = high_factor*pc_fit;
TC_low = low_factor*TC_fit;
TC_high = high_factor*TC_fit;
else
if A_low_temp == A_low
A_low = A_low - 2*A_spacing;
growth(1) = true;
else
growth(1) = false;
end
if A_high_temp == A_high
A_high = A_high + 2*A_spacing;
growth(2) = true;
else
growth(2) = false;
end
if b_low_temp == b_low
b_low = b_low - 2*b_spacing;
growth(3) = true;
else
growth(3) = false;
end
if b_high_temp == b_high
b_high = b_high + 2*b_spacing;
growth(4) = true;
else
growth(4) = false;
end
if pc_low_temp == pc_low
pc_low = pc_low - 2*pc_spacing;
growth(5) = true;
else
growth(5) = false;
end
if pc_high_temp == pc_high
pc_high = pc_high + 2*pc_spacing;
growth(6) = true;
else
growth(6) = false;
end
if TC_low_temp == TC_low
TC_low = TC_low - 2*TC_spacing;
growth(7) = true;
else
growth(7) = false;
end
if TC_high_temp == TC_high
TC_high = TC_high + 2*TC_spacing;
growth(8) = true;
else
growth(8) = false;
end
end
if growth(:)==false % Once all the regions incapsiluate the extrema we contract back down with a more refined grid
growing=false;
Ab_spacing = 20; % For speed, only 20 values will be used for A and b
pcTC_spacing = 40; % pc and TC are more important
end
else
if isempty(A_low_temp) % If one is empty they are all empty (could also use if s==1)
A_low = A_low + A_spacing/2;
A_high = A_high - A_spacing/2;
b_low = b_low + b_spacing/2;
b_high = b_high - b_spacing/2;
pc_low = pc_low + pc_spacing/2;
pc_high = pc_high - pc_spacing/2;
TC_low = TC_low + TC_spacing/2;
TC_high = TC_high - TC_spacing/2;
else
if A_low_temp == A_low
convergence(1) = true;
else
A_low = A_low_temp - A_spacing/2;
end
if A_high_temp == A_high
convergence(2) = true;
else
A_high = A_high_temp + A_spacing/2;
end
if b_low_temp == b_low
convergence(3) = true;
else
b_low = b_low_temp - A_spacing/2;
end
if b_high_temp == b_high
convergence(4) = true;
else
b_high = b_high_temp + A_spacing/2;
end
if pc_low_temp == pc_low
convergence(5) = true;
else
pc_low = pc_low_temp - pc_spacing/2;
end
if pc_high_temp == pc_high
convergence(6) = true;
else
pc_high = pc_high_temp + pc_spacing/2;
end
if TC_low_temp == TC_low
convergence(7) = true;
else
TC_low = TC_low_temp - TC_spacing/2;
end
if TC_high_temp == TC_high
convergence(8) = true;
else
TC_high = TC_high_temp + TC_spacing/2;
end
end
end
if convergence(:)==true % If all have converged we are done
converged=true;
end
iteration = iteration + 1;
if iteration == 30 % To avoid infinite loops
converged = true;
end
end
% subplot(3,2,1)
% plot(A_ext,b_ext)
% subplot(3,2,2)
% plot(pc_ext,b_ext)
% subplot(3,2,3)
% plot(A_ext,TC_ext)
% subplot(3,2,4)
% plot(pc_ext,TC_ext)
% subplot(3,2,5)
% plot(A_ext,pc_ext)
% subplot(3,2,6)
% plot(b_ext,TC_ext)
% % To plot just the pc and TC region
pc_scan=min(pc_range):pc_spacing:max(pc_range);
k=1;
for h=1:length(pc_scan)
j=1;
for i=1:length(pc_ext)
if pc_ext(i) == pc_scan(h)
% if (pc_ext(i) - pc_scan(h)) < 0.00000001
TC_scan(j) = TC_ext(i);
j=j+1;
end
end
% I did it this way because, for some reason, not every pc in the range of
% pc_scan actually has an accepted point. Somehow I missed the A,B and TC
% combinations that were required.
if j>1
TC_upper(k) = max(TC_scan);
TC_lower(k) = min(TC_scan);
pc_plot(k) = pc_scan(h);
k=k+1;
end
TC_scan=TC_fit;
end
% figure
% hold
%
% scatter(pc_ext,TC_ext,'x')
% plot(pc_plot,TC_upper);
% plot(pc_plot,TC_lower);
%
% hold
TC_low=min(TC_lower);
TC_high=max(TC_upper);
pc_low = min(pc_low);
pc_high = max(pc_high);
A_low/A_fit
A_high/A_fit
b_low/b_fit
b_high/b_fit
pc_low/pc_fit
pc_high/pc_fit
TC_low/TC_fit
TC_high/TC_fit
truly_rigorous_TC_rhoC_uncertainty
end