{
"cells": [
{
"cell_type": "markdown",
"id": "0b7a72f8-0f89-416b-a685-c5bad6bc5b57",
"metadata": {},
"source": [
"# Q1"
]
},
{
"cell_type": "code",
"execution_count": 276,
"id": "24f90d15-ecd6-458d-b961-bacee131799d",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import json\n",
"from tqdm import tqdm\n",
"\n",
"train_data = np.load('train_data.npy')\n",
"\n",
"class HiddenMarkovModel():\n",
" def __init__(self, initial_probabilities, transition_probs, emission_probs):\n",
" self.transition_probs = transition_probs\n",
" self.emission_probs = emission_probs\n",
" self.initial_probabilities = initial_probabilities\n",
" self._log_transition_probs = self._compute_log_probs(transition_probs)\n",
" self._log_emission_probs = self._compute_log_probs(emission_probs)\n",
" if not self._valid_hmm(): raise ValueError(\"not a valid markov model\")\n",
"\n",
" def baum_welch(self, seq, iterations=100):\n",
" for itr in tqdm(xrange(iterations)):\n",
" initial_update = dict([(s, 0.0) for s in self.transition_probs])\n",
" transition_update1 = dict(\n",
" [(s, dict([(s, 0) for s in self.transition_probs])) for s in self.transition_probs])\n",
" transition_update2 = dict([(s, 0) for s in self.transition_probs])\n",
" emission_update1 = dict([(s, dict([(o, 0) for o in self.emission_probs[s]])) for s in self.emission_probs])\n",
" emission_update2 = dict([(s, dict([(o, 0) for o in self.emission_probs[s]])) for s in self.emission_probs])\n",
"\n",
" for s in seq:\n",
" init, tr1, tr2, em1, em2 = self._baum_welch_iteration(s)\n",
" for s in self.transition_probs:\n",
" initial_update[s] += init[s]\n",
" for s in self.transition_probs:\n",
" for x in self.transition_probs[s]:\n",
" transition_update1[s][x] += tr1[s][x]\n",
"\n",
" for s in self.transition_probs:\n",
" transition_update2[s] += tr2[s]\n",
"\n",
" for s in self.emission_probs:\n",
" for o in self.emission_probs[s]:\n",
" emission_update1[s][o] += em1[s][o]\n",
" emission_update2[s][o] += em2[s][o]\n",
"\n",
" self.initial_probabilities = dict([(s, (1 / len(seq)) * initial_update[s]) for s in self.transition_probs])\n",
" for i in self.transition_probs:\n",
" for j in self.transition_probs[i]:\n",
" self.transition_probs[i][j] = (transition_update1[i][j] / transition_update2[i])\n",
"\n",
" for i in self.emission_probs:\n",
" for o in self.emission_probs[i]:\n",
" self.emission_probs[i][o] = (emission_update1[i][o] / emission_update2[i][o])\n",
"\n",
" self._log_transition_probs = self._compute_log_probs(self.transition_probs)\n",
" self._log_emission_probs = self._compute_log_probs(self.emission_probs)\n",
"\n",
"\n",
" def _baum_welch_iteration(self, seq, cons=100):\n",
" forward_probabilities = [dict([(s, 0.0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
" eps = 0.00000000000000001\n",
" forward_probabilities[0] = dict(\n",
" [(s, np.log(self.initial_probabilities[s] + eps) + self._log_emission_probs[s][seq[0]]) for s in\n",
" self.transition_probs])\n",
"\n",
" for i in xrange(1, len(seq)):\n",
" for j in self.transition_probs:\n",
" a = self._log_emission_probs[j][seq[i]]\n",
" b = self._log_sum_exp([forward_probabilities[i - 1][k] + self._log_transition_probs[k][j] for k in\n",
" self.transition_probs[j]])\n",
" forward_probabilities[i][j] = a + b\n",
"\n",
" backward_probabilities = [dict([(s, 0.0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
"\n",
" for j in self.transition_probs:\n",
" backward_probabilities[len(seq) - 1][j] = 0\n",
"\n",
" for i in range(len(seq) - 2, -1, -1):\n",
" for j in self.transition_probs:\n",
" backward_probabilities[i][j] = self._log_sum_exp([\n",
" backward_probabilities[i + 1][k] + self._log_transition_probs[j][k] + self._log_emission_probs[k][\n",
" seq[i + 1]]\n",
" for k in self.transition_probs[j]\n",
" ])\n",
" gamma = [dict([(s, 0) for s in self.transition_probs]) for t in xrange(len(seq))]\n",
" for t in xrange(len(seq)):\n",
" norm = sum(\n",
" [np.exp(forward_probabilities[t][k] + backward_probabilities[t][k] + 1200) for k in\n",
" self.transition_probs])\n",
" for j in self.transition_probs:\n",
" gamma[t][j] = np.exp(forward_probabilities[t][j] + backward_probabilities[t][j] + 1200) / norm\n",
"\n",
" zeta = [dict([(s, dict([(s, 0) for s in self.transition_probs])) for s in self.transition_probs]) for i in\n",
" xrange(len(seq))]\n",
" for t in xrange(len(seq) - 1):\n",
" norm = sum([sum([\n",
" np.exp(forward_probabilities[t][i] + self._log_transition_probs[i][j]\n",
" + backward_probabilities[t + 1][j] + self._log_emission_probs[j][seq[t + 1]] + 1200)\n",
" for j in self.transition_probs[i]]) for i in self.transition_probs])\n",
"\n",
" for i in self.transition_probs:\n",
" for j in self.transition_probs:\n",
" if j in self._log_transition_probs[i]:\n",
"\n",
" zeta[t][i][j] = np.exp(forward_probabilities[t][i] + self._log_transition_probs[i][j]\n",
" + backward_probabilities[t + 1][j] + self._log_emission_probs[j][\n",
" seq[t + 1]] + 1200) / norm\n",
" else:\n",
" zeta[t][i][j] = 0\n",
"\n",
" init = dict([(s, gamma[0][s]) for s in self.transition_probs])\n",
" trans_prob1 = {}\n",
" trans_prob2 = {}\n",
" for i in self.transition_probs:\n",
" trans_prob2[i] = 0\n",
" for t in range(len(seq) - 1):\n",
" trans_prob2[i] += gamma[t][i]\n",
"\n",
" for i in self.transition_probs:\n",
" trans_prob1[i] = {}\n",
" for j in self.transition_probs[i]:\n",
" trans_prob1[i][j] = 0\n",
" for t in range(len(seq) - 1):\n",
" trans_prob1[i][j] += zeta[t][i][j]\n",
"\n",
" em_prob1 = {}\n",
" for i in self.emission_probs:\n",
" em_prob1[i] = {}\n",
" for o in self.emission_probs[i]:\n",
" em_prob1[i][o] = sum([(seq[t] == o) * gamma[t][i] for t in xrange(len(seq))])\n",
"\n",
" em_prob2 = {}\n",
" for i in self.emission_probs:\n",
" em_prob2[i] = {}\n",
" for o in self.emission_probs[i]:\n",
" em_prob2[i][o] = sum([gamma[t][i] for t in xrange(len(seq))])\n",
"\n",
" return init, trans_prob1, trans_prob2, em_prob1, em_prob2\n",
"\n",
" def _log_sum_exp(self, arr):\n",
" a = np.max(arr)\n",
" # print(arr)\n",
" return a + np.log(np.sum([np.exp(x - a) for x in arr]))\n",
"\n",
" def _compute_log_probs(self, prob_matrix):\n",
" log_prob_matrix = {}\n",
" for i in prob_matrix:\n",
" log_prob_matrix[i] = {}\n",
" for j in prob_matrix[i]:\n",
" log_prob_matrix[i][j] = np.log(prob_matrix[i][j] + 0.00000000001)\n",
" return log_prob_matrix\n",
"\n",
" def _valid_hmm(self):\n",
" if (self.transition_probs != None) and (self.emission_probs != None):\n",
" valid = True\n",
" # print(\"1\",set(self.transition_probs.keys()), set(self.emission_probs.keys()))\n",
" valid &= (set(self.transition_probs.keys()) == set(self.emission_probs.keys()))\n",
"\n",
" return valid\n",
" else:\n",
" return True\n",
" \n",
" def likelihood(self, seq, log=True):\n",
" alpha = [dict([(s, 0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
" alpha[0] = dict([(i, np.log(self.initial_probabilities[i]) + self._log_emission_probs[i][seq[0]]) for i in\n",
" self.emission_probs])\n",
" for i in xrange(1, len(seq)):\n",
" for j in self.transition_probs:\n",
" alpha[i][j] = self._log_emission_probs[j][seq[i]] + self._log_sum_exp([\n",
" alpha[i - 1][k] + self._log_transition_probs[k][j] for k in self.transition_probs[j]])\n",
"\n",
" if log:\n",
" return self._log_sum_exp(list(alpha[-1].values()))\n",
" else:\n",
" return sum([np.exp(p) for p in alpha[-1].values()]) # return the sum of the last row\n",
"\n",
" def backward_likelihood(self, seq, log=True):\n",
" backward_probabilities = [dict([(s, 0.0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
"\n",
" for j in self.transition_probs:\n",
" backward_probabilities[len(seq) - 1][j] = 0\n",
"\n",
" for i in range(len(seq) - 2, -1, -1):\n",
" for j in self.transition_probs:\n",
" backward_probabilities[i][j] = self._log_sum_exp([\n",
" backward_probabilities[i + 1][k] + self._log_transition_probs[j][k] + self._log_emission_probs[k][\n",
" seq[i + 1]]\n",
" for k in self.transition_probs[j]\n",
" ])\n",
"\n",
" if log:\n",
" return self._log_sum_exp([(backward_probabilities[0][k] + self._log_emission_probs[k][seq[0]] + np.log(\n",
" self.initial_probabilities[k])) for k in self.transition_probs])\n",
" else:\n",
" # return sum([np.exp(p) for p in alpha[-1].values()]) # return the sum of the last row\n",
" return\n",
"\n",
" def viterbi(self, seq):\n",
" T = [dict([(s, 0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
" T[0] = dict([(i, np.log(self.initial_probabilities[i]) + self._log_emission_probs[i][seq[0]]) for i in\n",
" self.emission_probs])\n",
" T_bp = [dict([(s, 0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
" T_bp[0] = dict([(i, i) for i in self.emission_probs])\n",
"\n",
" for i in xrange(1, len(seq)):\n",
" for j in self.transition_probs:\n",
" T_bp[i][j] = max(self.transition_probs[j],\n",
" key=lambda k: T[i - 1][k] + self._log_transition_probs[k][j] +\n",
" self._log_emission_probs[j][seq[i]])\n",
"\n",
" prev_state = T_bp[i][j]\n",
" T[i][j] = T[i - 1][prev_state] + self._log_transition_probs[prev_state][j] + self._log_emission_probs[j][seq[i]]\n",
"\n",
" # construct the most likely sequence\n",
" state_sequence = [None for i in xrange(len(seq))]\n",
" state_sequence[-1] = max(self.transition_probs,\n",
" key=lambda k: T[-1][k])\n",
"\n",
" for i in xrange(len(seq) - 1):\n",
" i = len(seq) - 2 - i # go in reverse order\n",
" state_sequence[i] = T_bp[i + 1][state_sequence[i + 1]]\n",
"\n",
" return state_sequence\n",
"\n",
"\n",
"transition = {\n",
" 0: {1: 0.5, 5: 0.5},\n",
" 1: {0: 0.5, 2: 0.5},\n",
" 2: {1: 0.5, 3: 0.5},\n",
" 3: {2: (1 / 3), 4: (1 / 3), 8: (1 / 3)},\n",
" 4: {3: 0.5, 9: 0.5},\n",
" 5: {0: (1 / 3), 6: (1 / 3), 10: (1 / 3)},\n",
" 6: {5: 0.25, 7: 0.25, 11: 0.25, 12: 0.25},\n",
" 7: {6: (1 / 3), 8: (1 / 3), 12: (1 / 3)},\n",
" 8: {3: 0.2, 7: 0.2, 9: 0.2, 12: 0.2, 13: 0.2},\n",
" 9: {8: (1 / 3), 4: (1 / 3), 14: (1 / 3)},\n",
" 10: {5: 0.5, 15: 0.5},\n",
" 11: {6: (1 / 3), 12: (1 / 3), 16: (1 / 3)},\n",
" 12: {6: 0.125, 7: 0.125, 8: 0.125, 11: 0.125, 13: 0.125, 16: 0.125, 17: 0.125, 18: 0.125},\n",
" 13: {8: 0.25, 12: 0.25, 14: 0.25, 18: 0.25},\n",
" 14: {9: (1 / 3), 13: (1 / 3), 19: (1 / 3)},\n",
" 15: {10: 0.5, 20: 0.5},\n",
" 16: {11: (0.25), 12: (0.25), 17: (0.25), 21: 0.25},\n",
" 17: {12: (0.25), 16: (0.25), 18: (0.25), 22: 0.25},\n",
" 18: {12: 0.2, 13: 0.2, 17: 0.2, 19: 0.2, 23: 0.2},\n",
" 19: {14: (1 / 3), 18: (1 / 3), 24: (1 / 3)},\n",
" 20: {15: 0.5, 21: 0.5},\n",
" 21: {16: (1 / 3), 20: (1 / 3), 22: (1 / 3)},\n",
" 22: {17: (1 / 3), 21: (1 / 3), 23: (1 / 3)},\n",
" 23: {18: (1 / 3), 22: (1 / 3), 24: (1 / 3)},\n",
" 24: {19: 0.5, 23: 0.5}\n",
"}\n",
"\n",
"emission = {}\n",
"for i in range(25):\n",
" emission[i] = {0: (1 / 11), 1: (1 / 11), 2: (1 / 11), 3: (1 / 11), 4: (1 / 11), 5: (1 / 11), 6: (1 / 11), 7: (1 / 11),\n",
" 8: (1 / 11), 9: (1 / 11), 10: (1 / 11)}\n",
" \n",
"initial = {0: 0.04, 1: 0.04, 2: 0.04, 3: 0.04, 4: 0.04, 5: 0.04, 6: 0.04, 7: 0.04, 8: 0.04, 9: 0.04, 10: 0.04, 11: 0.04,\n",
" 12: 0.04, 13: 0.04, 14: 0.04, 15: 0.04, 16: 0.04, 17: 0.04, 18: 0.04, 19: 0.04, 20: 0.04, 21: 0.04, 22: 0.04,\n",
" 23: 0.04, 24: 0.04}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da5a4a59-6af2-43db-8b90-7ebf86a6f268",
"metadata": {},
"outputs": [],
"source": [
"\n",
"model = HiddenMarkovModel(initial, transition, emission)\n",
"model.baum_welch(train_data)\n",
"\n",
"output = json.dumps(model.transition_probs)\n",
"f = open(\"transition_probs.json\", \"w\")\n",
"f.write(output)\n",
"f.close()\n",
"\n",
"output = json.dumps(model.emission_probs)\n",
"f = open(\"emission_probs.json\", \"w\")\n",
"f.write(output)\n",
"f.close()\n",
"\n",
"output = json.dumps(model.initial_probabilities)\n",
"f = open(\"initial_probs.json\", \"w\")\n",
"f.write(output)\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": 278,
"id": "aea479bb-45b3-46e2-8d9b-f8f0d8f8ff70",
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"initial_converged = json.loads(Path('initial_probs.json').read_text())\n",
"transition_converged = json.loads(Path('transition_probs.json').read_text())\n",
"emission_converged = json.loads(Path('emission_probs.json').read_text())\n",
"\n",
"initial_converged = {int(k): v for k, v in initial_converged.items()}\n",
"transition_converged = {int(k): {int(x): z for x, z in v.items()} for k, v in transition_converged.items()}\n",
"emission_converged = {int(k): {int(x): z for x, z in v.items()} for k, v in emission_converged.items()}\n",
"\n",
"model = HiddenMarkovModel(initial_converged,transition_converged,emission_converged)"
]
},