diff --git a/jupyter/The Art of Memory Loss.html b/jupyter/The Art of Memory Loss.html index 85345fc..0a151ee 100644 --- a/jupyter/The Art of Memory Loss.html +++ b/jupyter/The Art of Memory Loss.html @@ -15534,9 +15534,9 @@ $$
Da die Übergangswahrscheinlichkeiten zum Zeitpunkt $t$ nur von $X_t$ abhängen, schreiben wir kurzerhand
+Da die Übergangswahrscheinlichkeiten von Zeitpunkt $t$ zu Zeitpunkt $t+1$ nur von $X_t$ abhängen, schreiben wir kurzerhand
$$ -P(X_t = R | X_{t-1} = L) = P(L \to R) = m_{L R} +P(X_{t+1} = R | X_{t} = L) = P(L \to R) = m_{L R} $$Wir schreiben die Wahrscheinlichkeiten, dass der Frosch zum Zeitpunkt $t$ in $L$ oder $R$ ist, als Vektoren.
$$ -X_0 = \begin{pmatrix}1 \\ 0\end{pmatrix} +X_0 = \begin{pmatrix}1 & 0\end{pmatrix} $$Ein Zeitschritt $t=0 \to t=1$ verändert die Verteilung gemäß $M$:
$$ -X_0 = \begin{pmatrix}1 \\ 0\end{pmatrix} \to \begin{pmatrix}1-p \\ p \end{pmatrix} = X_1 +X_0 = \begin{pmatrix}1 & 0\end{pmatrix} \to \begin{pmatrix}1-p & p \end{pmatrix} = X_1 $$from sympy import symbols as S, Matrix as Mat
-p, q = S("p q")
-M = Mat([[1-p, p], [q, 1-q]])
-
from numpy.linalg import matrix_power
-from sympy import simplify
-
-x_0 = Mat([[1, 0]])
-[ (x_0 @ (matrix_power(M, i))).subs(p, 0.6).subs(q, 0.2) for i in range(10)]
-
[Matrix([[1, 0]]), - Matrix([[0.4, 0.6]]), - Matrix([[0.28, 0.72]]), - Matrix([[0.256, 0.744]]), - Matrix([[0.2512, 0.7488]]), - Matrix([[0.25024, 0.74976]]), - Matrix([[0.250048, 0.749952]]), - Matrix([[0.2500096, 0.7499904]]), - Matrix([[0.25000192, 0.74999808]]), - Matrix([[0.250000384, 0.749999616000001]])]-
Den Folgezustand von $X = \begin{pmatrix}L & R\end{pmatrix}$ mit Übergangsmatrix $M$ berechnen wir als
+Aus einem gegebenen
+$$ +X_0 = \begin{pmatrix}1 & 0 \end{pmatrix} +$$können wir das Vektor-Matrix-Produkt
$$ \begin{align*} -\begin{pmatrix}L' \\ R'\end{pmatrix}^T &= \begin{pmatrix}L & R\end{pmatrix} \cdot M \\ -&= \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} \\ -=& \begin{pmatrix}L \cdot \color{green}{(1 - p)} + R \cdot \color{green}{q} \\ -L \cdot \color{orange}{p} + R \cdot \color{orange}{(1-q)} -\end{pmatrix}^T +X_1 &= X_0 \cdot M \\ +&= \begin{pmatrix} 1 & 0 \end{pmatrix}\cdot{}\begin{pmatrix}1-p & p \\q & 1-q\end{pmatrix} = \begin{pmatrix}1-p & p \end{pmatrix} \end{align*} -$$ +$$ausrechnen.
+-+$$ +\begin{align*} +& X_0 \cdot M \\ +&= \begin{pmatrix} 1 & 0 \end{pmatrix} \cdot{} \begin{pmatrix} \color{green}{1 - p} & \color{orange}{p} \\ \color{green}{q} & \color{orange}{1-q} \end{pmatrix} \\ +&= \begin{pmatrix} +\begin{pmatrix} 1 & 0 \end{pmatrix}\bullet{}\begin{pmatrix} \color{green}{1 - p} \\ \color{green}{q}\end{pmatrix} & +\begin{pmatrix} 1 & 0 \end{pmatrix}\bullet{}\begin{pmatrix} \color{orange}{p} \\ \color{orange}{1-q} \end{pmatrix} +\end{pmatrix}\\ +&= \begin{pmatrix} \color{green}{1 - p}& \color{orange}{p} +\end{pmatrix} +\end{align*} +$$Man berechnet die neuen Einträge als "Zeile mal Spalte"
+Matrixmultiplikation rechnet "Zeile mal Spalte"
+
Die Einträge von $X_1 = X_0 \cdot M$ errechnen sich genau so, wie die Folgezustände $L'$ und $R'$ aus $L$ und $R$.
+ +In Froschland ändert sich das Wetter jeden Tag. Es gibt Sonnenschein, Regen und bewölkte Tage.
+Je nach heutigem Wetter variiert die Wahrscheinlichkeit für das morgige Wetter:
+Die Übergangsmatrix des Froschwetters
+$$ +\begin{align*} +M = \frac{1}{10} \begin{pmatrix}6 & 3 & 1 \\ 4 & 3 & 3 \\ 1 & 5 & 4 \end{pmatrix} += \begin{pmatrix}0.6 & 0.3 & 0.1 \\ 0.4 & 0.3 & 0.3 \\ 0.1 & 0.5 & 0.4 \end{pmatrix} +\end{align*} +$$Wenn es heute regnet $X_0 = \begin{pmatrix}1 & 0 & 0 \end{pmatrix}$, ist die Wettervorschau für morgen $X_0 \cdot M$, bzw für in einer Woche $X_0 \cdot M \cdot \dots M = X_0 \cdot M^{14}$.
+ +Wir können also $X_{t+1} = X_t \cdot{} M$ ausrechnen. Allerdings könnten wir auch $M^{14}$ ausrechnen und einmal $X_0 \cdot M^{14}$ berechnen.
+Wir rechnen $v \cdot M$ als
++Zeile mal Spalte, für alle Spalten von $M$
Ein Matrixprodukt $M_1 \cdot M_2$ berechnet sich als
++$$ \begin{align*} -\begin{pmatrix}1 \\ 1\end{pmatrix}^T\cdot M &= \begin{pmatrix} (1-p) + q \\ p + (1-q)\end{pmatrix}^T \\ -& = \begin{pmatrix} (1-p) + q \\ p +(1-q)\end{pmatrix}^T \\ +M^2 &= \begin{pmatrix}0.6 & 0.3 & 0.1 \\ 0.4 & 0.3 & 0.3 \\ 0.1 & 0.5 & 0.4 \end{pmatrix} \cdot \begin{pmatrix}0.6 & 0.3 & 0.1 \\ 0.4 & 0.3 & 0.3 \\ 0.1 & 0.5 & 0.4 \end{pmatrix} \\ +&= +\begin{pmatrix} +\begin{pmatrix}0.6 & 0.3 & 0.1 \end{pmatrix}\bullet \begin{pmatrix} 0.6 \\ 0.4 \\ 0.1 \end{pmatrix} & +\begin{pmatrix}0.6 & 0.3 & 0.1\end{pmatrix}\bullet \begin{pmatrix} 0.3 \\ 0.3 \\ 0.5 \end{pmatrix} & +\begin{pmatrix}0.6 & 0.3 & 0.1\end{pmatrix}\bullet \begin{pmatrix} 0.1 \\ 0.3 \\ 0.4 \end{pmatrix}\\ +\begin{pmatrix}0.4 & 0.3 & 0.3 \end{pmatrix}\bullet \begin{pmatrix} 0.6 \\ 0.4 \\ 0.1 \end{pmatrix} & +\begin{pmatrix}0.4 & 0.3 & 0.3\end{pmatrix}\bullet \begin{pmatrix} 0.3 \\ 0.3 \\ 0.5 \end{pmatrix} & +\begin{pmatrix}0.4 & 0.3 & 0.3\end{pmatrix}\bullet \begin{pmatrix} 0.1 \\ 0.3 \\ 0.4 \end{pmatrix}\\ +\begin{pmatrix}0.1 & 0.5 & 0.4\end{pmatrix}\bullet \begin{pmatrix} 0.6 \\ 0.4 \\ 0.1 \end{pmatrix} & +\begin{pmatrix}0.1 & 0.5 & 0.4 \end{pmatrix}\bullet \begin{pmatrix} 0.3 \\ 0.3 \\ 0.5 \end{pmatrix} & +\begin{pmatrix}0.1 & 0.5 & 0.4 \end{pmatrix}\bullet \begin{pmatrix} 0.1 \\ 0.3 \\ 0.4 \end{pmatrix} +\end{pmatrix} \end{align*} $$für jede Zeile von $M_1$: Zeile mal Spalte, für alle Spalten von $M_2$
+
Um $X_{14}$ auszurechnen könnten wir 14 Vektor-Matrix multiplikationen rechnen +$$ + X_{14} = X_0 \cdot M \cdot \dots \cdot M +$$
+Oder stattdessen $M^{14}$ berechnen und mit einer Vektor-Matrix multiplikation +$$ +X_{14} = X_0 \cdot M^{14} +$$ +ausrechnen.
+$\leadsto$ Insgesamt fünf M-M Multiplikationen und eine V-M Multiplikation.
+ +v1 = Mat([[1, 0]])
-v1 * M
+#from sympy import symbols as S, Matrix as Mat
+from numpy import array
+
+M = array([[0.6, 0.3, 0.1], [0.4, 0.3, 0.3], [0.1, 0.5, 0.4]])
+x0 = [1, 0, 0]
+x0 @ M @ M @ M @ M @ M @ M @ M @ M @ M @ M @ M @ M @ M @ M
array([0.40909225, 0.3484844 , 0.24242335])
v2 = Mat([[1, 1]])
-v2 * M
+from numpy import linalg
+x0 @ linalg.matrix_power(M, 14)
+
+
+
Sehr brave Markovketten konvergieren mit $t \to \infty$ gegen eine eindeutige stationäre Verteilung $X^*$. +Mathematisch lässt sich diese Verteilung durch
+$$ + X^* \cdot M \overset{!}{=} X^* +$$ausdrücken und explizit als Lösung des Gleichungssystems (denn Matrizen stellen mehrere Gleichungen dar) ausrechnen. +Um eine stochastische Verteilung zu erhalten, brauchen wir die zusätzliche Gleichung
+$$ +1 = \sum_{i} X^*_i. +$$Allgemein drückt man Lösungen der oberen Gleichung durch die Form
+$$ +v^* M = \lambda v^* +$$aus. Hierbei ist $\lambda != 0$ eine Zahl, die man auch Eigenwert nennt. Eine Lösung für die Eigenwertgleichung ist also ein Vektor $v^*$, der sich nach Matrixmultiplikation mit $M$ um den Faktor $\lambda$ streckt, allerdings seine Richtung beibehält. Da in der obigen Gleichung der Streckfaktor $\lambda = 1$ ist, nennen wir $X^*$ Eigenvektor von $M$ zum Eigenwert $1$.
+In python können wir die Eigenwerte und -Vektoren berechnen lassen:
+ +# linalg.eig berechnet Rechts-Eigenvektoren von Matrizen, wir suchen allerdings Links-Eigenvektoren
+# Um zum richtigen Ergebnis zu kommen, müssen wir M also spiegeln (M.transpose())
+ews, evs = linalg.eig(M.transpose()) # berechnet Eigenwerte und Eigenvektoren
+
+# Wir sehen, dass ews[0] der Eigenwert 1 ist
+print(ews[0])
+
+# Folglich ist die 0-te Spalte von evs der dazugehörige Eigenvektor
+v = evs[:,0]
+
+# Wir müssen v auf Länge 1 skalieren, dass v ein stochastischer Vektor ist
+v /= sum(v)
+
+# Wir sehen, dass v und v@M der gleiche Vektor sind.
+print(v)
+print(v @ M)
0.9999999999999989 +[0.40909091 0.34848485 0.24242424] +[0.40909091 0.34848485 0.24242424] ++