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
/
KFE_discretized_univariate_test.m
147 lines (122 loc) · 6.61 KB
/
KFE_discretized_univariate_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
%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 = KFE_discretized_univariate_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.lower_test_tol = 1e-6; %Too high precision for some tests with big matrices
testCase.TestData.tolerances.default_csv_precision = '%.10f'; %Should be higher precision than tolerances.test_tol
end
function [A, x] = baseline_negative_drift_discretization(I, testCase) %Used by other test cases as a baseline.
mu_x = @(x) -0.01 * x;
sigma_bar = 0.1;
sigma_2_x = @(x) (sigma_bar*x).^2;
x_min = 1;
x_max = 2;
x = linspace(x_min, x_max, I)';
A = discretize_univariate_diffusion(x, mu_x(x), sigma_2_x(x));
end
function small_LLS_vs_eigenvalue_test(testCase) %Simple baseline check.
tolerances = testCase.TestData.tolerances;
I = 20; %Small matrix.
[A, x] = baseline_negative_drift_discretization(I, testCase);
f = stationary_distribution_discretized_univariate(A, x);
% dlmwrite(strcat(mfilename, '_1_f_output.csv'), f, 'precision', tolerances.default_csv_precision); %Uncomment to save again
f_check = dlmread(strcat(mfilename, '_1_f_output.csv'));
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
settings.method = 'LLS'; %Now using the LLS method
f_lls = stationary_distribution_discretized_univariate(A,x, settings);
verifyTrue(testCase,norm(f_lls - f_check, Inf) < tolerances.lower_test_tol, 'f value no longer matches');
%With a perfect initial guess!
settings.initial_guess = f_check;
f_lls = stationary_distribution_discretized_univariate(A,x, settings);
verifyTrue(testCase,norm(f_lls - f_check, Inf) < tolerances.lower_test_tol, 'f value no longer matches');
end
function medium_LLS_vs_eigenvalue_test(testCase) %Larger system using default options, etc.
tolerances = testCase.TestData.tolerances;
I = 1000; %Larger grid
settings.print_level = 1;
settings.method = 'eigenproblem'; %Will try to only find the appropriate eigenvector, which can be faster.
settings.num_basis_vectors = 100; %In general, need to tweak this to use the default eigenvector approach for large I
[A, x] = baseline_negative_drift_discretization(I, testCase);
tic;
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
%dlmwrite(strcat(mfilename, '_2_f_output.csv'), f, 'precision', tolerances.default_csv_precision); %Uncomment to save again
f_check = dlmread(strcat(mfilename, '_2_f_output.csv'));
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
settings.method = 'LLS'; %Now using the LLS method with the default pre-conditioner
tic;
f_lls = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f_lls - f_check, Inf) < tolerances.lower_test_tol, 'f value no longer matches');
settings.method = 'eigenproblem_all'; %Now using the eigenvalues, but calculating all
tic;
f_eigen_all = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f_eigen_all - f_check, Inf) < tolerances.lower_test_tol, 'f value no longer matches');
end
function medium_preconditioner_test(testCase) %tests of the various preconditioners. Some not worth much.
tolerances = testCase.TestData.tolerances;
I = 1000; %Larger grid
settings.print_level = 1;
[A, x] = baseline_negative_drift_discretization(I, testCase);
%Use the eigenproblem approach as a comparison
disp('Eigenvalue based solution');
tic;
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
%dlmwrite(strcat(mfilename, '_3_f_output.csv'), f, 'precision', tolerances.default_csv_precision); %Uncomment to save again
f_check = dlmread(strcat(mfilename, '_3_f_output.csv'));
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
%Setup basic LLS
settings.method = 'LLS'; %Now using the LLS method with the default pre-conditioner
settings.max_iterations = 50000; %Only really need this many to test the no preconditioner version.
settings.tolerance = 1E-8;
settings.print_level = 1;
%Use LLS with no preconditioners
tic;
disp('LLS no preconditioner');
settings.preconditioner = 'none';
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
tic;
disp('LLS with incomplete LU preconditioner and perfect initial guess');
settings.preconditioner = 'incomplete_LU';
settings.initial_guess = f_check;
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
tic;
disp('LLS incomplete LU preconditioner');
settings.preconditioner = 'incomplete_LU';
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
tic;
disp('LLS jacobi preconditioner');
settings.preconditioner = 'jacobi';
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
tic;
disp('LLS incomplete_cholesky preconditioner');
settings.preconditioner = 'incomplete_cholesky';
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
tic;
disp('LLS with incomplete LU preconditioner and mediocre initial guess');
settings.preconditioner = 'incomplete_LU';
settings.initial_guess = linspace(.005,0,I)';
f = stationary_distribution_discretized_univariate(A, x, settings);
toc;
verifyTrue(testCase,norm(f - f_check, Inf) < tolerances.test_tol, 'f value no longer matches');
end