
{
"cell_type": "markdown",
"id": "5d5ea80b-a4a9-4e0e-8223-ed6035a530f3",
"metadata": {},
"source": [
"# Q2"
]
},
{
"cell_type": "code",
"execution_count": 275,
"id": "e1466805-844c-4826-a95a-78b7cb37dc69",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 2%|▏ | 2/100 [00:00<00:13, 7.21it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-87.40822221594169 \t 1.0941866674560593e-38\n",
"-87.40822221594168 \t 1.0941866674560748e-38\n",
"--------------------\n",
"-96.61173593224723 \t 1.1016818575067096e-42\n",
"-96.61173593224727 \t 1.1016818575066626e-42\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 4%|▍ | 4/100 [00:00<00:12, 7.44it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-92.86014462227607 \t 4.691930838968532e-41\n",
"-92.8601446222761 \t 4.691930838968398e-41\n",
"--------------------\n",
"-94.30465596383002 \t 1.1066449260434495e-41\n",
"-94.30465596383004 \t 1.106644926043418e-41\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 6%|▌ | 6/100 [00:00<00:12, 7.41it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-82.78489959778312 \t 1.1142301856444717e-36\n",
"-82.78489959778302 \t 1.1142301856445827e-36\n",
"--------------------\n",
"-92.82644902727122 \t 4.852722004156363e-41\n",
"-92.82644902727121 \t 4.852722004156433e-41\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 8%|▊ | 8/100 [00:01<00:12, 7.33it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-89.58799416423282 \t 1.2371674629415378e-39\n",
"-89.58799416423292 \t 1.2371674629414148e-39\n",
"--------------------\n",
"-77.89742952855609 \t 1.4776657300430085e-34\n",
"-77.89742952855612 \t 1.4776657300429664e-34\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 10%|█ | 10/100 [00:01<00:12, 7.11it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-85.43701721839028 \t 7.855518790205181e-38\n",
"-85.43701721839041 \t 7.855518790204177e-38\n",
"--------------------\n",
"-110.09694789417114 \t 1.5328619896912804e-48\n",
"-110.09694789417118 \t 1.532861989691215e-48\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 12%|█▏ | 12/100 [00:01<00:12, 7.17it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-86.32498227308703 \t 3.232485224350067e-38\n",
"-86.32498227308707 \t 3.2324852243499294e-38\n",
"--------------------\n",
"-89.46806690436863 \t 1.3948009576418284e-39\n",
"-89.46806690436867 \t 1.3948009576417688e-39\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 14%|█▍ | 14/100 [00:01<00:11, 7.32it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-76.12267947149063 \t 8.71646434179358e-34\n",
"-76.12267947149067 \t 8.716464341793208e-34\n",
"--------------------\n",
"-92.233509597237 \t 8.780024653149411e-41\n",
"-92.23350959723705 \t 8.780024653149036e-41\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 16%|█▌ | 16/100 [00:02<00:11, 7.38it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-79.72073490522291 \t 2.3862983304297343e-35\n",
"-79.72073490522287 \t 2.386298330429836e-35\n",
"--------------------\n",
"-88.9677774652936 \t 2.3003037086900776e-39\n",
"-88.96777746529358 \t 2.3003037086901102e-39\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 18%|█▊ | 18/100 [00:02<00:11, 7.39it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-87.67616308202875 \t 8.37001389620064e-39\n",
"-87.67616308202875 \t 8.37001389620064e-39\n",
"--------------------\n",
"-93.33199988064571 \t 2.9270319758946686e-41\n",
"-93.33199988064571 \t 2.9270319758946686e-41\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 20%|██ | 20/100 [00:02<00:10, 7.44it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-74.67811147176513 \t 3.695798363785381e-33\n",
"-74.67811147176518 \t 3.6957983637852234e-33\n",
"--------------------\n",
"-94.41126153955418 \t 9.947411496260651e-42\n",
"-94.41126153955422 \t 9.947411496260228e-42\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 22%|██▏ | 22/100 [00:03<00:10, 7.21it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-96.24420477400315 \t 1.5910101544166728e-42\n",
"-96.2442047740032 \t 1.591010154416605e-42\n",
"--------------------\n",
"-89.2083520623479 \t 1.8084435612305197e-39\n",
"-89.20835206234788 \t 1.808443561230545e-39\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 24%|██▍ | 24/100 [00:03<00:10, 7.25it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-95.20628119183672 \t 4.491976089313837e-42\n",
"-95.20628119183671 \t 4.4919760893139004e-42\n",
"--------------------\n",
"-95.86368650839314 \t 2.3277156928730194e-42\n",
"-95.86368650839314 \t 2.3277156928730194e-42\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 26%|██▌ | 26/100 [00:03<00:10, 7.38it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-88.71821988597522 \t 2.952341961933446e-39\n",
"-88.71821988597517 \t 2.952341961933572e-39\n",
"--------------------\n",
"-91.41467720362908 \t 1.991178184654762e-40\n",
"-91.41467720362905 \t 1.9911781846548182e-40\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 28%|██▊ | 28/100 [00:03<00:09, 7.26it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-84.46502179894641 \t 2.0763813474967497e-37\n",
"-84.46502179894637 \t 2.0763813474968382e-37\n",
"--------------------\n",
"-90.39670154992757 \t 5.510757934273053e-40\n",
"-90.39670154992758 \t 5.510757934272975e-40\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 30%|███ | 30/100 [00:04<00:10, 6.82it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-83.43356570651397 \t 5.8245557722310415e-37\n",
"-83.43356570651393 \t 5.82455577223129e-37\n",
"--------------------\n",
"-85.24263616957687 \t 9.54099105462171e-38\n",
"-85.24263616957694 \t 9.54099105462103e-38\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 32%|███▏ | 32/100 [00:04<00:10, 6.55it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-108.3600088687375 \t 8.706549817337239e-48\n",
"-108.36000886873757 \t 8.70654981733662e-48\n",
"--------------------\n",
"-80.69702990879753 \t 8.989286136980333e-36\n",
"-80.69702990879752 \t 8.989286136980461e-36\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 34%|███▍ | 34/100 [00:04<00:09, 6.82it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-88.93375316634295 \t 2.3799166365846443e-39\n",
"-88.9337531663429 \t 2.3799166365847797e-39\n",
"--------------------\n",
"-79.7246428064165 \t 2.3769911100295996e-35\n",
"-79.72464280641643 \t 2.3769911100297685e-35\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 36%|███▌ | 36/100 [00:05<00:09, 6.52it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-97.77431326612859 \t 3.4447307085015524e-43\n",
"-97.77431326612852 \t 3.444730708501797e-43\n",
"--------------------\n",
"-82.83795802259169 \t 1.056651900881171e-36\n",
"-82.83795802259172 \t 1.0566519008811409e-36\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 39%|███▉ | 39/100 [00:05<00:07, 8.66it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-96.23146604232804 \t 1.6114072465802652e-42\n",
"-96.23146604232801 \t 1.611407246580311e-42\n",
"--------------------\n",
"-85.37467418816064 \t 8.360843721370662e-38\n",
"-85.37467418816067 \t 8.360843721370424e-38\n",
"--------------------\n",
"-89.48353211462116 \t 1.3733960101993952e-39\n",
"-89.48353211462118 \t 1.373396010199356e-39\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 41%|████ | 41/100 [00:05<00:06, 9.76it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-97.0700156740002 \t 6.966711632751486e-43\n",
"-97.07001567400025 \t 6.966711632751189e-43\n",
"--------------------\n",
"-100.72028238787712 \t 1.81024411049534e-44\n",
"-100.72028238787705 \t 1.8102441104954685e-44\n",
"--------------------\n",
"-84.15934908173891 \t 2.8187664468113575e-37\n",
"-84.15934908173895 \t 2.8187664468112372e-37\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 45%|████▌ | 45/100 [00:05<00:04, 11.33it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-88.55051706951673 \t 3.491395654405544e-39\n",
"-88.55051706951677 \t 3.491395654405395e-39\n",
"--------------------\n",
"-83.0923545087351 \t 8.19311307143034e-37\n",
"-83.09235450873508 \t 8.193113071430458e-37\n",
"--------------------\n",
"-84.56911402210281 \t 1.8711148293847844e-37\n",
"-84.56911402210285 \t 1.8711148293847046e-37\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 47%|████▋ | 47/100 [00:05<00:04, 11.91it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-104.77868156501671 \t 3.1274960885960125e-46\n",
"-104.77868156501674 \t 3.1274960885959234e-46\n",
"--------------------\n",
"-85.29474897722375 \t 9.056516524497316e-38\n",
"-85.29474897722379 \t 9.05651652449693e-38\n",
"--------------------\n",
"-78.15197486864379 \t 1.1455882873647317e-34\n",
"-78.15197486864399 \t 1.145588287364504e-34\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 51%|█████ | 51/100 [00:06<00:04, 12.16it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-88.22345648905699 \t 4.842165982706708e-39\n",
"-88.22345648905697 \t 4.842165982706777e-39\n",
"--------------------\n",
"-89.6800844768694 \t 1.1283249071996753e-39\n",
"-89.68008447686934 \t 1.1283249071997396e-39\n",
"--------------------\n",
"-103.20594217582423 \t 1.5074010498752065e-45\n",
"-103.20594217582425 \t 1.507401049875185e-45\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 53%|█████▎ | 53/100 [00:06<00:03, 12.55it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-87.0192308458694 \t 1.6144634750761637e-38\n",
"-87.01923084586947 \t 1.614463475076049e-38\n",
"--------------------\n",
"-94.67955922563597 \t 7.606587742950879e-42\n",
"-94.67955922563598 \t 7.60658774295077e-42\n",
"--------------------\n",
"-80.08134438715506 \t 1.6638494568055258e-35\n",
"-80.08134438715507 \t 1.6638494568055023e-35\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 57%|█████▋ | 57/100 [00:06<00:03, 12.76it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-78.74913040170539 \t 6.305030791610312e-35\n",
"-78.74913040170547 \t 6.305030791609774e-35\n",
"--------------------\n",
"-91.9892161841703 \t 1.1209623255062784e-40\n",
"-91.98921618417036 \t 1.1209623255062148e-40\n",
"--------------------\n",
"-84.86323534339658 \t 1.3943287231048913e-37\n",
"-84.86323534339657 \t 1.3943287231049111e-37\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 59%|█████▉ | 59/100 [00:06<00:03, 12.90it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-95.44014252875895 \t 3.5552713056826696e-42\n",
"-95.44014252875903 \t 3.555271305682417e-42\n",
"--------------------\n",
"-77.67738555392103 \t 1.841365853404063e-34\n",
"-77.67738555392107 \t 1.8413658534039845e-34\n",
"--------------------\n",
"-99.46139090397452 \t 6.374802994627711e-44\n",
"-99.46139090397452 \t 6.374802994627711e-44\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 63%|██████▎ | 63/100 [00:07<00:02, 13.02it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-79.3590738144842 \t 3.4260378246379254e-35\n",
"-79.35907381448418 \t 3.4260378246380227e-35\n",
"--------------------\n",
"-89.26822913313974 \t 1.7033373840019986e-39\n",
"-89.26822913313983 \t 1.7033373840018534e-39\n",
"--------------------\n",
"-92.34380214104321 \t 7.863145142123464e-41\n",
"-92.34380214104314 \t 7.863145142124023e-41\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 67%|██████▋ | 67/100 [00:07<00:02, 13.44it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-97.55762225940336 \t 4.278218734086457e-43\n",
"-97.55762225940337 \t 4.278218734086397e-43\n",
"--------------------\n",
"-74.08024314532335 \t 6.719843889124379e-33\n",
"-74.08024314532337 \t 6.719843889124283e-33\n",
"--------------------\n",
"-89.46985072590093 \t 1.3923150994831403e-39\n",
"-89.46985072590093 \t 1.3923150994831403e-39\n",
"--------------------\n",
"-93.53214853494003 \t 2.3960948776385237e-41\n",
"-93.53214853494005 \t 2.3960948776384895e-41\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 69%|██████▉ | 69/100 [00:07<00:02, 13.45it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-89.75822792080102 \t 1.0435107047283274e-39\n",
"-89.75822792080093 \t 1.0435107047284163e-39\n",
"--------------------\n",
"-94.11244207472551 \t 1.3411758618634945e-41\n",
"-94.1124420747255 \t 1.3411758618635137e-41\n",
"--------------------\n",
"-92.7607900983177 \t 5.182039550720493e-41\n",
"-92.76079009831767 \t 5.182039550720641e-41\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 73%|███████▎ | 73/100 [00:07<00:02, 12.92it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-85.71546437227586 \t 5.946299905501121e-38\n",
"-85.71546437227586 \t 5.946299905501121e-38\n",
"--------------------\n",
"-101.76610049832081 \t 6.361274093218553e-45\n",
"-101.76610049832074 \t 6.361274093219005e-45\n",
"--------------------\n",
"-85.80169650808843 \t 5.455024036966462e-38\n",
"-85.80169650808841 \t 5.455024036966539e-38\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 75%|███████▌ | 75/100 [00:08<00:02, 12.14it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-95.83219188936275 \t 2.402192870878932e-42\n",
"-95.83219188936279 \t 2.4021928708788293e-42\n",
"--------------------\n",
"-92.20531347753138 \t 9.031110467468759e-41\n",
"-92.20531347753135 \t 9.031110467469016e-41\n",
"--------------------\n",
"-100.95550384875033 \t 1.4308093210985211e-44\n",
"-100.95550384875031 \t 1.4308093210985413e-44\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 79%|███████▉ | 79/100 [00:08<00:01, 12.33it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-95.51918532740343 \t 3.2850720290462225e-42\n",
"-95.51918532740349 \t 3.285072029046036e-42\n",
"--------------------\n",
"-83.52554612554279 \t 5.3127113406937134e-37\n",
"-83.52554612554289 \t 5.312711340693185e-37\n",
"--------------------\n",
"-80.02804019016165 \t 1.7549459603667815e-35\n",
"-80.0280401901616 \t 1.7549459603668564e-35\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 83%|████████▎ | 83/100 [00:08<00:01, 13.00it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-79.89658996072365 \t 2.001482776400959e-35\n",
"-79.89658996072366 \t 2.0014827764009307e-35\n",
"--------------------\n",
"-100.26602924093567 \t 2.851128414417456e-44\n",
"-100.26602924093565 \t 2.851128414417496e-44\n",
"--------------------\n",
"-87.77547967364865 \t 7.578679326328344e-39\n",
"-87.77547967364868 \t 7.578679326328129e-39\n",
"--------------------\n",
"-89.77416246064237 \t 1.0270146196328767e-39\n",
"-89.77416246064236 \t 1.0270146196328914e-39\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 87%|████████▋ | 87/100 [00:09<00:00, 13.68it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-86.78206685282748 \t 2.0465729876691823e-38\n",
"-86.78206685282753 \t 2.046572987669095e-38\n",
"--------------------\n",
"-82.54134969095658 \t 1.4215014282104077e-36\n",
"-82.54134969095655 \t 1.4215014282104481e-36\n",
"--------------------\n",
"-89.94704926650442 \t 8.639584170620975e-40\n",
"-89.94704926650445 \t 8.639584170620728e-40\n",
"--------------------\n",
"-86.57053316188643 \t 2.5286874455360856e-38\n",
"-86.57053316188644 \t 2.5286874455360496e-38\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 89%|████████▉ | 89/100 [00:09<00:00, 13.58it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-77.56779224395252 \t 2.0546405500782094e-34\n",
"-77.56779224395248 \t 2.054640550078297e-34\n",
"--------------------\n",
"-99.93085977925003 \t 3.9863830359225533e-44\n",
"-99.93085977925004 \t 3.9863830359224966e-44\n",
"--------------------\n",
"-105.45007169439032 \t 1.5981434045489174e-46\n",
"-105.45007169439035 \t 1.5981434045488719e-46\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 93%|█████████▎| 93/100 [00:09<00:00, 13.30it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-94.75320538551817 \t 7.066522700501191e-42\n",
"-94.75320538551826 \t 7.066522700500588e-42\n",
"--------------------\n",
"-95.06009364773068 \t 5.199072571359702e-42\n",
"-95.06009364773072 \t 5.1990725713594805e-42\n",
"--------------------\n",
"-90.88138295281098 \t 3.394039371099483e-40\n",
"-90.88138295281095 \t 3.3940393710995794e-40\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 95%|█████████▌| 95/100 [00:09<00:00, 13.49it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-91.24412018883183 \t 2.3614681685546735e-40\n",
"-91.24412018883181 \t 2.361468168554707e-40\n",
"--------------------\n",
"-90.69811940737132 \t 4.076685628753131e-40\n",
"-90.69811940737128 \t 4.076685628753305e-40\n",
"--------------------\n",
"-96.87895765951693 \t 8.433411191589496e-43\n",
"-96.87895765951696 \t 8.433411191589257e-43\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 99%|█████████▉| 99/100 [00:09<00:00, 12.81it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-95.96284038104089 \t 2.10798712865435e-42\n",
"-95.96284038104089 \t 2.10798712865435e-42\n",
"--------------------\n",
"-90.09279498686587 \t 7.467861897169648e-40\n",
"-90.0927949868658 \t 7.46786189717018e-40\n",
"--------------------\n",
"-100.38579699711615 \t 2.529311407187092e-44\n",
"-100.3857969971162 \t 2.529311407186948e-44\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 100/100 [00:10<00:00, 9.93it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"-89.59896282555579 \t 1.223671543435187e-39\n",
"-89.59896282555573 \t 1.2236715434352567e-39\n",
"--------------------\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"test_data= np.load('test_data.npy')\n",
"\n",
"with open('forward_backward_output.txt', 'w') as f:\n",
" for test in tqdm(test_data):\n",
" f_prob = model.likelihood(test, log=True)\n",
" b_prob = model.backward_likelihood(test, log=True)\n",
" print(f_prob, \"\\t\", np.exp(f_prob))\n",
" print(b_prob, \"\\t\", np.exp(b_prob))\n",
" print(\"--------------------\")\n",
" f.write(str(np.exp(f_prob))+\"\\t\"+str(np.exp(b_prob))+\"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 277,
"id": "fea80da8-98ac-439b-af96-511ebc5d23df",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 8%|▊ | 8/100 [00:00<00:02, 37.02it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[16, 11, 12, 16, 12, 18, 17, 18, 17, 22, 23, 22, 23, 22, 21, 16, 21, 20, 21, 22, 23, 22, 21, 22, 23, 22, 17, 12, 8, 9, 4, 3, 2, 3, 4, 3, 4, 3, 8, 7, 8, 13, 12, 11, 12, 8, 7, 8, 3, 2]\n",
"======================\n",
"[16, 21, 16, 12, 17, 12, 11, 16, 21, 22, 17, 12, 16, 11, 12, 6, 11, 16, 11, 12, 11, 6, 5, 10, 5, 6, 11, 12, 16, 21, 20, 15, 20, 21, 16, 21, 20, 15, 20, 21, 16, 21, 16, 12, 6, 5, 6, 5, 6, 12]\n",
"======================\n",
"[21, 16, 12, 11, 6, 5, 6, 11, 6, 11, 6, 7, 8, 7, 8, 12, 17, 12, 11, 12, 7, 8, 9, 4, 3, 4, 9, 8, 12, 16, 21, 20, 21, 20, 15, 20, 21, 16, 12, 17, 12, 18, 17, 22, 17, 12, 13, 18, 17, 18]\n",
"======================\n",
"[17, 12, 11, 12, 11, 12, 16, 21, 16, 21, 16, 11, 12, 17, 22, 23, 22, 17, 12, 11, 12, 6, 7, 8, 13, 14, 19, 14, 19, 14, 13, 12, 16, 21, 16, 11, 12, 6, 11, 12, 8, 7, 8, 13, 12, 18, 19, 18, 19, 14]\n",
"======================\n",
"[21, 16, 11, 6, 7, 6, 12, 18, 17, 18, 17, 12, 18, 12, 6, 11, 16, 21, 16, 12, 11, 6, 5, 6, 5, 10, 5, 0, 5, 10, 5, 10, 5, 10, 5, 0, 1, 2, 1, 0, 1, 2, 1, 0, 1, 0, 1, 0, 1, 0]\n",
"======================\n",
"[20, 21, 16, 21, 22, 23, 22, 23, 22, 17, 18, 19, 18, 19, 18, 17, 18, 12, 18, 17, 22, 23, 22, 17, 12, 18, 19, 24, 23, 22, 23, 22, 23, 22, 21, 16, 11, 6, 12, 18, 17, 12, 13, 18, 17, 18, 17, 12, 7, 6]\n",
"======================\n",
"[5, 10, 5, 0, 5, 6, 11, 16, 12, 16, 21, 22, 23, 22, 21, 22, 17, 12, 16, 21, 22, 23, 22, 23, 22, 17, 18, 17, 22, 17, 18, 17, 22, 23, 24, 19, 18, 12, 18, 19, 18, 19, 18, 17, 18, 19, 24, 19, 18, 19]\n",
"======================\n",
"[18, 12, 11, 12, 18, 12, 17, 12, 18, 17, 22, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 22, 23, 22, 17, 22, 17, 22, 23, 22, 17, 22, 23, 22, 17, 12, 17, 22, 23, 22, 21, 20, 15, 20, 15, 20, 15]\n",
"======================\n",
"[12, 11, 6, 5, 10, 5, 6, 5, 6, 7, 6, 5, 10, 5, 0, 5, 10, 5, 10, 5, 6, 7, 8, 13, 14, 13, 14, 13, 8, 13, 12, 18, 17, 22, 17, 22, 23, 22, 17, 22, 17, 12, 13, 18, 19, 18, 17, 18, 13, 14]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 16%|█▌ | 16/100 [00:00<00:02, 37.65it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[19, 14, 13, 8, 3, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 5, 0, 1, 0, 1, 0, 1, 2, 3, 2, 1, 2, 3, 8, 9, 4, 9, 8, 12, 13, 8, 9, 4, 9, 14, 13, 14, 13, 14, 19, 24, 23, 22, 23]\n",
"======================\n",
"[5, 0, 1, 2, 3, 4, 9, 8, 12, 17, 18, 19, 18, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 22, 17, 22, 23, 22, 23, 22, 23, 22, 17, 12, 6, 5, 6, 7, 8, 7, 6, 12, 8, 7, 6, 7, 6, 11, 12]\n",
"======================\n",
"[15, 20, 15, 20, 15, 20, 15, 20, 21, 22, 23, 22, 23, 22, 17, 18, 17, 22, 17, 22, 23, 22, 17, 18, 12, 16, 12, 13, 18, 19, 14, 19, 18, 12, 17, 22, 23, 24, 19, 24, 23, 22, 17, 18, 17, 12, 16, 12, 16, 21]\n",
"======================\n",
"[4, 3, 2, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 2, 1, 2, 3, 4, 3, 4, 3, 8, 13, 12, 18, 17, 22, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 18, 12, 16, 11, 6, 5, 10, 5, 6]\n",
"======================\n",
"[15, 20, 15, 20, 21, 20, 21, 20, 15, 20, 21, 16, 11, 6, 5, 10, 5, 6, 11, 12, 11, 16, 21, 20, 21, 16, 11, 12, 18, 12, 11, 16, 11, 12, 18, 17, 22, 23, 22, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15]\n",
"======================\n",
"[6, 11, 12, 18, 17, 18, 19, 14, 19, 24, 23, 22, 23, 22, 17, 18, 19, 24, 23, 22, 23, 22, 17, 18, 17, 18, 17, 12, 11, 16, 11, 12, 18, 17, 22, 23, 22, 23, 22, 17, 18, 12, 11, 6, 5, 0, 1, 2, 3, 4]\n",
"======================\n",
"[22, 17, 12, 6, 11, 16, 21, 22, 17, 22, 23, 22, 23, 22, 17, 18, 17, 22, 23, 22, 23, 22, 23, 22, 17, 12, 8, 3, 2, 1, 2, 3, 8, 13, 12, 17, 12, 11, 12, 18, 12, 16, 21, 22, 17, 22, 23, 22, 23, 22]\n",
"======================\n",
"[16, 11, 12, 11, 12, 11, 16, 12, 16, 11, 12, 17, 22, 21, 22, 23, 22, 17, 18, 17, 22, 23, 22, 23, 22, 23, 22, 17, 22, 21, 16, 21, 20, 21, 22, 23, 22, 23, 22, 23, 24, 19, 14, 13, 12, 6, 5, 6, 5, 10]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 24%|██▍ | 24/100 [00:00<00:02, 37.39it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 1, 0, 5, 10, 5, 6, 7, 12, 16, 21, 22, 23, 22, 23, 22, 17, 22, 23, 22, 23, 22, 17, 18, 19, 18, 12, 8, 7, 8, 7, 6, 12, 7, 8, 12, 17, 12, 16, 11, 12, 11, 12, 17, 18, 19, 24, 23, 22, 17]\n",
"======================\n",
"[15, 20, 15, 20, 15, 20, 21, 16, 21, 16, 12, 16, 12, 18, 12, 16, 12, 18, 19, 18, 17, 22, 23, 22, 23, 24, 19, 18, 19, 18, 17, 22, 21, 16, 21, 16, 21, 20, 21, 16, 12, 16, 21, 22, 23, 22, 21, 22, 23, 22]\n",
"======================\n",
"[23, 24, 19, 24, 23, 22, 17, 22, 17, 22, 17, 16, 12, 6, 5, 6, 12, 18, 19, 14, 9, 8, 12, 6, 5, 6, 11, 12, 16, 17, 12, 18, 12, 17, 18, 19, 14, 19, 18, 13, 14, 19, 14, 13, 8, 13, 12, 18, 17, 22]\n",
"======================\n",
"[13, 8, 9, 14, 19, 24, 23, 22, 23, 22, 17, 12, 13, 14, 9, 8, 9, 8, 3, 2, 3, 8, 9, 4, 9, 8, 13, 14, 19, 18, 17, 12, 17, 22, 23, 24, 19, 14, 19, 14, 19, 18, 17, 22, 23, 22, 23, 22, 23, 22]\n",
"======================\n",
"[18, 17, 18, 17, 22, 23, 22, 23, 22, 21, 16, 12, 11, 16, 11, 12, 16, 11, 12, 11, 16, 11, 12, 16, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 21, 20, 21, 20, 15, 20, 21, 16, 11, 6, 12, 16, 21, 20, 15, 20]\n",
"======================\n",
"[16, 12, 16, 11, 12, 16, 12, 17, 22, 23, 22, 17, 18, 17, 22, 23, 22, 23, 22, 23, 24, 19, 18, 19, 18, 17, 12, 8, 3, 4, 3, 2, 1, 0, 1, 0, 5, 6, 7, 6, 11, 16, 12, 18, 12, 16, 21, 20, 15, 20]\n",
"======================\n",
"[10, 5, 10, 5, 6, 11, 6, 12, 11, 6, 5, 10, 5, 10, 5, 6, 11, 16, 21, 20, 21, 16, 12, 17, 16, 12, 17, 18, 12, 6, 5, 0, 1, 2, 1, 2, 1, 0, 1, 2, 3, 4, 3, 8, 3, 4, 3, 8, 3, 2]\n",
"======================\n",
"[13, 18, 17, 22, 23, 22, 23, 22, 23, 22, 17, 16, 12, 18, 19, 24, 19, 18, 17, 12, 8, 7, 12, 8, 7, 12, 11, 12, 16, 12, 16, 17, 18, 17, 22, 23, 22, 23, 22, 21, 16, 12, 7, 8, 12, 8, 7, 6, 11, 12]\n",
"======================\n",
"[9, 14, 19, 14, 19, 14, 19, 14, 19, 18, 17, 18, 17, 18, 17, 22, 21, 16, 12, 16, 11, 6, 5, 6, 12, 11, 16, 11, 12, 11, 12, 6, 7, 6, 11, 12, 17, 18, 19, 18, 12, 11, 16, 21, 20, 15, 20, 15, 20, 15]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 33%|███▎ | 33/100 [00:00<00:01, 39.04it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 12, 11, 12, 16, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 21, 16, 21, 16, 12, 18, 12, 16, 12, 17, 12, 16, 21, 22, 17, 18, 19, 18, 12, 11, 16, 12, 16, 11, 6, 12, 18, 12, 16, 12, 18, 17, 12, 16, 21]\n",
"======================\n",
"[1, 2, 3, 8, 13, 12, 17, 18, 19, 18, 17, 22, 21, 16, 12, 16, 21, 16, 21, 20, 21, 22, 17, 18, 17, 22, 23, 22, 23, 22, 23, 22, 21, 16, 12, 13, 18, 17, 22, 23, 22, 21, 20, 15, 20, 15, 20, 15, 20, 21]\n",
"======================\n",
"[22, 23, 24, 19, 18, 17, 22, 17, 22, 17, 12, 18, 17, 22, 23, 22, 17, 18, 17, 12, 18, 17, 22, 21, 20, 15, 20, 21, 22, 17, 18, 17, 12, 18, 17, 22, 23, 22, 23, 22, 23, 22, 17, 18, 17, 22, 17, 12, 16, 11]\n",
"======================\n",
"[6, 5, 6, 7, 8, 9, 4, 3, 4, 3, 4, 9, 4, 3, 4, 3, 8, 7, 6, 5, 6, 12, 16, 11, 12, 8, 3, 2, 1, 2, 1, 2, 1, 0, 5, 0, 5, 10, 15, 20, 21, 20, 21, 16, 11, 6, 7, 6, 5, 6]\n",
"======================\n",
"[16, 12, 11, 12, 17, 22, 17, 12, 11, 16, 11, 16, 17, 16, 21, 20, 21, 16, 17, 12, 17, 12, 11, 16, 21, 16, 11, 12, 6, 11, 12, 18, 17, 18, 17, 18, 19, 18, 19, 24, 19, 14, 19, 18, 17, 22, 17, 22, 17, 22]\n",
"======================\n",
"[18, 17, 18, 17, 22, 23, 22, 17, 22, 17, 22, 17, 12, 17, 22, 23, 22, 23, 22, 21, 20, 15, 20, 15, 20, 21, 22, 17, 22, 23, 22, 17, 22, 21, 20, 21, 16, 11, 6, 12, 18, 19, 14, 19, 14, 19, 18, 12, 16, 21]\n",
"======================\n",
"[15, 20, 21, 20, 21, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 12, 17, 22, 17, 12, 17, 16, 21, 20, 21, 16, 12, 7, 8, 12, 16, 21, 22, 23, 22, 23, 22, 17, 18, 17, 12, 16, 12, 17, 18, 19, 18, 12, 16, 21]\n",
"======================\n",
"[13, 12, 18, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 22, 21, 20, 15, 20, 15, 20, 15, 20, 21, 20, 15, 20, 15, 20, 15, 20, 21, 16, 11, 16, 12, 11, 6, 5, 10, 5, 10, 5, 6, 11, 6, 11, 6, 5, 0]\n",
"======================\n",
"[15, 20, 21, 16, 12, 17, 22, 17, 18, 17, 22, 17, 12, 11, 16, 11, 12, 11, 16, 12, 17, 22, 17, 12, 11, 12, 17, 12, 7, 8, 13, 14, 13, 12, 6, 11, 12, 11, 12, 11, 6, 7, 8, 3, 8, 12, 16, 17, 22, 21]\n",
"======================\n",
"[19, 18, 17, 12, 16, 21, 22, 23, 22, 23, 22, 21, 20, 15, 20, 15, 20, 15, 20, 21, 22, 17, 22, 23, 24, 19, 18, 17, 12, 18, 17, 22, 17, 22, 21, 20, 15, 20, 21, 20, 15, 20, 21, 22, 17, 18, 19, 14, 19, 14]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 42%|████▏ | 42/100 [00:01<00:01, 39.03it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[14, 13, 12, 17, 12, 17, 22, 23, 22, 17, 12, 16, 12, 6, 11, 12, 11, 12, 11, 6, 12, 16, 21, 22, 23, 22, 23, 22, 21, 16, 11, 12, 18, 19, 18, 12, 11, 12, 17, 22, 21, 16, 12, 6, 5, 6, 12, 17, 18, 19]\n",
"======================\n",
"[21, 16, 11, 16, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 21, 22, 17, 12, 16, 21, 20, 15, 20, 21, 16, 12, 18, 12, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 18, 12, 18]\n",
"======================\n",
"[6, 5, 6, 7, 6, 7, 6, 12, 11, 12, 18, 17, 12, 8, 9, 8, 12, 7, 8, 7, 8, 7, 8, 12, 16, 21, 20, 15, 20, 15, 20, 21, 20, 21, 16, 21, 22, 23, 24, 19, 14, 19, 14, 19, 18, 17, 12, 18, 12, 16]\n",
"======================\n",
"[12, 11, 12, 17, 12, 16, 11, 12, 8, 7, 8, 9, 8, 13, 14, 13, 8, 12, 16, 12, 17, 12, 6, 11, 6, 5, 6, 5, 6, 12, 17, 22, 23, 22, 23, 22, 23, 22, 17, 16, 12, 16, 21, 20, 15, 20, 21, 16, 21, 16]\n",
"======================\n",
"[0, 5, 0, 1, 2, 3, 4, 9, 14, 19, 24, 23, 24, 19, 18, 19, 24, 23, 22, 21, 16, 11, 12, 16, 21, 22, 23, 22, 23, 22, 23, 22, 17, 12, 8, 9, 4, 3, 2, 1, 0, 5, 10, 5, 10, 5, 6, 11, 16, 21]\n",
"======================\n",
"[4, 9, 4, 9, 4, 9, 4, 3, 2, 1, 2, 1, 0, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 5, 6, 11, 16, 17, 18, 19, 24, 23, 24, 19, 18, 17, 22, 17, 12, 17, 18, 17, 22, 23, 22, 21, 20, 15, 20, 15]\n",
"======================\n",
"[18, 19, 18, 17, 12, 11, 12, 7, 8, 3, 2, 3, 8, 9, 8, 13, 12, 13, 14, 19, 14, 19, 14, 13, 14, 19, 18, 12, 11, 16, 21, 22, 23, 22, 23, 22, 17, 18, 17, 22, 23, 22, 23, 22, 23, 22, 17, 22, 17, 22]\n",
"======================\n",
"[21, 20, 15, 20, 21, 20, 15, 20, 15, 20, 15, 20, 21, 16, 12, 16, 21, 22, 17, 22, 23, 22, 23, 22, 17, 12, 8, 3, 2, 1, 0, 1, 0, 1, 0, 5, 0, 5, 10, 5, 6, 11, 6, 7, 6, 12, 8, 7, 6, 11]\n",
"======================\n",
"[1, 0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 3, 2, 3, 8, 13, 12, 8, 13, 14, 19, 18, 17, 12, 17, 12, 18, 19, 18, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 21, 16, 11, 12, 17, 12, 16, 21, 20, 15]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 52%|█████▏ | 52/100 [00:01<00:01, 40.42it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[17, 22, 17, 12, 18, 17, 12, 6, 5, 0, 1, 0, 5, 0, 1, 0, 5, 6, 5, 6, 5, 10, 5, 6, 12, 11, 16, 21, 22, 17, 18, 12, 13, 14, 13, 14, 19, 14, 13, 8, 3, 2, 1, 2, 1, 0, 5, 10, 5, 10]\n",
"======================\n",
"[5, 10, 5, 6, 11, 12, 18, 17, 22, 23, 22, 17, 22, 23, 22, 23, 22, 17, 22, 23, 22, 23, 22, 17, 12, 18, 12, 8, 7, 6, 5, 0, 1, 2, 3, 4, 9, 8, 12, 16, 11, 12, 8, 13, 12, 8, 3, 2, 1, 0]\n",
"======================\n",
"[0, 1, 0, 1, 2, 1, 2, 1, 2, 1, 2, 1, 0, 5, 10, 15, 20, 15, 20, 15, 20, 15, 20, 21, 16, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 21, 22, 23, 24, 19, 14, 19, 14, 19, 14, 19, 14, 19, 14, 19]\n",
"======================\n",
"[22, 23, 22, 23, 22, 23, 24, 19, 14, 13, 8, 12, 17, 22, 17, 22, 23, 22, 17, 18, 17, 22, 21, 20, 15, 20, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 21, 16, 12, 18, 12, 7, 8, 7, 12, 18, 19, 24]\n",
"======================\n",
"[15, 20, 21, 20, 21, 16, 21, 16, 12, 16, 12, 6, 7, 8, 9, 8, 3, 2, 1, 2, 3, 4, 9, 8, 9, 8, 7, 8, 9, 4, 3, 8, 12, 7, 8, 13, 12, 16, 11, 16, 11, 12, 16, 21, 16, 12, 18, 12, 7, 8]\n",
"======================\n",
"[5, 10, 5, 0, 1, 0, 1, 2, 3, 4, 9, 8, 12, 17, 18, 17, 18, 17, 18, 17, 18, 12, 18, 19, 14, 13, 14, 13, 18, 17, 12, 11, 12, 6, 7, 8, 9, 4, 9, 4, 9, 14, 19, 18, 19, 24, 19, 18, 17, 12]\n",
"======================\n",
"[7, 6, 11, 6, 5, 10, 5, 0, 5, 10, 5, 6, 5, 10, 5, 10, 5, 10, 5, 10, 5, 6, 12, 16, 17, 18, 19, 18, 19, 24, 19, 14, 13, 12, 11, 12, 11, 16, 21, 16, 11, 12, 17, 22, 17, 12, 11, 12, 11, 12]\n",
"======================\n",
"[22, 17, 12, 18, 19, 18, 17, 18, 17, 18, 17, 12, 18, 17, 18, 17, 12, 11, 6, 7, 8, 3, 8, 12, 18, 12, 17, 22, 17, 22, 23, 22, 23, 22, 17, 18, 17, 18, 17, 12, 18, 19, 14, 19, 18, 19, 18, 17, 22, 17]\n",
"======================\n",
"[22, 23, 22, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 21, 22, 23, 22, 23, 22, 23, 22, 17, 22, 23, 22, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 18, 17, 22, 23, 22, 23]\n",
"======================\n",
"[1, 2, 1, 2, 3, 2, 3, 2, 3, 8, 12, 16, 21, 16, 11, 12, 16, 21, 20, 15, 20, 21, 16, 11, 12, 18, 12, 17, 22, 17, 12, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 18, 17, 12, 6]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 62%|██████▏ | 62/100 [00:01<00:00, 40.80it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[16, 21, 20, 21, 16, 11, 16, 12, 16, 11, 12, 16, 17, 12, 16, 21, 16, 12, 11, 12, 18, 12, 18, 17, 22, 17, 22, 17, 18, 17, 22, 23, 22, 23, 24, 19, 24, 23, 22, 23, 24, 19, 18, 19, 18, 12, 11, 12, 16, 12]\n",
"======================\n",
"[9, 4, 9, 14, 19, 18, 17, 22, 23, 22, 23, 22, 17, 12, 16, 17, 22, 17, 22, 17, 22, 17, 12, 7, 6, 7, 8, 12, 8, 7, 6, 11, 16, 12, 11, 12, 17, 22, 23, 22, 17, 22, 21, 20, 15, 20, 15, 20, 21, 16]\n",
"======================\n",
"[0, 1, 0, 5, 6, 12, 11, 12, 11, 12, 16, 17, 18, 17, 12, 18, 17, 18, 17, 12, 16, 12, 18, 17, 18, 17, 18, 12, 17, 18, 17, 18, 19, 18, 17, 12, 16, 17, 22, 23, 22, 23, 22, 17, 18, 19, 24, 23, 22, 23]\n",
"======================\n",
"[11, 6, 7, 6, 5, 6, 5, 10, 5, 0, 5, 10, 5, 10, 5, 0, 1, 2, 1, 0, 1, 2, 3, 2, 1, 0, 5, 10, 5, 6, 11, 16, 11, 6, 11, 16, 11, 6, 11, 16, 21, 22, 17, 12, 18, 19, 14, 19, 24, 23]\n",
"======================\n",
"[14, 13, 18, 19, 18, 12, 6, 7, 6, 7, 6, 5, 10, 5, 0, 1, 0, 1, 0, 5, 0, 5, 0, 1, 0, 1, 2, 1, 0, 5, 0, 1, 2, 3, 2, 1, 2, 1, 0, 5, 10, 5, 0, 5, 10, 15, 20, 15, 20, 15]\n",
"======================\n",
"[11, 12, 17, 22, 17, 18, 17, 22, 23, 22, 23, 22, 23, 22, 23, 22, 17, 12, 11, 12, 6, 11, 16, 12, 16, 17, 16, 12, 17, 18, 12, 13, 8, 3, 2, 1, 2, 1, 0, 1, 2, 3, 2, 1, 0, 5, 0, 5, 6, 12]\n",
"======================\n",
"[2, 3, 2, 1, 0, 5, 10, 5, 0, 1, 0, 5, 6, 12, 16, 21, 16, 12, 16, 21, 22, 17, 18, 17, 18, 19, 14, 19, 18, 17, 12, 11, 16, 17, 22, 23, 22, 23, 22, 17, 12, 18, 19, 14, 13, 12, 16, 21, 20, 15]\n",
"======================\n",
"[12, 16, 11, 6, 11, 12, 17, 22, 23, 22, 23, 22, 23, 22, 17, 18, 17, 18, 17, 18, 17, 12, 11, 16, 12, 17, 18, 12, 7, 6, 11, 12, 18, 17, 22, 17, 12, 6, 5, 0, 1, 2, 1, 2, 1, 2, 1, 0, 1, 0]\n",
"======================\n",
"[15, 20, 21, 16, 11, 16, 11, 6, 11, 16, 11, 12, 18, 17, 22, 17, 12, 16, 21, 16, 21, 20, 21, 16, 11, 12, 16, 17, 22, 17, 22, 17, 22, 23, 22, 17, 22, 23, 22, 21, 16, 17, 18, 17, 12, 18, 17, 22, 23, 22]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 72%|███████▏ | 72/100 [00:01<00:00, 40.71it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[6, 5, 10, 5, 10, 5, 6, 7, 8, 9, 8, 7, 6, 5, 0, 5, 10, 5, 6, 12, 16, 11, 12, 6, 11, 6, 5, 10, 5, 10, 5, 0, 5, 0, 1, 0, 1, 2, 1, 2, 3, 8, 7, 8, 3, 8, 12, 6, 7, 6]\n",
"======================\n",
"[20, 21, 16, 11, 6, 7, 12, 18, 19, 18, 19, 14, 13, 18, 17, 12, 17, 22, 23, 22, 21, 20, 15, 20, 21, 20, 21, 20, 21, 16, 12, 11, 12, 18, 12, 17, 22, 17, 18, 17, 12, 13, 14, 13, 12, 17, 22, 17, 12, 18]\n",
"======================\n",
"[10, 5, 6, 5, 10, 5, 6, 5, 6, 11, 12, 16, 11, 12, 16, 11, 6, 5, 10, 5, 10, 5, 6, 11, 6, 11, 16, 12, 16, 17, 12, 11, 12, 17, 22, 17, 12, 11, 12, 16, 12, 18, 17, 22, 17, 22, 21, 16, 12, 18]\n",
"======================\n",
"[15, 20, 15, 20, 15, 20, 21, 16, 21, 22, 17, 18, 13, 14, 13, 14, 19, 18, 12, 11, 12, 11, 16, 11, 12, 13, 18, 19, 18, 17, 16, 12, 8, 9, 4, 9, 4, 9, 8, 9, 4, 9, 8, 9, 4, 3, 2, 1, 0, 1]\n",
"======================\n",
"[18, 19, 18, 17, 22, 17, 22, 23, 22, 17, 18, 17, 22, 17, 22, 23, 22, 23, 22, 23, 22, 17, 18, 12, 11, 16, 21, 20, 21, 20, 21, 20, 15, 20, 15, 20, 15, 20, 21, 20, 21, 22, 17, 18, 17, 18, 19, 14, 13, 14]\n",
"======================\n",
"[20, 15, 20, 15, 20, 21, 22, 23, 22, 17, 18, 12, 17, 22, 17, 12, 16, 21, 20, 21, 16, 11, 16, 12, 17, 22, 17, 22, 23, 22, 17, 22, 17, 18, 17, 12, 16, 21, 16, 12, 16, 21, 20, 15, 20, 15, 20, 15, 20, 15]\n",
"======================\n",
"[16, 12, 16, 11, 16, 12, 18, 17, 12, 13, 8, 3, 2, 1, 0, 5, 6, 7, 12, 7, 6, 7, 8, 9, 4, 9, 8, 12, 16, 21, 16, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 21]\n",
"======================\n",
"[7, 8, 13, 14, 19, 18, 17, 18, 17, 22, 23, 22, 17, 18, 17, 18, 17, 22, 23, 22, 17, 18, 12, 17, 18, 19, 14, 19, 14, 9, 8, 13, 8, 12, 16, 11, 6, 5, 0, 5, 10, 5, 10, 5, 6, 11, 6, 7, 8, 9]\n",
"======================\n",
"[5, 10, 5, 10, 5, 0, 1, 0, 1, 2, 3, 2, 1, 2, 1, 2, 3, 8, 9, 4, 3, 8, 7, 6, 7, 6, 11, 12, 16, 12, 6, 7, 8, 3, 8, 12, 17, 18, 19, 14, 19, 14, 19, 14, 19, 18, 19, 18, 17, 18]\n",
"======================\n",
"[15, 20, 15, 20, 15, 20, 15, 10, 5, 10, 5, 6, 11, 6, 5, 6, 5, 6, 5, 10, 5, 6, 5, 6, 5, 10, 5, 6, 11, 16, 12, 7, 6, 7, 6, 11, 16, 21, 16, 12, 16, 12, 8, 7, 8, 13, 12, 7, 8, 12]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 81%|████████ | 81/100 [00:02<00:00, 39.59it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[13, 14, 19, 18, 17, 18, 19, 14, 19, 18, 12, 18, 17, 22, 21, 22, 23, 22, 17, 18, 17, 18, 17, 22, 21, 16, 12, 11, 6, 5, 10, 5, 10, 5, 10, 5, 10, 5, 6, 12, 11, 16, 21, 20, 15, 20, 15, 20, 15, 20]\n",
"======================\n",
"[16, 11, 12, 18, 19, 14, 13, 12, 7, 8, 3, 2, 3, 2, 1, 2, 3, 4, 3, 2, 3, 8, 12, 7, 8, 12, 13, 18, 17, 22, 17, 16, 12, 18, 19, 18, 17, 22, 17, 16, 12, 11, 16, 21, 16, 12, 13, 8, 7, 8]\n",
"======================\n",
"[13, 14, 19, 24, 23, 24, 19, 18, 17, 12, 11, 12, 11, 6, 7, 8, 7, 6, 12, 11, 12, 16, 21, 16, 21, 16, 11, 12, 11, 16, 12, 13, 8, 7, 8, 3, 4, 9, 14, 19, 24, 23, 22, 23, 22, 17, 22, 23, 24, 19]\n",
"======================\n",
"[2, 3, 8, 9, 8, 3, 4, 3, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 3, 2, 3, 8, 12, 17, 12, 11, 16, 17, 18, 12, 11, 12, 18, 19, 18, 12, 17, 22, 17, 12, 18, 17, 22, 23, 22, 17, 12]\n",
"======================\n",
"[19, 24, 23, 22, 23, 22, 17, 18, 17, 18, 12, 8, 7, 6, 7, 6, 7, 6, 11, 12, 18, 12, 17, 22, 17, 12, 18, 17, 18, 19, 14, 19, 14, 19, 24, 23, 22, 23, 22, 17, 18, 19, 18, 17, 12, 7, 8, 9, 8, 3]\n",
"======================\n",
"[4, 9, 8, 3, 4, 3, 4, 3, 4, 9, 8, 9, 4, 3, 2, 1, 2, 1, 0, 1, 2, 1, 2, 1, 0, 1, 2, 1, 0, 5, 6, 11, 6, 11, 12, 16, 21, 16, 11, 12, 16, 21, 20, 21, 20, 15, 20, 21, 20, 15]\n",
"======================\n",
"[12, 11, 6, 7, 6, 7, 8, 12, 16, 21, 16, 11, 12, 7, 8, 12, 11, 12, 7, 8, 13, 14, 19, 18, 12, 11, 16, 21, 16, 21, 20, 21, 20, 15, 20, 21, 22, 23, 22, 17, 18, 17, 12, 18, 12, 11, 12, 18, 19, 24]\n",
"======================\n",
"[2, 3, 8, 13, 18, 17, 22, 17, 12, 7, 8, 12, 16, 12, 18, 17, 12, 18, 17, 22, 23, 22, 21, 16, 12, 18, 12, 16, 21, 22, 23, 22, 21, 20, 15, 20, 15, 20, 21, 16, 17, 22, 23, 22, 21, 20, 21, 20, 15, 20]\n",
"======================\n",
"[15, 20, 15, 20, 21, 16, 11, 16, 11, 6, 7, 6, 12, 11, 16, 21, 20, 21, 16, 11, 12, 18, 17, 22, 23, 22, 17, 12, 18, 17, 12, 11, 12, 16, 12, 16, 21, 16, 12, 16, 12, 11, 12, 18, 19, 24, 23, 22, 23, 22]\n",
"======================\n",
"[22, 17, 18, 17, 12, 16, 11, 16, 21, 16, 21, 20, 21, 22, 23, 22, 21, 20, 15, 20, 15, 20, 21, 20, 21, 22, 23, 22, 17, 18, 12, 16, 17, 22, 23, 22, 23, 22, 23, 24, 19, 18, 19, 14, 13, 14, 13, 12, 16, 21]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" 89%|████████▉ | 89/100 [00:02<00:00, 39.21it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[22, 17, 18, 19, 18, 19, 18, 17, 18, 19, 18, 17, 18, 19, 24, 23, 22, 17, 18, 12, 16, 11, 12, 11, 6, 5, 6, 5, 10, 5, 10, 5, 6, 11, 12, 18, 17, 22, 23, 22, 23, 22, 23, 22, 21, 16, 12, 11, 12, 16]\n",
"======================\n",
"[2, 1, 0, 1, 2, 1, 0, 5, 6, 5, 10, 5, 10, 5, 6, 5, 6, 5, 10, 5, 0, 5, 0, 1, 0, 1, 2, 1, 0, 5, 6, 5, 0, 1, 2, 1, 0, 5, 6, 12, 8, 9, 8, 12, 17, 18, 17, 12, 16, 21]\n",
"======================\n",
"[5, 10, 5, 10, 5, 0, 1, 2, 3, 2, 3, 2, 1, 2, 3, 4, 3, 8, 3, 4, 3, 4, 9, 8, 12, 16, 11, 12, 18, 12, 16, 11, 12, 13, 8, 7, 6, 12, 11, 16, 21, 20, 21, 16, 12, 7, 8, 7, 6, 5]\n",
"======================\n",
"[15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15, 20, 21, 22, 23, 22, 23, 22, 23, 22, 23, 22, 21, 20, 21, 16, 21, 20, 15, 20, 15, 20, 15, 20]\n",
"======================\n",
"[10, 5, 6, 5, 6, 5, 6, 5, 6, 11, 16, 21, 16, 12, 16, 21, 16, 12, 6, 11, 6, 5, 6, 12, 13, 8, 9, 4, 3, 2, 3, 8, 7, 8, 7, 8, 7, 12, 16, 21, 16, 21, 20, 21, 22, 23, 22, 17, 12, 16]\n",
"======================\n",
"[5, 10, 5, 6, 12, 18, 12, 18, 12, 7, 8, 12, 13, 14, 13, 12, 16, 21, 20, 21, 20, 21, 16, 12, 13, 14, 19, 24, 19, 24, 23, 22, 17, 12, 6, 5, 10, 5, 10, 5, 6, 11, 12, 11, 16, 21, 16, 21, 16, 12]\n",
"======================\n",
"[5, 10, 5, 10, 5, 10, 5, 10, 5, 10, 5, 0, 1, 0, 1, 0, 1, 0, 1, 2, 1, 0, 5, 6, 5, 6, 12, 11, 12, 18, 12, 17, 18, 12, 16, 17, 12, 8, 3, 8, 9, 8, 12, 8, 13, 12, 8, 7, 8, 9]\n",
"======================\n",
"[6, 7, 6, 11, 6, 5, 6, 12, 17, 18, 17, 18, 19, 18, 12, 18, 12, 8, 7, 6, 5, 10, 5, 6, 5, 10, 5, 6, 11, 16, 21, 20, 21, 20, 21, 16, 11, 6, 5, 6, 5, 6, 5, 10, 5, 6, 11, 12, 8, 7]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 100/100 [00:02<00:00, 39.40it/s]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"[14, 19, 24, 23, 22, 23, 24, 19, 18, 17, 18, 17, 22, 17, 12, 16, 12, 18, 17, 22, 21, 16, 12, 11, 16, 12, 7, 8, 12, 6, 11, 12, 8, 13, 14, 19, 24, 23, 24, 19, 18, 17, 12, 11, 6, 7, 8, 7, 6, 11]\n",
"======================\n",
"[7, 6, 7, 8, 13, 12, 6, 7, 8, 12, 17, 18, 12, 17, 22, 23, 22, 17, 22, 23, 22, 17, 12, 13, 14, 13, 8, 12, 18, 17, 18, 12, 11, 12, 17, 18, 17, 18, 17, 18, 12, 18, 12, 11, 12, 11, 12, 7, 8, 12]\n",
"======================\n",
"[12, 18, 17, 18, 19, 24, 23, 22, 23, 24, 19, 14, 9, 4, 9, 8, 13, 18, 19, 24, 23, 18, 17, 22, 23, 22, 17, 18, 17, 12, 16, 21, 20, 15, 20, 15, 20, 21, 20, 21, 20, 15, 20, 15, 20, 15, 20, 15, 20, 15]\n",
"======================\n",
"[23, 22, 17, 22, 17, 12, 16, 21, 16, 21, 16, 12, 16, 21, 16, 21, 16, 12, 17, 22, 21, 20, 21, 16, 21, 16, 11, 16, 12, 11, 6, 5, 6, 7, 8, 13, 12, 18, 19, 24, 23, 22, 17, 22, 23, 24, 19, 18, 12, 6]\n",
"======================\n",
"[5, 10, 5, 0, 5, 10, 5, 6, 7, 8, 7, 8, 3, 2, 1, 0, 1, 0, 1, 2, 1, 2, 1, 2, 3, 2, 1, 0, 1, 0, 5, 0, 1, 2, 3, 4, 9, 8, 7, 8, 13, 14, 19, 24, 23, 22, 17, 12, 16, 21]\n",
"======================\n",
"[19, 14, 19, 14, 19, 14, 13, 8, 12, 17, 22, 23, 22, 23, 22, 21, 20, 15, 20, 21, 16, 12, 11, 16, 12, 7, 8, 9, 8, 12, 16, 11, 6, 7, 6, 5, 0, 1, 0, 5, 10, 5, 6, 5, 10, 5, 0, 5, 6, 11]\n",
"======================\n",
"[23, 22, 21, 16, 12, 17, 18, 12, 16, 11, 6, 11, 16, 21, 16, 12, 8, 7, 12, 18, 12, 17, 22, 17, 22, 17, 12, 11, 16, 21, 20, 15, 20, 21, 22, 23, 22, 23, 22, 23, 22, 17, 22, 17, 16, 12, 7, 8, 12, 6]\n",
"======================\n",
"[5, 6, 5, 6, 11, 6, 11, 16, 12, 17, 12, 16, 17, 22, 23, 22, 23, 22, 21, 20, 15, 20, 15, 20, 21, 22, 17, 22, 17, 12, 8, 9, 4, 9, 4, 3, 4, 3, 2, 1, 0, 1, 2, 1, 0, 5, 10, 5, 6, 5]\n",
"======================\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"test_data = np.load('test_data.npy')\n",
"\n",
"with open('viterbi_output.txt', 'w') as f:\n",
" for test in tqdm(test_data):\n",
" s = model.viterbi(test)\n",
" print(s)\n",
" print(\"======================\")\n",
" f.write(str(s)+\"\\n\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "20283576-b865-4a34-b5c7-a9f96251d3f5",
"metadata": {},
"source": [
"# Question 4"
]
},
{
"cell_type": "code",
"execution_count": 168,
"id": "b0d1e650-1f33-41ab-8c31-b20a419799bc",
"metadata": {},
"outputs": [],
"source": [
"def convert_dictionary_to_matrix(dictionary):\n",
" rows=len(dictionary)\n",
" matrix=np.zeros((rows,rows),dtype=np.float64)\n",
" for i in range(rows):\n",
" for j in range(rows):\n",
" value = dictionary[i].get(j)\n",
" matrix[i,j] = value if value is not None else 0\n",
" return matrix"
]
},
{
"cell_type": "code",
"execution_count": 169,
"id": "70f4b787-69ec-4a1c-93df-b4affe3278d1",
"metadata": {},
"outputs": [],
"source": [
"def get_stationary_distribution(Q):\n",
" evals, evecs = np.linalg.eig(Q.T)\n",
" evec1 = evecs[:,np.isclose(evals, 1)]\n",
" evec1 = evec1[:,0]\n",
" stationary = evec1 / evec1.sum()\n",
" stationary = stationary.real\n",
" stationary = stationary / stationary.sum()\n",
" return stationary"
]
},
{
"cell_type": "code",
"execution_count": 170,
"id": "b9730102-5892-436d-b987-2cf7dd3490d3",
"metadata": {},
"outputs": [],
"source": [
"def remove_edge(dictionary, left, right):\n",
" dictionary[left].pop(right)\n",
" dictionary[right].pop(left)"
]
},