-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathmonte_carlo.py
More file actions
23 lines (19 loc) · 802 Bytes
/
monte_carlo.py
File metadata and controls
23 lines (19 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## https://mp.weixin.qq.com/s/tvYMwNIqA9ZNKb88PiSIew
import numpy as np
import math
import matplotlib.pyplot as plt
from scipy.stats import norm
from pandas_datareader import data
#download Apple price data into DataFrame
apple = data.DataReader('AAPL', 'yahoo', start='1/1/2000')
#calculate the compound annual growth rate (CAGR) which
#will give us our mean return input (mu)
days = (apple.index[-1] - apple.index[0]).days
cagr = ((((apple['Adj Close'][-1]) / apple['Adj Close'][1])) ** (365.0/days)) - 1
print ('CAGR =',str(round(cagr,4)*100)+"%")
mu = cagr
#create a series of percentage returns and calculate
#the annual volatility of returns
apple['Returns'] = apple['Adj Close'].pct_change()
vol = apple['Returns'].std() * sqrt(252)
print ("Annual Volatility =",str(round(vol,4)*100)+"%")