-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquant_alpha_proof.py
More file actions
383 lines (298 loc) · 17.5 KB
/
Copy pathquant_alpha_proof.py
File metadata and controls
383 lines (298 loc) · 17.5 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from manim import *
import numpy as np
import random
config.background_color = "#0A0A0F" # Slightly deeper, richer cinematic black
class QuantAlphaProof(Scene):
def construct(self):
self.TEAL = "#00E5C3"
self.AMBER = "#F5A623"
self.RED = "#FF3366"
self.GREEN = "#20C997"
self.GRAY = "#8B949E"
# Veritasium-style sleek progress bar at the very bottom edge
total_estimated_time = 240
progress_bar = Rectangle(width=0, height=0.03, color=self.TEAL, fill_opacity=1).to_edge(DOWN, buff=0).to_edge(LEFT, buff=0)
progress_bar.add_updater(lambda m: m.become(
Rectangle(
width=max(0.01, min(1.0, self.renderer.time / total_estimated_time) * config.frame_width),
height=0.03, color=self.TEAL, fill_opacity=1
).to_edge(DOWN, buff=0).to_edge(LEFT, buff=0)
).set_z_index(100))
self.add(progress_bar)
self.scene1_architecture()
self.scene2_aggregation()
self.scene3_regime()
self.scene4_aria()
self.scene5_execution()
self.scene6_convergence()
self.scene7_closing()
def show_cinematic_title(self, text):
title = Text(text, font_size=48, weight=BOLD, color=WHITE)
title.set_color_by_gradient(self.TEAL, WHITE)
self.play(FadeIn(title, shift=UP*0.5, scale=0.9), run_time=2)
self.wait(1.5)
self.play(FadeOut(title, shift=UP*0.5, scale=1.1), run_time=1.5)
def scene1_architecture(self):
self.show_cinematic_title("1. System Architecture")
# State vector
state_vec = MathTex(
r"\mathbf{S}_t = [\alpha_t, \sigma_t, \mu_t, L_t, \Gamma_t]",
color=WHITE
).scale(1.1).to_edge(UP, buff=1)
state_vec[0][0:2].set_color(self.TEAL) # Highlight S_t
self.play(Write(state_vec), run_time=1.5)
# Elegant layout for Nodes
labels = ["Market Data", "Hypothesis\nGenerators", "Risk\nEngine", "Execution\nRouter"]
nodes = VGroup()
for i, label in enumerate(labels):
box = RoundedRectangle(corner_radius=0.2, height=1.2, width=2.5, color=self.GRAY, fill_color="#161B22", fill_opacity=0.8)
txt = Text(label, font_size=20, color=WHITE, line_spacing=0.8).move_to(box.get_center())
nodes.add(VGroup(box, txt))
nodes.arrange(RIGHT, buff=1.0).shift(DOWN * 0.5)
self.play(AnimationGroup(*[FadeIn(n, shift=UP*0.3) for n in nodes], lag_ratio=0.2), run_time=2.5)
# Sweeping edges
edges = VGroup()
for i in range(len(nodes)-1):
edge = Arrow(nodes[i].get_right(), nodes[i+1].get_left(), color=self.GRAY, buff=0.1, stroke_width=3, max_tip_length_to_length_ratio=0.1)
edges.add(edge)
self.play(Create(edges), run_time=1.5)
# Data flow pulses (cinematic glow dots)
for _ in range(2):
pulses = VGroup()
for edge in edges:
pulse = Dot(color=self.TEAL, radius=0.08).move_to(edge.get_start())
glow = Dot(color=self.TEAL, radius=0.2, fill_opacity=0.3).move_to(pulse)
pulses.add(VGroup(pulse, glow))
self.add(pulses)
self.play(
AnimationGroup(
*[MoveAlongPath(p, e, rate_func=smooth) for p, e in zip(pulses, edges)],
lag_ratio=0.3
),
run_time=2
)
# Pulse the nodes as data arrives
self.play(
AnimationGroup(
*[n[0].animate.set_color(self.TEAL).set_stroke(width=4) for n in nodes[1:]],
lag_ratio=0.3
),
run_time=0.5
)
self.play(*[n[0].animate.set_color(self.GRAY).set_stroke(width=2) for n in nodes[1:]], run_time=0.5)
self.remove(pulses)
self.play(FadeOut(VGroup(nodes, edges, state_vec), shift=DOWN*0.5), run_time=1.5)
def scene2_aggregation(self):
self.show_cinematic_title("2. Hypothesis Aggregation")
eq1 = MathTex(
r"\hat{S}_t = \sum_{i=1}^{N} w_i \cdot h_i(X_t) \quad \text{s.t.} \quad \sum w_i = 1"
).scale(1.1).to_edge(UP, buff=1)
self.play(FadeIn(eq1, shift=DOWN*0.3), run_time=1.5)
axes = Axes(
x_range=[-5, 5, 1], y_range=[0, 1.2, 0.2], x_length=10, y_length=4.5,
axis_config={"color": self.GRAY, "stroke_width": 2}
).shift(DOWN * 0.5)
self.play(Create(axes), run_time=1.5)
colors = color_gradient([self.RED, self.AMBER, self.TEAL, BLUE], 9)
np.random.seed(42)
gaussians = VGroup()
for i in range(9):
mu = np.random.uniform(-3, 3)
sigma = np.random.uniform(1.0, 2.5)
curve = axes.plot(lambda x: 1/(sigma * np.sqrt(2 * np.pi)) * np.exp(-0.5 * ((x - mu)/sigma)**2), color=colors[i], stroke_opacity=0.6)
gaussians.add(curve)
self.play(AnimationGroup(*[Create(g) for g in gaussians], lag_ratio=0.1), run_time=3)
eq2 = MathTex(r"w_i^{t+1} \propto w_i^t \cdot P(r_t | h_i)", color=self.AMBER).next_to(eq1, DOWN, buff=0.3)
self.play(Write(eq2), run_time=1.5)
# Cinematic Bayesian collapse
target_curve = axes.plot(lambda x: 1/(0.4 * np.sqrt(2 * np.pi)) * np.exp(-0.5 * (x/0.4)**2), color=self.TEAL, stroke_width=5)
target_fill = axes.get_area(target_curve, color=self.TEAL, opacity=0.3)
self.play(
AnimationGroup(
*[Transform(g, target_curve.copy().set_stroke(opacity=0.1)) for g in gaussians],
lag_ratio=0.05
),
run_time=3.5
)
self.play(FadeIn(target_curve), FadeIn(target_fill), run_time=1)
self.wait(1.5)
self.play(FadeOut(VGroup(eq1, eq2, axes, gaussians, target_curve, target_fill)), run_time=1.5)
def scene3_regime(self):
self.show_cinematic_title("3. Volatility Regimes")
# To prevent overlap, divide the screen into Left (HMM) and Right (Math)
node_bull = Circle(radius=0.6, color=self.GREEN, fill_color=self.GREEN, fill_opacity=0.2).move_to(LEFT*4 + UP*2)
node_bear = Circle(radius=0.6, color=self.RED, fill_color=self.RED, fill_opacity=0.2).move_to(LEFT*6 + DOWN*0.5)
node_side = Circle(radius=0.6, color=self.AMBER, fill_color=self.AMBER, fill_opacity=0.2).move_to(LEFT*2 + DOWN*0.5)
t_bull = Text("Bull", font_size=20).move_to(node_bull)
t_bear = Text("Bear", font_size=20).move_to(node_bear)
t_side = Text("Sideways", font_size=20).move_to(node_side)
nodes = VGroup(node_bull, node_bear, node_side, t_bull, t_bear, t_side)
a1 = CurvedArrow(node_bull.get_bottom(), node_bear.get_top(), angle=TAU/8, color=self.GRAY)
a2 = CurvedArrow(node_bear.get_right(), node_side.get_left(), angle=TAU/8, color=self.GRAY)
a3 = CurvedArrow(node_side.get_top(), node_bull.get_right(), angle=TAU/8, color=self.GRAY)
hmm_group = VGroup(nodes, a1, a2, a3).shift(LEFT * 0.5)
# Math perfectly isolated on the right
trans_matrix = MathTex(
r"A = \begin{bmatrix} P(H|H) & P(B|H) & P(S|H) \\ P(H|B) & P(B|B) & P(S|B) \\ P(H|S) & P(B|S) & P(S|S) \end{bmatrix}"
).scale(0.7).move_to(RIGHT*3 + UP*1.5)
emission = MathTex(
r"P(x_t | z_t) = \mathcal{N}(\mu_{z_t}, \sigma^2_{z_t})"
).scale(0.85).next_to(trans_matrix, DOWN, buff=0.8)
self.play(FadeIn(hmm_group, shift=RIGHT*0.5), run_time=2)
self.play(Write(trans_matrix), Write(emission), run_time=2)
# Axes spanning the bottom cleanly
axes = Axes(
x_range=[0, 10, 1], y_range=[0, 10, 2], x_length=12, y_length=3,
axis_config={"color": self.GRAY}
).to_edge(DOWN, buff=1)
path_points = []
np.random.seed(101)
val = 4.0
for x in np.linspace(0, 10, 120):
if x < 3.5: # Bull
val += np.random.normal(0.12, 0.1)
elif x < 7: # Bear
val += np.random.normal(-0.15, 0.3)
else: # Sideways
val += np.random.normal(0, 0.15)
path_points.append(axes.c2p(x, val))
price_line = VMobject(color=WHITE, stroke_width=3).set_points_as_corners(path_points)
self.play(Create(axes), run_time=1)
self.play(Create(price_line), run_time=4, rate_func=linear)
# Heatmap underneath perfectly aligned below axis
heatmap = VGroup()
for i in range(120):
x = i * (10 / 120)
if x < 3.5:
color = self.GREEN
elif x < 7:
color = self.RED
else:
color = self.AMBER
rect = Rectangle(width=12/120, height=0.3, color=color, fill_opacity=0.6, stroke_width=0)
rect.move_to(axes.c2p(x + (10/240), -1))
heatmap.add(rect)
self.play(FadeIn(heatmap, lag_ratio=0.05), run_time=2)
self.wait(1.5)
self.play(FadeOut(VGroup(hmm_group, trans_matrix, emission, axes, price_line, heatmap)), run_time=1.5)
def scene4_aria(self):
self.show_cinematic_title("4. ARIA Failure Modes")
# 4a: Cleaned up spacing
title_4a = Text("A. Liquidity Derailment (LOB)", font_size=28, color=self.TEAL).to_edge(UP, buff=1)
eq_lob = MathTex(r"\frac{\partial P}{\partial t} + v \cdot \frac{\partial P}{\partial x} = \sigma \cdot \Delta P").next_to(title_4a, DOWN, buff=0.5)
axes_4a = Axes(x_range=[0, 5, 1], y_range=[0, 5, 1], x_length=6, y_length=3, axis_config={"color": self.GRAY}).next_to(eq_lob, DOWN, buff=0.5)
true_lob = axes_4a.plot(lambda x: x, color=WHITE, stroke_width=4)
div_lob = axes_4a.plot(lambda x: x + 0.3*x**2, color=self.RED, stroke_width=4)
self.play(FadeIn(title_4a), Write(eq_lob), Create(axes_4a), Create(true_lob), run_time=2)
self.play(Create(div_lob), run_time=1.5)
eq_corr = MathTex(r"R(t) = \alpha \int_0^t e^{-\lambda(t-s)} \varepsilon(s) ds", color=self.AMBER).move_to(eq_lob)
corr_lob = axes_4a.plot(lambda x: x + 0.1*np.sin(4*x)*np.exp(-x/2), color=self.TEAL, stroke_width=4)
self.play(ReplacementTransform(eq_lob, eq_corr), Transform(div_lob, corr_lob), run_time=2)
self.wait(1)
self.play(FadeOut(VGroup(title_4a, eq_corr, axes_4a, true_lob, div_lob)), run_time=1)
# 4b: Sequential clearing avoids all overlaps
title_4b = Text("B. Hyper-Rational Breakdown", font_size=28, color=self.TEAL).to_edge(UP, buff=1)
eq_nash = MathTex(r"|U_i(s^*) - U_i(s_i, s^*_{-i})| \le \epsilon", color=WHITE).next_to(title_4b, DOWN, buff=0.5)
axes_4b = Axes(x_range=[0, 5, 1], y_range=[0, 1, 0.5], x_length=6, y_length=3, axis_config={"color": self.GRAY}).next_to(eq_nash, DOWN, buff=0.5)
overfit = axes_4b.plot(lambda x: 1 - np.exp(-3*x), color=self.RED, stroke_width=4)
bounded = axes_4b.plot(lambda x: 0.7 - 0.7*np.exp(-1.5*x), color=self.TEAL, stroke_width=4)
self.play(FadeIn(title_4b), Write(eq_nash), Create(axes_4b), Create(overfit), run_time=2.5)
self.play(Transform(overfit, bounded), run_time=2)
self.wait(1)
self.play(FadeOut(VGroup(title_4b, eq_nash, axes_4b, overfit)), run_time=1)
# 4c: Beautiful NumberPlane descent
title_4c = Text("C. ABM Calibration", font_size=28, color=self.TEAL).to_edge(UP, buff=1)
eq_loss = MathTex(r"\mathcal{L}(\theta) = ||m_{sim}(\theta) - m_{real}||^2_W").next_to(title_4c, DOWN, buff=0.5)
plane = NumberPlane(x_range=[-3, 3, 1], y_range=[-3, 3, 1], x_length=6, y_length=4, background_line_style={"stroke_opacity": 0.3}).next_to(eq_loss, DOWN, buff=0.5)
self.play(FadeIn(title_4c), Write(eq_loss), Create(plane), run_time=2)
dot = Dot(plane.c2p(-2.5, 2.5), color=self.RED, radius=0.1)
self.play(FadeIn(dot, scale=0))
path = VMobject(color=self.AMBER, stroke_width=3)
path_points = [plane.c2p(-2.5, 2.5), plane.c2p(-1, 0.8), plane.c2p(0.5, -0.2), plane.c2p(1.2, 0)]
path.set_points_smoothly(path_points)
self.play(MoveAlongPath(dot, path), Create(path), run_time=3, rate_func=smooth)
pulse = Circle(radius=0.1, color=self.TEAL).move_to(dot)
self.play(dot.animate.set_color(self.TEAL), pulse.animate.scale(5).set_opacity(0), run_time=1.5)
self.wait(1)
self.play(FadeOut(VGroup(title_4c, eq_loss, plane, dot, path)), run_time=1.5)
def scene5_execution(self):
self.show_cinematic_title("5. Risk-Adjusted Execution")
eqs = VGroup(
MathTex(r"f^* = \frac{\mu_p - r_f}{\gamma \cdot \sigma^2_p}"),
MathTex(r"SR = \frac{R_p - r_f}{\sigma_p}"),
MathTex(r"MDD = \max \frac{\Delta P}{P_{peak}}")
).arrange(DOWN, buff=0.8, aligned_edge=LEFT).to_edge(LEFT, buff=1.5)
eqs[0][0][0:2].set_color(self.TEAL) # Highlight Kelly variable
self.play(AnimationGroup(*[Write(eq) for eq in eqs], lag_ratio=0.3), run_time=3)
axes = Axes(x_range=[0, 10, 2], y_range=[0, 20, 5], x_length=7, y_length=5, axis_config={"color": self.GRAY}).to_edge(RIGHT, buff=1)
np.random.seed(202)
curve_pts, upper_pts, lower_pts = [], [], []
val = 5.0
for x in np.linspace(0, 10, 100):
val *= np.exp(np.random.normal(0.015, 0.04))
curve_pts.append(axes.c2p(x, val))
conf_spread = 0.5 + (x * 0.15)
upper_pts.append(axes.c2p(x, val + conf_spread))
lower_pts.append(axes.c2p(x, val - conf_spread))
equity = VMobject(color=WHITE, stroke_width=3).set_points_as_corners(curve_pts)
self.play(Create(axes), run_time=1)
self.play(Create(equity), run_time=3.5, rate_func=linear)
poly_pts = upper_pts + lower_pts[::-1]
conf_region = Polygon(*poly_pts, color=self.TEAL, stroke_width=0, fill_opacity=0.2)
self.play(FadeIn(conf_region), equity.animate.set_color(self.TEAL), run_time=2)
stats = Text("SR: 1.87 α: 0.23 MDD: 4.2%", font_size=24, color=self.AMBER, weight=BOLD).next_to(axes, UP, buff=0.3)
self.play(Write(stats), run_time=1.5)
self.wait(1.5)
self.play(FadeOut(VGroup(eqs, axes, equity, conf_region, stats)), run_time=1.5)
def scene6_convergence(self):
self.show_cinematic_title("6. Convergence Proof")
thm1 = MathTex(r"\lim_{T\to\infty} \frac{1}{T} \sum_{t=1}^T r_t = \mathbb{E}^*[r] \text{ a.s.}").scale(1.2).to_edge(UP, buff=1)
self.play(FadeIn(thm1, shift=DOWN*0.3), run_time=1.5)
axes = Axes(x_range=[0, 100, 20], y_range=[-0.5, 1, 0.5], x_length=10, y_length=4, axis_config={"color": self.GRAY}).shift(UP * 0.2)
# Proper DashedLine to completely bypass the previous TypeError
target = DashedLine(start=axes.c2p(0, 0.2), end=axes.c2p(100, 0.2), color=self.TEAL, dash_length=0.1)
np.random.seed(303)
samples = np.random.normal(0.2, 0.8, 100)
avgs = [np.mean(samples[:i]) for i in range(1, 101)]
avg_pts = [axes.c2p(i, avgs[i-1]) for i in range(1, 101)]
avg_line = VMobject(color=WHITE, stroke_width=3).set_points_as_corners(avg_pts)
self.play(Create(axes), Create(target), run_time=2)
self.play(Create(avg_line), run_time=4, rate_func=smooth)
thm2 = MathTex(
r"P(|\hat{S}_T - \mathbb{E}[S]| \ge \epsilon) \le 2 \exp\left(-\frac{2T^2\epsilon^2}{\sum c_i^2}\right)",
color=self.AMBER
).next_to(axes, DOWN, buff=0.5)
self.play(Write(thm2), run_time=2)
self.wait(1.5)
self.play(FadeOut(VGroup(thm1, axes, target, avg_line, thm2)), run_time=1.5)
def scene7_closing(self):
title = Text("QuantAlpha", font_size=80, weight=BOLD)
title.set_color_by_gradient(self.TEAL, "#0088FF")
math_objs = VGroup(
MathTex(r"\Sigma"), MathTex(r"\int"), MathTex(r"\mathbb{E}"),
MathTex(r"\alpha"), MathTex(r"\sigma"), MathTex(r"\mu"),
MathTex(r"\hat{S}_t"), MathTex(r"\mathcal{N}"), MathTex(r"\nabla"),
MathTex(r"\infty"), MathTex(r"\theta^*"), MathTex(r"\Gamma")
)
for obj in math_objs:
obj.set_color(WHITE).set_opacity(0.4).scale(1.5)
theta = random.uniform(0, TAU)
r = random.uniform(4, 8)
obj.move_to([r * np.cos(theta), r * np.sin(theta), 0])
self.play(AnimationGroup(*[FadeIn(m, scale=0.5) for m in math_objs], lag_ratio=0.1), run_time=2)
self.play(
AnimationGroup(
*[m.animate.move_to(ORIGIN).scale(0.1).set_opacity(0).rotate(PI) for m in math_objs],
lag_ratio=0.0
),
run_time=2.5,
rate_func=rush_into
)
self.play(FadeIn(title, scale=0.8), run_time=1.5)
subtitle = Text(
"Targeting NeurIPS 2026 — NSE Level-3 Calibrated — Multi-Agent Autonomous Alpha",
font_size=22, color=self.GRAY
).next_to(title, DOWN, buff=0.6)
self.play(Write(subtitle), run_time=2)
self.wait(3)
self.play(FadeOut(VGroup(title, subtitle), scale=1.1), run_time=2)