{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import math\n",
    "from matplotlib import pyplot as plt\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In a LED display each number corresponds to a pattern of on(1) or off(0) for\n",
    "the 7 different elements that compose the display.\n",
    "For instance,  the patterns for numbers 2 and 3 are:\n",
    "\n",
    " $$\\mathbf{c}(2) = (+1,-1,+1,+1,+1,-1,+1)$$\n",
    "\n",
    " $$\\mathbf{c}(3) = (+1,-1,+1,+1,-1,+1,+1)$$\n",
    "\n",
    "Imagine you have a LED display that is not working properly. This\n",
    "defective LED is such that, for a given number the LED wants to display:\n",
    "\n",
    " \n",
    " * Elements adopt their correct state with probability $1-f$.\n",
    "\n",
    " * Elements adopt the incorrect state with probability $f$,\n",
    " \n",
    "\n",
    "**The LED is allowed to display ONLY a number \"2\" or a number \"3\"**.\n",
    "\n",
    "And it does so by emitting a pattern\n",
    "\n",
    "$$\\mathbf{p}=(p1,p2,p3,p4,p5,p6,p7),$$ \n",
    "where $p_i = +1 or -1$\n",
    "\n",
    "Calculate the posterior probability that the intended number was a \"2\", given the pattern $\\mathbf{p}$ you observe in the LED, that is, \n",
    "\n",
    "$$\n",
    "P(n=2\\mid \\mathbf{p}).\n",
    "$$\n",
    "\n",
    "\n",
    "Show that you can express that posterior probability as a logistic function,\n",
    "\n",
    "$$\n",
    "P(n=2\\mid \\mathbf{p}) = \\frac{1}{1+e^{-(\\mathbf{w}\\mathbf{p} + w_0)}}\n",
    "$$\n",
    "\n",
    "for some weights $\\mathbf{w}$, and some constant $w_0$.\n",
    "\n",
    "You can assume that the prior probabilities for either number, $p(2)$ and $p(3)$, are given.\n",
    "\n",
    "*Hint*: $x^y = e^{y\\log x}$ for any two real numbers $x, y$.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**The solution**\n",
    "\n",
    "$$ \n",
    "P(2\\mid \\mathbf{p}) = \\frac{1}{1 - e^{-(\\mathbf{w}\\,\\mathbf{p} + w_0)}},\n",
    "$$\n",
    "\n",
    "with weights,\n",
    "\n",
    "$$\\begin{aligned}\n",
    "\\mathbf{w}\n",
    "&=\\frac{1}{2}\\,\\log \\frac{1-f}{f}\\left[\\mathbf{c_2} - \\mathbf{c_3}\\right]\\\\\n",
    "\\end{aligned}\n",
    "$$\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1 -1  1  1  1 -1  1]\n",
      "1\n",
      "dict_keys(['2', 3])\n",
      "(7,)\n"
     ]
    }
   ],
   "source": [
    "# LED segment\n",
    "#\n",
    "#      1\n",
    "#    -----\n",
    "#   |     |\n",
    "#  2|     | 3\n",
    "#   |  4  |\n",
    "#    -----\n",
    "#   |     |\n",
    "#  5|     | 6\n",
    "#   |  7  |\n",
    "#    -----\n",
    "#\n",
    "import numpy as np\n",
    "import math\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "# dictionary (keys, values)\n",
    "number = {\n",
    "    \"2\": np.array([ 1, -1, 1,  1,  1, -1,  1]),\n",
    "    3:   np.array([ 1, -1, 1,  1, -1,  1,  1]),\n",
    "}\n",
    "\n",
    "print(number[\"2\"])\n",
    "print(number[3][0])\n",
    "print(number.keys())\n",
    "\n",
    "# correct form to see the shape of a dictionary entry\n",
    "print(number[3].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.01 post2 0.5 post3 0.5 sum 1.0\n",
      "0.02 post2 0.5 post3 0.5 sum 1.0\n",
      "0.03 post2 0.5 post3 0.5 sum 1.0\n",
      "0.04 post2 0.5 post3 0.5 sum 1.0\n",
      "0.05 post2 0.5 post3 0.5 sum 1.0\n",
      "0.060000000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.07 post2 0.5 post3 0.5 sum 1.0\n",
      "0.08 post2 0.5 post3 0.5 sum 1.0\n",
      "0.09 post2 0.5 post3 0.5 sum 1.0\n",
      "0.09999999999999999 post2 0.5 post3 0.5 sum 1.0\n",
      "0.10999999999999999 post2 0.5 post3 0.5 sum 1.0\n",
      "0.11999999999999998 post2 0.5 post3 0.5 sum 1.0\n",
      "0.12999999999999998 post2 0.5 post3 0.5 sum 1.0\n",
      "0.13999999999999999 post2 0.5 post3 0.5 sum 1.0\n",
      "0.15 post2 0.5 post3 0.5 sum 1.0\n",
      "0.16 post2 0.5 post3 0.5 sum 1.0\n",
      "0.17 post2 0.5 post3 0.5 sum 1.0\n",
      "0.18000000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.19000000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.20000000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.21000000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.22000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.23000000000000007 post2 0.5 post3 0.5 sum 1.0\n",
      "0.24000000000000007 post2 0.5 post3 0.5 sum 1.0\n",
      "0.25000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.26000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.2700000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.2800000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.2900000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3000000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3100000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3200000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3300000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.34000000000000014 post2 0.5 post3 0.5 sum 1.0\n",
      "0.35000000000000014 post2 0.5 post3 0.5 sum 1.0\n",
      "0.36000000000000015 post2 0.5 post3 0.5 sum 1.0\n",
      "0.37000000000000016 post2 0.5 post3 0.5 sum 1.0\n",
      "0.38000000000000017 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3900000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4000000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4100000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4200000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4300000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4400000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.45000000000000023 post2 0.5 post3 0.5 sum 1.0\n",
      "0.46000000000000024 post2 0.5 post3 0.5 sum 1.0\n",
      "0.47000000000000025 post2 0.5 post3 0.5 sum 1.0\n",
      "0.48000000000000026 post2 0.5 post3 0.5 sum 1.0\n",
      "0.49000000000000027 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5000000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5100000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5200000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5300000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5400000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5500000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5600000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5700000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5800000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5900000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6000000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6100000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6200000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6300000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6400000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6500000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6600000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6700000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6800000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6900000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7000000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7100000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7200000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7300000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7400000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7500000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7600000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7700000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7800000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7900000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8000000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8100000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8200000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8300000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8400000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8500000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8600000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8700000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8800000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8900000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9100000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9200000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9300000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9400000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9500000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9600000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9700000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9800000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9900000000000007 post2 0.5 post3 0.5 sum 1.0\n"
     ]
    }
   ],
   "source": [
    "# The linear logistic activation function\n",
    "\n",
    "# if the variables are vectors it does the escalar product\n",
    "# (as it is the case here)\n",
    "# inputs  x[D]\n",
    "# weights w[D]\n",
    "# returns y[1]\n",
    "#\n",
    "# if the variables are matrice, it does the matrix multiplication\n",
    "# inputs  x[N,I]\n",
    "# weights w[I,O]\n",
    "# returns y[N,O]\n",
    "def activity(x,W):\n",
    "    xW = np.dot(x,W)\n",
    "    y = 1/(1+np.exp(-xW))\n",
    "    return y\n",
    "\n",
    "# the definition of the posterior for the number 2\n",
    "def posterior_number2(number, pattern, f):\n",
    "    post2 = 0\n",
    "    \n",
    "    w = 0.5 * np.log((1-f)/f) * (number['2'] - number[3])\n",
    "    \n",
    "    post2 = activity(pattern,w)\n",
    "    \n",
    "    return post2;\n",
    "\n",
    "# the definition of the posterior for number 3\n",
    "def posterior_number3(number, pattern, f):\n",
    "    post3 = 0\n",
    "    \n",
    "    w = 0.5 * np.log((1-f)/f) * (number[3] - number['2'])\n",
    "    \n",
    "    post3 = activation(pattern,w)\n",
    "    \n",
    "    return post3;\n",
    "\n",
    "\n",
    "# test an all-off pattern\n",
    "#\n",
    "# The all-off pattern fails similarly for both numbers,\n",
    "# and the posteriors are the same for all values of f\n",
    "pattern = np.array([-1,-1,-1,-1,-1,-1,-1,])\n",
    "\n",
    "f = 0.01\n",
    "while (f < 1):\n",
    "    post2 = posterior_number2(number, pattern, f)\n",
    "    post3 = posterior_number3(number, pattern, f)\n",
    "    \n",
    "    # we can check that the sum of the two is 1.    \n",
    "    print(f\"{f} post2 {post2} post3 {post3} sum {post2+post3}\")\n",
    "    f += 0.01\n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.01 post2 0.5 post3 0.5 sum 1.0\n",
      "0.02 post2 0.5 post3 0.5 sum 1.0\n",
      "0.03 post2 0.5 post3 0.5 sum 1.0\n",
      "0.04 post2 0.5 post3 0.5 sum 1.0\n",
      "0.05 post2 0.5 post3 0.5 sum 1.0\n",
      "0.060000000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.07 post2 0.5 post3 0.5 sum 1.0\n",
      "0.08 post2 0.5 post3 0.5 sum 1.0\n",
      "0.09 post2 0.5 post3 0.5 sum 1.0\n",
      "0.09999999999999999 post2 0.5 post3 0.5 sum 1.0\n",
      "0.10999999999999999 post2 0.5 post3 0.5 sum 1.0\n",
      "0.11999999999999998 post2 0.5 post3 0.5 sum 1.0\n",
      "0.12999999999999998 post2 0.5 post3 0.5 sum 1.0\n",
      "0.13999999999999999 post2 0.5 post3 0.5 sum 1.0\n",
      "0.15 post2 0.5 post3 0.5 sum 1.0\n",
      "0.16 post2 0.5 post3 0.5 sum 1.0\n",
      "0.17 post2 0.5 post3 0.5 sum 1.0\n",
      "0.18000000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.19000000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.20000000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.21000000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.22000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.23000000000000007 post2 0.5 post3 0.5 sum 1.0\n",
      "0.24000000000000007 post2 0.5 post3 0.5 sum 1.0\n",
      "0.25000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.26000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.2700000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.2800000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.2900000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3000000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3100000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3200000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3300000000000001 post2 0.5 post3 0.5 sum 1.0\n",
      "0.34000000000000014 post2 0.5 post3 0.5 sum 1.0\n",
      "0.35000000000000014 post2 0.5 post3 0.5 sum 1.0\n",
      "0.36000000000000015 post2 0.5 post3 0.5 sum 1.0\n",
      "0.37000000000000016 post2 0.5 post3 0.5 sum 1.0\n",
      "0.38000000000000017 post2 0.5 post3 0.5 sum 1.0\n",
      "0.3900000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4000000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4100000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4200000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4300000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.4400000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.45000000000000023 post2 0.5 post3 0.5 sum 1.0\n",
      "0.46000000000000024 post2 0.5 post3 0.5 sum 1.0\n",
      "0.47000000000000025 post2 0.5 post3 0.5 sum 1.0\n",
      "0.48000000000000026 post2 0.5 post3 0.5 sum 1.0\n",
      "0.49000000000000027 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5000000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5100000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5200000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5300000000000002 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5400000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5500000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5600000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5700000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5800000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.5900000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6000000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6100000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6200000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6300000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6400000000000003 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6500000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6600000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6700000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6800000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.6900000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7000000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7100000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7200000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7300000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7400000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7500000000000004 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7600000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7700000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7800000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.7900000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8000000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8100000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8200000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8300000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8400000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8500000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8600000000000005 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8700000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8800000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.8900000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9000000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9100000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9200000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9300000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9400000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9500000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9600000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9700000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9800000000000006 post2 0.5 post3 0.5 sum 1.0\n",
      "0.9900000000000007 post2 0.5 post3 0.5 sum 1.0\n"
     ]
    }
   ],
   "source": [
    "# test an all-on pattern\n",
    "#\n",
    "# The all-on pattern fails similarly for both numbers,\n",
    "# and the posteriors are the same for all values of f\n",
    "\n",
    "pattern = np.array([1,1,1,1,1,1,1,])\n",
    "\n",
    "f = 0.01\n",
    "while (f < 1):\n",
    "    post2 = posterior_number2(number, pattern, f)\n",
    "    post3 = posterior_number3(number, pattern, f)\n",
    "    \n",
    "    # we can check that the sum of the two is 1.    \n",
    "    print(f\"{f} post2 {post2} post3 {post3} sum {post2+post3}\")\n",
    "\n",
    "    f += 0.01\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.01 post2 0.00010201999591920018 post3 0.9998979800040808\n",
      "0.02 post2 0.0004163197335553707 post3 0.9995836802664446\n",
      "0.03 post2 0.0009556169038012314 post3 0.9990443830961988\n",
      "0.04 post2 0.0017331022530329286 post3 0.998266897746967\n",
      "0.05 post2 0.0027624309392265205 post3 0.9972375690607735\n",
      "0.060000000000000005 post2 0.0040577096483318306 post3 0.9959422903516683\n",
      "0.07 post2 0.005633478960680619 post3 0.9943665210393193\n",
      "0.08 post2 0.0075046904315197015 post3 0.9924953095684802\n",
      "0.09 post2 0.00968667782827075 post3 0.9903133221717292\n",
      "0.09999999999999999 post2 0.012195121951219507 post3 0.9878048780487805\n",
      "0.10999999999999999 post2 0.015046008455608054 post3 0.984953991544392\n",
      "0.11999999999999998 post2 0.018255578093306284 post3 0.9817444219066936\n",
      "0.12999999999999998 post2 0.02184026880330834 post3 0.9781597311966916\n",
      "0.13999999999999999 post2 0.025816649104320334 post3 0.9741833508956798\n",
      "0.15 post2 0.030201342281879193 post3 0.9697986577181208\n",
      "0.16 post2 0.0350109409190372 post3 0.9649890590809629\n",
      "0.17 post2 0.040261911395932035 post3 0.9597380886040681\n",
      "0.18000000000000002 post2 0.04597048808172534 post3 0.9540295119182746\n",
      "0.19000000000000003 post2 0.05215255706443228 post3 0.9478474429355677\n",
      "0.20000000000000004 post2 0.05882352941176473 post3 0.9411764705882353\n",
      "0.21000000000000005 post2 0.06599820413049991 post3 0.9340017958695002\n",
      "0.22000000000000006 post2 0.07369062119366633 post3 0.9263093788063337\n",
      "0.23000000000000007 post2 0.08191390523381858 post3 0.9180860947661815\n",
      "0.24000000000000007 post2 0.09068010075566757 post3 0.9093198992443323\n",
      "0.25000000000000006 post2 0.10000000000000002 post3 0.8999999999999999\n",
      "0.26000000000000006 post2 0.10988296488946689 post3 0.8901170351105331\n",
      "0.2700000000000001 post2 0.1203367448002642 post3 0.8796632551997359\n",
      "0.2800000000000001 post2 0.13136729222520113 post3 0.868632707774799\n",
      "0.2900000000000001 post2 0.142978578714723 post3 0.8570214212852769\n",
      "0.3000000000000001 post2 0.15517241379310354 post3 0.8448275862068966\n",
      "0.3100000000000001 post2 0.16794826983572192 post3 0.832051730164278\n",
      "0.3200000000000001 post2 0.1813031161473089 post3 0.818696883852691\n",
      "0.3300000000000001 post2 0.19523126568662616 post3 0.8047687343133738\n",
      "0.34000000000000014 post2 0.209724238026125 post3 0.7902757619738751\n",
      "0.35000000000000014 post2 0.2247706422018351 post3 0.7752293577981649\n",
      "0.36000000000000015 post2 0.24035608308605363 post3 0.7596439169139463\n",
      "0.37000000000000016 post2 0.25646309479205714 post3 0.7435369052079428\n",
      "0.38000000000000017 post2 0.27307110438729226 post3 0.7269288956127077\n",
      "0.3900000000000002 post2 0.290156428843953 post3 0.709843571156047\n",
      "0.4000000000000002 post2 0.307692307692308 post3 0.692307692307692\n",
      "0.4100000000000002 post2 0.3256489732661762 post3 0.6743510267338239\n",
      "0.4200000000000002 post2 0.3439937597503904 post3 0.6560062402496096\n",
      "0.4300000000000002 post2 0.3626912514711655 post3 0.6373087485288346\n",
      "0.4400000000000002 post2 0.38170347003154614 post3 0.6182965299684539\n",
      "0.45000000000000023 post2 0.40099009900990135 post3 0.5990099009900987\n",
      "0.46000000000000024 post2 0.42050874403815625 post3 0.5794912559618438\n",
      "0.47000000000000025 post2 0.4402152251893189 post3 0.5597847748106811\n",
      "0.48000000000000026 post2 0.46006389776357876 post3 0.5399361022364213\n",
      "0.49000000000000027 post2 0.4800079968012801 post3 0.51999200319872\n",
      "0.5000000000000002 post2 0.5000000000000004 post3 0.49999999999999956\n",
      "0.5100000000000002 post2 0.519992003198721 post3 0.48000799680127904\n",
      "0.5200000000000002 post2 0.5399361022364222 post3 0.4600638977635778\n",
      "0.5300000000000002 post2 0.559784774810682 post3 0.44021522518931805\n",
      "0.5400000000000003 post2 0.5794912559618447 post3 0.4205087440381553\n",
      "0.5500000000000003 post2 0.5990099009900995 post3 0.4009900990099005\n",
      "0.5600000000000003 post2 0.6182965299684547 post3 0.38170347003154526\n",
      "0.5700000000000003 post2 0.6373087485288353 post3 0.36269125147116466\n",
      "0.5800000000000003 post2 0.6560062402496105 post3 0.3439937597503895\n",
      "0.5900000000000003 post2 0.6743510267338246 post3 0.32564897326617537\n",
      "0.6000000000000003 post2 0.6923076923076928 post3 0.3076923076923072\n",
      "0.6100000000000003 post2 0.7098435711560478 post3 0.29015642884395215\n",
      "0.6200000000000003 post2 0.7269288956127087 post3 0.27307110438729143\n",
      "0.6300000000000003 post2 0.7435369052079437 post3 0.2564630947920564\n",
      "0.6400000000000003 post2 0.7596439169139471 post3 0.2403560830860528\n",
      "0.6500000000000004 post2 0.7752293577981657 post3 0.22477064220183438\n",
      "0.6600000000000004 post2 0.7902757619738757 post3 0.20972423802612428\n",
      "0.6700000000000004 post2 0.8047687343133745 post3 0.1952312656866255\n",
      "0.6800000000000004 post2 0.8186968838526918 post3 0.18130311614730824\n",
      "0.6900000000000004 post2 0.8320517301642788 post3 0.16794826983572128\n",
      "0.7000000000000004 post2 0.844827586206897 post3 0.15517241379310298\n",
      "0.7100000000000004 post2 0.8570214212852776 post3 0.1429785787147224\n",
      "0.7200000000000004 post2 0.8686327077747994 post3 0.1313672922252006\n",
      "0.7300000000000004 post2 0.8796632551997362 post3 0.12033674480026366\n",
      "0.7400000000000004 post2 0.8901170351105335 post3 0.10988296488946638\n",
      "0.7500000000000004 post2 0.9000000000000005 post3 0.09999999999999959\n",
      "0.7600000000000005 post2 0.9093198992443329 post3 0.09068010075566708\n",
      "0.7700000000000005 post2 0.9180860947661819 post3 0.0819139052338181\n",
      "0.7800000000000005 post2 0.9263093788063342 post3 0.07369062119366587\n",
      "0.7900000000000005 post2 0.9340017958695006 post3 0.06599820413049949\n",
      "0.8000000000000005 post2 0.9411764705882357 post3 0.058823529411764365\n",
      "0.8100000000000005 post2 0.9478474429355681 post3 0.05215255706443192\n",
      "0.8200000000000005 post2 0.954029511918275 post3 0.045970488081725006\n",
      "0.8300000000000005 post2 0.9597380886040683 post3 0.040261911395931736\n",
      "0.8400000000000005 post2 0.964989059080963 post3 0.03501094091903694\n",
      "0.8500000000000005 post2 0.969798657718121 post3 0.030201342281878946\n",
      "0.8600000000000005 post2 0.9741833508956799 post3 0.025816649104320112\n",
      "0.8700000000000006 post2 0.9781597311966919 post3 0.021840268803308138\n",
      "0.8800000000000006 post2 0.9817444219066939 post3 0.018255578093306093\n",
      "0.8900000000000006 post2 0.9849539915443921 post3 0.015046008455607884\n",
      "0.9000000000000006 post2 0.9878048780487807 post3 0.012195121951219358\n",
      "0.9100000000000006 post2 0.9903133221717294 post3 0.009686677828270613\n",
      "0.9200000000000006 post2 0.9924953095684804 post3 0.007504690431519583\n",
      "0.9300000000000006 post2 0.9943665210393196 post3 0.00563347896068051\n",
      "0.9400000000000006 post2 0.9959422903516683 post3 0.004057709648331741\n",
      "0.9500000000000006 post2 0.9972375690607735 post3 0.002762430939226447\n",
      "0.9600000000000006 post2 0.998266897746967 post3 0.0017331022530328716\n",
      "0.9700000000000006 post2 0.9990443830961988 post3 0.0009556169038011899\n",
      "0.9800000000000006 post2 0.9995836802664446 post3 0.00041631973355534294\n",
      "0.9900000000000007 post2 0.9998979800040808 post3 0.00010201999591918661\n"
     ]
    }
   ],
   "source": [
    "# test a pattern affecting segments (5 and 6)\n",
    "# that make 2 and 3 different:\n",
    "#\n",
    "# 2 = [ 1, -1, 1,  1,  1, -1,  1]\n",
    "# 3 = [ 1, -1, 1,  1, -1,  1,  1]\n",
    "#\n",
    "# the pattern is 3, and that's what it says for f small,\n",
    "# and as f grows to total switch, then it becomes 2.\n",
    "#\n",
    "pattern = np.array([1,-1,1,1,-1,+1,1])\n",
    "\n",
    "f = 0.01\n",
    "while (f < 1):\n",
    "    post2 = posterior_number2(number, pattern, f)\n",
    "    post3 = posterior_number3(number, pattern, f)\n",
    "    \n",
    "    # we can check that the sum of the two is 1.    \n",
    "    print(f\"{f} post2 {post2} post3 {post3} sum {pos2+pos3}\")\n",
    "\n",
    "    f += 0.01\n",
    "    \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
