Skip to content

Commit

Permalink
Decompose forward function into initialize, predict, update (#105)
Browse files Browse the repository at this point in the history
* Decompose forward function into initialize, predict, update

* Fixes

* Remove unused predict

---------

Co-authored-by: Guillaume Dalle <[email protected]>
  • Loading branch information
THargreaves and gdalle authored Oct 2, 2024
1 parent 38eb996 commit 1656f7d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/HiddenMarkovModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ include("utils/lightdiagnormal.jl")
include("utils/lightcategorical.jl")
include("utils/limits.jl")

include("inference/predict.jl")
include("inference/forward.jl")
include("inference/viterbi.jl")
include("inference/forward_backward.jl")
Expand Down
64 changes: 34 additions & 30 deletions src/inference/forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ function initialize_forward(
return ForwardStorage(α, logL, B, c)
end

function _forward_digest_observation!(
current_state_marginals::AbstractVector{<:Real},
current_obs_likelihoods::AbstractVector{<:Real},
hmm::AbstractHMM,
obs,
control,
)
a, b = current_state_marginals, current_obs_likelihoods

obs_logdensities!(b, hmm, obs, control)
logm = maximum(b)
b .= exp.(b .- logm)

a .*= b
c = inv(sum(a))
lmul!(c, a)

logL = -log(c) + logm
return c, logL
end

function _forward!(
storage::ForwardOrForwardBackwardStorage,
hmm::AbstractHMM,
Expand All @@ -73,36 +94,19 @@ function _forward!(
)
(; α, B, c, logL) = storage
t1, t2 = seq_limits(seq_ends, k)

# Initialization
Bₜ₁ = view(B, :, t1)
obs_logdensities!(Bₜ₁, hmm, obs_seq[t1], control_seq[t1])
logm = maximum(Bₜ₁)
Bₜ₁ .= exp.(Bₜ₁ .- logm)

init = initialization(hmm)
αₜ₁ = view(α, :, t1)
αₜ₁ .= init .* Bₜ₁
c[t1] = inv(sum(αₜ₁))
lmul!(c[t1], αₜ₁)

logL[k] = -log(c[t1]) + logm

# Loop
for t in t1:(t2 - 1)
Bₜ₊₁ = view(B, :, t + 1)
obs_logdensities!(Bₜ₊₁, hmm, obs_seq[t + 1], control_seq[t + 1])
logm = maximum(Bₜ₊₁)
Bₜ₊₁ .= exp.(Bₜ₊₁ .- logm)

trans = transition_matrix(hmm, control_seq[t])
αₜ, αₜ₊₁ = view(α, :, t), view(α, :, t + 1)
mul!(αₜ₊₁, transpose(trans), αₜ)
αₜ₊₁ .*= Bₜ₊₁
c[t + 1] = inv(sum(αₜ₊₁))
lmul!(c[t + 1], αₜ₊₁)

logL[k] += -log(c[t + 1]) + logm
logL[k] = zero(eltype(logL))
for t in t1:t2
αₜ = view(α, :, t)
Bₜ = view(B, :, t)
if t == t1
copyto!(αₜ, initialization(hmm))
else
αₜ₋₁ = view(α, :, t - 1)
predict_next_state!(αₜ, hmm, αₜ₋₁, control_seq[t - 1])
end
cₜ, logLₜ = _forward_digest_observation!(αₜ, Bₜ, hmm, obs_seq[t], control_seq[t])
c[t] = cₜ
logL[k] += logLₜ
end

@argcheck isfinite(logL[k])
Expand Down
10 changes: 10 additions & 0 deletions src/inference/predict.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function predict_next_state!(
next_state_marginals::AbstractVector{<:Real},
hmm::AbstractHMM,
current_state_marginals::AbstractVector{<:Real},
control=nothing,
)
trans = transition_matrix(hmm, control)
mul!(next_state_marginals, transpose(trans), current_state_marginals)
return next_state_marginals
end
6 changes: 6 additions & 0 deletions src/types/abstract_hmm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ log_initialization(hmm::AbstractHMM) = elementwise_log(initialization(hmm))
transition_matrix(hmm, control)
Return the matrix of state transition probabilities for `hmm` (possibly when `control` is applied).
!!! note
When processing sequences, the control at time `t` influences the transition from time `t` to `t+1` (and not from time `t-1` to `t`).
"""
function transition_matrix end

Expand All @@ -82,6 +85,9 @@ function transition_matrix end
Return the matrix of state transition log-probabilities for `hmm` (possibly when `control` is applied).
Falls back on `transition_matrix`.
!!! note
When processing sequences, the control at time `t` influences the transition from time `t` to `t+1` (and not from time `t-1` to `t`).
"""
function log_transition_matrix(hmm::AbstractHMM, control)
return elementwise_log(transition_matrix(hmm, control))
Expand Down

2 comments on commit 1656f7d

@gdalle
Copy link
Owner

@gdalle gdalle commented on 1656f7d Oct 3, 2024

Choose a reason for hiding this comment

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

@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/116500

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.6.0 -m "<description of version>" 1656f7d5624717cd90c3047d5da041afc64c157b
git push origin v0.6.0

Please sign in to comment.