aboutsummaryrefslogtreecommitdiff
path: root/tests/aux/test_kernel_pre.m
diff options
context:
space:
mode:
Diffstat (limited to 'tests/aux/test_kernel_pre.m')
-rw-r--r--tests/aux/test_kernel_pre.m46
1 files changed, 22 insertions, 24 deletions
diff --git a/tests/aux/test_kernel_pre.m b/tests/aux/test_kernel_pre.m
index a1c8337..69f6f1e 100644
--- a/tests/aux/test_kernel_pre.m
+++ b/tests/aux/test_kernel_pre.m
@@ -1,32 +1,32 @@
function test_kernel_pre()
-
+
kerneltype = 'rbf';
rand('state', 123456);
n = 10;
m = 5;
cutoff = 5e-3;
-
+
X = rand(n, m);
Z = [ones(n, 1), X];
-
+
set_matrix(Z, "data->Z", "data->m+1");
-
+
K = zeros(n, n);
if strcmp(kerneltype, 'poly')
- # Polynomial kernel
- # (gamma * <x_1, x_2> + c)^d
+ % Polynomial kernel
+ % (gamma * <x_1, x_2> + c)^d
gamma = 1.5;
c = 3.0;
d = 1.78;
-
+
for ii=1:n
for jj=1:n
K(ii, jj) = (gamma * (X(ii, :) * X(jj, :)') + c)^d;
end
end
elseif strcmp(kerneltype, 'rbf')
- # RBF kernel
- # exp(-gamma * norm(x1 - x2)^2)
+ % RBF kernel
+ % exp(-gamma * norm(x1 - x2)^2)
gamma = 0.348
for ii=1:n
for jj=1:n
@@ -34,8 +34,8 @@ function test_kernel_pre()
end
end
elseif strcmp(kerneltype, 'sigmoid')
- # Sigmoid kernel
- # tanh(gamma * <x_1, x_2> + c)
+ % Sigmoid kernel
+ % tanh(gamma * <x_1, x_2> + c)
gamma = 1.23;
c = 1.6;
for ii=1:n
@@ -44,29 +44,27 @@ function test_kernel_pre()
end
end
end
-
- K(1, 2)
- [P, Sigma] = eig(K);
+ [P, values] = eig(K);
- eigenvalues = diag(Sigma);
+ eigenvalues = diag(values);
ratios = eigenvalues ./ eigenvalues(end, end);
-
+
realP = fliplr(P(:, ratios > cutoff));
- realSigma = flipud(eigenvalues(ratios > cutoff));
-
+ realSigma = sqrt(flipud(eigenvalues(ratios > cutoff)));
+
assert_matrix(realSigma, "data->Sigma", "1");
-
+
r = sum(ratios > cutoff);
fprintf("mu_assert(data->r == %i);\n", r);
-
+
M = realP * diag(realSigma);
-
+
newZ = [ones(n, 1) M];
assert_matrix_abs(newZ, "data->Z", "data->r+1");
-
+
assert_matrix(Z, "data->RAW", "data->m+1");
-
+
end
function set_matrix(A, name, cols)
@@ -98,4 +96,4 @@ function assert_matrix_abs(A, name, cols)
end
end
fprintf("\n");
-end \ No newline at end of file
+end