Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow heterogeneous distributions #102

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HiddenMarkovModels"
uuid = "84ca31d5-effc-45e0-bfda-5a68cd981f47"
authors = ["Guillaume Dalle"]
version = "0.5.2"
version = "0.5.3"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
1 change: 0 additions & 1 deletion examples/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,3 @@ control_seq = fill(nothing, last(seq_ends)); #src
test_identical_hmmbase(rng, hmm, 100; hmm_guess) #src
test_coherent_algorithms(rng, hmm, control_seq; seq_ends, hmm_guess) #src
test_type_stability(rng, hmm, control_seq; seq_ends, hmm_guess) #src
test_identical_hmmbase(rng, transpose_hmm(hmm), 100; hmm_guess=transpose_hmm(hmm_guess)) #src
21 changes: 12 additions & 9 deletions ext/HiddenMarkovModelsDistributionsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@
fit

function HiddenMarkovModels.fit_in_sequence!(
dists::AbstractVector{D}, i::Integer, x_nums::AbstractVector, w::AbstractVector
) where {D<:UnivariateDistribution}
return dists[i] = fit(D, x_nums, w)
dists::AbstractVector{<:UnivariateDistribution},
i::Integer,
x_nums::AbstractVector,
w::AbstractVector,
)
return dists[i] = fit(typeof(dists[i]), x_nums, w)
end

function HiddenMarkovModels.fit_in_sequence!(
dists::AbstractVector{D},
dists::AbstractVector{<:MultivariateDistribution},
i::Integer,
x_vecs::AbstractVector{<:AbstractVector},
w::AbstractVector,
) where {D<:MultivariateDistribution}
return dists[i] = fit(D, reduce(hcat, x_vecs), w)
)
return dists[i] = fit(typeof(dists[i]), reduce(hcat, x_vecs), w)
end

function HiddenMarkovModels.fit_in_sequence!(
dists::AbstractVector{D},
dists::AbstractVector{<:MatrixDistribution},
i::Integer,
x_mats::AbstractVector{<:AbstractMatrix},
w::AbstractVector,
) where {D<:MatrixDistribution}
return dists[i] = fit(D, reduce(dcat, x_mats), w)
)
return dists[i] = fit(typeof(dists[i]), reduce(dcat, x_mats), w)

Check warning on line 36 in ext/HiddenMarkovModelsDistributionsExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/HiddenMarkovModelsDistributionsExt.jl#L36

Added line #L36 was not covered by tests
end

dcat(M1, M2) = cat(M1, M2; dims=3)
Expand Down
24 changes: 24 additions & 0 deletions test/correctness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,27 @@ end
test_type_stability(rng, hmm, control_seq; seq_ends, hmm_guess)
@test_skip test_allocations(rng, hmm, control_seq; seq_ends, hmm_guess)
end

@testset "Normal transposed" begin # issue 99
dists = [Normal(μ[1][1]), Normal(μ[2][1])]
dists_guess = [Normal(μ_guess[1][1]), Normal(μ_guess[2][1])]

hmm = transpose_hmm(HMM(init, trans, dists))
hmm_guess = transpose_hmm(HMM(init_guess, trans_guess, dists_guess))

test_identical_hmmbase(rng, hmm, T; hmm_guess)
test_coherent_algorithms(rng, hmm, control_seq; seq_ends, hmm_guess, init=false)
test_type_stability(rng, hmm, control_seq; seq_ends, hmm_guess)
test_allocations(rng, hmm, control_seq; seq_ends, hmm_guess)
end

@testset "Normal and Exponential" begin # issue 101
dists = [Normal(μ[1][1]), Exponential(1.0)]
dists_guess = [Normal(μ_guess[1][1]), Exponential(0.8)]

hmm = HMM(init, trans, dists)
hmm_guess = HMM(init_guess, trans_guess, dists_guess)

test_identical_hmmbase(rng, hmm, T; hmm_guess)
test_coherent_algorithms(rng, hmm, control_seq; seq_ends, hmm_guess, init=false)
end
Loading