Skip to content

Instantly share code, notes, and snippets.

@gavincangan
Created March 5, 2019 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavincangan/927ba96493b97c45a17a2296604d313a to your computer and use it in GitHub Desktop.
Save gavincangan/927ba96493b97c45a17a2296604d313a to your computer and use it in GitHub Desktop.
Check whether a kernel results in a positive semi-definite covariance matrix using rejection sampling
% https://stats.stackexchange.com/a/394487/204863
clear; close all;
num_points = 100;
xrange = [-100, 100];
x = xrange(1) + 2 * xrange(2) * rand(num_points, 1);
yrange = [0, 20];
y = yrange(1) + 2 * yrange(2) * rand(num_points, 1);
lambda = 1e3;
cov = zeros(num_points);
for i = 1:num_points
for j = 1:num_points
% term1 = (x(i)-x(j))^2;
% term2 = 1 + y(i) * y(j);
cov(i, j) = ((2*y(i))/(y(i).^2+1))^(3/2) .* exp(-1 * ( (x(i)^2 + (y(i))^2) ) ./ ( y(i)^2 + 1 ) );
end
end
eval = eig(cov);
min(eval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment