11using Distributions
2- using HiddenMarkovModels: LightCategorical, LightDiagNormal, logdensityof, rand_prob_vec
2+ using HiddenMarkovModels:
3+ LightCategorical, LightDiagNormal, logdensityof, rand_prob_vec, rand_trans_mat
34using LinearAlgebra
45using Statistics
56using StatsAPI: fit!
@@ -8,6 +9,29 @@ using Test
89
910rng = StableRNG (63 )
1011
12+ function test_randprobvec (p)
13+ @test all (>= (0 ), p)
14+ @test sum (p) ≈ 1
15+ end
16+
17+ function test_randtransmat (A)
18+ foreach (eachrow (A)) do p
19+ test_randprobvec (p)
20+ end
21+ end
22+
23+ @testset " Rand prob" begin
24+ n = 10
25+ test_randprobvec (rand_prob_vec (n))
26+ test_randprobvec (rand_prob_vec (rng, n))
27+ test_randprobvec (rand_prob_vec (Float32, n))
28+ test_randprobvec (rand_prob_vec (rng, Float32, n))
29+ test_randtransmat (rand_trans_mat (n))
30+ test_randtransmat (rand_trans_mat (rng, n))
31+ test_randtransmat (rand_trans_mat (Float32, n))
32+ test_randtransmat (rand_trans_mat (rng, Float32, n))
33+ end
34+
1135function test_fit_allocs (dist, x, w)
1236 dist_copy = deepcopy (dist)
1337 allocs = @allocated fit! (dist_copy, x, w)
1741@testset " LightCategorical" begin
1842 p = rand_prob_vec (rng, 10 )
1943 dist = LightCategorical (p)
44+ @test startswith (string (dist), " LightCategorical" )
2045 x = [(@inferred rand (rng, dist)) for _ in 1 : 100_000 ]
2146 # Simulation
2247 val_count = zeros (Int, length (p))
3863 μ = randn (rng, 10 )
3964 σ = rand (rng, 10 )
4065 dist = LightDiagNormal (μ, σ)
66+ @test startswith (string (dist), " LightDiagNormal" )
4167 x = [(@inferred rand (rng, dist)) for _ in 1 : 100_000 ]
4268 # Simulation
4369 @test mean (x) ≈ μ atol = 2e-2
0 commit comments