
{
"cell_type": "code",
"execution_count": 171,
"id": "ff4ed3a2-9f31-451c-807c-3568bceb6841",
"metadata": {},
"outputs": [],
"source": [
"def normalize_matrix(matrix):\n",
" \"\"\"\n",
" input: two dimensional matrix\n",
" returns a matrix which its rows sum to 1\n",
" \"\"\"\n",
" return matrix / np.sum(matrix,1)[:,None]"
]
},
{
"cell_type": "code",
"execution_count": 216,
"id": "f95a8eeb-0529-479d-b078-1db66850bb4a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.02112507, 0.0285067 , 0.02265319, 0.01969918, 0.01347683,\n",
" 0.03876496, 0.04224324, 0.02300952, 0.03733263, 0.01653681,\n",
" 0.01811561, 0.04662704, 0.11368048, 0.02230126, 0.01947599,\n",
" 0.02869246, 0.07049459, 0.07837297, 0.05633585, 0.03169953,\n",
" 0.0491273 , 0.05711506, 0.08066256, 0.05072891, 0.01322225])"
]
},
"execution_count": 216,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import copy\n",
"transition_matrix = convert_dictionary_to_matrix(model.transition_probs)\n",
"transition2 =copy.deepcopy(model.transition_probs)\n",
"\n",
"remove_edge(transition2,8,12)\n",
"remove_edge(transition2,12,13)\n",
"remove_edge(transition2,18,19)\n",
"remove_edge(transition2,16,17)\n",
"remove_edge(transition2,6,12)\n",
"\n",
"transition_matrix2 = convert_dictionary_to_matrix(transition2)\n",
"transition_matrix2 = normalize_matrix(transition_matrix2)\n",
"\n",
"stationary_distribution=get_stationary_distribution(transition_matrix)\n",
"stationary_distribution"
]
},