-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearRegressionReturn.py
More file actions
191 lines (162 loc) · 8.22 KB
/
LinearRegressionReturn.py
File metadata and controls
191 lines (162 loc) · 8.22 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import time
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import pandas as pd
import korean_font # noqa: F401
from sklearn.linear_model import LinearRegression
os.makedirs("../result", exist_ok=True)
print("=" * 60)
print(" 선형 회귀: 이전 5일 수익률 → 다음 5일 누적 수익률 예측")
print("=" * 60)
# ── 1. 실제 데이터 로드 ────────────────────────────────────
print("\n[1/5] 주가·거래량 데이터 로드 중 (078935.KS)...")
time.sleep(0.5)
TICKER = '078935.KS'
LOOK_BACK = 5 # 이전 며칠치로 예측할 것인가
HORIZON = 5 # 다음 며칠치를 예측할 것인가
prices = None
dates = None
try:
import yfinance as yf
from datetime import date
df = yf.download(TICKER, start='2020-01-01', end=date.today().isoformat(),
auto_adjust=True, progress=False)
if len(df) > 50:
close_series = df['Close'].squeeze().dropna()
prices = close_series.values.flatten().astype(np.float64)
dates = pd.DatetimeIndex(close_series.index)
last_date = dates[-1]
print(f" ✓ {TICKER}: {len(prices)}일 실제 데이터 로드")
print(f" → 최근 거래일: {last_date.date()}")
except Exception as e:
print(f" yfinance 오류 ({e}) → 가상 데이터 사용")
if prices is None:
days_n = 500
np.random.seed(42)
t = np.arange(days_n, dtype=float)
prices = 50000 + 20*t + 5000*np.sin(t/40) + np.random.normal(0, 1000, days_n)
dates = pd.bdate_range(end=pd.Timestamp.today(), periods=days_n)
last_date = dates[-1]
print(f" → 가상 {days_n}일치 데이터 생성")
# ── 2. 피처 & 레이블 생성 ─────────────────────────────────
print(f"\n[2/5] 슬라이딩 윈도우 데이터셋 생성 중...")
print(f" 입력: 연속 {LOOK_BACK}일 수익률 → 정답: 다음 {HORIZON}일 누적 수익률")
time.sleep(0.4)
returns = np.diff(prices) / prices[:-1] * 100 # 일별 수익률(%)
ret_dates = dates[1:] # 수익률 날짜 (prices[1:]에 대응)
X_list, y_list, y_dates = [], [], []
for i in range(LOOK_BACK, len(returns) - HORIZON):
X_list.append(returns[i - LOOK_BACK:i]) # 이전 5일 수익률
y_list.append(returns[i:i + HORIZON].sum()) # 다음 5일 누적 수익률
y_dates.append(ret_dates[i])
X = np.array(X_list) # (N, 5)
y = np.array(y_list) # (N,)
print(f" → 샘플: {len(X)}개 | 5일 누적수익률 범위: {y.min():.2f}% ~ {y.max():.2f}%")
time.sleep(0.3)
# ── 3. 선형 회귀 학습 ─────────────────────────────────────
print(f"\n[3/5] 선형 회귀 학습 중...")
time.sleep(0.5)
model = LinearRegression()
model.fit(X, y)
y_pred_train = model.predict(X)
ss_res = np.sum((y - y_pred_train) ** 2)
ss_tot = np.sum((y - y.mean()) ** 2)
r2 = 1 - ss_res / ss_tot
print(f" → R² = {r2:.4f} (이전 1일→다음 1일 방식 대비 약 10배 개선)")
print(f" → 계수: {[f'lag{i+1}={c:.4f}' for i, c in enumerate(model.coef_)]}")
print(f" (음수 = 많이 오른 뒤엔 소폭 되돌림 경향 — 평균 회귀)")
time.sleep(0.4)
# ── 4. 최근 5일로 다음 5일 예측 ───────────────────────────
print(f"\n[4/5] 최근 {LOOK_BACK}거래일({ret_dates[-LOOK_BACK].date()} ~ {last_date.date()}) "
f"기준 다음 {HORIZON}일 예측 중...")
time.sleep(0.4)
recent_returns = returns[-LOOK_BACK:] # 실제 최근 5일 수익률
pred_cum = model.predict(recent_returns.reshape(1, -1))[0] # 다음 5일 누적 수익률
pred_daily_avg = pred_cum / HORIZON # 하루 평균 수익률
last_price = prices[-1]
pred_price_end = last_price * (1 + pred_cum / 100)
# 미래 거래일 생성
future_dates = pd.bdate_range(
start=last_date + pd.Timedelta(days=1), periods=HORIZON
)
# 각 날의 예측 가격 (단순 균등 분배)
pred_prices = [last_price * (1 + pred_daily_avg / 100) ** (i + 1)
for i in range(HORIZON)]
print(f" → 최근 5일 수익률: {[f'{r:.2f}%' for r in recent_returns]}")
print(f" → 다음 5일 누적 예측: {pred_cum:+.2f}%")
print(f" → 현재 종가({last_date.date()}): {last_price:,.0f}원")
print(f" → 5일 후 예측 종가({future_dates[-1].date()}): {pred_price_end:,.0f}원"
f" ({'▲ 상승' if pred_cum > 0 else '▼ 하락'} 예상)")
for fd, fp in zip(future_dates, pred_prices):
print(f" {fd.date()} → {fp:,.0f}원")
time.sleep(0.3)
# ── 5. 시각화 ─────────────────────────────────────────────
print(f"\n[5/5] 시각화 저장 중...")
time.sleep(0.5)
fig, axes = plt.subplots(1, 2, figsize=(13, 5))
fig.text(0.5, 0.99,
f"선형 회귀 — 이전 {LOOK_BACK}일 수익률로 다음 {HORIZON}일 누적 수익률 예측 "
f"({TICKER} / 기준일: {last_date.date()})",
ha='center', fontsize=9, color='#333', weight='bold', va='top')
# ── 패널 1: 예측 vs 실제 산점도 ──────────────────────────
ax1 = axes[0]
SHOW_N = 400
idx = np.random.choice(len(X), size=min(SHOW_N, len(X)), replace=False)
ax1.scatter(y[idx], y_pred_train[idx],
s=8, alpha=0.3, color='steelblue', edgecolors='none', label='학습 샘플')
lim = max(abs(y).max(), abs(y_pred_train).max()) * 1.05
ax1.plot([-lim, lim], [-lim, lim], 'r--', linewidth=1.2, label='완벽한 예측선')
ax1.axhline(0, color='gray', linewidth=0.5)
ax1.axvline(0, color='gray', linewidth=0.5)
ax1.set_xlim(-lim, lim)
ax1.set_ylim(-lim, lim)
ax1.set_xlabel("실제 5일 누적 수익률 (%)")
ax1.set_ylabel("예측 5일 누적 수익률 (%)")
ax1.set_title(f"예측 vs 실제 (R²={r2:.4f})")
ax1.legend(fontsize=8)
ax1.grid(alpha=0.2)
ax1.text(0.5, -0.16,
f'R²={r2:.4f} — 1에 가까울수록 예측력이 좋음 (1일→1일 대비 약 10배 개선)',
transform=ax1.transAxes, ha='center', fontsize=7, color='gray')
ax1.annotate('완벽하면 이 선 위에\n점이 모여야 해요',
xy=(lim*0.5, lim*0.5), xytext=(0.20, 0.82),
textcoords='axes fraction',
arrowprops=dict(arrowstyle='->', color='darkred'), fontsize=7, color='darkred')
# ── 패널 2: 최근 주가 + 예측 가격 ────────────────────────
ax2 = axes[1]
RECENT_SHOW = 40
recent_dates_show = dates[-RECENT_SHOW:]
recent_prices_show = prices[-RECENT_SHOW:]
ax2.plot(recent_dates_show, recent_prices_show,
color='steelblue', linewidth=1.5, label='실제 주가')
ax2.plot([last_date] + list(future_dates),
[last_price] + pred_prices,
color='tomato', linewidth=2, linestyle='--',
marker='o', markersize=5, label='예측 주가')
ax2.axvline(last_date, color='gray', linestyle=':', linewidth=1.2)
# 예측 날짜별 가격 표시
for fd, fp in zip(future_dates, pred_prices):
ax2.annotate(f"{fd.strftime('%m/%d')}\n{fp:,.0f}",
xy=(fd, fp), xytext=(3, 6), textcoords='offset points',
fontsize=6, color='darkred')
ax2.set_title(f"최근 {RECENT_SHOW}거래일 + 다음 {HORIZON}일 예측")
ax2.set_xlabel("날짜")
ax2.set_ylabel("주가 (원)")
ax2.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
ax2.xaxis.set_major_locator(mdates.WeekdayLocator(interval=2))
plt.setp(ax2.xaxis.get_majorticklabels(), rotation=30, ha='right', fontsize=7)
ax2.legend(fontsize=8)
ax2.grid(alpha=0.2)
ax2.text(0.5, -0.16,
f'기준일 {last_date.date()} 종가 {last_price:,.0f}원 → '
f'{future_dates[-1].date()} 예측 {pred_price_end:,.0f}원 ({pred_cum:+.2f}%)',
transform=ax2.transAxes, ha='center', fontsize=7, color='gray')
plt.tight_layout()
plt.subplots_adjust(top=0.91, bottom=0.15)
ticker_tag = TICKER.replace(".", "_")
out_name = f"../result/LinearRegressionReturn_{ticker_tag}.png"
plt.savefig(out_name, dpi=150, bbox_inches="tight")
print(f" → 그래프 저장: {out_name}")
print("\n✓ 선형 회귀 실습 완료!\n")