-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayground.m
More file actions
201 lines (158 loc) · 4.45 KB
/
playground.m
File metadata and controls
201 lines (158 loc) · 4.45 KB
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
%
% This is a playground for testing RBF and RKHS function expansion.
%
clc
clear
test_case_1d = 'cardinal';
num_node_1d = 21;
test_case_2d = 'const_one';
num_node_2d = 100;
% 1D interpolation
if strcmp(test_case_1d, 'noisy_sine')
xi = linspace(0, 2 * pi, num_node_1d);
yi = sin(xi)' + 0.2 * rand(num_node_1d, 1);
plot_y_range = [-1.5 1.5];
elseif strcmp(test_case_1d, 'cardinal')
xi = linspace(-1, 1, num_node_1d);
yi = zeros(num_node_1d, 1);
yi(mean(1:size(xi, 2))) = 1;
plot_y_range = [-0.5 1.5];
elseif strcmp(test_case_1d, 'const_one')
xi = linspace(-1, 1, num_node_1d);
yi = ones(num_node_1d, 1);
plot_y_range = [0.995 1.005];
end
fig = figure();
subplot(4, 2, 1)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'ga', 'eps', 3, 'poly_deg', -1)
subplot(4, 2, 2)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'ga', 'eps', 3, 'poly_deg', 2)
subplot(4, 2, 3)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'ga', 'eps', 3, 'poly_deg', 1)
subplot(4, 2, 4)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'ga', 'eps', 9, 'poly_deg', -1)
subplot(4, 2, 5)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'ga', 'eps', 11, 'poly_deg', -1)
subplot(4, 2, 6)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'ga', 'eps', 11, 'poly_deg', 0)
subplot(4, 2, 7)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'phs', 'pow', 3, 'poly_deg', -1)
subplot(4, 2, 8)
test_rbf_1d(xi, yi, plot_y_range, 'kernel', 'phs', 'pow', 3, 'poly_deg', 1)
% -------------------------------------------------------------------------
function res = dist(x1, x2)
res = norm(x2 - x1);
end
function res = kernel_ga(x, x0, varargin)
eps = cell2mat(varargin(4));
res = exp(-(dist(x, x0) * eps)^2);
end
function res = kernel_phs(x, x0, varargin)
pow = cell2mat(varargin(4));
res = (dist(x, x0))^pow;
end
function [indices, col] = create_polynomial(d, l, col, indices)
if ~exist('indices', 'var')
indices = zeros(l, nchoosek(l + d, l) - 1);
col = 1;
for i = 1 : l
indices(1:i,col) = 1;
col = col + 1;
[indices, col] = create_polynomial(d, i, col, indices);
end
else
while 1
j = col - 1;
done = 1;
for i = l : -1 : 1
if indices(i,j) ~= d
done = 0;
break
end
end
if done
return
end
indices(:,col) = indices(:,j);
indices(i:l,col) = indices(i,col) + 1;
col = col + 1;
end
end
end
function res = create_coef_matrix(x, kernel)
n = size(x, 2);
res = zeros(n, n); % Note: Coefficient matrix is full.
for i = 1 : n
for j = 1 : n
res(i,j) = kernel(x(i), x(j));
end
end
end
function P = create_polynomial_matrix(x, l)
[d, n] = size(x);
pi = create_polynomial(d, l);
P = ones(n, size(pi, 2) + 1);
for i = 1 : n
for j = 2 : size(P, 2)
for k = 1 : d
if pi(k,j-1) ~= 0
P(i,j) = P(i,j) * x(pi(k,j-1),i);
end
end
end
end
end
function res = interp(xi, kernel, w, xo)
n = size(xi, 2);
res = 0;
for i = 1 : n
res = res + w(i) * kernel(xi(i), xo);
end
end
function test_rbf_1d(xi, yi, plot_y_range, varargin)
kernel_name = cell2mat(varargin(2));
display(['==> Using kernel_', kernel_name])
if strcmp(kernel_name, 'ga')
kernel = @(x, x0) kernel_ga(x, x0, varargin{:});
elseif strcmp(kernel_name, 'phs')
kernel = @(x, x0) kernel_phs(x, x0, varargin{:});
end
[d, n] = size(xi);
A = create_coef_matrix(xi, kernel);
l = cell2mat(varargin(6));
xo = linspace(min(xi) - 0.2 * (max(xi) - min(xi)), max(xi) + 0.2 * (max(xi) - min(xi)), 201);
if l == -1
w = A \ yi;
yo = arrayfun(@(x) interp(xi, kernel, w, x), xo);
else
P = create_polynomial_matrix(xi, l);
A = [A P; P' zeros(size(P, 2))];
L = [yi; zeros(nchoosek(l + d, l), 1)];
W = A \ L;
w = W(1:n);
yo = arrayfun(@(x) interp(xi, kernel, w, x), xo);
P = create_polynomial_matrix(xo, l);
yo = yo + W(n+1:end)' * P';
end
plot(xo, yo, 'r-', 'linewidth', 3);
hold on;
plot(xi, yi, 'o', 'markerfacecolor', 'blue');
set(gca, 'linewidth', 4, 'fontsize', 16)
xlim([min(xo) max(xo)])
ylim(plot_y_range)
grid on
if strcmp(kernel_name, 'ga')
eps = cell2mat(varargin(4));
if l == -1
title(['Gaussian RBF with shape parameter ', num2str(eps)])
else
title(['Gaussian RBF with polynomials and shape parameter ', num2str(eps)])
end
elseif strcmp(kernel_name, 'phs')
if l == -1
title('PHS RBF without polynomials')
else
title('PHS RBF with polynomials')
end
end
end