
{
"cell_type": "code",
"execution_count": 180,
"id": "66eeaef4-7379-4be8-8955-c11a288fef55",
"metadata": {},
"outputs": [],
"source": [
"from random import uniform\n",
"import random\n",
"import pandas as pd\n",
"def w_choice(seq):\n",
" \"\"\"\n",
" choose an item from list of items whose probability is given\n",
" example: input [0.3,0.4,0.3] \n",
" output: it may choose from [0,1,2] with given probabilty\n",
" \"\"\"\n",
" total_prob = sum([item for item in seq])\n",
" chosen = random.uniform(0,total_prob)\n",
" cumulative = 0 \n",
" for i in range(len(seq)):\n",
" cumulative += seq[i]\n",
" if cumulative > chosen:\n",
" return i"
]
},
{
"cell_type": "code",
"execution_count": 181,
"id": "db2aba28-9838-4a07-bdcd-e0bb94e5711b",
"metadata": {},
"outputs": [],
"source": [
"state_count = len(transition_matrix)\n",
"a_array = np.zeros((state_count,state_count))\n",
"for i in range(state_count):\n",
" for j in range(state_count):\n",
" r_nominator = stationary_distribution[j]*transition_matrix2[j,i] \n",
" r_denominator = stationary_distribution[i]*transition_matrix2[i,j]\n",
"\n",
" a = 0\n",
" if r_denominator == 0:\n",
" a = 1\n",
" else:\n",
" a = min(r_nominator / r_denominator,1)\n",
" \n",
" a_array[i,j] = a"
]
},