def test_frac_diff_ffd(self): """ Assert that for any positive real number d, 1. Length of the output is the same as the length of the input 2. First element is NaN """ data_series = self.data['close'].to_frame() for diff_amt in np.arange(0.1, 1, 0.1): fd_series = fracdiff.frac_diff_ffd(data_series, diff_amt=diff_amt) self.assertTrue(fd_series.shape[0] == len(data_series)) self.assertTrue(isinstance(fd_series['close'][0], np.float64) and math.isnan(fd_series['close'][0]))
def plot_min_ffd2(series): """ Advances in Financial Machine Learning, Chapter 5, section 5.6, page 85. References: * https://www.wiley.com/en-us/Advances+in+Financial+Machine+Learning-p-9781119482086 This function plots the graph to find the minimum D value that passes the ADF test. It allows to determine d - the amount of memory that needs to be removed to achieve stationarity. This function covers the case of 0 < d << 1, when the original series is "mildly non-stationary." The right y-axis on the plot is the ADF statistic computed on the input series downsampled to a daily frequency. The x-axis displays the d value used to generate the series on which the ADF statistic is computed. The left y-axis plots the correlation between the original series (d=0) and the differentiated series at various d values. Examples on how to interpret the results of this function are available in the corresponding part in the book Advances in Financial Machine Learning. :param series: (pd.DataFrame) Dataframe that contains a 'close' column with prices to use. :return: (plt.AxesSubplot) A plot that can be displayed or used to obtain resulting data. """ results = pd.DataFrame( columns=['adfStat', 'pVal', 'lags', 'nObs', '95% conf', 'corr']) # Iterate through d values with 0.1 step for d_value in np.linspace(0, 1, 11): # close_prices = np.log(series[['close']]).resample('1D').last() # Downcast to daily obs close_prices = np.log(series[['close']]) close_prices.dropna(inplace=True) print2(" ") print2(close_prices) # Applying fractional differentiation differenced_series = frac_diff_ffd(close_prices, diff_amt=d_value, thresh=0.01).dropna() # Correlation between the original and the differentiated series corr = np.corrcoef(close_prices.loc[differenced_series.index, 'close'], differenced_series['close'])[0, 1] # Applying ADF differenced_series = adfuller(differenced_series['close'], maxlag=1, regression='c', autolag=None) # Results to dataframe results.loc[d_value] = list(differenced_series[:4]) + [ differenced_series[4]['5%'] ] + [corr] # With critical value # Plotting plot = results[['adfStat', 'corr']].plot(secondary_y='adfStat', figsize=(10, 8)) plt.axhline(results['95% conf'].mean(), linewidth=1, color='r', linestyle='dotted') return plot