-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek9.html
More file actions
366 lines (312 loc) · 13.9 KB
/
Copy pathweek9.html
File metadata and controls
366 lines (312 loc) · 13.9 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Week 9: Model Assumptions (IP §3b) — QR Training</title>
<link rel="stylesheet" href="styles.css">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;600;700&family=IBM+Plex+Sans:wght@400;600;700&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<link rel="icon" type="image/png" href="Delta.png">
</head>
<body>
<nav>
<div class="nav-brand">
<img src="ALGO.png" alt="AlgoGators" class="nav-logo-img">
<span class="nav-divider">|</span>
<span class="nav-section">QR Training Hub</span>
</div>
<div class="nav-links">
<a href="qr-home.html">Home</a>
<a href="week1.html">W1</a>
<a href="week2.html">W2</a>
<a href="week3.html">W3</a>
<a href="week4.html">W4</a>
<a href="week5.html">W5</a>
<a href="week6.html">W6</a>
<a href="week7.html">W7</a>
<a href="week8.html">W8</a>
<a href="week9.html" class="active">W9</a>
<a href="week10.html">W10</a>
</div>
</nav>
<div class="week-hero">
<canvas class="week-hero-canvas" id="w9-canvas"></canvas>
<div class="week-hero-inner">
<div class="week-hero-badge">Week 09 — Phase 3: Research Execution</div>
<h1>Model Assumptions<br>(IP §3b)</h1>
<div class="week-hero-pill">Section 3b — Model Assumptions</div>
<p class="week-hero-objective">Test and document every model assumption. This IS Section 3b of your IP. The most critical section for QT risk management.</p>
</div>
</div>
<div class="container">
<div class="ip-anchor">
<span class="ip-anchor-label">IP Anchor</span>
<span class="ip-anchor-section">Section 3b — Model assumptions</span>
<span class="ip-anchor-desc">Mandatory every time you use a model. QT uses this section to adjust risk management. Document everything.</span>
</div>
<div class="section">
<h2 class="section-title">What this week covers</h2>
<p>This is the most technically detailed week. Section 3b is a direct reference for completing the assumption-testing portion of every IP. You will learn to test five critical assumptions (covered in Week 5), plus the special case of cointegration for pairs trading. Every test, result, and implication goes in Section 3b.</p>
</div>
<div class="section">
<h2 class="section-title">Section 3b checklist</h2>
<p>Copy this checklist into your IP. Complete every row. Report the test, the result, and what you'll do if it fails.</p>
<table style="font-size: 0.9rem;">
<thead>
<tr>
<th>Assumption</th>
<th>Test</th>
<th>Python</th>
<th>Fail condition</th>
<th>If it fails</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Normality of residuals</strong></td>
<td>Jarque-Bera, Shapiro-Wilk</td>
<td><code>scipy.stats.jarque_bera</code></td>
<td>p < 0.05</td>
<td>Use robust methods; report fat-tail risk</td>
</tr>
<tr>
<td><strong>Stationarity</strong></td>
<td>ADF, KPSS</td>
<td><code>statsmodels.adfuller</code></td>
<td>ADF p > 0.05</td>
<td>Difference series; use returns not prices</td>
</tr>
<tr>
<td><strong>No autocorrelation</strong></td>
<td>Ljung-Box</td>
<td><code>statsmodels.acorr_ljungbox</code></td>
<td>p < 0.05</td>
<td>Use Newey-West HAC standard errors</td>
</tr>
<tr>
<td><strong>Homoscedasticity</strong></td>
<td>Breusch-Pagan</td>
<td><code>statsmodels.het_breuschpagan</code></td>
<td>p < 0.05</td>
<td>Use HC3/HC4 robust SE or GARCH model</td>
</tr>
<tr>
<td><strong>No multicollinearity</strong></td>
<td>VIF</td>
<td><code>statsmodels.variance_inflation_factor</code></td>
<td>VIF > 10</td>
<td>Drop or combine correlated features</td>
</tr>
</tbody>
</table>
</div>
<div class="section">
<h2 class="section-title">Deep dive: Cointegration (pairs trading)</h2>
<p>For pairs strategies, you're not testing stationarity of individual series — you're testing stationarity of the spread (the linear combination).</p>
<h3 style="margin-top: 1.5rem;">Engle-Granger cointegration test</h3>
<p>Two series are cointegrated if a linear combination of them is stationary.</p>
<p style="margin-top: 1rem;"><strong>Method:</strong></p>
<ol style="font-size: 0.95rem;">
<li>Regress y on x: \(y_t = \alpha + \beta x_t + \varepsilon_t\)</li>
<li>Extract residuals ε</li>
<li>Test residuals for stationarity (ADF test)</li>
<li>If residuals are stationary, the pair is cointegrated</li>
</ol>
<p style="margin-top: 1.5rem;"><strong>Python:</strong></p>
<pre><code>from statsmodels.tsa.stattools import coint
score, p_value, critical_values = coint(series_a, series_b)
print(f"Cointegration test p-value: {p_value:.4f}")
# p < 0.05 → cointegrated at 5% significance
</code></pre>
<h3 style="margin-top: 1.5rem;">Johansen test (multiple assets)</h3>
<p>For strategies with 3+ assets, use Johansen's cointegration test.</p>
<p>\[ \Delta \mathbf{y}_t = \Pi \mathbf{y}_{t-1} + \sum_{i=1}^{p-1} \Gamma_i \Delta \mathbf{y}_{t-i} + \varepsilon_t \]</p>
<p style="margin-top: 1rem;"><strong>Interpretation:</strong> The rank of Π determines the number of cointegrating relationships (linearly independent stationary combinations).</p>
</div>
<div class="section">
<h2 class="section-title">Section 3b template with real example</h2>
<div class="card" style="background: var(--bg-elevated); font-size: 0.9rem;">
<p style="color: var(--accent); font-weight: 600;">SECTION 3B: MODEL ASSUMPTIONS — Ridge Regression Commodity Strategy</p>
<p style="margin-top: 1rem;"><strong>Strategy uses a ridge regression to predict corn returns from 3 factors:</strong></p>
<ul style="margin-left: 1.5rem; font-size: 0.9rem;">
<li>Factor 1: 30-day momentum</li>
<li>Factor 2: 60-day seasonality (deviation from 10-year average)</li>
<li>Factor 3: COT speculator positioning (normalized)</li>
</ul>
<p style="margin-top: 1rem;"><strong>3b.1 Normality of residuals</strong></p>
<p>Test: Jarque-Bera on regression residuals (2020–2023 in-sample)</p>
<p>Result: JB stat = 12.4, p-value = 0.002 → reject normality</p>
<p>Implication: Residuals have fat tails. Daily returns exhibit kurtosis = 4.2 (excess = 1.2). Documented.</p>
<p>Action: Report expected maximum drawdown assuming fat tails. Use robust standard errors (HC3).</p>
<p style="margin-top: 1rem;"><strong>3b.2 Stationarity of predictors</strong></p>
<p>Test: ADF on each factor</p>
<ul style="margin-left: 1.5rem; font-size: 0.85rem;">
<li>Momentum (30-day): ADF stat = -8.7, p < 0.001 → stationary ✓</li>
<li>Seasonality (deviation): ADF stat = -9.2, p < 0.001 → stationary ✓</li>
<li>COT positioning (normalized): ADF stat = -6.4, p < 0.001 → stationary ✓</li>
</ul>
<p>All factors are stationary. No issues.</p>
<p style="margin-top: 1rem;"><strong>3b.3 No autocorrelation in residuals</strong></p>
<p>Test: Ljung-Box on residuals, lags 1–20</p>
<p>Result: All p-values > 0.05. No significant autocorrelation detected. ✓</p>
<p style="margin-top: 1rem;"><strong>3b.4 Homoscedasticity</strong></p>
<p>Test: Breusch-Pagan heteroscedasticity test</p>
<p>Result: BP stat = 18.3, p = 0.0003 → heteroscedasticity present</p>
<p>Action: Volatility clusters (expected in commodity markets). Use HC3 robust standard errors for inference.</p>
<p style="margin-top: 1rem;"><strong>3b.5 No multicollinearity</strong></p>
<p>Test: Variance Inflation Factor on 3 factors</p>
<ul style="margin-left: 1.5rem; font-size: 0.85rem;">
<li>Momentum: VIF = 1.3</li>
<li>Seasonality: VIF = 1.1</li>
<li>COT: VIF = 1.2</li>
</ul>
<p>All VIF < 2. No multicollinearity concerns. ✓</p>
<p style="margin-top: 1rem;"><strong>Summary:</strong> Ridge regression suitable. Primary concerns: fat-tail risk (documented), heteroscedasticity (robust SE applied). Model assumptions documented and acceptable for live trading.</p>
</div>
</div>
<div class="section">
<h2 class="section-title">Assumption testing code patterns</h2>
<h3 style="margin-top: 1.5rem;">Normality (Jarque-Bera)</h3>
<pre><code>from scipy import stats
jb_stat, jb_p = stats.jarque_bera(residuals)
print(f"Jarque-Bera: stat={jb_stat:.4f}, p={jb_p:.4f}")
if jb_p < 0.05:
print("Residuals are NOT normally distributed (fat tails likely)")
else:
print("Residuals are consistent with normality")
</code></pre>
<h3 style="margin-top: 1.5rem;">Stationarity (ADF)</h3>
<pre><code>from statsmodels.tsa.stattools import adfuller
result = adfuller(series, autolag='AIC')
print(f"ADF stat: {result[0]:.4f}, p-value: {result[1]:.4f}")
if result[1] < 0.05:
print("Series is stationary (reject unit root)")
else:
print("Series is non-stationary (unit root present)")
</code></pre>
<h3 style="margin-top: 1.5rem;">Multicollinearity (VIF)</h3>
<pre><code>from statsmodels.stats.outliers_influence import variance_inflation_factor
import pandas as pd
vif_data = pd.DataFrame()
vif_data["Feature"] = X.columns
vif_data["VIF"] = [
variance_inflation_factor(X.values, i)
for i in range(X.shape[1])
]
print(vif_data)
if (vif_data["VIF"] > 10).any():
print("Multicollinearity detected. Consider dropping features.")
</code></pre>
</div>
<div class="section">
<h2 class="section-title">Common mistakes</h2>
<div class="mistakes-list">
<h4 style="margin-top: 0;">Five assumption-testing failures</h4>
<ul>
<li><strong>Skipping Section 3b entirely.</strong> It's mandatory. At minimum: test residuals for normality and autocorrelation, test series for stationarity. Report results.</li>
<li><strong>Running ADF but not reporting it in Section 3b.</strong> If you ran the test, report it. The p-value, the interpretation, what you'll do if it fails.</li>
<li><strong>Building a multi-factor model without checking VIF.</strong> Correlated factors will cancel each other out in live trading, even if they work in backtest. Check VIF before submitting.</li>
<li><strong>Ignoring fat tails.</strong> "Close enough to normal" is not a valid analysis. If Jarque-Bera p < 0.05, document the deviation. Report expected extreme drawdown.</li>
<li><strong>Using price levels in regression.</strong> Prices are non-stationary. Test with ADF first. If non-stationary, use returns or cointegration.</li>
</ul>
</div>
</div>
<div class="week-nav">
<a href="week8.html" class="week-nav-button">← Week 8: Signal Construction</a>
<a href="week10.html" class="week-nav-button">Week 10: Backtest Results →</a>
</div>
</div>
<footer>
AlgoGators Investment Fund — QR Training Program // Internal Use Only
</footer>
<script>
/* Week 10 — morphing Gaussian distribution */
(function () {
const hero = document.querySelector('.week-hero');
const canvas = document.getElementById('w9-canvas');
const ctx = canvas.getContext('2d');
let W, H;
function resize() {
const r = hero.getBoundingClientRect();
W = canvas.width = Math.round(r.width);
H = canvas.height = Math.round(r.height);
}
function gauss(x, mu, sig) {
return Math.exp(-0.5 * ((x - mu) / sig) ** 2) / (sig * Math.sqrt(2 * Math.PI));
}
const STATES = [
{ mu: 0.50, sig: 0.10 },
{ mu: 0.58, sig: 0.13 },
{ mu: 0.50, sig: 0.17 },
{ mu: 0.50, sig: 0.07 },
];
let sIdx = 0, sT = 0, lastTs = null;
function ease(t) { return t < 0.5 ? 2*t*t : -1+(4-2*t)*t; }
function lerp(a, b, t) { return a + (b - a) * t; }
function frame(ts) {
if (!lastTs) lastTs = ts;
const dt = Math.min(ts - lastTs, 40);
lastTs = ts;
sT += dt * 0.00042;
if (sT >= 1) { sT = 0; sIdx = (sIdx + 1) % STATES.length; }
const from = STATES[sIdx];
const to = STATES[(sIdx + 1) % STATES.length];
const et = ease(sT);
const mu = lerp(from.mu, to.mu, et);
const sig = lerp(from.sig, to.sig, et);
ctx.clearRect(0, 0, W, H);
const peak = gauss(mu, mu, sig);
const scY = H * 0.72;
const baseY = H * 0.88;
/* fill */
ctx.beginPath();
ctx.moveTo(0, baseY);
for (let i = 0; i <= W; i++) {
const v = gauss(i / W, mu, sig);
ctx.lineTo(i, baseY - (v / peak) * scY);
}
ctx.lineTo(W, baseY);
ctx.closePath();
ctx.fillStyle = 'rgba(255,125,42,0.055)';
ctx.fill();
/* curve */
ctx.beginPath();
for (let i = 0; i <= W; i++) {
const v = gauss(i / W, mu, sig);
const y = baseY - (v / peak) * scY;
i === 0 ? ctx.moveTo(i, y) : ctx.lineTo(i, y);
}
ctx.strokeStyle = 'rgba(255,125,42,0.48)';
ctx.lineWidth = 1.8;
ctx.stroke();
/* mean line */
ctx.beginPath();
ctx.moveTo(mu * W, baseY);
ctx.lineTo(mu * W, baseY - scY * 1.06);
ctx.strokeStyle = 'rgba(255,125,42,0.16)';
ctx.lineWidth = 1;
ctx.setLineDash([3, 6]);
ctx.stroke();
ctx.setLineDash([]);
/* ±1σ ticks */
for (const s of [-1, 1]) {
const sx = (mu + s * sig) * W;
const sv = gauss(mu + s * sig, mu, sig);
ctx.beginPath();
ctx.moveTo(sx, baseY);
ctx.lineTo(sx, baseY - (sv / peak) * scY);
ctx.strokeStyle = 'rgba(255,238,215,0.10)';
ctx.lineWidth = 1;
ctx.stroke();
}
requestAnimationFrame(frame);
}
new ResizeObserver(resize).observe(hero);
resize();
requestAnimationFrame(frame);
})();
</script>
<script src="cursor.js"></script>
</body>
</html>