BTC Changepoint detection using Roerich (Direct Density Ratio Estimation)
2 years ago in Python
import numpy as np
import pandas as pd
import roerich
import matplotlib.pyplot as plt
#Mung and Pickling the raw CSV data
#qtr_year = 131490
#btc_df = pd.read_csv("bitstampUSD_1-min_data_2012-01-01_to_2021-03-31.csv")
#btc_df.tail(qtr_year).to_pickle('qtr_year.pkl')
# 2nd stage of data munging
btc_df = pd.read_pickle('qtr_year.pkl')
close_prices = btc_df['Close'].values
min_price = np.nanmin(close_prices)
max_price = np.nanmax(close_prices)
close_prices = np.array([(x - min_price)/max_price for x in close_prices])
close_prices = close_prices[np.isfinite(close_prices)]
n_samples = len(close_prices)
T = np.arange(n_samples)
cpd = roerich.OnlineNNClassifier(net='default', scaler="default", metric="KL_sym",
periods=1, window_size=1000, lag_size=5000, step=1000, n_epochs=10,
lr=0.1, lam=0.0001, optimizer="Adam"
)
# Detect change points
score, peaks = cpd.predict(close_prices)
print('score', score)
print('peaks', peaks)
fig, axs = plt.subplots(2, 1)
axs[0].plot(T, close_prices)
axs[0].vlines(peaks, 0, 1, transform=axs[0].get_xaxis_transform(), colors='r')
axs[1].plot(T, score)
plt.show()