{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from constants import *\n", "from maths import gaussian\n", "from markov import MarkovModel\n", "from markovlog import LogMarkovModel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scratchpad\n", "====================" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "state1.mean\n", "state_transition[1,1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gaussian(490, 5, 500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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", "execution_count": null, "metadata": {}, "outputs": [], "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()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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", "execution_count": null, "metadata": {}, "outputs": [], "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", "execution_count": null, "metadata": {}, "outputs": [], "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])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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", "execution_count": null, "metadata": {}, "outputs": [], "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", "\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", "# plt.zlabel(\"Forward Likelihood\")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from numpy import log as ln\n", "\n", "ln(0.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = MarkovModel(states=[state1, state2], observations=observations, state_transitions=state_transition)\n", "model.populate()\n", "\n", "model.baum_welch_state_transitions()" ] } ], "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", "version": "3.8.6-final" } }, "nbformat": 4, "nbformat_minor": 4 }