Created
December 10, 2021 08:06
-
-
Save Moligaloo/ce4b1b549c99716b289f9b8977590f8b to your computer and use it in GitHub Desktop.
Viterbi algorithm written in IoLanguage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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