Computerübung 1
Sydney Stern 100 243 00
import random #importing
import numpy as np
import matplotlib.pyplot as plt
1) First we will write a script generating a random walk in d dimensions with k steps with each step equally distributed in every dimension [−1/2,1/2].
def randomwalk(d, k): #random walk with d dimensions and k steps
pos = np.zeros((k+1, d))
for n in range(1, k):
step = np.zeros(d)
for m in range(d):
step[m] = random.uniform(-1/2, 1/2)
pos [n+1] = pos[n] + step
return pos
2) Now we want to plot 3 1-D walks for 10,000 steps:
k0 = 10000 #setting parameters
d1 = 1
X1 = randomwalk(d1, k0)
X2 = randomwalk(d1, k0)
X3 = randomwalk(d1, k0)
plt.style.use('seaborn')
fig = plt.figure(figsize=(15, 7))
plt.xlim(0,k0)
plt.title("Random Walks in 1-D", fontsize=18)
plt.xlabel("Time")
plt.ylabel("Position")
plt.plot(X1, label='Randomwalk1')
plt.plot(X2, label= 'Randomwalk2')
plt.plot(X3, label= 'Randomwalk3')
plt.legend(["Random walk 1", "Random walk 2", "Random walk 3"], loc ="upper right")
plt.show()
3) In this part we will repeat the process for two dimensional walks.
k0=10000
d2 = 2
Y1 = randomwalk(d2, k0)
Y1 = np.array([Y1])
Y2 = randomwalk(d2, k0)
Y2 = np.array([Y2])
Y3 = randomwalk(d2, k0)
Y3 = np.array([Y3])
fig = plt.figure(figsize=(15, 7))
plt.scatter(Y1[0][:,0], Y1[0][:,1], s=1, c='red', marker='o', label="Random walk 1")
plt.scatter(Y2[0][:,0], Y2[0][:,1], s=1, c='orange', marker='o', label="Random walk 2")
plt.scatter(Y3[0][:,0], Y3[0][:,1], s=1, c='blue', marker='o', label="Random walk 3")
plt.xlabel("x")
plt.ylabel("y")
plt.title("2-D Random Walks")
plt.legend(loc='lower left')
plt.show()
4. Now to write a script for the positions of W walkers:
def wp(d, k, w):
#für d-dimensionale Arrays
walkerpositions = np.zeros((w, d))
for l in range(w):
walks = np.zeros(d)
for n in range(k):
step = np.zeros(d)
for m in range(d):
step[m] = random.uniform(-1/2, 1/2)
walks += step
walkerpositions[l] = walks
return [walkerpositions]
wp(2, 5, 3) #testing the script
5. Here we create a scatter plot of the coordinates of 10000 walkers.
w0 = 10000
Z1 = wp(2, 1, w0)
Z2 = wp(2, 10, w0)
fig = plt.figure(figsize=(15, 7))
plt.scatter(Z1[0][:,0], Z1[0][:,1], s=1, c='green', marker='o', label="k=1")
plt.scatter(Z2[0][:,0], Z2[0][:,1], s=1, c='purple', marker='o', label="k=10")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Endpositionen der w = 10000 Random walkers in 2-D nach 1 und 10 Schritten")
plt.legend(loc='lower left')
plt.show()
from scipy import stats
def gauss(x, k):
w = 10000
positions = np.zeros(w)
for n in range(10000):
for m in range(k):
step = random.uniform(-1/2, 1/2)
walks = 0
walks += step
positions[n] = walks
mu = np.mean(positions) #mean
sigma = np.std(positions, ddof=1)
gauss = 1 / (np.sqrt(2 * np.pi) * sigma) * np.exp(-((x - mu) ** 2)/(2 * sigma ** 2))
return gauss
x = np.linspace(-5, 5)
np.mean(gauss(x, 10))
data1 = wp(1, 1, 10000)
data2 = wp(1, 10, 10000)
data3 = wp(1, 20, 10000)
fig, ax = plt.subplots(figsize=(9, 7), dpi=100)
fig.tight_layout()
ax1 = plt.subplot(321)
plt.title("Gauß-Verteilung für k=1")
p, bins, patches = ax1.hist(data1[0], bins=50, density= True)
ax1.plot(bins, gauss(bins, 1), color='orange')
plt.xlim(-1/2,1/2)
ax1.set_xlabel('measurement')
ax1.set_ylabel('probability')
ax2 = plt.subplot(322)
plt.title("Gauß-Verteilung für k=10")
p, bins, patches = ax2.hist(data2[0], bins=50, density= True)
ax2.plot(bins, gauss(bins, 10), color='orange')
ax2.set_xlabel('measurement')
ax2.set_ylabel('probability')
ax3 = plt.subplot(323)
plt.title("Gauß-Verteilung für k=20")
p, bins, patches = ax3.hist(data3[0], bins=50, density= True)
ax3.plot(bins, gauss(bins, 20), color='orange')
plt.xlim(-4.5,4.5)
ax3.set_xlabel('measurement')
ax3.set_ylabel('probability')
plt.show()