{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5cd9ffa9",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# The Art of Memory Loss\n",
    "### and the Art of Memory Loss and [...]\n",
    "#### by Dominic Zimmer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cb168bd",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div class=\"alert alert-info\">\n",
    "“Die Kunst des Gedächtnisverlustes beschäftigt sich damit, trotz großer Vergesslichkeit auf lange Sicht stets die Richtigen Antworten zu wissen.”\n",
    "    \n",
    "— *Irgendjemand, Saarbrücken 2023*\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5384b59e",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<div class=\"img-row\">\n",
    "<img src=\"https://cdn-icons-png.flaticon.com/256/2491/2491325.png\" />\n",
    "<img src=\"https://cdn-icons-png.flaticon.com/512/3997/3997691.png\"/>\n",
    "<img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/2008px-Google_%22G%22_Logo.svg.png\" />\n",
    "<img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/ChatGPT_logo.svg/1024px-ChatGPT_logo.svg.png\" />\n",
    "<img src=\"https://static.vecteezy.com/system/resources/previews/023/636/525/original/3d-rendering-radiation-icons-sign-caution-danger-symbol-free-png.png\" />\n",
    "<img src=\"https://thegraphicsfairy.com/wp-content/uploads/2016/07/Frog-Drawing-Green-3-GraphicsFairy.jpg\" />\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffc10d60",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Frösche, Wetten, Wetter und Wetterfrösche\n",
    "motivierendes bildchen"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab8003a9",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "TODO: 2-state beispiel\n",
    "\n",
    "```mermaid\n",
    "stateDiagram-v2\n",
    "    direction lr\n",
    "    l --> r: p\n",
    "    l --> l: 1-p\n",
    "    r --> r: 1-q\n",
    "    r --> l: q\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43e49851",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Frösche\n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "    \n",
    "- ein Frosch sitzt auf einer von zwei Seerosen $S = \\{l, r\\}$\n",
    "- mit $p$ hüpft er von $l$ nach $r$\n",
    "- mit $q$ hüpft er von $r$ nach $l$\n",
    "- mit $1-p$ bzw $1-q$ bleibt er sitzen\n",
    "- er beginnt auf $s_0 = l$\n",
    "    \n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25a3f2ca",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "    \n",
    "Wie oft ist der Frosch im Schnitt in $l$, wie oft in $r$?\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "feb11fcd",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Frösche Simulieren"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f8a3a4d9",
   "metadata": {
    "hideCode": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "uniform = np.random.random # Zufall in [0, 1]\n",
    "# Kein numpy? -> from random import random as uniform\n",
    "\n",
    "p = 0.6 # von l nach r\n",
    "q = 0.2 # von r nach l\n",
    "N = 100_000 # Simulationen\n",
    "\n",
    "# Los geht's!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "41017b8e",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100000/100000\n",
      "  0.2501 of the time at 0 (25015 total)\n",
      "  0.7499 of the time at 1 (74985 total)\n"
     ]
    }
   ],
   "source": [
    "visits = [0, 0]\n",
    "cur = 0\n",
    "for i in range(N):\n",
    "    if cur == 0:\n",
    "        if uniform() < p:\n",
    "            cur = 1\n",
    "            visits[1] += 1\n",
    "        else:\n",
    "            visits[0] += 1\n",
    "    elif cur == 1:\n",
    "        if uniform() < q:\n",
    "            cur = 0\n",
    "            visits[0] += 1\n",
    "        else:\n",
    "            visits[1] += 1\n",
    "n = sum(visits)\n",
    "print(f\"{n}/{N}\")\n",
    "print(f\"  {visits[0]/n:.4f} of the time at 0 ({visits[0]} total)\")\n",
    "print(f\"  {visits[1]/n:.4f} of the time at 1 ({visits[1]} total)\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca2c3139",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div class=\"alert alert-warning\">\n",
    "\n",
    "**Warum** ist der Frosch im Schnitt 25% in $l$ und 75% in $r$?\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3840a6f7",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [
     "hide_cell"
    ]
   },
   "source": [
    "## Frösche Analysieren\n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "    \n",
    "Die Wahrscheinlichkeiten für Wechsel sind\n",
    "    \n",
    "$P(l \\to r) = p$ und $P(r \\to l) = q$\n",
    "\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bf90648",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [
     "hide_cell"
    ]
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "\n",
    "Wie können wir $P(\\text{Frosch links})$ für große $t$ vorraussagen?\n",
    "    \n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57a24526",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [
     "hide_cell"
    ]
   },
   "source": [
    "\n",
    "| $t$ | $P(\\text{ Frosch links zu }t)$ | $P(\\text{ Frosch rechts zu }t)$ |\n",
    "| -------- | -------- | -------- |\n",
    "| $0$ | <span>$1$</span> | <span>$0$</span> |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74e64a9a",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Frösche Analysieren\n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "    \n",
    "Die Wahrscheinlichkeiten für Wechsel sind $P(l \\to r) = p$ und $P(r \\to l) = q$\n",
    "\n",
    "</div>\n",
    "\n",
    "<div class=\"alert alert-success\">\n",
    "\n",
    "Wie können wir $P(\\text{Frosch links})$ für große $t$ vorraussagen?\n",
    "    \n",
    "</div>\n",
    "\n",
    "| $t$ | $P(\\text{ Frosch links zu }t)$ | $P(\\text{ Frosch rechts zu }t)$ |\n",
    "| -------- | -------- | -------- |\n",
    "| $0$ | <span>$1$</span> | <span>$0$</span> |\n",
    "| <span>$1$</span> | <span>$1-p$</span> | <span>$p$</span> |\n",
    "| <span>$2$</span> | <span>$(1-p)^2 + pq$</span> | <span>$p(1-p) + p(1-q)$</span>|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bce42c67",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    },
    "tags": [
     "hide_cell"
    ]
   },
   "source": [
    "<div class=\"alert alert-info\">\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "P_{t+1}(\\text{Frosch links}) &= P_t(\\text{Frosch links}) \\cdot P(l \\to l) \\\\\n",
    "& + P_t(\\text{Frosch rechts}) \\cdot P(r \\to l)\\\\\n",
    "P_{t+1}(\\text{Frosch rechts}) &= P_t(\\text{Frosch rechts}) \\cdot P(r \\to r) \\\\\n",
    "&+ P_t(\\text{Frosch links}) \\cdot P(l \\to r)\n",
    "\\end{align*}\n",
    "$$\n",
    "    \n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d33a0be9",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    },
    "tags": [
     "hide_cell"
    ]
   },
   "source": [
    "## Frösche Analysieren\n",
    "\n",
    "Unser Experiment zeigt für große $t$:\n",
    "$$\n",
    "    P_{t-2}(\\text{Frosch links}) \\approx P_{t-1}(\\text{Frosch links}) \\approx P_{t}(\\text{Frosch links}) \\approx \\dots L\n",
    "$$\n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "P_{t+1}(\\text{Frosch links}) &= P_t(\\text{Frosch links}) \\cdot P(l \\to l) \\\\\n",
    "& + P_t(\\text{Frosch rechts}) \\cdot P(r \\to l)\\\\\n",
    "P_{t+1}(\\text{Frosch rechts}) &= P_t(\\text{Frosch rechts}) \\cdot P(r \\to r) \\\\\n",
    "&+ P_t(\\text{Frosch links}) \\cdot P(l \\to r)\n",
    "\\end{align*}\n",
    "$$\n",
    "    \n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85a20830",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Frösche Analysieren\n",
    "\n",
    "Unser Experiment zeigt für große $t$:\n",
    "$$\n",
    "    P_{t-2}(\\text{Frosch links}) \\approx P_{t-1}(\\text{Frosch links}) \\approx P_{t}(\\text{Frosch links}) \\approx \\dots L\n",
    "$$\n",
    "<div class=\"alert alert-info\">\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "L &= L \\cdot P(l \\to l) + R \\cdot P(r \\to l)\\\\\n",
    "R &= R \\cdot P(r \\to r) + L \\cdot P(l \\to r)\\\\\n",
    "1 &= L + R\n",
    "\\end{align*}\n",
    "$$\n",
    "    \n",
    "</div>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfa9063c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "$$\n",
    "\\begin{align*}\n",
    "L &= \\frac{q}{p+q} = 25\\% \\\\\n",
    "R &= \\frac{p}{p+q} = 75\\%\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74f18541",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Mathematik und Gedächtnisverlust"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6af0597d",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div class=\"alert alert-info\">\n",
    "    \n",
    "**Definition** *(Gedächtnislosigkeit)*\n",
    "\n",
    "Ein Prozess ist *gedächtnislos*, wenn zu jedem Zeitpunkt seine Entscheidung nur vom aktuellen Zustand abhängt, nicht aber von dem Weg, wie der aktuelle Zustand erreicht wurde.\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4507aa2",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div class=\"alert alert-success\">\n",
    "    \n",
    "**Beobachtung**\n",
    "\n",
    "Das Verhalten der Frösche ist gedächtnislos. Die Aktionen des Frosches und deren Wahrscheinlichkeiten $p$ und $1-p$ (bzw $q$ und $1-q$) hängen nur von der aktuellen Seerose ab. \n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90451df9",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Frösche — Formell"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40c3b0b5",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "> Der Frosch kann auf der linken oder rechten Seerose sitzen.\n",
    "\n",
    "Wir modellieren die beiden Seerosen als die zwei Zustände $S = \\{L, R\\}$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a034c732",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "> Zufällig hüpft er zwischen den Seerosen hin und her\n",
    "\n",
    "Die Zufallsvariable $X_t$ beschreibt wo der Frosch zum Zeitpunkt $t$ ist."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47b022ef",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Wir könnten zum Beispiel beobachten, dass\n",
    "\n",
    "$$\n",
    "\\dots \\quad X_3 = L \\quad X_4 = L \\quad X_5 = R \\quad X_6 = L \\quad \\dots\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bea4ee3",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "> Am Anfang sitzt der Frosch auf der linken Seerose.\n",
    "\n",
    "Die Wahrscheinlichkeit $P(X_0 = L) = 1$.\n",
    "\n",
    "Analog ergibt sich $P(X_0 = R) = 0$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12dde881",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "> Der Frosch hüpft mit Wahrscheinlichkeit $p$ von links nach rechts\n",
    "\n",
    "Die Übergangswahrscheinlichkeit, dass der Frosch zum Zeitpunkt $t-1$ in $L$, und zum Zeitpunkt $t$ in $R$ ist, ist $p$.\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "&\\phantom{==}\\color{gray}{P(X_t = R \\mid X_{t-1} = L, X_{t-2} = L)}\\\\ \n",
    "&=\\color{gray}{P(X_t = R \\mid X_{t-1} = L, X_{t-2} = R)}\\\\ \n",
    "&= P(X_t = R \\mid X_{t-1} = L) \\\\&= p\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ded753c",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Da die Übergangswahrscheinlichkeiten zum Zeitpunkt $t$ nur von $X_t$ abhängen, schreiben wir kurzerhand\n",
    "\n",
    "$$\n",
    "P(X_t = R | X_{t-1} = L) = P(L \\to R) = m_{L R}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c8c91ad",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Wir können alle Übergangswahrscheinlichkeiten $P(s \\to s')$ als Matrix $M$ notieren:\n",
    "\n",
    "$$\n",
    "M = \n",
    "\\begin{pmatrix} m_{LL} & m_{LR} \\\\ m_{RL} & m_{RR}\n",
    "\\end{pmatrix} = \n",
    "\\begin{pmatrix} 1 - p & p \\\\ q & 1-q\n",
    "\\end{pmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4287f203",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Wir schreiben die Wahrscheinlichkeiten, dass der Frosch zum Zeitpunkt $t$ in $L$ oder $R$ ist, als Vektoren.\n",
    "\n",
    "$$\n",
    "X_0 = \\begin{pmatrix}1 \\\\ 0\\end{pmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25a506f5",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "Ein Zeitschritt $t=0 \\to t=1$ verändert die Verteilung gemäß $M$:\n",
    "\n",
    "$$\n",
    "X_0 = \\begin{pmatrix}1 \\\\ 0\\end{pmatrix} \\to \\begin{pmatrix}1-p \\\\ p \\end{pmatrix} = X_1\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d56b780",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Matrixmultiplikation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5db5186d",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Den Folgezustand von $X = \\begin{pmatrix}L & R\\end{pmatrix}$ mit Übergangsmatrix $M$ berechnen wir als\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "\\begin{pmatrix}L' \\\\ R'\\end{pmatrix}^T &= \\begin{pmatrix}L & R\\end{pmatrix} \\cdot M \\\\\n",
    "&= \\begin{pmatrix}L & R\\end{pmatrix} \\cdot \\begin{pmatrix} \\color{green}{1 - p} & \\color{orange}{p} \\\\ \\color{green}{q} & \\color{orange}{1-q} \\end{pmatrix} \\\\\n",
    "=& \\begin{pmatrix}L \\cdot \\color{green}{(1 - p)} + R \\cdot \\color{green}{q} \\\\\n",
    "L \\cdot \\color{orange}{p} + R \\cdot \\color{orange}{(1-q)} \n",
    "\\end{pmatrix}^T\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "782d4f7a",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "> Man berechnet die neuen Einträge als \"Zeile mal Spalte\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e9f7e2f6",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "from sympy import symbols as S, Matrix as Mat\n",
    "p, q = S(\"p q\")\n",
    "M = Mat([[1-p, p], [q, 1-q]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daddfd1e",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Beispiele\n",
    "\n",
    "$$\n",
    "\\begin{pmatrix}1 & 0\\end{pmatrix}\\cdot M = \\begin{pmatrix}1-p & p\\end{pmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1a6597e",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "$$\n",
    "\\begin{align*}\n",
    "\\begin{pmatrix}1 \\\\ 1\\end{pmatrix}^T\\cdot M &= \\begin{pmatrix} (1-p) + q \\\\ p + (1-q)\\end{pmatrix}^T \\\\\n",
    "& = \\begin{pmatrix} (1-p) + q \\\\ p +(1-q)\\end{pmatrix}^T \\\\\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aa2e9aca",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}1 - p & p\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([[1 - p, p]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v1 = Mat([[1, 0]])\n",
    "v1 * M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b31deb83",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\left[\\begin{matrix}- p + q + 1 & p - q + 1\\end{matrix}\\right]$"
      ],
      "text/plain": [
       "Matrix([[-p + q + 1, p - q + 1]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v2 = Mat([[1, 1]])\n",
    "v2 * M"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a0fa546",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Frösche brauchen Zufall\n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "\n",
    "Viele Leute werfen Münzen in den Teich der Frösche.\n",
    "Wir sollen einen Frosch aussuchen, der der neue Schatzmeister werden soll. \n",
    "Wie können wir aus den 7 Fröschen **fair** einen auswählen?\n",
    "    \n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8c72431",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "**Bedingungen**:\n",
    "- Jeder Frosch wird mit $\\frac{1}{7}$ ausgewählt\n",
    "- Zufall nur durch die Münzen"
   ]
  },
  {
   "attachments": {
    "image-3.png": {
     "image/png": "[encoded data, 116464 characters]"
    }
   },
   "cell_type": "markdown",
   "id": "e58156b3",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "![image-3.png](attachment:image-3.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "88435c94",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.5, 0.5, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,\n",
       "       0. ])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy import array\n",
    "from numpy.linalg import matrix_power\n",
    "\n",
    "M = array([\n",
    "[ 0.0 , 0.5 , 0.5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.5 , 0.5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5 , 0.5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5 , 0.5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5 , 0.5 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5 , 0.5 , 0.0], \n",
    "[ 0.5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 , 0.0], \n",
    "[ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0]\n",
    "])\n",
    "v = array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ])\n",
    "v @ M\n",
    "# v @ matrix_power(M, 3*1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "418ac9c9",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Markovketten\n",
    "\n",
    "Die Prozesse, die wir bisher gesehen haben lassen sich als *Markovketten* verstehen. Die Ketten, die wir betrachten teilen die folgenden Eigenschaften:\n",
    "\n",
    "- Zeit vergeht für unsere Prozesse $X_1, X_2, \\dots$ **diskret**\n",
    "- Wir können die Übergangswahrscheinlichkeiten in einer $n \\times n$ Matrix $M$ auflisten\n",
    "\n",
    "Außerdem ist $M$ eine *stochastische Matrix* und erfüllt\n",
    "\n",
    "- jeder Eintrag $m_{ij}$ ist eine Zahl von 0 bis 1\n",
    "- jede Zeile summiert sich zu $1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1aa7ccc8",
   "metadata": {
    "hideCode": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 57772 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "import networkx as nx\n",
    "\n",
    "seed = 13649  # Seed random number generators for reproducibility\n",
    "G = nx.random_k_out_graph(10, 3, 0.5, seed=seed)\n",
    "pos = nx.spring_layout(G, seed=seed)\n",
    "\n",
    "node_sizes = [3 + 10 * i for i in range(len(G))]\n",
    "M = G.number_of_edges()\n",
    "edge_colors = range(2, M + 2)\n",
    "edge_alphas = [(5 + i) / (M + 4) for i in range(M)]\n",
    "cmap = plt.cm.plasma\n",
    "\n",
    "nodes = nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color=\"indigo\")\n",
    "edges = nx.draw_networkx_edges(\n",
    "    G,\n",
    "    pos,\n",
    "    node_size=node_sizes,\n",
    "    arrowstyle=\"->\",\n",
    "    arrowsize=10,\n",
    "    edge_color=edge_colors,\n",
    "    edge_cmap=cmap,\n",
    "    width=2,\n",
    ") \n",
    "# set alpha value for each edge\n",
    "for i in range(M):\n",
    "    edges[i].set_alpha(edge_alphas[i])\n",
    "\n",
    "pc = mpl.collections.PatchCollection(edges, cmap=cmap)\n",
    "pc.set_array(edge_colors)\n",
    "\n",
    "ax = plt.gca()\n",
    "ax.set_axis_off()\n",
    "plt.colorbar(pc, ax=ax)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
