markov-models/scratchpad.ipynb

222 lines
4.8 KiB
Plaintext
Raw Normal View History

2020-12-10 21:43:15 +00:00
{
"cells": [
{
2020-12-11 21:33:20 +00:00
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-10 21:43:15 +00:00
"metadata": {},
2020-12-11 21:33:20 +00:00
"outputs": [],
2020-12-10 21:43:15 +00:00
"source": [
2020-12-11 21:33:20 +00:00
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
2020-12-10 21:43:15 +00:00
"\n",
2020-12-11 21:33:20 +00:00
"from constants import *\n",
"from maths import gaussian\n",
2020-12-31 19:30:39 +00:00
"from markov import MarkovModel\n",
"from markovlog import LogMarkovModel"
2020-12-11 21:33:20 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Scratchpad\n",
"===================="
2020-12-10 21:43:15 +00:00
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-10 21:43:15 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-10 21:43:15 +00:00
"source": [
"state1.mean\n",
"state_transition[1,1]"
2020-12-10 21:43:15 +00:00
]
},
2020-12-11 21:33:20 +00:00
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-11 21:33:20 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-11 21:33:20 +00:00
"source": [
"gaussian(490, 5, 500)"
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-11 21:33:20 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-11 21:33:20 +00:00
"source": [
"x = list(range(0, 1000))\n",
"y = [gaussian(i, 500, 50) for i in x]\n",
"\n",
"plt.plot(x, y)\n",
"plt.show()"
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-11 21:33:20 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-11 21:33:20 +00:00
"source": [
"x = np.linspace(-500, 500, 120)\n",
"y = [gaussian(i, 0, 100) for i in x]\n",
"\n",
"plt.plot(x, y)\n",
"plt.show()"
]
},
2020-12-10 21:43:15 +00:00
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-10 21:43:15 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-11 21:33:20 +00:00
"source": [
"for i in state_transition:\n",
" print(i)\n",
" # for j in i:\n",
" # print(j)\n",
"\n",
"print(state_transition[1][1])\n",
"print(state_transition.shape)"
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-11 21:33:20 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-11 21:33:20 +00:00
"source": [
"a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"print(a)\n",
"print(a[:, 1] / 5)\n",
"a[:, 2] / 2\n",
"print(a)"
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-11 21:33:20 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-11 21:33:20 +00:00
"source": [
"array1 = np.array([[1, 2], [3, 4]])\n",
"array2 = np.array([[1, 2], [3, 4]])\n",
"\n",
"print(array1 * array2)\n",
"print(array1[:, 1] * array2[:, 1])"
]
},
2020-12-23 20:12:08 +00:00
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-23 20:12:08 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-23 20:12:08 +00:00
"source": [
"model = MarkovModel(states=[state1, state2], observations=observations, state_transitions=state_transition)\n",
"\n",
"model.get_other_state_number(2)"
]
},
{
"source": [
"# Forward Probability Surface"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
2020-12-23 20:12:08 +00:00
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
2020-12-23 20:12:08 +00:00
"source": [
"model = MarkovModel(states=[state1, state2], observations=observations, state_transitions=state_transition)\n",
"model.populate_forward()\n",
"\n",
"fig = plt.figure(figsize=(18,6))\n",
"ax = fig.add_subplot(1, 1, 1, projection=\"3d\")\n",
"\n",
"y_width = 0.6\n",
2020-12-23 20:12:08 +00:00
"\n",
"X = np.arange(1, 10)\n",
"Y = np.arange(1, 3) - 0.5*y_width\n",
"X, Y = np.meshgrid(X, Y)\n",
"Z = np.zeros(model.forward.size)\n",
"\n",
"dx = np.ones(model.forward.size)\n",
"dy = y_width * np.ones(model.forward.size)\n",
"\n",
"# ax.plot_surface(X, Y, model.forward, cmap=cm.coolwarm)\n",
"ax.bar3d(X.flatten(), Y.flatten(), Z, dx, dy, model.forward.flatten(), shade=True)\n",
"\n",
"ax.set_yticks([1, 2])\n",
"\n",
"ax.set_xlabel(\"Observation\")\n",
"ax.set_ylabel(\"State\")\n",
"ax.view_init(30, -80)\n",
2020-12-23 20:12:08 +00:00
"# plt.zlabel(\"Forward Likelihood\")\n",
"fig.show()"
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
"source": [
"from numpy import log as ln\n",
"\n",
"ln(0.5)"
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
"source": [
"model = LogMarkovModel(states=[state1, state2], observations=observations, state_transitions=state_transition)\n",
"\n",
"print(model.state_transitions)\n",
"model.log_state_transitions()\n",
"print(model.state_transitions)"
]
},
{
"cell_type": "code",
2021-01-05 11:10:29 +00:00
"execution_count": null,
"metadata": {},
2021-01-05 11:10:29 +00:00
"outputs": [],
"source": [
"model = MarkovModel(states=[state1, state2], observations=observations, state_transitions=state_transition)\n",
"model.populate()\n",
"\n",
"model.baum_welch_state_transitions()"
]
2020-12-10 21:43:15 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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",
2020-12-31 19:30:39 +00:00
"version": "3.8.6-final"
2020-12-10 21:43:15 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 4
}