1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
rng = np.random.default_rng(2)
dates = pd.date_range("2021-01-01", periods=260, freq="D")
driver = np.sin(2 * np.pi * np.arange(len(dates)) / 45)
series_a = 120 + 8 * driver + rng.normal(0, 2.0, size=len(dates))
series_b = 100 + 6 * driver + rng.normal(0, 2.2, size=len(dates))
rolling_corr = (
pd.Series(series_a, index=dates)
.rolling(window=30, min_periods=20)
.corr(pd.Series(series_b, index=dates))
)
fig, axs = plt.subplots(2, 1, figsize=(7, 5), sharex=True)
axs[0].plot(dates, series_a, color="#2563eb", label="系列A")
axs[0].plot(dates, series_b, color="#f97316", label="系列B", alpha=0.85)
axs[0].set_ylabel("値")
axs[0].set_title("系列Aと系列Bの推移")
axs[0].legend(loc="upper left")
axs[0].grid(alpha=0.3)
axs[1].plot(rolling_corr.index, rolling_corr, color="#10b981", linewidth=1.8, label="30日ローリング相関")
axs[1].axhline(0, color="#475569", linewidth=1, linestyle=":")
axs[1].axhline(0.5, color="#94a3b8", linewidth=1, linestyle="--")
axs[1].axhline(-0.5, color="#94a3b8", linewidth=1, linestyle="--")
axs[1].set_ylabel("相関")
axs[1].set_xlabel("日付")
axs[1].set_title("ローリング相関(30日窓)")
axs[1].grid(alpha=0.3)
fig.tight_layout()
fig.savefig("static/images/timeseries/rolling_corr.svg")
|