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

Decompose forward function into initialize, predict, update #105

Merged
merged 5 commits into from
Oct 2, 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
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
Loading