This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
discretize_nonuniform_univariate_diffusion_test.m
208 lines (165 loc) · 9.7 KB
/
discretize_nonuniform_univariate_diffusion_test.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
%Using the unit testing framework in matlab. See https://www.mathworks.com/help/matlab/matlab_prog/write-simple-test-case-with-functions.html
%To run tests:
% runtests %would run all of them in the current directory
% runtests('my_test') %runs just the my_test.m file
% runtests('my_test/my_function_test') %runs only `my_function_test function in `my_test'.
function tests = discretize_nonuniform_univariate_diffusion_test
tests = functiontests(localfunctions);
end
%This is run at the beginning of the test. Not required.
function setupOnce(testCase)
addpath('../lib/');
testCase.TestData.tolerances.test_tol = 1e-9;
testCase.TestData.tolerances.default_csv_precision = '%.10f'; %Should be higher precision than tolerances.test_tol
end
function zero_drift_test(testCase)%Simple and small with zero drift with uniform grid
tolerances = testCase.TestData.tolerances;
mu_x = @(x) zeros(numel(x),1);
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 0.01;
x_max = 1;
I = 5;
x = logspace(log10(x_min),log10(x_max),I)';
[A, Delta_p, Delta_m] = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
%dlmwrite(strcat(mfilename, '_1_A_output.csv'), full(A), 'precision', tolerances.default_csv_precision); %Uncomment to save again
A_check = dlmread(strcat(mfilename, '_1_A_output.csv'));
verifyTrue(testCase,norm(A - A_check, Inf) < tolerances.test_tol, 'A value no longer matches');
%The following are worth testing for almost every matrix in the test suit.
verifyTrue(testCase,is_stochastic_matrix(testCase, A), 'Intensity matrix rows do not sum to 0');
verifyTrue(testCase,is_negative_diagonal(testCase, A), 'Intensity Matrix diagonal has positive elements');
verifyTrue(testCase,isbanded(A,1,1), 'Intensity Matrix is not tridiagonal');
verifyTrue(testCase,is_negative_definite(testCase, A), 'Intensity Matrix is not positive definite');
end
function zero_drift_for_large_sample_test(testCase)
tolerances = testCase.TestData.tolerances;
mu_x = @(x) zeros(numel(x),1);
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 0.01;
x_max = 1;
I = 1000; % It gets very slow when sample size is getting bigger.
x = logspace(log10(x_min),log10(x_max),I)';
[A, Delta_p, Delta_m] = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
verifyTrue(testCase, (nnz(A) == 2998), 'Number of non-zero values is wrong')
%The following are worth testing for almost every matrix in the test suit.
verifyTrue(testCase,is_stochastic_matrix(testCase, A), 'Intensity matrix rows do not sum to 0');
verifyTrue(testCase,is_negative_diagonal(testCase, A), 'Intensity Matrix diagonal has positive elements');
verifyTrue(testCase,isbanded(A,1,1), 'Intensity Matrix is not tridiagonal');
verifyTrue(testCase,is_negative_definite(testCase, A), 'Intensity Matrix is not positive definite');
end
function negative_drift_uniform_grid_test(testCase)
tolerances = testCase.TestData.tolerances;
mu_x = @(x) ones(numel(x),1) * (-1);
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 0.01;
x_max = 1;
I = 1001;
x = logspace(log10(x_min),log10(x_max),I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
%To save the file again, can uncomment this.
%[indices_i, indices_j, values_ij] = find(A); %Uncomment to save again
%dlmwrite(strcat(mfilename, '_4_A_output.csv'), [indices_i indices_j values_ij], 'precision', tolerances.default_csv_precision); %Uncomment to save again
%Load and check against the sparse matrix file.
A_check = spconvert(dlmread(strcat(mfilename, '_4_A_output.csv')));
verifyTrue(testCase,norm(A - A_check, Inf) < tolerances.test_tol, 'A value no longer matches');
%The following are worth testing for almost every matrix in the test suit.
verifyTrue(testCase,is_stochastic_matrix(testCase, A), 'Intensity matrix rows do not sum to 0');
verifyTrue(testCase,is_negative_diagonal(testCase, A), 'Intensity Matrix diagonal has positive elements');
verifyTrue(testCase,isbanded(A,1,1), 'Intensity Matrix is not tridiagonal');
end
function positive_drift_uniform_grid_test(testCase)
tolerances = testCase.TestData.tolerances;
mu_x = @(x) ones(numel(x),1);
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 0.01;
x_max = 1;
I = 1001;
x = logspace(log10(x_min),log10(x_max),I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
%To save the file again, can uncomment this.
%[indices_i, indices_j, values_ij] = find(A); %Uncomment to save again
%dlmwrite(strcat(mfilename, '_5_A_output.csv'), [indices_i indices_j values_ij], 'precision', tolerances.default_csv_precision); %Uncomment to save again
%Load and check against the sparse matrix file.
A_check = spconvert(dlmread(strcat(mfilename, '_5_A_output.csv')));
verifyTrue(testCase,norm(A - A_check, Inf) < tolerances.test_tol, 'A value no longer matches');
%The following are worth testing for almost every matrix in the test suit.
verifyTrue(testCase,is_stochastic_matrix(testCase, A), 'Intensity matrix rows do not sum to 0');
verifyTrue(testCase,is_negative_diagonal(testCase, A), 'Intensity Matrix diagonal has positive elements');
verifyTrue(testCase,isbanded(A,1,1), 'Intensity Matrix is not tridiagonal');
end
function monotonically_increasing_mu_test(testCase) % mu is monotonically increasing in x with mu(x_min)<0 and mu(x_max)>0
tolerances = testCase.TestData.tolerances;
mu_x = @(x) (x - 0.5);
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 0.01;
x_max = 1;
I = 5;
x = logspace(log10(x_min),log10(x_max),I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
%To save the file again, can uncomment this.
%[indices_i, indices_j, values_ij] = find(A); %Uncomment to save again
%dlmwrite(strcat(mfilename, '_6_A_output.csv'), [indices_i indices_j values_ij], 'precision', tolerances.default_csv_precision); %Uncomment to save again
%Load and check against the sparse matrix file.
A_check = spconvert(dlmread(strcat(mfilename, '_6_A_output.csv')));
verifyTrue(testCase,norm(A - A_check, Inf) < tolerances.test_tol, 'A value no longer matches');
%The following are worth testing for almost every matrix in the test suit.
verifyTrue(testCase,is_stochastic_matrix(testCase, A), 'Intensity matrix rows do not sum to 0');
verifyTrue(testCase,is_negative_diagonal(testCase, A), 'Intensity Matrix diagonal has positive elements');
verifyTrue(testCase,isbanded(A,1,1), 'Intensity Matrix is not tridiagonal');
end
function monotonically_decreasing_mu_test(testCase) % mu is monotonically increasing in x with mu(x_min)<0 and mu(x_max)>0
tolerances = testCase.TestData.tolerances;
mu_x = @(x) (x - 0.5) * (-1);
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 0.01;
x_max = 1;
I = 5;
x = logspace(log10(x_min),log10(x_max),I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
%To save the file again, can uncomment this.
%[indices_i, indices_j, values_ij] = find(A); %Uncomment to save again
%dlmwrite(strcat(mfilename, '_7_A_output.csv'), [indices_i indices_j values_ij], 'precision', tolerances.default_csv_precision); %Uncomment to save again
%Load and check against the sparse matrix file.
A_check = spconvert(dlmread(strcat(mfilename, '_7_A_output.csv')));
verifyTrue(testCase,norm(A - A_check, Inf) < tolerances.test_tol, 'A value no longer matches');
%The following are worth testing for almost every matrix in the test suit.
verifyTrue(testCase,is_stochastic_matrix(testCase, A), 'Intensity matrix rows do not sum to 0');
verifyTrue(testCase,is_negative_diagonal(testCase, A), 'Intensity Matrix diagonal has positive elements');
verifyTrue(testCase,isbanded(A,1,1), 'Intensity Matrix is not tridiagonal');
end
function concave_mu_test(testCase) % mu is concave in x with mu(x_min)<0, mu(x_max)<0 and mu(x)>0 for some x
tolerances = testCase.TestData.tolerances;
mu_x = @(x) (-(x - 0.5).^2 + 0.1);
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 0.01;
x_max = 1;
I = 5;
x = logspace(log10(x_min),log10(x_max),I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
%To save the file again, can uncomment this.
%[indices_i, indices_j, values_ij] = find(A); %Uncomment to save again
%dlmwrite(strcat(mfilename, '_8_A_output.csv'), [indices_i indices_j values_ij], 'precision', tolerances.default_csv_precision); %Uncomment to save again
%Load and check against the sparse matrix file.
A_check = spconvert(dlmread(strcat(mfilename, '_8_A_output.csv')));
verifyTrue(testCase,norm(A - A_check, Inf) < tolerances.test_tol, 'A value no longer matches');
%The following are worth testing for almost every matrix in the test suit.
verifyTrue(testCase,is_stochastic_matrix(testCase, A), 'Intensity matrix rows do not sum to 0');
verifyTrue(testCase,is_negative_diagonal(testCase, A), 'Intensity Matrix diagonal has positive elements');
verifyTrue(testCase,isbanded(A,1,1), 'Intensity Matrix is not tridiagonal');
end
%These are utility functions for testing returned matrices.
function result = is_stochastic_matrix(testCase, A)
result = (max(abs(full(sum(A,2)))) < testCase.TestData.tolerances.test_tol);
end
function result = is_negative_diagonal(testCase, A)
result = (max(full(diag(A))) < 0);
end
function result = is_negative_definite(testCase, A)
result =all(eig(full(A)) < testCase.TestData.tolerances.test_tol);
end