Skip to content

Commit

Permalink
Added test for qda discriminants
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Thatcher committed Mar 6, 2016
1 parent 8c4a9b6 commit 2b9ec1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/qda.jl
Expand Up @@ -75,11 +75,12 @@ function discriminants_qda{T<:BlasReal}(
δ = Array(T, n, k) # discriminant function values
H = Array(T, n, p) # temporary array to prevent re-allocation k times
Q = Array(T, n, p) # Q := H*W_k
hᵀh = Array(T, n)
for j = 1:k
translate!(copy!(H, Z), -vec(M[j,:]))
s = dot_rows(BLAS.gemm!('N', 'N', one(T), H, W_k[j], zero(T), Q))
dotrows!(BLAS.gemm!('N', 'N', one(T), H, W_k[j], zero(T), Q), hᵀh)
for i = 1:n
δ[i, j] = -s[i]/2 + log(priors[j])
δ[i, j] = -hᵀh[i]/2 + log(priors[j])
end
end
δ
Expand Down
18 changes: 18 additions & 0 deletions test/test_qda.jl
Expand Up @@ -118,3 +118,21 @@ for T in FloatingPointTypes
@test_approx_eq model.W_k[i] W_k_tmp[i]
end
end

info("Testing ", MOD.discriminants_qda)
for T in FloatingPointTypes
X_tmp = copy(convert(Matrix{T}, X))
priors_tmp = convert(Vector{T}, priors)

model = qda(X_tmp, y)
Z2 = hcat([MOD.dotrows((X_tmp .- M[i,:])*model.W_k[i]) for i in eachindex(priors_tmp)]...)
δ = -Z2/2 .+ log(priors_tmp)'
@test_approx_eq δ MOD.discriminants(model, X_tmp)

for γ in (zero(T), convert(T, 0.25), convert(T, 0.75), one(T))
model = qda(X_tmp, y)
Z2 = hcat([MOD.dotrows((X_tmp .- M[i,:])*model.W_k[i]) for i in eachindex(priors_tmp)]...)
δ = -Z2/2 .+ log(priors_tmp)'
@test_approx_eq δ MOD.discriminants(model, X_tmp)
end
end

0 comments on commit 2b9ec1f

Please sign in to comment.