Article Directory
This article was first published on the official account: Python for Finance
Link: https://mp.weixin.qq.com/s/43KQgH-BArop29uJ3Fe7MA
Definition of Monte Carlo Simulation
Basic idea : Monte Carlo Simulation (MCS) is to simulate the change of risk factors under a certain statistical distribution assumption. First, it is assumed that the asset income is a random process, and according to the set price change process, a large number of possible future scenarios are simulated, and then the investment portfolio change value in a certain scenario is sorted, and the distribution of the investment portfolio change is given. Based on this, the VaR value under different confidence levels can be estimated.
Basic steps :
For each Monte Carlo simulation, the price of the next trading day is simulated for each asset in the asset portfolio according to the random process formula. The ε in the formula can be assumed to obey the t distribution or the normal distribution (that is, the distribution that the return on assets obeys). , and then you can get the rate of return of each asset, multiply it by its respective weight and market value to get the return of each asset on the next trading day, and add all of them together to get the simulated return of the asset portfolio on the next trading day.
Assuming that the stock price conforms to the geometric Brownian motion, that is,
d S t = μ t S tdt + σ t S td W t dS_t=\mu_tS_tdt+\sigma_tS_tdW_tdSt=mtStdt+ptStdWt
Simplify the process to obtain the asset price change process in a specific period (0,T):
Δ S t = S t ( μ Δ t + σ ε t Δ t ) , t = 1 , 2 , . . . , N , N Δ t = T \Delta S_t=S_t(\mu\Delta t+\sigma\varepsilon_t\sqrt{\Delta t}),t=1,2,...,N,N\Delta t=TWILL _t=St( m D t+in etΔt),t=1,2,...,N,NΔt=T
于是得到:
S t + 1 = S t + S t ( μ Δ t + σ ε t Δ t ) , t = 1 , 2 , . . . , N , N Δ t = T S_{t+1}=S_t+S_t(\mu\Delta t+\sigma\varepsilon_t\sqrt{\Delta t}),t=1,2,...,N,N\Delta t=T St+1=St+St( m D t+in etΔt),t=1,2,...,N,NΔt=Definition
:
S t + 1 = S te ( μ − σ 2 2 ) Δ t + σ ε t Δ t S_{t+1}=S_te^{(\mu-\frac{\sigma^2} 2)\Delta t+\sigma\varepsilon_t\sqrt{\Delta t}}St+1=Ste( m −2p2) Δ t + σ etΔt
where μ \muμ is the average return rate,σ 2 \sigma^2p2 is the yield variance,ε \varepsilonε obeys t distribution or normal distribution.
Functional expression:
return = ( μ − σ 2 2 ) dt + σ ε tdt return=(\mu-\frac{\sigma^2}2)dt+\sigma\varepsilon_t\sqrt{dt}return=( m−2p2)dt+in etdt
Python Implementation of Monte Carlo Simulation
Monte Carlo simulation method for simulating stock return series
Rationalization:
return = ( μ − σ 2 2 ) dt + σ ε tdt return=(\mu-\frac{\sigma^2}2)dt+\sigma\varepsilon_t\sqrt{dt};return=( m−2p2)dt+in etdt
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
import pandas as pd
import math
'''
s:股票现价
t:期限(年)
r:股票年化收益率
sigma:股票年化波动率
nper_per_year:每年的期数
'''
def generate_simulated_stock_returns(t,r,sigma,nper_per_year):
simulated_returns=[]
dt=1/nper_per_year
term = int(t*nper_per_year)
for i in range (1, term+1):
z=np.random.normal()
simulated_return = (r-(sigma**2/2))*dt + z*sigma*(dt**(1/2))
simulated_returns.append(simulated_return)
array_return=np.array(simulated_returns)
return array_return
# 初始股价s:100; 预期收益率r:10%;标准差:30%
s=100;r=0.1;sigma=0.3
#1年期、每年2期
t=1;nper_per_year=2
array_return = generate_simulated_stock_returns(t,r,sigma,nper_per_year)
print(array_return)
#2年期、每年24期
t=2;nper_per_year=24
array_return = generate_simulated_stock_returns(t,r,sigma,nper_per_year)
print(array_return)
[ 0.21738696 -0.06383675]
[ 0.02899686 0.00131385 -0.09489962 -0.00440415 -0.0357566 -0.05227566
0.07905745 -0.03065636 -0.01726008 -0.0059791 0.05072394 0.01448947
0.03098366 -0.05170335 0.0161574 -0.18380967 -0.0629412 0.00289641
0.14890079 -0.05693315 0.0931597 0.0037413 -0.05493882 0.12309281
0.06119329 0.04241972 -0.02030099 -0.05180438 -0.05970102 0.0229074
0.12618542 0.0770313 0.05075201 -0.04261307 0.00168359 0.03529421
0.0850315 -0.09281302 -0.08985412 0.02220526 0.01642511 0.04967819
0.07372143 -0.01799848 0.05595597 -0.00384655 -0.09679426 -0.08459783]
Monte Carlo simulation method to simulate stock price sequence
股价为:
S i = S i − 1 × e r i − 1 S_i = S_{i-1} \times e^{r_{i-1}} Si=Si−1×eri−1
def generate_simulated_stock_values(s,t,r,sigma,nper_per_year):
rate=generate_simulated_stock_returns(t,r,sigma,nper_per_year)
stock_price = [s]
term = int(t*nper_per_year)
for i in range(1, term+1):
values = stock_price[i-1]*math.e**(rate[i-1])
stock_price.append(values)
array_price = np.array(stock_price)
return array_price
#1年期、每年2期
t=1;nper_per_year=2
array_price = generate_simulated_stock_values(s,t,r,sigma,nper_per_year)
print(array_price)
#2年期、每年24期
t=2;nper_per_year=24
array_price = generate_simulated_stock_values(s,t,r,sigma,nper_per_year)
print(array_price)
[100. 105.03146796 100.4594981 ]
[100. 95.90978914 95.65450188 104.70632493 102.12337933
104.26726892 99.06536039 100.63054422 93.6685905 88.57596138
92.41510048 89.91265499 86.27490259 87.29911775 84.26798089
86.5798334 87.06325173 87.61229376 81.72201584 85.49976969
82.96816113 80.11385795 83.01588423 77.73720797 72.770712
63.60523084 65.39745198 69.02682262 67.64864604 62.52653157
61.57041633 58.01208479 62.16882528 66.41108904 66.4236716
59.67428405 68.38557448 70.2657609 75.26920257 77.15860959
80.52151818 74.45625968 71.23642008 70.7874225 69.68587971
75.54529952 67.20571691 67.86359575 67.393064 ]
Monte Carlo Simulation Method to Draw Simulated Stock Price Sequence Chart
def plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials=1):
term = int(t*nper_per_year) + 1
x_axis = np.linspace(0,t,term)
for i in range(num_trials):
price=generate_simulated_stock_values(s,t,r,sigma,nper_per_year)
plt.plot(x_axis, price)
plt.title(str(num_trials)+" simulated trials")
plt.xlabel("years")
plt.ylabel("value")
plt.show()
# 2年期、每年250期,模拟5次
t=2;nper_per_year=250;num_trials=5
plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials)
# 2年期、每年250期,模拟1000次
t=2;nper_per_year=250;num_trials=1000
plot_simulated_stock_values(s,t,r,sigma,nper_per_year,num_trials)