蒙特卡洛模拟通常被用来对不同结果的概率建模,这些结果因为一些相关的随机因素而难以预测。蒙特卡洛模拟的构建方法千差万别,但它们一般会根据一个假设的概率分布来生成随机的输入,然后根据这些输入来计算或者聚合出结果。简单来说,蒙特卡洛模拟就是至少运行模型上千次,得到一系列的结果,这些结果会呈现一个概率分布,后结合所有结果来组成一个概况。 蒙特卡洛模拟的应用非常广,尤其是在金融领域,例如期权定价模型(Black Scholes Model)、违约风险分析(在线价值,Value at Risk)等。 蒙特卡洛模拟的简单模型应是单根或者随机步行模型。随机步行模型通过变量历史值加上一个随机噪声来得到当前的结果,该随机噪声通常被定义为是一个标准正态分布。请见下式:
S
t
=
S
t
−
1
+
Z
t
,
S
0
=
0
,
Z
t
∼
N
(
0
,
1
)
S_t=S_{t-1}+Z_t,S_0=0,Z_t \sim N(0,1)
St=St−1+Zt,S0=0,Zt∼N(0,1)
随机步行
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set()
monte_carlo =[]for i inrange(5):
forecasts =[0]
np.random.seed(i)for j inrange(10):
forecast = forecasts[j-1]+ np.random.normal()
forecasts.append(forecast)
monte_carlo.append(forecasts)
plt.figure(figsize=(12,8))
plt.plot(np.array(monte_carlo).T);
GBM模型公式如下:
S
t
=
S
0
e
x
p
(
(
μ
−
σ
2
/
2
)
t
−
σ
×
W
t
)
S_t=S_0exp((\mu-\sigma^2/2)t-\sigma\times W_t)
St=S0exp((μ−σ2/2)t−σ×Wt)
W
t
−
W
t
−
1
=
d
t
Z
t
W_t-W_{t-1}=\sqrt{dt}Z_t
Wt−Wt−1=dtZt
Z
t
∼
N
(
0
,
1
)
Z_t\sim N(0,1)
Zt∼N(0,1)
这里,
μ
\mu
μ为偏移率,确定了随机过程平均值的变化,
σ
\sigma
σ是该变化的波动。 GBM模型亦可以写成如下形式:
S
t
=
s
t
−
1
e
x
p
(
(
μ
−
σ
2
/
2
)
d
t
−
σ
d
t
Z
t
)
S_t=s_{t-1}exp((\mu-\sigma^2/2)dt-\sigma \sqrt{dt}Z_t)
St=st−1exp((μ−σ2/2)dt−σdtZt) 下图展示了GBM模型的10个模拟,参数为:
S
0
=
1
,
μ
=
0.05
,
σ
=
0.2
S_0=1,\mu=0.05,\sigma=0.2
S0=1,μ=0.05,σ=0.2
monte_carlo =[]
no_of_iter =10
sigma =0.2
mu =0.5
delta_t =0.25for i inrange(no_of_iter):
np.random.seed(i)
forecasts =[1]for j inrange(10):
forecast = forecasts[j]*np.exp((mu-sigma**2/2)*(delta_t)+ sigma*np.sqrt(delta_t)*np.random.normal())
forecasts.append(forecast)
monte_carlo.append(forecasts)
x = np.array(monte_carlo)
plt.figure(figsize=(12,8))
plt.plot(x.T);
金融预测应用蒙特卡洛模拟
我们先建立一个简单的金融模型,
P
r
o
f
i
t
=
R
e
v
e
n
u
e
−
F
i
x
e
d
C
o
s
t
−
V
a
r
i
a
b
l
e
C
o
s
t
Profit=Revenue-FixedCost-VariableCost
Profit=Revenue−FixedCost−VariableCost