En utilisant la décomposition des modèles de statistiques, testez si vous pouvez voir l'effet de calendrier appelé effet Halloween, Vendre en mai, etc.
import numpy as np
import pandas as pd
import statsmodels.api as sm
import pandas_datareader.data as web
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
plt.rcParams['axes.xmargin'] = 0.01
plt.rcParams['axes.ymargin'] = 0.01
window = 250*3
'''S&Acquisition de données P500'''
data1 = web.DataReader("^GSPC", "yahoo", "1980/1/1").dropna()
#Rendement de la trésorerie Acquisition de données sur 10 ans
data2 = web.DataReader("^TNX", "yahoo", "1980/1/1").dropna()
#Volatilité des taux annuels quotidiens= 10%Tirer parti des paramètres ajustés
ret = pd.DataFrame(data1['Adj Close'] / data1['Adj Close'].shift(1) - 1)
ret = 0.1 * ret / (ret.rolling(252).std()*np.sqrt(252)).shift(1)
data1 = 100*((1 + ret).cumprod()).dropna(axis=0)
data1_freq = data1.resample('M').last().fillna(method='ffill')
data1_freq['ret1'] = data1_freq['Adj Close']/data1_freq['Adj Close'].shift(1) - 1
#Recréez des tours mensuels
data1_freq_equity_curve = 100*((1 + data1_freq['ret1']).cumprod()).dropna(axis=0)
data1_freq_equity_curve.index = pd.to_datetime(data1_freq_equity_curve.index)
#10 ans pour toute l'année(120 mois), 5 années(60 mois)
res_all = sm.tsa.seasonal_decompose(data1_freq_equity_curve, freq=12)
res_10Y = sm.tsa.seasonal_decompose(data1_freq_equity_curve[-120:], freq=12)
res_5Y = sm.tsa.seasonal_decompose(data1_freq_equity_curve[-60:], freq=12)
seasonality = pd.concat([res_all.seasonal,
res_10Y.seasonal,
res_5Y.seasonal], axis=1)
seasonality.columns = ['1981-', '10Y', '5Y']
m_seasonality = seasonality.groupby(seasonality.index.month).mean()
# Creat Figure
month = m_seasonality.index
height = m_seasonality * 100
bar_width = 0.3
plt.figure(figsize=(10, 4), dpi=80)
plt.bar(month, height['1981-'], tick_label=month,
width=0.3, label='1981-')
plt.bar(month + bar_width, height['10Y'], tick_label=month,
width=0.3, label='10Y')
plt.bar(month + 2*bar_width, height['5Y'], tick_label=month,
width=0.3, label='5Y')
plt.xticks(month + bar_width, month)
plt.legend()
plt.title("Average Return by Month")
plt.xlabel("Month")
plt.ylabel("Ave.Ret(%)")
plt.show()
Regard sur les retours en saison
Decomposition(Trend, Seasonality, Residual)
res_10Y.plot()
plt.show()
Recommended Posts