
{
"cell_type": "code",
"execution_count": 182,
"id": "32d48586-715c-4316-ba70-da011aca18f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.02180875, 0.02994216, 0.0234925 , 0.02023764, 0.01302789,\n",
" 0.04241142, 0.03536303, 0.02535294, 0.03216247, 0.01639951,\n",
" 0.01965901, 0.0513758 , 0.10217224, 0.02017778, 0.02124298,\n",
" 0.03131307, 0.07050133, 0.08362803, 0.05162414, 0.01804102,\n",
" 0.05382912, 0.06143372, 0.08667532, 0.05355925, 0.01456888])"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_transition_matrix=a_array*transition_matrix2\n",
"stationary2= get_stationary_distribution(normalize_matrix(new_transition_matrix))\n",
"stationary2"
]
},
{
"cell_type": "code",
"execution_count": 241,
"id": "cac71a05-31fd-41aa-bd2f-58d51ff12d7d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, True, True, True, True, True, True,\n",
" True, True, True, True, True, True, True, True, True,\n",
" True, True, True, True, True, True, True])"
]
},
"execution_count": 241,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"### check if they are close\n",
"(stationary2-stationary_distribution) < 0.01"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "f0f3f755-28ad-427d-a748-a0829af8cb42",
"metadata": {},
"outputs": [],
"source": [
"### sampling\n",
"iterations = 1000\n",
"sample_items = 100\n",
"x_array = np.zeros((iterations,sample_items))\n",
"for e in range(iterations):\n",
" x_array[e,0] = int(random.uniform(0,state_count))\n",
" for i in range(sample_items-1):\n",
" x = int(x_array[e,i])\n",
" y = w_choice(transition_matrix2[x])\n",
" \n",
" if random.uniform(0,1) <= a_array[x,y]:\n",
" x_array[e,i+1] = y\n",
" else:\n",
" x_array[e,i+1] = x"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "f37793d6-9eb4-477a-ae18-0368f8104b23",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, True, True, True, True, True, True,\n",
" True, True, True, True, True, True, True, True, True,\n",
" True, True, True, True, True, True, True])"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x_array=x_array.reshape(-1)\n",
"imperical_stationary=pd.Series(x_array).value_counts().sort_index()/len(x_array)\n",
"np.array(imperical_stationary)-stationary_distribution < 0.01"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "e7521b45-07dc-486a-bc91-5d3c24e1bf37",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.02180875, 0.02994216, 0.0234925 , 0.02023764, 0.01302789,\n",
" 0.04241142, 0.03536303, 0.02535294, 0.03216247, 0.01639951,\n",
" 0.01965901, 0.0513758 , 0.10217224, 0.02017778, 0.02124298,\n",
" 0.03131307, 0.07050133, 0.08362803, 0.05162414, 0.01804102,\n",
" 0.05382912, 0.06143372, 0.08667532, 0.05355925, 0.01456888])"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stationary2= get_stationary_distribution(normalize_matrix(new_transition_matrix))\n",
"stationary2"
]
},