Lorenzo Biasi and Michael Aichmüller

## Exercise 1.

``````

In [98]:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

def figure_label():
plt.xlabel('time')
plt.ylabel('x')

``````
``````

In [106]:

def brownian_motion(T, N):
z = np.hstack(([0], np.random.randn(N - 1)))
return np.cumsum(z * np.sqrt(T / N))

T, N, n = 1, 10001, 100
t = np.linspace(0, T, N)
motions = np.zeros((n, N))
for i in range(n):
motions[i, :] = brownian_motion(T, N)

plt.plot(t, np.mean(motions, axis=0))
plt.plot(t, np.var(motions, axis=0))
figure_label()

``````
``````

``````

## Exercise 2.

``````

In [100]:

def geometric_brownian_motion(T, N, sigma, r, x):
t = np.linspace(0, T, N)
W = brownian_motion(T, N)
return x * np.exp((r + sigma ** 2 / 2) * t + sigma * W)

sigma, r, x = 2, 4, 2

for i in range(n):
motions[i, :] = geometric_brownian_motion(T, N, sigma, r, x)
plt.plot(t, np.mean(motions, axis=0)), figure_label()
plt.figure()
plt.plot(t, np.var(motions, axis=0)), figure_label()

``````
``````

Out[100]:

([<matplotlib.lines.Line2D at 0x7f24fd896c50>], None)

``````

## Exercise 3.

``````

In [107]:

def geometric_brownian_motion(T, N, t_0, x, y):
t = np.linspace(0, T, N)
W = brownian_motion(T, N)
return x + W - (t - t_0) / (T - t_0) * (W[-1] - y + x)

t_0, x, y = 0, -1, 2
t = np.linspace(0, T, N)
motions = np.zeros((n, N))
for i in range(n):
motions[i, :] = geometric_brownian_motion(T, N, t_0, x, y)

plt.plot(t, np.mean(motions, axis=0)), figure_label()
plt.figure()
plt.plot(t, np.var(motions, axis=0)), figure_label()

``````
``````

Out[107]:

([<matplotlib.lines.Line2D at 0x7f24fd5cc978>], None)

``````
``````

In [ ]:

``````
``````

In [ ]:

``````