-
Notifications
You must be signed in to change notification settings - Fork 5
/
bootTB.m
126 lines (118 loc) · 3.68 KB
/
bootTB.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
function [amp, ph] = bootTB(f, time, a, p, B, no, option)
% BOOTB Computes functional tolerance bounds
% -------------------------------------------------------------------------
% function computes tolerance bounds for function data containing
% phase and amplitude variation using bootstrap sampling
%
% Usage: [amp, ph] = bootTB(f, time, a, p, B, no)
% [amp, ph] = bootTB(f, time, a, p, B, no, option)
%
% Inputs:
% f (M,N): matrix defining N functions of M samples
% time : time vector of length M
% a: confidence level
% p: coverage level
% B: number of boostrap samples
% no: number of principal components
%
% default options
% option.parallel = 1; % turns offs MATLAB parallel processing (need
% parallel processing toolbox)
% option.closepool = 0; % determines wether to close matlabpool
% option.smooth = 0; % smooth data using standard box filter
% option.sparam = 25; % number of times to run filter
% option.showplot = 1; % turns on and off plotting
% option.method = 'DP1'; % optimization method (DP, DP2, SIMUL, RBFGS)
% option.w = 0.0; % BFGS weight
% option.MaxItr = 20; % maximum iterations
%
% Outputs:
% two struct containing
% amp:
% median_y: median function
% Q1: First quartile
% Q3: Second quartile
% Q1a: Lower amplitude TB based on alpha
% Q3a: Upper amplitude TB based on alpha
% minn: minimum extreme function
% maxx: maximum extreme function
% outlier_index: indexes of outlier functions
% fmedian: median function
% plt: surface plot
% ph:
% median_x: median warping function
% Q1: First quartile
% Q3: Second quartile
% Q1a: Lower phase TB based on alpha
% Q3a: Upper phase TB based on alpha
% minn: minimum extreme function
% maxx: maximum extreme function
% outlier_index: indexes of outlier functions
% plt: surface plot
[M, ~] = size(f);
if nargin < 7
option.parallel = 1;
option.closepool = 0;
option.smooth = 0;
option.sparam = 25;
option.showplot = 1;
option.method = 'DP1';
option.w = 0.0;
option.MaxItr = 20;
end
%% Align Data
out_med = fdawarp(f,time);
out_med = out_med.time_warping_median(0,option);
%% Calculate CI
if isempty(gcp('nocreate'))
% prompt user for number threads to use
nThreads = input('Enter number of threads to use: ');
if nThreads > 1
parpool(nThreads);
elseif nThreads > 12 % check if the maximum allowable number of threads is exceeded
while (nThreads > 12) % wait until user figures it out
fprintf('Maximum number of threads allowed is 12\n Enter a number between 1 and 12\n');
nThreads = input('Enter number of threads to use: ');
end
if nThreads > 1
parpool(nThreads);
end
end
end
fprintf('Bootstrap Sampling. \n');
obj = ProgressBar(B, ...
'IsParallel', true, ...
'WorkerDirectory', pwd, ...
'Title', 'Progress' ...
);
obj.setup([], [], []);
bootlwr_amp = zeros(M,B);
bootupr_amp = zeros(M,B);
bootlwr_ph = zeros(M,B);
bootupr_ph = zeros(M,B);
parfor (k = 1:B)
samples = joint_gauss_model(out_med, 100, no);
obja = ampbox(samples);
amp_b = obja.construct_boxplot(1-p,.3);
objp = phbox(samples);
ph_b = objp.construct_boxplot(1-p,.3);
bootlwr_amp(:,k) = amp_b.Q1a;
bootupr_amp(:,k) = amp_b.Q3a;
bootlwr_ph(:,k) = ph_b.Q1a;
bootupr_ph(:,k) = ph_b.Q3a;
updateParallel([], pwd);
end
obj.release();
delete(gcp('nocreate'));
%% Tolerance Bounds
boot_amp = [bootlwr_amp bootupr_amp];
boot_amp_q = f_to_srvf(boot_amp, time);
boot_ph = [bootlwr_ph bootupr_ph];
boot_out = out_med;
boot_out.fn = boot_amp;
boot_out.qn = boot_amp_q;
boot_out.gam = boot_ph;
obj1 = ampbox(boot_out);
amp = obj1.construct_boxplot(a,.3);
obj1 = phbox(boot_out);
ph = obj1.construct_boxplot(a,.3);