
{
"cell_type": "code",
"execution_count": 115,
"id": "779fe9d6-dcc4-4591-9236-4b991a365cf1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, True, True, True, True, True, True,\n",
" True, True, True, True, True, True, True, True, True,\n",
" True, True, True, True, True, True, True])"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(stationary2-stationary_distribution) < 0.01"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "9255f66a-d145-4ee3-a432-4b8e7d2b5ace",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "be147677-ce87-468d-8ab8-eaa70c3b7984",
"metadata": {},
"source": [
"### Question 5"
]
},
{
"cell_type": "code",
"execution_count": 157,
"id": "945c6ac0-f81c-43af-b6ab-48096a2096f8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.03623193, 0.05314085, 0.04338589, 0.039074 , 0.04035088,\n",
" 0.05016627, 0.0429694 , 0.02380878, 0.04097378, 0.04377247,\n",
" 0.0249732 , 0.03266443, 0.05314085, 0.02670919, 0.03966445,\n",
" 0.03096373, 0.04686786, 0.04565732, 0.03981049, 0.04184281,\n",
" 0.04970957, 0.04558968, 0.05314085, 0.03842504, 0.01696628])"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"min_matrix=np.where(transition_matrix<transition_matrix.T,transition_matrix,transition_matrix.T)\n",
"proposal_stationary=get_stationary_distribution(normalize_matrix(min_matrix))\n",
"proposal_stationary"
]
},