# Step-by-step hidden Markov chain log-likelihood calculation

**URL:** https://discourse.julialang.org/t/step-by-step-hidden-markov-chain-log-likelihood-calculation/527
**Category:** Statistics
**Tags:** question
**Created:** [November 23, 2016, 3:12pm UTC](https://discourse.julialang.org/t/step-by-step-hidden-markov-chain-log-likelihood-calculation/527 "2016-11-23T15:12:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 23, 2016, 3:12pm UTC](https://discourse.julialang.org/t/step-by-step-hidden-markov-chain-log-likelihood-calculation/527/1 "2016-11-23T15:12:44Z")

</div>

I have a HMC for which the transition and observation matrices are time varying (and have some 0 elements).

I would like to calculate the log-likelihood (for Bayesian inference). Which library should I use? I looked at a few of them, but they seemed to have a high-level interface for constant matrices, and I need the low-level building blocks for updating, because of the time variation.

I need a numerically stable implementation, I can always program the algorithm from Rabiner’s tutorial, but would prefer not to do it if it has been done. Playing well with `AutoDiff.jl` would be a bonus (for NUTS/Hamiltonian MC).

---

<div class="post-metadata">

### Author: ![jmxpearson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmxpearson/32/3819_2.png) [@jmxpearson](https://discourse.julialang.org/u/jmxpearson)
#### Post date: [November 24, 2016, 1:17pm UTC](https://discourse.julialang.org/t/step-by-step-hidden-markov-chain-log-likelihood-calculation/527/2 "2016-11-24T13:17:19Z")

</div>

Not sure about AutoDiff.jl, but our code follows the conventions of Distributions.jl and will work with ForwarDiff.jl:

[https://github.com/jmxpearson/VinDsl.jl/blob/master/src/distributions/HMM.jl](https://github.com/jmxpearson/VinDsl.jl/blob/master/src/distributions/HMM.jl)

---

<div class="post-metadata">

### Author: ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)
#### Post date: [November 24, 2016, 1:47pm UTC](https://discourse.julialang.org/t/step-by-step-hidden-markov-chain-log-likelihood-calculation/527/3 "2016-11-24T13:47:05Z")

</div>

something weird is going on with the logz member. you don’t set it in the constructor, but you use it later. maybe simply missing from new?

---

<div class="post-metadata">

### Author: ![jmxpearson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmxpearson/32/3819_2.png) [@jmxpearson](https://discourse.julialang.org/u/jmxpearson)
#### Post date: [November 24, 2016, 2:14pm UTC](https://discourse.julialang.org/t/step-by-step-hidden-markov-chain-log-likelihood-calculation/527/4 "2016-11-24T14:14:47Z")

</div>

Thanks for that. Fixed now.
