()
| 179 | return states |
| 180 | |
| 181 | def fit_coin(): |
| 182 | X = [] |
| 183 | for line in open('coin_data.txt'): |
| 184 | # 1 for H, 0 for T |
| 185 | x = [1 if e == 'H' else 0 for e in line.rstrip()] |
| 186 | X.append(x) |
| 187 | |
| 188 | hmm = HMM(2) |
| 189 | hmm.fit(X) |
| 190 | L = hmm.log_likelihood_multi(X).sum() |
| 191 | print("LL with fitted params:", L) |
| 192 | |
| 193 | # try true values |
| 194 | hmm.pi = np.array([0.5, 0.5]) |
| 195 | hmm.A = np.array([[0.1, 0.9], [0.8, 0.2]]) |
| 196 | hmm.B = np.array([[0.6, 0.4], [0.3, 0.7]]) |
| 197 | L = hmm.log_likelihood_multi(X).sum() |
| 198 | print("LL with true params:", L) |
| 199 | |
| 200 | # try viterbi |
| 201 | print("Best state sequence for:", X[0]) |
| 202 | print(hmm.get_state_sequence(X[0])) |
| 203 | |
| 204 | |
| 205 | if __name__ == '__main__': |
no test coverage detected