-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprediction_v2.py
More file actions
310 lines (254 loc) · 12.3 KB
/
prediction_v2.py
File metadata and controls
310 lines (254 loc) · 12.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
Planet Labs (PL) — Price Prediction V2 (Upgraded)
===================================================
After getting a C+ on V1, I rebuilt the prediction models to fix
all the problems. This version includes:
- ARIMA / SARIMAX via auto_arima (picks whichever has lower AIC)
- Ridge regression with engineered features
- Walk-forward validation for every model
- Bootstrap prediction intervals
What I learned: simple curve-fitting doesn't work for stocks.
You need autocorrelation-aware models, real features, and proper
out-of-sample testing.
Outputs: results.json, prices.csv
Disclaimer: Educational project only — not financial advice.
"""
import json
import os
from datetime import datetime, timedelta, timezone
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression, RidgeCV
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
TICKER = "PL"
OUTPUT_DIR = os.path.join("data", "planet-labs", "predictions")
FORECAST_DAYS = 126
RANDOM_SEED = 42
def load_data():
"""Get PL price history + basic fundamentals from Yahoo Finance."""
try:
import yfinance as yf
except ImportError:
os.system("pip install yfinance")
import yfinance as yf
stock = yf.Ticker(TICKER)
hist = stock.history(period="max").reset_index()
info = stock.info
fundamentals = {k: info.get(v) for k, v in {
"pe_ratio": "trailingPE", "price_to_sales": "priceToSalesTrailing12Months",
"revenue_growth": "revenueGrowth", "gross_margin": "grossMargins",
"operating_margin": "operatingMargins",
}.items()}
return hist, fundamentals, info
def build_features(hist):
"""Engineer features: lagged returns, SMAs, volatility, volume, momentum."""
df = hist.copy()
close = df["Close"]
for period in (1, 5, 21):
df[f"ret_{period}d"] = close.pct_change(period)
for window in (10, 50, 200):
df[f"sma_{window}"] = close.rolling(window).mean()
df["price_vs_sma50"] = close / df["sma_50"] - 1
df["price_vs_sma200"] = close / df["sma_200"] - 1
df["sma_50_vs_200"] = df["sma_50"] / df["sma_200"] - 1
df["vol_21d"] = df["ret_1d"].rolling(21).std()
df["vol_63d"] = df["ret_1d"].rolling(63).std()
if "Volume" in df.columns:
df["vol_ratio"] = df["Volume"] / df["Volume"].rolling(20).mean()
df["vol_trend"] = df["Volume"].rolling(5).mean() / df["Volume"].rolling(20).mean()
df["momentum_14d"] = df["ret_1d"].rolling(14).apply(lambda x: (x > 0).sum() / len(x), raw=True)
df["day_of_week"] = pd.to_datetime(df["Date"]).dt.dayofweek
return df.dropna().reset_index(drop=True)
def build_v1_baseline(hist):
"""V1 linear + polynomial baseline, kept for comparison."""
df = hist.copy()
df["date_ordinal"] = df["Date"].apply(lambda x: x.toordinal())
X, y = df["date_ordinal"].values.reshape(-1, 1), df["Close"].values
linear = LinearRegression().fit(X, y)
poly = np.poly1d(np.polyfit(df["date_ordinal"].values, y, 3))
return {
"linear": linear, "poly_model": poly,
"r2_linear": r2_score(y, linear.predict(X)),
"r2_poly": r2_score(y, poly(df["date_ordinal"].values)),
"mae_linear": mean_absolute_error(y, linear.predict(X)),
"mae_poly": mean_absolute_error(y, poly(df["date_ordinal"].values)),
}
def build_arima_model(close_prices):
"""Fit ARIMA via auto_arima. Keeps it simple — no SARIMAX to avoid crashes."""
import pmdarima as pm
from pmdarima.arima import ndiffs
vals = close_prices.values # plain numpy — no pandas index issues
d = ndiffs(vals, alpha=0.05, test="adf", max_d=3)
model = pm.auto_arima(vals, d=d, seasonal=False, stepwise=True,
error_action="ignore", suppress_warnings=True,
max_p=5, max_q=5, trace=False)
return model
def arima_walk_forward(close_prices, n_test=30):
"""Walk-forward: fit once, predict+update one day at a time."""
import pmdarima as pm
from pmdarima.arima import ndiffs
vals = close_prices.values # plain numpy array
train = vals[:-n_test]
d = ndiffs(train, alpha=0.05, test="adf", max_d=3)
model = pm.auto_arima(train, d=d, seasonal=False, stepwise=True,
error_action="ignore", suppress_warnings=True,
max_p=5, max_q=5)
actuals, preds = [], []
for i in range(n_test):
# predict returns numpy array when input was numpy
pred = float(model.predict(n_periods=1)[0])
actual = float(vals[len(train) + i])
preds.append(pred)
actuals.append(actual)
model.update([actual])
actuals, preds = np.array(actuals), np.array(preds)
return {
"actuals": actuals, "predictions": preds,
"mae": float(mean_absolute_error(actuals, preds)),
"rmse": float(np.sqrt(mean_squared_error(actuals, preds))),
"r2": float(r2_score(actuals, preds)),
"directional_accuracy": float(np.mean(np.sign(np.diff(preds)) == np.sign(np.diff(actuals))) * 100),
}
FEATURE_COLS = [
"ret_1d", "ret_5d", "ret_21d",
"price_vs_sma50", "price_vs_sma200", "sma_50_vs_200",
"vol_21d", "vol_63d", "momentum_14d",
]
def _get_feature_cols(df):
cols = list(FEATURE_COLS)
if "vol_ratio" in df.columns:
cols.extend(["vol_ratio", "vol_trend"])
return cols
def ridge_walk_forward(df_features, n_test=63):
"""Walk-forward for Ridge: train on past, predict next day, slide forward."""
cols = _get_feature_cols(df_features)
df = df_features.copy()
df["target"] = df["Close"].shift(-1) / df["Close"] - 1
df = df.dropna(subset=cols + ["target"]).reset_index(drop=True)
train_size = len(df) - n_test
actuals, preds, p_actual, p_pred = [], [], [], []
for i in range(n_test):
train = df.iloc[:train_size + i]
row = df.iloc[train_size + i]
scaler = StandardScaler()
X_s = scaler.fit_transform(train[cols].values)
ridge = RidgeCV(alphas=[0.01, 0.1, 1.0, 10.0, 100.0], cv=5).fit(X_s, train["target"].values)
pred_ret = ridge.predict(scaler.transform(row[cols].values.reshape(1, -1)))[0]
actuals.append(row["target"])
preds.append(pred_ret)
p_actual.append(row["Close"] * (1 + row["target"]))
p_pred.append(row["Close"] * (1 + pred_ret))
actuals, preds = np.array(actuals), np.array(preds)
p_actual, p_pred = np.array(p_actual), np.array(p_pred)
return {
"actuals": p_actual, "predictions": p_pred,
"mae": float(mean_absolute_error(p_actual, p_pred)),
"rmse": float(np.sqrt(mean_squared_error(p_actual, p_pred))),
"r2": float(r2_score(p_actual, p_pred)),
"directional_accuracy": float(np.mean(np.sign(preds) == np.sign(actuals)) * 100),
}
def bootstrap_forecast(model, n_ahead=FORECAST_DAYS, n_boot=1000):
"""Prediction intervals via bootstrap-resampled residuals."""
residuals = np.array(model.resid()) # force numpy
base = np.array(model.predict(n_periods=n_ahead)) # force numpy
boots = np.array([base + np.cumsum(np.random.choice(residuals, n_ahead, replace=True)) * 0.3
for _ in range(n_boot)])
return {
"forecast": base,
"ci_90_lower": np.percentile(boots, 5, axis=0),
"ci_90_upper": np.percentile(boots, 95, axis=0),
"ci_50_lower": np.percentile(boots, 25, axis=0),
"ci_50_upper": np.percentile(boots, 75, axis=0),
}
def save_summary(hist, v1, arima_model, arima_wf, ridge_wf, ci, fundamentals, price):
summary = {
"ticker": TICKER, "company": "Planet Labs PBC",
"version": "v2 — ARIMA + Ridge + walk-forward validation",
"current_price": round(price, 2),
"run_date": datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC"),
"training_days": len(hist),
"v1_baseline": {
"note": "Kept for comparison — no out-of-sample validation",
"linear_regression": {"r2_train": round(v1["r2_linear"], 4), "mae_train": round(v1["mae_linear"], 2)},
"polynomial_deg3": {"r2_train": round(v1["r2_poly"], 4), "mae_train": round(v1["mae_poly"], 2)},
},
"v2_models": {
"arima": {
"order": list(arima_model.order), "aic": round(float(arima_model.aic()), 2),
"walk_forward": {
"test_days": len(arima_wf["actuals"]), "mae": round(arima_wf["mae"], 2),
"rmse": round(arima_wf["rmse"], 2), "r2_oos": round(arima_wf["r2"], 4),
"directional_accuracy_pct": round(arima_wf["directional_accuracy"], 1),
},
"forecast_6mo": {
"point": round(float(ci["forecast"][-1]), 2),
"ci_90": [round(float(ci["ci_90_lower"][-1]), 2), round(float(ci["ci_90_upper"][-1]), 2)],
"ci_50": [round(float(ci["ci_50_lower"][-1]), 2), round(float(ci["ci_50_upper"][-1]), 2)],
},
},
"ridge_regression": {
"features_used": ["returns", "SMA crossovers", "volatility", "volume", "momentum"],
"walk_forward": {
"test_days": len(ridge_wf["actuals"]), "mae": round(ridge_wf["mae"], 2),
"rmse": round(ridge_wf["rmse"], 2), "r2_oos": round(ridge_wf["r2"], 4),
"directional_accuracy_pct": round(ridge_wf["directional_accuracy"], 1),
},
},
},
"fundamentals_snapshot": {k: round(v, 4) if isinstance(v, float) else v
for k, v in fundamentals.items() if v is not None},
"improvements_over_v1": [
"ARIMA handles autocorrelation", "Ridge uses multiple features",
"All metrics are out-of-sample", "Prediction intervals show uncertainty",
],
"disclaimer": "Educational model only. Not financial advice.",
}
filepath = os.path.join(OUTPUT_DIR, "results.json")
with open(filepath, "w") as f:
json.dump(summary, f, indent=2)
print(f" Saved: {filepath}")
def save_predictions_csv(ci, last_date):
future = pd.bdate_range(start=last_date + timedelta(days=1), periods=FORECAST_DAYS)
n = min(len(future), len(ci["forecast"]))
pd.DataFrame({
"Date": future[:n].strftime("%Y-%m-%d"),
"v2_ARIMA": np.round(ci["forecast"][:n], 2),
"v2_ARIMA_CI90_Lower": np.round(ci["ci_90_lower"][:n], 2),
"v2_ARIMA_CI90_Upper": np.round(ci["ci_90_upper"][:n], 2),
}).to_csv(os.path.join(OUTPUT_DIR, "prices.csv"), index=False)
print(f" Saved: {os.path.join(OUTPUT_DIR, 'prices.csv')}")
def main():
np.random.seed(RANDOM_SEED)
print(f"{'=' * 60}\n Planet Labs (PL) — Price Prediction V2 (Upgraded)\n{'=' * 60}")
os.makedirs(OUTPUT_DIR, exist_ok=True)
print("\n[1/7] Loading data...")
hist, fundamentals, _ = load_data()
price = float(hist["Close"].iloc[-1])
last_date = hist["Date"].iloc[-1]
print(f" {len(hist)} days, ${price:.2f}")
print("\n[2/7] Engineering features...")
df_feat = build_features(hist)
print("\n[3/7] V1 baseline...")
v1 = build_v1_baseline(hist)
print(f" Linear R²={v1['r2_linear']:.4f} Poly R²={v1['r2_poly']:.4f}")
print("\n[4/7] Fitting ARIMA...")
arima_model = build_arima_model(hist["Close"])
print(f" Order: {arima_model.order}, AIC: {arima_model.aic():.2f}")
print("\n[5/7] ARIMA walk-forward (predict+update)...")
arima_wf = arima_walk_forward(hist["Close"], n_test=30)
print(f" R²={arima_wf['r2']:.4f} MAE=${arima_wf['mae']:.2f} Dir={arima_wf['directional_accuracy']:.1f}%")
print("\n[6/7] Ridge walk-forward...")
ridge_wf = ridge_walk_forward(df_feat, n_test=63)
print(f" R²={ridge_wf['r2']:.4f} MAE=${ridge_wf['mae']:.2f} Dir={ridge_wf['directional_accuracy']:.1f}%")
print("\n[7/7] Forecasting with prediction intervals...")
ci = bootstrap_forecast(arima_model)
end_p, lo, hi = float(ci["forecast"][-1]), float(ci["ci_90_lower"][-1]), float(ci["ci_90_upper"][-1])
print(f" 6mo: ${end_p:.2f} [{lo:.2f}, {hi:.2f}]")
save_summary(hist, v1, arima_model, arima_wf, ridge_wf, ci, fundamentals, price)
save_predictions_csv(ci, last_date)
print(f"\n{'=' * 60}")
print(f" ARIMA R²(OOS): {arima_wf['r2']:.4f} Ridge R²(OOS): {ridge_wf['r2']:.4f}")
print(f"{'=' * 60}")
if __name__ == "__main__":
main()