Pearson Random Walk
import pandas as pd
import numpy as np
from numpy.random import uniform
import matplotlib.pyplot as plt
import seaborn as sns
set() sns.
Origen
Origen del problema de la caminata aleatoria de Pearson.
Solución al problema.
Sea \[ \overrightarrow{R}_n = \overrightarrow{r}_1 + \overrightarrow{r}_2 + ... + \overrightarrow{r}_n \]
donde
\[\begin{equation} \overrightarrow{R}_x = l \sum_{i=1}^{n} cos(\theta_i)\\ \overrightarrow{R}_y = l \sum_{i=1}^{n} sen(\theta_i) \end{equation}\]
\[|R_n| = \sqrt{\overrightarrow{R}_x + \overrightarrow{R}_y} \]
# Pearson Random Walk function
def PearsonRandomWalk(nsteps, l=1):
= [0]*(nsteps+1)
rx = [0]*(nsteps+1)
ry for i in range(1, nsteps+1):
= uniform(low=0, high=2*np.pi, size=1)[0]
theta = l*np.cos(theta)
rx[i] = l*np.sin(theta)
ry[i] = np.array(rx)
rx = np.array(ry)
ry = rx.cumsum()
Rx = ry.cumsum()
Ry = Rx**2 + Ry**2
R2 = np.sqrt(R2)
R
return rx, ry, Rx, Ry, R2, R
= 5000
nsteps = 5000
npaths # multiple paths
= [], [], [], [], [], []
rxs, rys, Rxs, Rys, R2s, Rs # loop
for i in range(npaths):
= PearsonRandomWalk(nsteps)
rx, ry, Rx, Ry, R2, R
rxs.append(rx)
rys.append(ry)
Rxs.append(Rx)
Rys.append(Ry)
R2s.append(R2) Rs.append(R)
Caminata Aleatoria de Pearson
= np.array([*range(nsteps+1)])
x # media y varianza de la distribución Rayleigh
= np.sqrt(x/2)*np.sqrt(np.pi/2)
mean = ((x/2))*((4-np.pi)/2)
variance # gráfico
=(18,8))
plt.figure(figsizefor r in Rs:
'-', color='grey', alpha=0.4)
plt.plot(x, r, '-', color='red', alpha=0.4)
plt.plot(x, mean, + 3*np.sqrt(variance), '--', color='blue', alpha=0.4)
plt.plot(x, mean 0]*len(x), '--', color='blue', alpha=0.4)
plt.plot(x, ['Pearson Random Walk')
plt.title( plt.show()
Veamos el promedio de las trayetorias con respecto a \(t\).
= np.array([*range(nsteps+1)])
x =(18,8))
plt.figure(figsize=0) , '.', color='grey', alpha=0.9, label='Promedio')
plt.plot(np.array(Rs).mean(axis/2)*np.sqrt(np.pi/2), '-', color='blue', alpha=0.4, label='Valor esperado teórico')
plt.plot(x, np.sqrt(x'Pearson Random Walk')
plt.title(
plt.legend() plt.show()
Distribución de \(\overrightarrow{R}_n\)
= [r[nsteps] for r in Rs]
Rn =(16,7))
plt.figure(figsize='red')
sns.kdeplot(Rn, color=40, density=True) plt.hist(Rn, bins
'Distribución de $R_n$')
plt.title( plt.show()
Distribución de \(\overrightarrow{R}_x\)
\[\overrightarrow{R}_x \sim N(0, n/2)\]
= [rx[nsteps] for rx in Rxs]
Rx =(16,7))
plt.figure(figsize='red')
sns.kdeplot(Rx, color=50, density=True) plt.hist(Rx, bins
'Distribución de $R_x$')
plt.title( plt.show()
Distribución de \(\overrightarrow{R}_y\)
\[\overrightarrow{R}_y \sim N(0, n/2)\]
= [ry[nsteps] for ry in Rys]
Ry =(16,7))
plt.figure(figsize='red')
sns.kdeplot(Ry, color=50, density=True) plt.hist(Ry, bins
'Distribución de $R_y$')
plt.title( plt.show()
Distribución de \(R_n^2\)
Sea \(W= R_n^2 = \overrightarrow{R}_x^2 + \overrightarrow{R}_y^2\), entonces la distribución viene dada por
\[\begin{equation} f_W(w) = \frac{1}{n} \exp\{- \frac{w}{n}\}, \ \ \ w>0 \end{equation}\]
= [r2[nsteps] for r2 in R2s]
R2 =(16,7))
plt.figure(figsize=50, density=True) plt.hist(R2, bins
'Distribución de $R_n^2$')
plt.title( plt.show()
Trayectoria del Caminante
=(14,10))
plt.figure(figsize1], Rys[1], 'o', color='blue', markersize=3)
plt.plot(Rxs[1], Rys[1], color='black', linewidth=0.8)
plt.plot(Rxs['Pearson Random Walk')
plt.title('Time')
plt.xlabel(#mplcyberpunk.add_glow_effects()
plt.show()