
{
"cell_type": "markdown",
"id": "c534b271-5423-480b-88d4-e4275ef86bc1",
"metadata": {},
"source": [
"> Yes, It is logical, "
]
},
{
"cell_type": "markdown",
"id": "9c422cc3-7e1c-481d-a63c-ec674c97e614",
"metadata": {},
"source": [
"# Extra Section for testing purposes"
]
},
{
"cell_type": "code",
"execution_count": 191,
"id": "fc170d5b-21e3-4b79-a9ca-22b59ded684a",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"transition = np.array([[0.1,0.2,0.7],\n",
"\t\t\t\t\t[0.3,0.6,0.1],\n",
"\t\t\t\t\t[0.3,0.4,0.3] ])"
]
},
{
"cell_type": "code",
"execution_count": 192,
"id": "a098fce0-1f65-4e54-883a-ed0f2ff013d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.25 , 0.4375, 0.3125],\n",
" [0.25 , 0.4375, 0.3125],\n",
" [0.25 , 0.4375, 0.3125]])"
]
},
"execution_count": 192,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linalg.matrix_power(transition,10000)"
]
},
{
"cell_type": "code",
"execution_count": 193,
"id": "fdc121b1-5d25-4e77-a55e-4ff5ed2b876d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.25 , 0.4375, 0.3125])"
]
},
"execution_count": 193,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stationary_dist=get_stationary_distribution(transition)\n",
"stationary_dist"
]
},
{
"cell_type": "code",
"execution_count": 205,
"id": "5b2b9834-9201-4198-8820-d689ea4f5d0c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 0 0 1 1 2 0 0 2 0]\n"
]
}
],
"source": [
"def metropolis_hastings(sample_items, transition, stationary_dist):\n",
" sample = np.zeros(sample_items,dtype=np.int32)\n",
" for i in range(sample_items-1):\n",
" x = sample[i]\n",
" y = w_choice(transition[x])\n",
"\n",
" r_nominator = stationary_dist[y]*transition[y,x] \n",
" r_denominator = stationary_dist[x]*transition[x,y]\n",
"\n",
" a = 0\n",
" if r_denominator == 0:\n",
" a = 1\n",
" else:\n",
" a = min(r_nominator / r_denominator,1)\n",
"\n",
" if random.uniform(0,1) <= a:\n",
" sample[i+1] = y\n",
" else:\n",
" sample[i+1] = x\n",
" return sample\n",
"print(metropolis_hastings(10,transition,stationary_dist))"
]
},
{
"cell_type": "code",
"execution_count": 213,
"id": "be7b0fc5-4b26-4f0e-be15-9ce335721bd2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0 0.260744\n",
"1.0 0.427493\n",
"2.0 0.311763\n",
"dtype: float64"
]
},
"execution_count": 213,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"number_of_iterations = 100000\n",
"samples_per_iteration = 100\n",
"samples=np.zeros((number_of_iterations,samples_per_iteration))\n",
"for i in range(number_of_iterations):\n",
" samples[i]= metropolis_hastings(samples_per_iteration,transition,stationary_dist)\n",
" \n",
"samples=samples.reshape(-1)\n",
"imperical_stationary=pd.Series(samples).value_counts().sort_index()/len(samples)\n",
"imperical_stationary"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6417d7c5-a90f-445d-b995-256f57104ee0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
{
"cells": [
{
"cell_type": "markdown",
"id": "0b7a72f8-0f89-416b-a685-c5bad6bc5b57",
"metadata": {},
"source": [
"# Q1"
]
},
{
"cell_type": "code",
"execution_count": 276,
"id": "24f90d15-ecd6-458d-b961-bacee131799d",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import json\n",
"from tqdm import tqdm\n",
"\n",
"train_data = np.load('train_data.npy')\n",
"\n",
"class HiddenMarkovModel():\n",
" def __init__(self, initial_probabilities, transition_probs, emission_probs):\n",
" self.transition_probs = transition_probs\n",
" self.emission_probs = emission_probs\n",
" self.initial_probabilities = initial_probabilities\n",
" self._log_transition_probs = self._compute_log_probs(transition_probs)\n",
" self._log_emission_probs = self._compute_log_probs(emission_probs)\n",
" if not self._valid_hmm(): raise ValueError(\"not a valid markov model\")\n",
"\n",
" def baum_welch(self, seq, iterations=100):\n",
" for itr in tqdm(xrange(iterations)):\n",
" initial_update = dict([(s, 0.0) for s in self.transition_probs])\n",
" transition_update1 = dict(\n",
" [(s, dict([(s, 0) for s in self.transition_probs])) for s in self.transition_probs])\n",
" transition_update2 = dict([(s, 0) for s in self.transition_probs])\n",
" emission_update1 = dict([(s, dict([(o, 0) for o in self.emission_probs[s]])) for s in self.emission_probs])\n",
" emission_update2 = dict([(s, dict([(o, 0) for o in self.emission_probs[s]])) for s in self.emission_probs])\n",
"\n",
" for s in seq:\n",
" init, tr1, tr2, em1, em2 = self._baum_welch_iteration(s)\n",
" for s in self.transition_probs:\n",
" initial_update[s] += init[s]\n",
" for s in self.transition_probs:\n",
" for x in self.transition_probs[s]:\n",
" transition_update1[s][x] += tr1[s][x]\n",
"\n",
" for s in self.transition_probs:\n",
" transition_update2[s] += tr2[s]\n",
"\n",
" for s in self.emission_probs:\n",
" for o in self.emission_probs[s]:\n",
" emission_update1[s][o] += em1[s][o]\n",
" emission_update2[s][o] += em2[s][o]\n",
"\n",
" self.initial_probabilities = dict([(s, (1 / len(seq)) * initial_update[s]) for s in self.transition_probs])\n",
" for i in self.transition_probs:\n",
" for j in self.transition_probs[i]:\n",
" self.transition_probs[i][j] = (transition_update1[i][j] / transition_update2[i])\n",
"\n",
" for i in self.emission_probs:\n",
" for o in self.emission_probs[i]:\n",
" self.emission_probs[i][o] = (emission_update1[i][o] / emission_update2[i][o])\n",
"\n",
" self._log_transition_probs = self._compute_log_probs(self.transition_probs)\n",
" self._log_emission_probs = self._compute_log_probs(self.emission_probs)\n",
"\n",
"\n",
" def _baum_welch_iteration(self, seq, cons=100):\n",
" forward_probabilities = [dict([(s, 0.0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
" eps = 0.00000000000000001\n",
" forward_probabilities[0] = dict(\n",
" [(s, np.log(self.initial_probabilities[s] + eps) + self._log_emission_probs[s][seq[0]]) for s in\n",
" self.transition_probs])\n",
"\n",
" for i in xrange(1, len(seq)):\n",
" for j in self.transition_probs:\n",
" a = self._log_emission_probs[j][seq[i]]\n",
" b = self._log_sum_exp([forward_probabilities[i - 1][k] + self._log_transition_probs[k][j] for k in\n",
" self.transition_probs[j]])\n",
" forward_probabilities[i][j] = a + b\n",
"\n",
" backward_probabilities = [dict([(s, 0.0) for s in self.transition_probs]) for _ in xrange(len(seq))]\n",
"\n",
" for j in self.transition_probs:\n",
" backward_probabilities[len(seq) - 1][j] = 0\n",
"\n",
" for i in range(len(seq) - 2, -1, -1):\n",
" for j in self.transition_probs:\n",
" backward_probabilities[i][j] = self._log_sum_exp([\n",
" backward_probabilities[i + 1][k] + self._log_transition_probs[j][k] + self._log_emission_probs[k][\n",
" seq[i + 1]]\n",
" for k in self.transition_probs[j]\n",
" ])\n",
" gamma = [dict([(s, 0) for s in self.transition_probs]) for t in xrange(len(seq))]\n",
" for t in xrange(len(seq)):\n",
" norm = sum(\n",
" [np.exp(forward_probabilities[t][k] + backward_probabilities[t][k] + 1200) for k in\n",
Xet Storage Details
- Size:
- 82.1 kB
- Xet hash:
- 1d7871b91013abed548009bbe4653a566f0c9c0f34e50d89a49eff48843b9f6d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.