Skip to content

Instantly share code, notes, and snippets.

@Moligaloo
Created December 10, 2021 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Moligaloo/ce4b1b549c99716b289f9b8977590f8b to your computer and use it in GitHub Desktop.
Save Moligaloo/ce4b1b549c99716b289f9b8977590f8b to your computer and use it in GitHub Desktop.
Viterbi algorithm written in IoLanguage
obs := list("normal", "cold", "dizzy")
states := list("Healthy", "Fever")
start_p := Map with(
"Healthy", 0.6,
"Fever", 0.4
)
trans_p := Map with(
"Healthy", Map with(
"Healthy", 0.7,
"Fever", 0.3
),
"Fever", Map with(
"Healthy", 0.4,
"Fever", 0.6
)
)
emit_p := Map with(
"Healthy", Map with(
"normal", 0.5,
"cold", 0.4,
"dizzy", 0.1
),
"Fever", Map with(
"normal", 0.1,
"cold", 0.3,
"dizzy", 0.6
)
)
last_states := nil
path := obs map(
ob,
last_states = if(last_states,
states map(
state,
list(state,
states map(
new_state,
last_states at(new_state) * trans_p at(state) at(new_state) * emit_p at(new_state) at(ob)
) reduce(max)
)
) ,
states map(state,
list(
state,
start_p at(state) * emit_p at(state) at(ob)
)
)
) asMap
last_states asList reduce(a,b, if(a second > b second, a,b)) first
)
path print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment