{
  "cells": [
    {
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "from matplotlib  import pyplot as plt\n",
        "from matplotlib.backends.backend_pdf import PdfPages\n",
        "import matplotlib as mpl\n"
      ],
      "outputs": [],
      "execution_count": 2,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T08:39:25.127Z",
          "iopub.execute_input": "2022-08-14T08:39:25.137Z",
          "iopub.status.idle": "2022-08-14T08:39:26.175Z",
          "shell.execute_reply": "2022-08-14T08:39:26.185Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sz = 14\n",
        "bg = np.zeros((sz+1,sz+1))"
      ],
      "outputs": [],
      "execution_count": 217,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:37:47.981Z",
          "iopub.execute_input": "2022-08-14T09:37:47.993Z",
          "iopub.status.idle": "2022-08-14T09:37:48.010Z",
          "shell.execute_reply": "2022-08-14T09:37:48.020Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def draw_grid(ax, sz):\n",
        "  ax.vlines(np.linspace(-0.5,sz+0.5,  sz+2), -1, sz+2, colors=\"black\", lw=0.5)\n",
        "  ax.hlines(np.linspace(-0.5,sz+0.5,  sz+2), -1, sz+2, colors=\"black\", lw=0.5)\n",
        "  ax.set_xlim(-0.5, sz+0.5)\n",
        "  ax.set_ylim(-0.5, sz+0.5)  \n",
        "  return ax"
      ],
      "outputs": [],
      "execution_count": 218,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:37:48.193Z",
          "iopub.execute_input": "2022-08-14T09:37:48.204Z",
          "iopub.status.idle": "2022-08-14T09:37:48.221Z",
          "shell.execute_reply": "2022-08-14T09:37:48.233Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "fig, ax = plt.subplots(figsize=(10,10))\n",
        "ax.matshow(bg, cmap=\"Greens\")\n",
        "ax = draw_grid(ax, sz)\n",
        "ax.scatter([7],[7], c=\"red\", s=100)\n",
        "fig.savefig(\"walk1.png\", transparent=True)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 720x720 with 1 Axes>",
            "image/png": "[encoded data, 9988 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 219,
      "metadata": {
        "collapsed": false,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:37:48.791Z",
          "iopub.execute_input": "2022-08-14T09:37:48.802Z",
          "iopub.status.idle": "2022-08-14T09:37:48.933Z",
          "shell.execute_reply": "2022-08-14T09:37:48.954Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "bg = np.zeros((sz+1,sz+1))\n",
        "bg[[6,8], [7,7]] = 1\n",
        "bg[[7,7], [6,8]] = 1\n"
      ],
      "outputs": [],
      "execution_count": 195,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:20.150Z",
          "iopub.execute_input": "2022-08-14T09:25:20.161Z",
          "iopub.status.idle": "2022-08-14T09:25:20.176Z",
          "shell.execute_reply": "2022-08-14T09:25:20.185Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "fig, ax = plt.subplots(figsize=(10,10))\n",
        "ax.matshow(bg, cmap=\"Greens\", vmax=2)\n",
        "ax = draw_grid(ax, sz)\n",
        "ax.scatter([7],[7], c=\"red\", s=100)\n",
        "for i,j in zip([1,0,-1,0], [0,1,0,-1]):\n",
        "    ax.annotate(\"\", xy=(7+i, 7+j), xytext=(7, 7),       arrowprops=dict(arrowstyle=\"->\"))\n",
        "fig.savefig(\"walk2.png\", transparent=True)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 720x720 with 1 Axes>",
            "image/png": "[encoded data, 11208 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 196,
      "metadata": {
        "collapsed": false,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:21.500Z",
          "iopub.execute_input": "2022-08-14T09:25:21.510Z",
          "iopub.status.idle": "2022-08-14T09:25:21.765Z",
          "shell.execute_reply": "2022-08-14T09:25:21.788Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "bg = np.zeros((sz+1,sz+1))\n",
        "bg[[6,8], [7,7]] = 0.5\n",
        "bg[[7,7], [6,8]] = 0.5\n",
        "bg[[7 ], [8]] = 1"
      ],
      "outputs": [],
      "execution_count": 197,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:25.732Z",
          "iopub.execute_input": "2022-08-14T09:25:25.741Z",
          "iopub.status.idle": "2022-08-14T09:25:25.755Z",
          "shell.execute_reply": "2022-08-14T09:25:25.764Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "fig, ax = plt.subplots(figsize=(10,10))\n",
        "ax.matshow(bg, cmap=\"Greens\", vmax=2)\n",
        "ax = draw_grid(ax, sz)\n",
        "ax.scatter([7],[7], c=\"pink\", s=100)\n",
        "ax.scatter([8],[7], c=\"red\", s=100)\n",
        "ax.plot([7, 8],[7,7], c=\"red\", lw=3)\n",
        "fig.savefig(\"walk3.png\", transparent=True)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 720x720 with 1 Axes>",
            "image/png": "[encoded data, 10636 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 198,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:31.185Z",
          "iopub.execute_input": "2022-08-14T09:25:31.194Z",
          "iopub.status.idle": "2022-08-14T09:25:31.427Z",
          "shell.execute_reply": "2022-08-14T09:25:31.446Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "bg = np.zeros((sz+1,sz+1))\n",
        "bg[[6,8], [8,8]] = 1\n",
        "bg[[7,7], [7,9]] = 1\n"
      ],
      "outputs": [],
      "execution_count": 199,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:32.184Z",
          "iopub.execute_input": "2022-08-14T09:25:32.195Z",
          "iopub.status.idle": "2022-08-14T09:25:32.214Z",
          "shell.execute_reply": "2022-08-14T09:25:32.225Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "fig, ax = plt.subplots(figsize=(10,10))\n",
        "ax.matshow(bg, cmap=\"Greens\", vmax=2)\n",
        "ax = draw_grid(ax, sz)\n",
        "ax.scatter([7],[7], c=\"pink\", s=100)\n",
        "ax.scatter([8],[7], c=\"red\", s=100)\n",
        "ax.plot([7, 8],[7,7], c=\"red\", lw=3)\n",
        "for i,j in zip([1,0,-1,0], [0,1,0,-1]):\n",
        "    ax.annotate(\"\", xy=(8+i, 7+j), xytext=(8,7),       arrowprops=dict(arrowstyle=\"->\"))\n",
        "fig.savefig(\"walk4.png\", transparent=True)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 720x720 with 1 Axes>",
            "image/png": "[encoded data, 11492 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 201,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:39.931Z",
          "iopub.execute_input": "2022-08-14T09:25:39.943Z",
          "iopub.status.idle": "2022-08-14T09:25:40.229Z",
          "shell.execute_reply": "2022-08-14T09:25:40.249Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "np.random.seed(42)\n",
        "bg = np.zeros((sz+1,sz+1))\n",
        "i = np.random.randint(-1,2, size=(50))\n",
        "j = np.random.randint(-1,2, size=(50))\n",
        "j[i!=0] = 0\n",
        "i[j!=0] = 0\n",
        "i[0]=0\n",
        "j[0]=0\n",
        "#i[i==0] = -1\n",
        "#j[i==j]=-1\n",
        "#j[i==j]=1\n",
        "#i==j\n",
        "i+7,j"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 202,
          "data": {
            "text/plain": "(array([7, 6, 8, 8, 6, 6, 8, 7, 8, 8, 8, 8, 6, 8, 7, 6, 7, 7, 7, 7, 6, 6,\n        7, 7, 6, 6, 6, 8, 8, 8, 7, 8, 7, 7, 8, 7, 8, 8, 6, 8, 6, 8, 8, 6,\n        6, 8, 7, 6, 7, 7]),\n array([ 0,  0,  0,  0,  0,  0,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,\n         0,  0, -1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0, -1,\n         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, -1,  0, -1, -1]))"
          },
          "metadata": {}
        }
      ],
      "execution_count": 202,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:43.403Z",
          "iopub.execute_input": "2022-08-14T09:25:43.415Z",
          "iopub.status.idle": "2022-08-14T09:25:43.442Z",
          "shell.execute_reply": "2022-08-14T09:25:43.453Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "i.cumsum()+7"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 203,
          "data": {
            "text/plain": "array([ 7,  6,  7,  8,  7,  6,  7,  7,  8,  9, 10, 11, 10, 11, 11, 10, 10,\n       10, 10, 10,  9,  8,  8,  8,  7,  6,  5,  6,  7,  8,  8,  9,  9,  9,\n       10, 10, 11, 12, 11, 12, 11, 12, 13, 12, 11, 12, 12, 11, 11, 11])"
          },
          "metadata": {}
        }
      ],
      "execution_count": 203,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:44.486Z",
          "iopub.execute_input": "2022-08-14T09:25:44.497Z",
          "iopub.status.idle": "2022-08-14T09:25:44.519Z",
          "shell.execute_reply": "2022-08-14T09:25:44.529Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "fig, ax = plt.subplots(figsize=(10,10))\n",
        "ax.matshow(bg, cmap=\"Greens\", vmax=2)\n",
        "ax = draw_grid(ax, sz)\n",
        "ax.scatter([i.cumsum()+7],[j.cumsum()+7], c=\"red\", alpha=0.3, s=100)\n",
        "#ax.scatter([8],[7], c=\"red\", s=100)\n",
        "ax.plot(i.cumsum()+7,j.cumsum()+7, c=\"red\", alpha=0.3, lw=3)\n",
        "fig.savefig(\"walk5.png\", transparent=True)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 720x720 with 1 Axes>",
            "image/png": "[encoded data, 16552 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 204,
      "metadata": {
        "collapsed": false,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:25:50.619Z",
          "iopub.execute_input": "2022-08-14T09:25:50.629Z",
          "iopub.status.idle": "2022-08-14T09:25:50.863Z",
          "shell.execute_reply": "2022-08-14T09:25:50.881Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def next_step_probability(current_i, current_j, sigma_i, sigma_j, size):\n",
        "    grid_ii, grid_jj = np.mgrid[0:size, 0:size]\n",
        "    rad = (\n",
        "        (((grid_ii - current_i) ** 2) / (sigma_i ** 2))\n",
        "        + (((grid_jj - current_j) ** 2)/ (sigma_j ** 2))\n",
        "    )\n",
        "\n",
        "    p_next_step = np.exp(-(rad / 2.0)) / (2.0 * np.pi * sigma_i * sigma_j)\n",
        "    return p_next_step / p_next_step.sum()\n"
      ],
      "outputs": [],
      "execution_count": 209,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:36:39.525Z",
          "iopub.execute_input": "2022-08-14T09:36:39.537Z",
          "iopub.status.idle": "2022-08-14T09:36:39.552Z",
          "shell.execute_reply": "2022-08-14T09:36:39.563Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "bg = next_step_probability(7, 7, 2, 3, sz+1)\n",
        "plt.matshow(bg)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 214,
          "data": {
            "text/plain": "<matplotlib.image.AxesImage at 0x128caeef0>"
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 288x288 with 1 Axes>",
            "image/png": "[encoded data, 5452 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 214,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:37:29.171Z",
          "iopub.execute_input": "2022-08-14T09:37:29.184Z",
          "iopub.status.idle": "2022-08-14T09:37:29.283Z",
          "shell.execute_reply": "2022-08-14T09:37:29.303Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "fig, ax = plt.subplots(figsize=(10,10))\n",
        "ax.matshow(bg, cmap=\"Greens\")\n",
        "ax = draw_grid(ax, sz)\n",
        "ax.scatter([7],[7], c=\"red\", s=100)\n",
        "fig.savefig(\"walk6.png\", transparent=True)"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 720x720 with 1 Axes>",
            "image/png": "[encoded data, 13632 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 216,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:37:38.672Z",
          "iopub.execute_input": "2022-08-14T09:37:38.687Z",
          "iopub.status.idle": "2022-08-14T09:37:38.928Z",
          "shell.execute_reply": "2022-08-14T09:37:38.949Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "grid_ii, grid_jj = np.mgrid[0:200, 0:200]\n",
        "my_i = np.sin(grid_ii/130) + np.sin(grid_ii/10)\n",
        "my_i /= my_i.max()\n",
        "my_j = np.sin(grid_jj/100) + np.sin(grid_jj/50)+ np.sin(grid_jj/10)\n",
        "my_j /= my_j.max()\n",
        "#grid_jj\n",
        "\n",
        "fig, ax = plt.subplots(figsize=(10,10))\n",
        "ax.matshow(my_j+my_i, cmap=\"Reds\")\n",
        "ax = draw_grid(ax, 100)\n",
        "#ax.scatter([7],[7], c=\"red\", s=100)\n",
        "fig.savefig(\"walk7.png\", transparent=True)\n",
        "\n"
      ],
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 720x720 with 1 Axes>",
            "image/png": "[encoded data, 129500 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 227,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-14T09:43:00.706Z",
          "iopub.execute_input": "2022-08-14T09:43:00.723Z",
          "iopub.status.idle": "2022-08-14T09:43:00.972Z",
          "shell.execute_reply": "2022-08-14T09:43:00.998Z"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Simulating some data"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "parameters = {\"sigma_i\" : 2,\n",
        "              \"sigma_j\" : 3}"
      ],
      "outputs": [],
      "execution_count": 2,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:41:44.483Z",
          "iopub.execute_input": "2022-08-08T19:41:44.491Z",
          "iopub.status.idle": "2022-08-08T19:41:44.503Z",
          "shell.execute_reply": "2022-08-08T19:41:44.569Z"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## in a maze"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "initial_act = np.ones((200, 200))\n",
        "initial_act[50:100, 50:60] = 0\n",
        "initial_act[20:89, 80:90] = 0\n",
        "initial_act[90:120, 0:10] = 0\n",
        "initial_act[120:200, 30:40] = 0\n",
        "initial_act[180:190, 50:60] = 0\n",
        "\n",
        "initial_act[50:60, 50:200] = 0\n",
        "initial_act[179:189, 80:130] = 0\n",
        "initial_act[110:120, 0:190] = 0\n",
        "initial_act[120:200, 30:40] = 0\n",
        "initial_act[180:190, 50:60] = 0"
      ],
      "outputs": [],
      "execution_count": 3,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:41:44.514Z",
          "iopub.execute_input": "2022-08-08T19:41:44.521Z",
          "iopub.status.idle": "2022-08-08T19:41:44.533Z",
          "shell.execute_reply": "2022-08-08T19:41:44.573Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "plt.matshow(initial_act)\n",
        "plt.colorbar()"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 4,
          "data": {
            "text/plain": "<matplotlib.colorbar.Colorbar at 0x114cd65f0>"
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 345.6x345.6 with 2 Axes>",
            "image/png": "[encoded data, 6788 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 4,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:41:44.545Z",
          "iopub.execute_input": "2022-08-08T19:41:44.553Z",
          "iopub.status.idle": "2022-08-08T19:41:44.686Z",
          "shell.execute_reply": "2022-08-08T19:41:44.703Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "rw = random_walk_model(parameters, seed=42)"
      ],
      "outputs": [],
      "execution_count": 5,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:41:45.281Z",
          "iopub.execute_input": "2022-08-08T19:41:45.289Z",
          "iopub.status.idle": "2022-08-08T19:41:45.301Z",
          "shell.execute_reply": "2022-08-08T19:41:45.308Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "rw.set_activation_map(initial_act)"
      ],
      "outputs": [],
      "execution_count": 6,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:41:45.979Z",
          "iopub.execute_input": "2022-08-08T19:41:46.005Z",
          "iopub.status.idle": "2022-08-08T19:41:46.016Z",
          "shell.execute_reply": "2022-08-08T19:41:46.023Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "TIME=2000\n",
        "for t in range(TIME):\n",
        "  rw.evolve_step()"
      ],
      "outputs": [],
      "execution_count": 9,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:41:48.383Z",
          "iopub.execute_input": "2022-08-08T19:41:48.390Z",
          "iopub.status.idle": "2022-08-08T19:41:50.279Z",
          "shell.execute_reply": "2022-08-08T19:41:50.286Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "plt.matshow(initial_act)\n",
        "plt.plot(rw.trajectory[:TIME,1], rw.trajectory[:TIME,0])\n",
        "plt.xlim(0,200)\n",
        "plt.ylim(0,200)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 11,
          "data": {
            "text/plain": "(0.0, 200.0)"
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 288x288 with 1 Axes>",
            "image/png": "[encoded data, 21396 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 11,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:42:08.182Z",
          "iopub.execute_input": "2022-08-08T19:42:08.191Z",
          "iopub.status.idle": "2022-08-08T19:42:08.277Z",
          "shell.execute_reply": "2022-08-08T19:42:08.285Z"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## in a mountain area"
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "grid_ii, grid_jj = np.mgrid[0:200, 0:200]\n",
        "my_i = np.sin(grid_ii/130) + np.sin(grid_ii/10)\n",
        "my_i /= my_i.max()\n",
        "my_j = np.sin(grid_jj/100) + np.sin(grid_jj/50)+ np.sin(grid_jj/10)\n",
        "my_j /= my_j.max()\n",
        "#grid_jj\n",
        "plt.matshow(my_j+my_i)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 56,
          "data": {
            "text/plain": "<matplotlib.image.AxesImage at 0x128660100>"
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 288x288 with 1 Axes>",
            "image/png": "[encoded data, 63288 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 56,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:52:23.771Z",
          "iopub.execute_input": "2022-08-08T19:52:23.779Z",
          "iopub.status.idle": "2022-08-08T19:52:23.896Z",
          "shell.execute_reply": "2022-08-08T19:52:23.919Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "rw = random_walk_model(parameters, seed=45)\n",
        "rw.set_activation_map(my_j+my_i)"
      ],
      "outputs": [],
      "execution_count": 68,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:34.095Z",
          "iopub.execute_input": "2022-08-08T19:53:34.103Z",
          "iopub.status.idle": "2022-08-08T19:53:34.116Z",
          "shell.execute_reply": "2022-08-08T19:53:34.126Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "TIME=2000\n",
        "for t in range(TIME):\n",
        "  rw.evolve_step()"
      ],
      "outputs": [],
      "execution_count": 69,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:34.295Z",
          "iopub.execute_input": "2022-08-08T19:53:34.304Z",
          "iopub.status.idle": "2022-08-08T19:53:36.480Z",
          "shell.execute_reply": "2022-08-08T19:53:36.517Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "plt.matshow(rw.activation_map)\n",
        "plt.plot(rw.trajectory[:TIME,1], rw.trajectory[:TIME,0])\n",
        "plt.xlim(0,200)\n",
        "plt.ylim(0,200)"
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 70,
          "data": {
            "text/plain": "(0.0, 200.0)"
          },
          "metadata": {}
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 288x288 with 1 Axes>",
            "image/png": "[encoded data, 77268 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ],
      "execution_count": 70,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:36.492Z",
          "iopub.execute_input": "2022-08-08T19:53:36.500Z",
          "iopub.status.idle": "2022-08-08T19:53:36.634Z",
          "shell.execute_reply": "2022-08-08T19:53:36.651Z"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Evaluating some data\n",
        "\n",
        "The parameters should still be recoverable even with background \"noise\""
      ],
      "metadata": {
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "rw_eval = random_walk_model(parameters)"
      ],
      "outputs": [],
      "execution_count": 71,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:51.309Z",
          "iopub.execute_input": "2022-08-08T19:53:51.320Z",
          "iopub.status.idle": "2022-08-08T19:53:51.335Z",
          "shell.execute_reply": "2022-08-08T19:53:51.346Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "TIME=2000\n",
        "lik = 0\n",
        "for t in range(TIME):\n",
        "  lik += rw_eval.evolve_step(sim=False, dat=rw.trajectory[t,:])\n",
        "lik\n",
        "  "
      ],
      "outputs": [
        {
          "output_type": "execute_result",
          "execution_count": 72,
          "data": {
            "text/plain": "-9306.592515719194"
          },
          "metadata": {}
        }
      ],
      "execution_count": 72,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:51.445Z",
          "iopub.execute_input": "2022-08-08T19:53:51.456Z",
          "iopub.status.idle": "2022-08-08T19:53:52.935Z",
          "shell.execute_reply": "2022-08-08T19:53:53.166Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "class gridsearch_dat():\n",
        "  def __init__(self, ps, actual, grid):\n",
        "    self.name = ps\n",
        "    self.actual = actual\n",
        "    self.grid = grid\n",
        "    self.res = None"
      ],
      "outputs": [],
      "execution_count": 73,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:52.952Z",
          "iopub.execute_input": "2022-08-08T19:53:52.962Z",
          "iopub.status.idle": "2022-08-08T19:53:52.976Z",
          "shell.execute_reply": "2022-08-08T19:53:53.172Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sigma_i = gridsearch_dat(\"sigma_i\", 2, [1.4,1.6,1.8,2, 2.2, 2.4])\n",
        "sigma_j = gridsearch_dat(\"sigma_j\", 3, [2.4,2.6,2.8,3, 3.2, 3.4])\n",
        "gs_list = [sigma_i, sigma_j]"
      ],
      "outputs": [],
      "execution_count": 74,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:52.990Z",
          "iopub.execute_input": "2022-08-08T19:53:52.999Z",
          "iopub.status.idle": "2022-08-08T19:53:53.013Z",
          "shell.execute_reply": "2022-08-08T19:53:53.177Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#just to be safe\n",
        "pdict = parameters.copy()"
      ],
      "outputs": [],
      "execution_count": 75,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:53.026Z",
          "iopub.execute_input": "2022-08-08T19:53:53.034Z",
          "iopub.status.idle": "2022-08-08T19:53:53.049Z",
          "shell.execute_reply": "2022-08-08T19:53:53.182Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sim_dat = rw.trajectory[:TIME,:]"
      ],
      "outputs": [],
      "execution_count": 76,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:53.062Z",
          "iopub.execute_input": "2022-08-08T19:53:53.071Z",
          "iopub.status.idle": "2022-08-08T19:53:53.085Z",
          "shell.execute_reply": "2022-08-08T19:53:53.188Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "sim_dat = np.expand_dims(sim_dat, axis=0)"
      ],
      "outputs": [],
      "execution_count": 77,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:53.098Z",
          "iopub.execute_input": "2022-08-08T19:53:53.108Z",
          "iopub.status.idle": "2022-08-08T19:53:53.122Z",
          "shell.execute_reply": "2022-08-08T19:53:53.194Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "for p in gs_list:\n",
        "    res = rec.grid_search(p.grid, p.name, sim_dat, pdict)\n",
        "    p.res = res"
      ],
      "outputs": [],
      "execution_count": 78,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:53:53.140Z",
          "iopub.execute_input": "2022-08-08T19:53:53.148Z",
          "shell.execute_reply": "2022-08-08T19:54:09.614Z",
          "iopub.status.idle": "2022-08-08T19:54:09.541Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def plot_gs(P):\n",
        "  fig, ax = plt.subplots(figsize= (35,10))\n",
        "  for tr in range(3):\n",
        "      ax.axvline(P.actual, c=\"red\", lw=5)\n",
        "      ax.plot(P.grid, P.res, \".-\", c=\"black\")\n",
        "      fpname = P.name\n",
        "      ax.set_title(f\"{fpname}\", fontsize=35)\n",
        "      ax.set_xlabel(f\"Parameter Value\", fontsize=20, color=\"grey\")\n",
        "      ax.set_ylabel(f\"Likelihood\", fontsize=20, color=\"grey\")\n",
        "      ax.tick_params(axis='both', labelsize=20,labelcolor=\"grey\", color=\"grey\")\n",
        "      ax.annotate(f\"actual={P.actual}\", (P.actual, np.median(P.res)), size=30, color=\"red\")\n",
        "  return fig, ax\n",
        "\n",
        "mpl.rcParams[\"axes.prop_cycle\"]=mpl.cycler(color=[\"slateblue\",\"rebeccapurple\",\"orchid\"])\n",
        "mpl.rc('axes',edgecolor='grey')"
      ],
      "outputs": [],
      "execution_count": 79,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:54:09.556Z",
          "iopub.execute_input": "2022-08-08T19:54:09.565Z",
          "iopub.status.idle": "2022-08-08T19:54:09.580Z",
          "shell.execute_reply": "2022-08-08T19:54:09.620Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [
        "with PdfPages('param_rec2.pdf') as pdf:\n",
        "    for P in gs_list:\n",
        "        print(P.name)\n",
        "        fig, ax = plot_gs(P)\n",
        "        pdf.savefig(fig)"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "sigma_i\n",
            "sigma_j\n"
          ]
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 2520x720 with 1 Axes>",
            "image/png": "[encoded data, 62648 characters]\n"
          },
          "metadata": {
            "needs_background": "dark"
          }
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": "<Figure size 2520x720 with 1 Axes>",
            "image/png": "[encoded data, 58424 characters]\n"
          },
          "metadata": {
            "needs_background": "dark"
          }
        }
      ],
      "execution_count": 80,
      "metadata": {
        "collapsed": false,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        },
        "execution": {
          "iopub.status.busy": "2022-08-08T19:54:09.595Z",
          "iopub.execute_input": "2022-08-08T19:54:09.605Z",
          "iopub.status.idle": "2022-08-08T19:54:10.184Z",
          "shell.execute_reply": "2022-08-08T19:54:10.201Z"
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    },
    {
      "cell_type": "code",
      "source": [],
      "outputs": [],
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "jupyter": {
          "source_hidden": false,
          "outputs_hidden": false
        },
        "nteract": {
          "transient": {
            "deleting": false
          }
        }
      }
    }
  ],
  "metadata": {
    "kernel_info": {
      "name": "sawenv"
    },
    "language_info": {
      "name": "python",
      "version": "3.10.3",
      "mimetype": "text/x-python",
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "pygments_lexer": "ipython3",
      "nbconvert_exporter": "python",
      "file_extension": ".py"
    },
    "kernelspec": {
      "argv": [
        "/Users/lisa/Documents/virtual_envs/sawenv/bin/python3",
        "-m",
        "ipykernel_launcher",
        "-f",
        "{connection_file}"
      ],
      "display_name": "sawenv",
      "language": "python",
      "metadata": {
        "debugger": true
      },
      "name": "sawenv"
    },
    "nteract": {
      "version": "0.28.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}