()
| 124 | |
| 125 | |
| 126 | def fit_coin(): |
| 127 | X = [] |
| 128 | for line in open('coin_data.txt'): |
| 129 | # 1 for H, 0 for T |
| 130 | x = [1 if e == 'H' else 0 for e in line.rstrip()] |
| 131 | X.append(x) |
| 132 | |
| 133 | hmm = HMM(2) |
| 134 | hmm.fit(X) |
| 135 | L = hmm.get_cost_multi(X).sum() |
| 136 | print("LL with fitted params:", L) |
| 137 | |
| 138 | # try true values |
| 139 | # remember these must be in their "pre-softmax" forms |
| 140 | pi = np.log( np.array([0.5, 0.5]) ) |
| 141 | A = np.log( np.array([[0.1, 0.9], [0.8, 0.2]]) ) |
| 142 | B = np.log( np.array([[0.6, 0.4], [0.3, 0.7]]) ) |
| 143 | hmm.set(pi, A, B) |
| 144 | L = hmm.get_cost_multi(X).sum() |
| 145 | print("LL with true params:", L) |
| 146 | |
| 147 | |
| 148 | if __name__ == '__main__': |
no test coverage detected