Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

MEAN REVERSION HURST

216 views
Kernel: SageMath (stable)

Testing Mean Reversion Code

from datetime import datetime
from pandas_datareader import data
import pandas as pd
import numpy as np
from numpy import log, polyfit, sqrt, std, subtract
import statsmodels.tsa.stattools as ts
/ext/anaconda3/lib/python3.5/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead. from pandas.core import datetools
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
import pprint
symbList = ['GOOG','FB']
start_date = '2008/01/01'
end_date = '2018/01/01'
# Fetching data and initializing the IV column #here's the way to add a new column: Initialize it, ha! dataAll = pd.read_csv("CompaniesSP.csv") #create a new column and give value 0, just to do something new in the data file dataAll['IV'] = 0 #take the first8 columns of the file, iterating name range data=dataAll.loc[:, 'Date':'SBUX'] data.head()
tickers = ['AAPL', 'MSFT']
from pandas import read_csv series = read_csv('CompaniesSP.csv', header=0, parse_dates=[0], index_col=0, squeeze=True) print(type(series)) series.describe()
<class 'pandas.core.frame.DataFrame'>
# This line is necessary for the plot to appear in a Jupyter notebook (you don't need it if you're working in Rodeo) %matplotlib inline # Control the default size of figures in this Jupyter notebook (n/a if working in Rodeo) %pylab inline pylab.rcParams['figure.figsize'] = (15, 9) # Change the size of plots # Plot the adjusted closing price of AAPL series.loc[:, 'AAPL':'SBUX'].plot(grid = True)
Populating the interactive namespace from numpy and matplotlib
/ext/anaconda3/lib/python3.5/site-packages/IPython/core/magics/pylab.py:160: UserWarning: pylab import has clobbered these variables: ['datetime'] `%matplotlib` prevents importing * from pylab and numpy "\n`%matplotlib` prevents importing * from pylab and numpy"
<matplotlib.axes._subplots.AxesSubplot at 0x7fb0b21b54e0>
WARNING: Some output was deleted.
series.loc[:, 'AAPL'].plot(grid = True)
<matplotlib.axes._subplots.AxesSubplot at 0x7fb0b1de2320>
WARNING: Some output was deleted.
#create changes and add the corresponding new columns series['GOOG_Ret'] = series['GOOG'].pct_change().dropna() series['SPX_Ret'] = series['SPX'].pct_change().dropna() series['AAPL_Ret'] = series['AAPL'].pct_change().dropna() #rolling correlation of google and sp500 of returns yearly (250 days) series['GOOG_Ret'].rolling(252).corr(series['SPX_Ret']).plot(figsize=(15,4))
<matplotlib.axes._subplots.AxesSubplot at 0x7fb0b1ff3470>
WARNING: Some output was deleted.
#create the historical 30day vol series['SPX_HV21'] = series['SPX_Ret'].rolling(21).std() * np.sqrt(252) * 100 series['GOOG_HV21'] = series['GOOG_Ret'].rolling(21).std() * np.sqrt(252) * 100 series['AAPL_HV21'] = series['AAPL_Ret'].rolling(21).std() * np.sqrt(252) * 100 series[['GOOG_HV21','AAPL_HV21','SPX_HV21']].plot(figsize=(15,8), grid= True)
<matplotlib.axes._subplots.AxesSubplot at 0x7fb0b1f56908>
WARNING: Some output was deleted.
AAPL_TS=series['AAPL_Ret']
def hurst(AAPL_TS): # Create the range of lag values lags= range (2,100) # Calculate the array of variances of the lagged differences tau=[sqrt(std(subtract(AAPL_TS[lag:], AAPL_TS[-lag]))) for lag in lags] # Use a linear fit to estimate the hurst exponent poly = polyfit(log(lags), log(tau), 1) # Return the hurst exponent from the polyfit output return poly[0]*2.0
hurst(AAPL_TS)
-0.029860508371503839
series.loc[:, 'AAPL_Ret'].plot(grid = True)
<matplotlib.axes._subplots.AxesSubplot at 0x7fb0b1df9278>
WARNING: Some output was deleted.
AAPL_Price=series['AAPL']
def hurst(AAPL_Price): # Create the range of lag values lags= range (2,100) # Calculate the array of variances of the lagged differences tau=[sqrt(std(subtract(AAPL_Price[lag:], AAPL_Price[-lag]))) for lag in lags] # Use a linear fit to estimate the hurst exponent poly = polyfit(log(lags), log(tau), 1) # Return the hurst exponent from the polyfit output return poly[0]*2.0
hurst(AAPL_Price)
0.0010023252339726358
AAPL_Price2=series['2000-08-31':'2008-08-31']['AAPL']
AAPL_Price2.head()
Date 2000-08-31 2.935255 2000-09-01 3.055676 2000-09-05 3.007508 2000-09-06 2.814835 2000-09-07 2.986434 Name: AAPL, dtype: float64
AAPL_Price2.tail()
Date 2008-08-25 16.622875 2008-08-26 16.727882 2008-08-27 16.827114 2008-08-28 16.737513 2008-08-29 16.331944 Name: AAPL, dtype: float64
hurst(AAPL_Price2)
0.00350593165566435
AAPL_Price3=series['2008-08-31':'2018-04-30']['AAPL']
hurst(AAPL_Price3)
-0.0052954428489820054
AAPL_Price3=series['2008-08-31':'2018-04-30']['AAPL'].plot()
WARNING: Some output was deleted.
AAPL_Price16=series['2008-08-31':'2016-04-30']['AAPL']
hurst(AAPL_Price16)
-0.0067036462086532758