<jupyter_start><jupyter_text>Arfima Time Series Simulation  Table of Contents [1. Fractional Differencing](1)[2. Arfima Simulation testing](2)<jupyter_code>import numpy as np
import pandas as pd
from datetime import datetime as dt

import matplotlib.pyplot as plt
%matplotlib inline

plt.rcParams['font.size'] = 13

datasets_folder = "datasets/"

btc = pd.read_csv(datasets_folder + 'coin_Bitcoin.csv')
btc['Date'] = [dt.strptime(x, '%Y-%m-%d %H:%M:%S') for x in btc['Date']]

date_condition = "2017-10-02 23:59:59"
btc = btc.iloc[btc.index[btc["Date"] == date_condition].tolist()[0]:]
btc.set_index('Date', inplace = True)
close_price = btc['Close']
# btc.to_csv('BitcoinTransformed.csv')
%run ArfimaSimulation.ipynb<jupyter_output><empty_output><jupyter_text>1. Fractional Differencing<jupyter_code>def plot_weights(dRange, nPlots, length):
    w = pd.DataFrame()
    for d in np.linspace(dRange[0], dRange[1], nPlots):
        w_ = get_weights(d, length)     
        w_ = pd.DataFrame(w_, index=range(w_.shape[0])[::-1],
                          columns=[round(d, 2)])
        w = w.join(w_, how='outer')

    w[np.isnan(w)] = 0
    ax = w.plot(figsize=(20,10))
    ax.legend(loc='upper right');
    ax.set_facecolor('#383838')
    ax.grid(True)

    plt.show()

plot_weights(dRange=[0, 1], nPlots=11, length=5)
df_result = fracdiff_FFD(btc[['Close']], 0.4244, 1e-5)
df_result
fig, ax1 = plt.subplots(figsize=(20,10))

color1 = 'tab:blue'
ax1.plot(df_result.index, df_result['Close'], color=color1)
ax1.set_facecolor('#383838')
ax1.set_ylabel('Differentiated prices', color=color1)
ax1.set_title('Fractionally differentiated BTC close price',
          fontsize=16)
ax1.grid(True)

color2 = 'red'
ax2 = ax1.twinx()
ax2.plot(df_result.index, btc['Close'].values[-(df_result.shape[0]):], color=color2, label='Prices')
ax2.set_ylabel('Prices', color=color2)
plt.show()
from statsmodels.tsa.stattools import adfuller

adf = adfuller(df_result['Close'])
print(f"p-value of BTC: {float(adf[1])}")<jupyter_output>p-value of BTC: 0.032108751903561515<jupyter_text>2. Arfima Simulation testing<jupyter_code>from numpy.testing import assert_equal, assert_almost_equal
from statsmodels.tsa.arima.model import ARIMA
# stationary white noise

white_noise = arfima_sim([], 0, [], 1000)
arma = ARIMA(white_noise, order=(0,0,0)).fit()
print(arma.summary())

adf = adfuller(white_noise)
assert_almost_equal(adf[1], 0.00000, decimal=4)
# stationary ARIMA([0.3, 0.25, 0.4], 1, [0.2], 1000)
    
arima_series = arfima_sim([0.3, 0.25, 0.4], 1, [0.5], 1000, noise_alpha=0.25)
arima = ARIMA(arima_series, order=(3,1,1)).fit()
print(arima.summary())

adf = adfuller(arima_series)
assert_almost_equal(adf[1], 0.00000, decimal=4)
# stationary ARFIMA([0.3], 0.7, [-0.4], 1000])
    
arfima_series = arfima_sim([0.3], 0.7, [-0.4], 1000)

adf = adfuller(arfima_series)
assert_almost_equal(adf[1], 0.00000, decimal=4)
arfima_series = arfima_sim([0.5], 0.5, [-0.4], 1000, noise_std=0.1, noise_alpha=1.8)

fig, ax1 = plt.subplots(figsize=(20,10))

color1 = 'tab:blue'
ax1.plot(np.arange(0, len(arfima_series)), arfima_series, color=color1)
ax1.set_facecolor('#383838')
ax1.set_ylabel('Generated timeseries', color=color1)
ax1.set_title('Arfima generated series', fontsize=16)
ax1.grid(True)

color2 = 'red'
ax2 = ax1.twinx()
ax2.plot(np.arange(0, len(arfima_series)), btc['Close'].values[-(len(arfima_series)):], color=color2, label='Prices')
ax2.set_ylabel('Prices', color=color2)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1759656652360515, 'fraction_numerical': 0.06580829756795423, 'mean_word_length': 5.736030828516378, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '605058', 'license': 'Apache-2.0', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1438, 'n_words': 249, 'path': 'Jupyter-crypto_analysis/ArfimaUsage.ipynb', 'repo_name': 'MaxPayne5100/CryptoAnalyzer', 'size': '3495'}