ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
edhec_risk_kit_105.py
(2436B)
1 import pandas as pd
2
3 def drawdown(return_series: pd.Series):
4 """Takes a time series of asset returns.
5 returns a DataFrame with columns for
6 the wealth index,
7 the previous peaks, and
8 the percentage drawdown
9 """
10 wealth_index = 1000*(1+return_series).cumprod()
11 previous_peaks = wealth_index.cummax()
12 drawdowns = (wealth_index - previous_peaks)/previous_peaks
13 return pd.DataFrame({"Wealth": wealth_index,
14 "Previous Peak": previous_peaks,
15 "Drawdown": drawdowns})
16
17
18 def get_ffme_returns():
19 """
20 Load the Fama-French Dataset for the returns of the Top and Bottom Deciles by MarketCap
21 """
22 me_m = pd.read_csv("data/Portfolios_Formed_on_ME_monthly_EW.csv",
23 header=0, index_col=0, na_values=-99.99)
24 rets = me_m[['Lo 10', 'Hi 10']]
25 rets.columns = ['SmallCap', 'LargeCap']
26 rets = rets/100
27 rets.index = pd.to_datetime(rets.index, format="%Y%m").to_period('M')
28 return rets
29
30
31 def get_hfi_returns():
32 """
33 Load and format the EDHEC Hedge Fund Index Returns
34 """
35 hfi = pd.read_csv("data/edhec-hedgefundindices.csv",
36 header=0, index_col=0, parse_dates=True)
37 hfi = hfi/100
38 hfi.index = hfi.index.to_period('M')
39 return hfi
40
41
42 def skewness(r):
43 """
44 Alternative to scipy.stats.skew()
45 Computes the skewness of the supplied Series or DataFrame
46 Returns a float or a Series
47 """
48 demeaned_r = r - r.mean()
49 # use the population standard deviation, so set dof=0
50 sigma_r = r.std(ddof=0)
51 exp = (demeaned_r**3).mean()
52 return exp/sigma_r**3
53
54
55 def kurtosis(r):
56 """
57 Alternative to scipy.stats.kurtosis()
58 Computes the kurtosis of the supplied Series or DataFrame
59 Returns a float or a Series
60 """
61 demeaned_r = r - r.mean()
62 # use the population standard deviation, so set dof=0
63 sigma_r = r.std(ddof=0)
64 exp = (demeaned_r**4).mean()
65 return exp/sigma_r**4
66
67 import scipy.stats
68 def is_normal(r, level=0.01):
69 """
70 Applies the Jarque-Bera test to determine if a Series is normal or not
71 Test is applied at the 1% level by default
72 Returns True if the hypothesis of normality is accepted, False otherwise
73 """
74 if isinstance(r, pd.DataFrame):
75 return r.aggregate(is_normal)
76 else:
77 statistic, p_value = scipy.stats.jarque_bera(r)
78 return p_value > level
79