Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

Commit

Permalink
Fixed centering bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Thatcher committed Jul 29, 2016
1 parent 236c6b3 commit 7c19a04
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/kernelutilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
function kernelstatistics{T<:AbstractFloat}(K::Matrix{T})
(n = size(K,1)) == size(K,2) || throw(DimensionMismatch("Kernel matrix must be square."))
μ_κ = vec(sum(K,1))
μ_k = sum(κ)/(n^2)
broadcast!(/, κ, κ, n)
μ_k = sum(μ_κ)/(n^2)
broadcast!(/, μ_κ, μ_κ, n)
return (μ_κ, μ_k)
end

Expand All @@ -22,7 +22,7 @@ function center_symmetric!{T<:AbstractFloat}(KC::KernelCenterer{T}, K::Matrix{T}
μ_κ = KC.mu_kappa
length(μ_κ) == n || throw(DimensionMismatch("Kernel statistics do not match matrix."))
for j = 1:n, i = 1:n
@inbounds K[i,j] += μ_k - 2*μ_κ[i]
@inbounds K[i,j] += μ_k - μ_κ[i] - μ_κ[j]
end
return K
end
Expand Down
35 changes: 34 additions & 1 deletion test/kernelutilities.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
n = 30
n = 3
m = 20
p = 5

Expand All @@ -16,3 +16,36 @@ for T in FloatingPointTypes
N = (C'W)*C
@test_approx_eq N K
end

info("Testing ", MOD.kernelstatistics.env.name)
for T in FloatingPointTypes
X = rand(T, n, n)
x, s = MOD.kernelstatistics(X)

@test_approx_eq x vec(mean(X,1))
@test_approx_eq s mean(X)
end

info("Testing ", MOD.KernelCenterer.name.name)
for T in FloatingPointTypes
K = rand(T, n, n)

k, s = MOD.kernelstatistics(K)
KC = MOD.KernelCenterer(K)

@test_approx_eq k KC.mu_kappa
@test_approx_eq s KC.mu_k
end

info("Testing ", MOD.center_symmetric!.env.name)
for T in FloatingPointTypes
X = rand(T, n, p)
K = X*transpose(X)
KC = MOD.KernelCenterer(K)
MOD.center_symmetric!(KC, K)

Xc = X .- mean(X,1)
Kc = Xc*transpose(Xc)

@test_approx_eq K Kc
end

0 comments on commit 7c19a04

Please sign in to comment.