Last active
August 29, 2015 14:14
-
-
Save vrld/5ddd10fe0a5f6937a835 to your computer and use it in GitHub Desktop.
Cholesky Decomposition and simple (!) gaussian process regression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chol_(A::Matrix, L::Matrix) | |
for i = 1:size(A,1) | |
s = 0 | |
@simd for j = 1:i-1 | |
@inbounds L[i,j] = (A[i,j] - sum([L[i,k]*L[j,k] for k = 1:j-1])) / L[j,j] | |
@inbounds s += L[i,j]^2 | |
end | |
L[i,i] = sqrt(A[i,i] - s) | |
end | |
L | |
end | |
cholesky(A) = chol_(A, zeros(A)) | |
function cholesky!(A) | |
chol_(A, A) | |
for i = 1:size(A,1) | |
@simd for j = 1:i-1 @inbounds A[j,i] = 0 end | |
end | |
A | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function learn_GP(X::Matrix, y::Vector, K::Matrix, noise::Real) | |
L = cholesky!(K + noise * eye(size(K,1))) | |
alpha = L' \ (L \ y) | |
alpha, (x::Vector -> [kernel(X[i,:], x) for i in size(X,1)]) | |
end | |
function learn_GP(X::Matrix, y::Vector, kernel::Function, noise::Real) | |
K = zeros(size(X,1), size(X,1)) | |
for i = 1:size(X,1), j = 1:i | |
@inbounds K[i,j] = kernel(X[i,:], X[j,:]) | |
@inbounds K[j,i] = K[i,j] | |
end | |
learn_GP(X, y, K, noise) | |
end | |
function predict_GP(alpha::Vector, kernel::Function, x::Vector) | |
s = 0 | |
@simd for i = 1:size(alpha,1) | |
@inbounds s += alpha[i] * kernel(x) | |
end | |
s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment