-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_validation.m
169 lines (148 loc) · 6.81 KB
/
test_validation.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
%%This file is Copyright (C) 2018 Megha Gaur.
function [best_params] = test_validation(params0,params1,params2,sort_valid1)
X = [ones(length(sort_valid1(:,1)),1) sort_valid1(:,1)];
%One piece Regression, that is 0 breakpoint
y_pred = X*params0;
rsq0 = 1-(sum((sort_valid1(:,2)-y_pred).^2)/sum((sort_valid1(:,2) - mean(sort_valid1(:,2))).^2));
partition_data1 = [sort_valid1(:,1) sort_valid1(:,2) y_pred];
%plot_data(partition_data1,[0 0 0],[0 0 0])
close(gcf);
clearvars partition_data1 y_pred
%2-pc regression, 1 breakpoint
for i = 1:1:size(params1,1)
rows_part1 = sort_valid1(:,1) <=params1(i,1);
rows_part2 = sort_valid1(:,1) > params1(i,1);
data_part1 = sort_valid1(rows_part1,:);
data_part2 = sort_valid1(rows_part2,:);
y_actual = [data_part1(:,2) ; data_part2(:,2)];
x_part1 = [ones(length(data_part1(:,1)),1) data_part1(:,1)];
x_part2 = [ones(length(data_part2(:,1)),1) data_part2(:,1)];
if isempty(data_part1) && isempty(data_part2)
y_pred_part1 = 0;
y_pred_part2 = 0;
y_pred = 0;
y_denom = 0;
elseif isempty(data_part1)
y_pred_part1 = 0;
y_pred_part2 = x_part2 * params1(i,4:5)';
y_pred = y_pred_part2;
y_denom = data_part2(:,2)-mean(data_part2(:,2));
elseif isempty(data_part2)
y_pred_part1 = x_part1 * params1(i,2:3)';
y_pred_part2 = 0;
y_pred = y_pred_part1;
y_denom = data_part1(:,2)-mean(data_part1(:,2));
else
y_pred_part1 = x_part1 * params1(i,2:3)';
y_denom_part1 = data_part1(:,2)-mean(data_part1(:,2));
y_pred_part2 = x_part2 * params1(i,4:5)';
y_denom_part2 = data_part2(:,2)-mean(data_part2(:,2));
y_pred= [y_pred_part1 ; y_pred_part2];
y_denom = [y_denom_part1 ; y_denom_part2];
% partition_data1 = [data_part1(:,1) data_part1(:,2) y_pred_part1];
% partition_data2 = [data_part2(:,1) data_part2(:,2) y_pred_part2];
%plot_data(partition_data1,partition_data2,[0 0 0])
%close(gcf);
end
Rsq1_part1(i) = 1-(sum((data_part1(:,2)-y_pred_part1).^2)/sum((data_part1(:,2)-mean(y_pred_part1)).^2));
Rsq1_part2(i) = 1-(sum((data_part2(:,2)-y_pred_part2).^2)/sum((data_part2(:,2)-mean(y_pred_part2)).^2));
cd1(i) = 1-(sum((y_actual-y_pred).^2)./(sum(y_denom.^2)));
end
[rsq1_p1,idx_rsq1_p1] = max(Rsq1_part1);
[rsq1_p2,idx_rsq1_p2] = max(Rsq1_part2);
[maxcd1,idx_cd1] = max(cd1);
%plot_data(partition_data1,partition_data2,[0 0 0])
clearvars partition_data1 partition_data2 y_pred
%%3-pc regression, 2 breakpoints
for i = 1:1:size(params2,1)
rows_part1 = sort_valid1(:,1) <=params2(i,1);
rows_part2 = sort_valid1(:,1) > params2(i,1) & sort_valid1(:,1) <= params2(i,2);
rows_part3 = sort_valid1(:,1) > params2(i,2);
data_part1 = sort_valid1(rows_part1,:);
data_part2 = sort_valid1(rows_part2,:);
data_part3 = sort_valid1(rows_part3,:);
y_actual = [data_part1(:,2) ; data_part2(:,2); data_part3(:,2)];
x_part1 = [ones(length(data_part1(:,1)),1) data_part1(:,1)];
x_part2 = [ones(length(data_part2(:,1)),1) data_part2(:,1)];
x_part3 = [ones(length(data_part3(:,1)),1) data_part3(:,1)];
if isempty(data_part1) && isempty(data_part2) && isempty(data_part3)
y_pred_part1 = 0;
y_pred_part2 = 0;
y_pred_part3 = 0;
y_pred = 0;
y_denom = 0;
elseif isempty(data_part1) && isempty(data_part2)
y_pred_part1 = 0;
y_pred_part2 = 0;
y_pred_part3 = x_part3 * params2(i,7:8)';
y_denom = data_part3(:,2) - mean(data_part3(:,2));
y_pred = y_pred_part3;
elseif isempty(data_part1) && isempty(data_part3)
y_pred_part1 = 0;
y_pred_part2 = x_part2 * params2(i,5:6)';
y_pred_part3 = 0;
y_pred = y_pred_part2;
y_denom = data_part2(:,2) - mean(data_part2(:,2));
elseif isempty(data_part2) && isempty(data_part3)
y_pred_part1 = x_part1 * params2(i,3:4)';
y_pred_part2 = 0;
y_pred_part3 = 0;
y_pred = y_pred_part1;
y_denom = data_part1(:,2) - mean(data_part1(:,2));
elseif isempty(data_part1)
y_pred_part1 = 0;
y_pred_part2 = x_part2 * params2(i,5:6)';
y_pred_part3 = x_part3 * params2(i,7:8)';
y_pred = [y_pred_part2;y_pred_part3];
y_denom_part2 = data_part2(:,2) - mean(data_part2(:,2));
y_denom_part3 = data_part3(:,2) - mean(data_part3(:,2));
y_denom = [y_denom_part2 ; y_denom_part3];
elseif isempty(data_part2)
y_pred_part1 = x_part1 * params2(i,3:4)';
y_pred_part2 = 0;
y_pred_part3 = x_part3 * params2(i,7:8)';
y_pred = [y_pred_part1;y_pred_part3];
y_denom_part1 = data_part1(:,2) - mean(data_part1(:,2));
y_denom_part3 = data_part3(:,2) - mean(data_part3(:,2));
y_denom = [y_denom_part1 ; y_denom_part3];
elseif isempty(data_part1)
y_pred_part1 = 0;
y_pred_part2 = x_part2 * params2(i,5:6)';
y_pred_part3 = x_part3 * params2(i,7:8)';
y_pred = [y_pred_part2 ; y_pred_part3];
y_denom_part2 = data_part2(:,2) - mean(data_part2(:,2));
y_denom_part3 = data_part3(:,2) - mean(data_part3(:,2));
y_denom = [y_denom_part2 ; y_denom_part3];
else
y_pred_part1 = x_part1 * params2(i,3:4)';
y_denom_part1 = data_part1(:,2) - mean(data_part1(:,2));
y_pred_part2 = x_part2 * params2(i,5:6)';
y_denom_part2 = data_part2(:,2) - mean(data_part2(:,2));
y_pred_part3 = x_part3 * params2(i,7:8)';
y_denom_part3 = data_part3(:,2) - mean(data_part3(:,2));
y_pred = [y_pred_part1 ; y_pred_part2 ; y_pred_part3];
y_denom = [y_denom_part1 ; y_denom_part2 ; y_denom_part3];
% partition_data1 = [data_part1(:,1) data_part1(:,2) y_pred_part1];
% partition_data2 = [data_part2(:,1) data_part2(:,2) y_pred_part2];
% partition_data3 = [data_part3(:,1) data_part3(:,2) y_pred_part3];
% plot_data(partition_data1,partition_data2,partition_data3)
end
Rsq2_part1(i) = 1-(sum((data_part1(:,2)-y_pred_part1).^2)/sum((data_part1(:,2)-mean(y_pred_part1)).^2));
Rsq2_part2(i) = 1-(sum((data_part2(:,2)-y_pred_part2).^2)/sum((data_part2(:,2)-mean(y_pred_part2)).^2));
Rsq2_part3(i) = 1-(sum((data_part3(:,2)-y_pred_part3).^2)/sum((data_part3(:,2)-mean(y_pred_part3)).^2));
cd2(i) = 1 - (sum((y_actual-y_pred).^2)./(sum(y_denom.^2)));
end
[rsq2_p1,idx_rsq2_p1] = max(Rsq2_part1);
[rsq2_p2,idx_rsq2_p2] = max(Rsq2_part2);
[rsq2_p3,idx_rsq2_p3] = max(Rsq2_part3);
[maxcd2,idx_cd2] = max(cd2);
[maxRsq,num_bp]=max([rsq0 maxcd1 maxcd2]); %Find the number of breakpoints as per the value of Rsq or Coefficient of determination
if num_bp == 3
best_params = params2(idx_cd2,:);
elseif num_bp == 2
best_params = params1(idx_cd1,:);
else
best_params = params0;
disp('No breakpoint')
end
end