irongit
time-series-cookbook/ch1/bitcoin_seasonality.py
52 lines1.5 KBPython
1"""
2Personal interest to see if bitcoin has seasonality.
3"""
4
5import os
6from statsmodels.graphics.tsaplots import plot_acf
7import pandas as pd
8import matplotlib.pyplot as plt
9from statsmodels.tsa.seasonal import STL
10
11# Chart config
12plt.rcParams["figure.figsize"] = (12, 6)
13
14# Ensure assets directory
15os.makedirs("assets", exist_ok=True)
16
17# Load solar data
18data = pd.read_csv(
19 "../assets/btc_yearly.csv",
20 parse_dates=["Date"],
21 index_col="Date",
22 thousands=",",
23 converters={
24 "Change %": lambda x: float(x.rstrip("%")) / 100,
25 "Vol.": lambda x: pd.to_numeric(x.replace("K", "000").replace("M", "000000").replace('B', '000000000') if x != '' else pd.NA),
26 },
27)
28# cutoff = data.index.max() - pd.DateOffset(years=5) # pyright: ignore[reportOperatorIssue]
29# series = data["Price"]
30series = data["Price"].iloc[:365*8]
31# print(data['vol'])
32
33# Resample to daily
34series_daily = series.resample("D").sum()
35
36# Compute autocorrelation, acf = Auto Correlation Function
37# Compares against lags up to a year away
38# acf_scores = acf(x=series_daily, nlags=365)
39
40# Compute partial autocorrelation
41# pacf_scores = pacf(x=series_daily, nlags=365)
42
43# Convience functions to do math and plot
44plot_acf(series_daily, lags=365)
45plt.savefig("assets/btc_autocorrelation.png")
46
47result = STL(endog=series_daily, period=365).fit()
48result.plot()
49plt.savefig("assets/btc_stl_decomposition.png")
50
51# plot_pacf(series_daily, lags=365)
52# plt.savefig("assets/partion_autocorrelation.png")