Using Python to Simulate Brownian Motion

Brownian motion is a phenomenon that particles in the seemingly motionless liquid are still undergone unceasing collisions in an erratic way. It was firstly observed by Robert Brown in 1827. In 1923, Norbert Wiener had attempted to formulate this observations in mathematical terms; thus it is also known as Wiener process.

Owing to its randomness, Brownian motion has a wide range of applications, ranging from chaotic oscillations to stock market fluctuations. In this article, I will describe its basic property and how to visualise it and its variants with Python.

The Model of Brownian Motion

To begin with, we should see how to make Brownian motion in a rather formal way.

We firstly consider one dimensional coordinate system for simplicity. Imagine that you put a particle at the origin (x=0) in the very beginning, and it may encounter random collisions along the x-coordinate afterwards. Let X ( t ) X(t) X(t) be the position of the particle after t t t units of time ( X ( 0 ) = 0 X(0)=0 X(0)=0).

When the particle is undergone some collisions, we say there are events occurred. From physical obervations, scientists find that the probability of events occurred in any two equal time intervals, say [x,t]
and [x+h,t+h], are not only equal but also indepedent. In other words, they have the same probability distribution, and no matter how many events occurred in [x,t], it would not affect the number of events occurred over [x+h,t+h]. This can be represented as
在这里插入图片描述

Visualise the Brownian Motion

Now we are ready to draw our Brownian motion in Python.

Some Toolkits

Below are the modules we will use to draw our plots.

from math import sqrt, exp
from random import random, gauss
import numpy as np
import matplotlib.pyplot as plt

Visualisation

在这里插入图片描述

mean = 0
std = random()  # standard deviation

N = 1000    # generate N points
dt = 1/N    # time interval = [0,1]

data = []
x = 0
for t in range(N):
    dx = gauss(mean, std*sqrt(dt))  # gauss(mean, standard deviation)
    x = x + dx                      # compute X(t) incrementally
    data.append((dt*t, x+dx))

data = np.array(data)

plt.figure()
plt.plot(data[:, 0], data[:, 1], linewidth=0.5)
plt.scatter(data[0, 0], data[0, 1],marker="^",color='r',label="Origin")
plt.xlabel('t')
plt.ylabel('x')
plt.legend()
plt.title("Brownian motion")
plt.show()

在这里插入图片描述

2D-Brownian Motion

Similarly, if we extend our coordinate system to two dimensions,
在这里插入图片描述

mean = 0
std = random()

N = 1000    # generate N points
dt = 1/N    # time interval = [0,1]

data = []
x, y = 0, 0
for t in range(N):
    dx = gauss(mean, std*sqrt(dt))
    dy = gauss(mean, std*sqrt(dt))
    x, y = x+dx, y+dy
    data.append((x, y))

data = np.array(data)

plt.figure()
plt.plot(data[:, 0], data[:, 1], linewidth=0.5)
plt.scatter(data[0, 0], data[0, 1],marker="^",color='r',label="Origin")
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title("2D-Brownian motion")
plt.show()

在这里插入图片描述

3D-Brownian Motion

The Brownian motion over 3-dim coordinate system is also trivial when you grasp the idea.

mean = 0
std = random()  # standard deviation

N = 1000    # generate N points
dt = 1/N    # time interval = [0,1]

data = []
x, y, z = 0, 0, 0
for t in range(N):
    dx = gauss(mean, std*sqrt(dt))
    dy = gauss(mean, std*sqrt(dt))
    dz = gauss(mean, std*sqrt(dt))
    x, y, z = x+dx, y+dy, z+dz
    data.append((x, y, z))

data = np.array(data)

plt.figure()
ax = plt.axes(projection='3d')
ax.plot3D(data[:, 0], data[:, 1], data[:, 2], linewidth=0.5)
ax.plot3D(data[0, 0], data[0, 1], data[0, 2], marker='^', color='r')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('3D-Brownian motion')
plt.show()

在这里插入图片描述

Geometric Brownian Motion

在这里插入图片描述

The Relationship between Stock Prices at Time t

在这里插入图片描述

mean = random()
std = random()

N = 253     # trading days in a year
dt = 1/N

x = 10.0    # initial stock price
data = [(0, x)]
for t in range(1, N):
    ratio = exp(mean*dt) * exp(std * gauss(0, sqrt(dt)))
    x = x * ratio
    data.append((dt*t, x))

data = np.array(data)

plt.figure()
plt.plot(data[:, 0], data[:, 1], linewidth=0.5)
plt.plot(data[0, 0], data[0, 1], marker='^', color='r')
plt.xlabel('t')
plt.ylabel('price')
plt.ylim([0, max(data[:,1]+10)])
plt.title("Geometric Brownian motion")
plt.show()

在这里插入图片描述

See https://lucien-east.github.io/2021/07/27/Brownian-motion/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值