Skip to content

Commit

Permalink
test: add efficency test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nimrais committed Sep 25, 2024
1 parent 956ec86 commit 654e994
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,54 @@ end
end
end
end

@testitem "MvNormalMeanScalePrecision: Fisher is faster then for full parametrization" begin
include("./normal_family_setuptests.jl")
using BenchmarkTools
using LinearAlgebra
using JET

rng = StableRNG(42)
for k in 20:40 begin
μ = randn(rng, k)
γ = rand(rng)
cov = γ * I(k)

ef_small = convert(ExponentialFamilyDistribution, MvNormalMeanScalePrecision(μ, γ))
ef_full = convert(ExponentialFamilyDistribution, MvNormalMeanCovariance(μ, cov))

fi_small = fisherinformation(ef_small)
fi_full = fisherinformation(ef_full)

@test_opt fisherinformation(ef_small)
@test_opt fisherinformation(ef_full)

fi_mvsp_time = @elapsed fisherinformation(ef_small)
fi_mvsp_alloc = @allocated fisherinformation(ef_small)

fi_full_time = @elapsed fisherinformation(ef_full)
fi_full_alloc = @allocated fisherinformation(ef_full)

@test_opt cholinv(fi_small)
@test_opt cholinv(fi_full)

cholinv_time_small = @elapsed cholinv(fi_small)
cholinv_alloc_small = @allocated fisherinformation(ef_small)

cholinv_time_full = @elapsed cholinv(fi_full)
cholinv_alloc_full = @allocated cholinv(fi_full)

fi_small = fisherinformation(ef_small)
fi_full = fisherinformation(ef_full)

# small time is supposed to be O(k) and full time is supposed to O(k^2)
# the constant C is selected to account to fluctuations in test runs
C = 0.9
@testset "MvNormalMeanScalePrecision: Fisher is faster then for full parametrization k=$k" begin
@test fi_mvsp_time < fi_full_time/(C*k)
@test fi_mvsp_alloc < fi_full_alloc/(C*k)
@test cholinv_time_small < cholinv_time_full/(C*k)
@test cholinv_alloc_small < cholinv_alloc_full/(C*k)
end
end
end

0 comments on commit 654e994

Please sign in to comment.