{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[-1 -1 -1 -1 -1]\n" ] } ], "source": [ "\n", "import numpy\n", "\n", "\n", "# Run the nanochip dispersal hidden markov model n times starting at state s\n", "def run_HMM(n,s):\n", " \n", " \n", " symbols = []\n", " \n", " for i in range(n):\n", " \n", "\n", " # Emit the symbol\n", " randomNum = numpy.random.uniform()\n", " if s == 0:\n", " \n", " \n", " symbol = -1\n", " ### TODO: select a symbol (0 or 1) to emit when the state is 0\n", " \n", " ###\n", " symbols.append(symbol)\n", " \n", " elif s == 1:\n", " \n", " symbol = -1\n", " ### TODO: select a symbol (0 or 1) to emit when the state is 1\n", " \n", " ###\n", " symbols.append(symbol)\n", " \n", " \n", " # Choose the next state\n", " randomNum = numpy.random.uniform()\n", " if s == 0:\n", " \n", " newState = 0\n", " ### TODO: select the next state when the current state is 0\n", " \n", " ###\n", " s = newState\n", " \n", " elif s == 1:\n", " \n", " newState = 0\n", " ### TODO: select the next state when the current state is 1\n", " \n", " ###\n", " s = newState\n", " \n", "\n", " return numpy.array(symbols)\n", " \n", " \n", "\n", "print(run_HMM(5,0))\n", "\n", "\n", "#proportion_murky = numpy.mean(run_HMM(10000,0))\n", "#print(\"At equilibrium the water is murky with probability\", proportion_murky)\n", "\n", "\n", "\n", "def run_HMM_k_times(k,n,s):\n", " \n", " \n", " simulations = numpy.zeros([k,n])\n", " \n", " ### TODO: call run_HMM k times and add each simulation to the rows of 'simulations'\n", " \n", " return simulations\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n" ] } ], "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.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }