stan-dev / stan-dev/math

hmm_hidden_state returns NaN

Open
#2,677 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug good first issue
Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

Several users have reported that hmm_hidden_state returns nan, in cases where the probability of the hidden state being in one particular state goes to 1. There seems to be some numerical stability issue. Computing the log unnormalized probability first and then using softmax solves the issue in the reported problems.

This issue is raised on the Stan forum, notably here. A proposed coding of the method in Stan is given here.

Example

See discussion on Stan forum.

Expected Output

API doesn't change but function returns 1 or 0, as it does when manually writing the code in Stan with a log_sum_exp.

Current Version:

v4.3.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation and existing tests for hmm_hidden_state, then reproduce the NaN case described in the Stan forum discussion. Compare its calculation with the reported log_sum_exp approach; done means the API remains unchanged and the function returns 0 or 1 instead of NaN in the described limiting cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.