Skip to content

Commit

Permalink
Merge pull request #37 from invenia/wct/cov
Browse files Browse the repository at this point in the history
Implement cov for ILMMs
  • Loading branch information
willtebbutt committed Sep 14, 2021
2 parents 04ab945 + df8a14d commit 67bb504
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LinearMixingModels"
uuid = "b8ce4b42-e81b-4a39-a84a-67f74a9a16dd"
authors = ["Invenia Technical Computing Corporation"]
version = "0.1.4"
version = "0.1.5"

[deps]
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
Expand Down
21 changes: 19 additions & 2 deletions src/ilmm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ function Distributions._rand!(
end
end

# See AbstractGPs.jl API docs.
function AbstractGPs.mean_and_var(fx::FiniteGP{<:ILMM})
function _intermediate_mean_and_var_and_cov_quantities(fx::FiniteGP{<:ILMM})
f, H, σ², x = unpack(fx)
p, m = size(H)
n = length(x)
Expand All @@ -116,19 +115,37 @@ function AbstractGPs.mean_and_var(fx::FiniteGP{<:ILMM})
latent_mean, latent_cov = mean_and_cov(f(x_mo_input))

H_full = kron(H, Matrix(I, n, n))
return H_full, latent_mean, latent_cov, σ²
end

# See AbstractGPs.jl API docs.
function AbstractGPs.mean_and_var(fx::FiniteGP{<:ILMM})
H_full, latent_mean, latent_cov, σ² = _intermediate_mean_and_var_and_cov_quantities(fx)

M = H_full * latent_mean
V = AbstractGPs.diag_Xt_A_X(cholesky(latent_cov), H_full') .+ σ²

return collect(vec(M)), V
end

# See AbstractGPs.jl API docs.
function AbstractGPs.mean_and_cov(fx::FiniteGP{<:ILMM})
H_full, latent_mean, latent_cov, σ² = _intermediate_mean_and_var_and_cov_quantities(fx)

M = H_full * latent_mean
C = AbstractGPs.Xt_A_X(cholesky(latent_cov), H_full') + σ² * I

return collect(vec(M)), C
end

# See AbstractGPs.jl API docs.
AbstractGPs.mean(fx::FiniteGP{<:ILMM}) = mean_and_var(fx)[1]

# See AbstractGPs.jl API docs.
AbstractGPs.var(fx::FiniteGP{<:ILMM}) = mean_and_var(fx)[2]

AbstractGPs.cov(fx::FiniteGP{<:ILMM}) = mean_and_cov(fx)[2]

# See AbstractGPs.jl API docs.
function AbstractGPs.logpdf(fx::FiniteGP{<:ILMM}, y::AbstractVector{<:Real})
f, H, σ², x = unpack(fx)
Expand Down
5 changes: 3 additions & 2 deletions test/ilmm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function test_ilmm(rng, kernels, H, x_train, x_test, y_train, y_test)

@test isapprox(mean(ilmmx), mean(n_ilmmx))
@test isapprox(var(ilmmx), var(n_ilmmx))
@test isapprox(cov(ilmmx), cov(n_ilmmx))
@test isapprox(logpdf(ilmmx, y_train), logpdf(n_ilmmx, y_train))
@test _is_approx(marginals(ilmmx), marginals(n_ilmmx))
@test length(rand(rng, ilmmx)) == size(H, 1) * length(x_train.x)
Expand All @@ -31,8 +32,8 @@ function test_ilmm(rng, kernels, H, x_train, x_test, y_train, y_test)
@test gradient(logpdf, pi, y_test) isa Tuple

@testset "primary_public_interface" begin
test_finitegp_primary_public_interface(rng, ilmmx)
test_finitegp_primary_public_interface(rng, pi)
test_finitegp_primary_and_secondary_public_interface(rng, ilmmx)
test_finitegp_primary_and_secondary_public_interface(rng, pi)
end
end

Expand Down
5 changes: 3 additions & 2 deletions test/oilmm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function test_oilmm(rng, kernels, H::Orthogonal, x_train, x_test, y_train, y_tes

@test isapprox(mean(ilmmx), mean(oilmmx))
@test isapprox(var(ilmmx), var(oilmmx))
@test isapprox(cov(ilmmx), cov(oilmmx))
@test isapprox(logpdf(ilmmx, y_train), logpdf(oilmmx, y_train))
@test _is_approx(marginals(ilmmx), marginals(oilmmx))
@test length(rand(rng, oilmmx)) == size(H, 1) * length(x_train.x)
Expand All @@ -31,8 +32,8 @@ function test_oilmm(rng, kernels, H::Orthogonal, x_train, x_test, y_train, y_tes
@test gradient(logpdf, po, y_test) isa Tuple

@testset "primary_public_interface" begin
test_finitegp_primary_public_interface(rng, oilmmx)
test_finitegp_primary_public_interface(rng, po)
test_finitegp_primary_and_secondary_public_interface(rng, oilmmx)
test_finitegp_primary_and_secondary_public_interface(rng, po)
end
end

Expand Down

2 comments on commit 67bb504

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/44826

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" 67bb504492e33b96f9262b97862a49be0fc27e30
git push origin v0.1.5

Please sign in to comment.