| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | import os |
| 6 | from statsmodels.graphics.tsaplots import plot_acf |
| 7 | import pandas as pd |
| 8 | import matplotlib.pyplot as plt |
| 9 | from statsmodels.tsa.seasonal import STL |
| 10 | |
| 11 | |
| 12 | plt.rcParams["figure.figsize"] = (12, 6) |
| 13 | |
| 14 | |
| 15 | os.makedirs("assets", exist_ok=True) |
| 16 | |
| 17 | |
| 18 | data = 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 | |
| 29 | |
| 30 | series = data["Price"].iloc[:365*8] |
| 31 | |
| 32 | |
| 33 | |
| 34 | series_daily = series.resample("D").sum() |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | plot_acf(series_daily, lags=365) |
| 45 | plt.savefig("assets/btc_autocorrelation.png") |
| 46 | |
| 47 | result = STL(endog=series_daily, period=365).fit() |
| 48 | result.plot() |
| 49 | plt.savefig("assets/btc_stl_decomposition.png") |
| 50 | |
| 51 | |
| 52 | |