Viterbi Algorithm, to find the most likely path of states from the start and the expected output. https://en.wikipedia.org/wiki/Viterbi_algorithm Wikipedia example >>> observations = ["normal", "cold", "dizzy"] >>> states = ["Healthy", "Fever"] >>> start_p = {"Healthy
(
observations_space: list,
states_space: list,
initial_probabilities: dict,
transition_probabilities: dict,
emission_probabilities: dict,
)
| 2 | |
| 3 | |
| 4 | def viterbi( |
| 5 | observations_space: list, |
| 6 | states_space: list, |
| 7 | initial_probabilities: dict, |
| 8 | transition_probabilities: dict, |
| 9 | emission_probabilities: dict, |
| 10 | ) -> list: |
| 11 | """ |
| 12 | Viterbi Algorithm, to find the most likely path of |
| 13 | states from the start and the expected output. |
| 14 | |
| 15 | https://en.wikipedia.org/wiki/Viterbi_algorithm |
| 16 | |
| 17 | Wikipedia example |
| 18 | |
| 19 | >>> observations = ["normal", "cold", "dizzy"] |
| 20 | >>> states = ["Healthy", "Fever"] |
| 21 | >>> start_p = {"Healthy": 0.6, "Fever": 0.4} |
| 22 | >>> trans_p = { |
| 23 | ... "Healthy": {"Healthy": 0.7, "Fever": 0.3}, |
| 24 | ... "Fever": {"Healthy": 0.4, "Fever": 0.6}, |
| 25 | ... } |
| 26 | >>> emit_p = { |
| 27 | ... "Healthy": {"normal": 0.5, "cold": 0.4, "dizzy": 0.1}, |
| 28 | ... "Fever": {"normal": 0.1, "cold": 0.3, "dizzy": 0.6}, |
| 29 | ... } |
| 30 | >>> viterbi(observations, states, start_p, trans_p, emit_p) |
| 31 | ['Healthy', 'Healthy', 'Fever'] |
| 32 | >>> viterbi((), states, start_p, trans_p, emit_p) |
| 33 | Traceback (most recent call last): |
| 34 | ... |
| 35 | ValueError: There's an empty parameter |
| 36 | >>> viterbi(observations, (), start_p, trans_p, emit_p) |
| 37 | Traceback (most recent call last): |
| 38 | ... |
| 39 | ValueError: There's an empty parameter |
| 40 | >>> viterbi(observations, states, {}, trans_p, emit_p) |
| 41 | Traceback (most recent call last): |
| 42 | ... |
| 43 | ValueError: There's an empty parameter |
| 44 | >>> viterbi(observations, states, start_p, {}, emit_p) |
| 45 | Traceback (most recent call last): |
| 46 | ... |
| 47 | ValueError: There's an empty parameter |
| 48 | >>> viterbi(observations, states, start_p, trans_p, {}) |
| 49 | Traceback (most recent call last): |
| 50 | ... |
| 51 | ValueError: There's an empty parameter |
| 52 | >>> viterbi("invalid", states, start_p, trans_p, emit_p) |
| 53 | Traceback (most recent call last): |
| 54 | ... |
| 55 | ValueError: observations_space must be a list |
| 56 | >>> viterbi(["valid", 123], states, start_p, trans_p, emit_p) |
| 57 | Traceback (most recent call last): |
| 58 | ... |
| 59 | ValueError: observations_space must be a list of strings |
| 60 | >>> viterbi(observations, "invalid", start_p, trans_p, emit_p) |
| 61 | Traceback (most recent call last): |
nothing calls this directly
no test coverage detected