statanim 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- statanim/__init__.py +0 -0
- statanim/animations/__init__.py +0 -0
- statanim/animations/clt_demo.py +1852 -0
- statanim/animations/flip_roll.py +1851 -0
- statanim/animations/sampling.py +2291 -0
- statanim/animations/transitions.py +1857 -0
- statanim/axes/__init__.py +0 -0
- statanim/axes/axes3d.py +1989 -0
- statanim/axes/grid3d.py +1127 -0
- statanim/axes/number_plane3d.py +1526 -0
- statanim/charts/__init__.py +0 -0
- statanim/charts/bar_chart3d.py +1572 -0
- statanim/charts/box_plot3d.py +1977 -0
- statanim/charts/heat_map3d.py +1639 -0
- statanim/charts/histogram3d.py +1019 -0
- statanim/charts/line_plot3d.py +1891 -0
- statanim/charts/scatter_plot3d.py +1469 -0
- statanim/charts/violin_plot3d.py +1567 -0
- statanim/core/__init__.py +0 -0
- statanim/core/base.py +1585 -0
- statanim/core/colors.py +1373 -0
- statanim/core/math_utils.py +2081 -0
- statanim/core/tex_utils.py +2111 -0
- statanim/distributions/__init__.py +0 -0
- statanim/distributions/base_dist.py +1936 -0
- statanim/distributions/cdf_viz.py +1773 -0
- statanim/distributions/continuous_dists.py +1769 -0
- statanim/distributions/discrete_dists.py +1734 -0
- statanim/distributions/normal3d.py +1877 -0
- statanim/distributions/pdf_viz.py +2247 -0
- statanim/distributions/pmf_viz.py +1936 -0
- statanim/inference/__init__.py +0 -0
- statanim/inference/confidence_interval.py +1693 -0
- statanim/inference/error_types.py +1825 -0
- statanim/inference/hypothesis.py +1821 -0
- statanim/inference/sampling_dist.py +1933 -0
- statanim/probability/__init__.py +0 -0
- statanim/probability/bayes.py +1682 -0
- statanim/probability/prob_tree.py +1478 -0
- statanim/probability/sample_space.py +1642 -0
- statanim/probability/venn3d.py +1857 -0
- statanim/props/__init__.py +0 -0
- statanim/props/card.py +2259 -0
- statanim/props/coin.py +1011 -0
- statanim/props/die.py +1391 -0
- statanim/props/spinner.py +1591 -0
- statanim/props/urn.py +1531 -0
- statanim/regression/__init__.py +0 -0
- statanim/regression/correlation.py +2805 -0
- statanim/regression/regression_plane.py +2286 -0
- statanim/regression/residuals.py +2599 -0
- statanim/scenes/__init__.py +0 -0
- statanim/scenes/demo_bayes.py +2056 -0
- statanim/scenes/demo_clt.py +1756 -0
- statanim/scenes/demo_distributions.py +1905 -0
- statanim/scenes/demo_hypothesis.py +1791 -0
- statanim/ui/__init__.py +0 -0
- statanim/ui/labels.py +1844 -0
- statanim/ui/panels.py +1937 -0
- statanim/ui/table3d.py +1745 -0
- statanim/ui/ticker.py +2007 -0
- statanim-0.1.0.dist-info/METADATA +463 -0
- statanim-0.1.0.dist-info/RECORD +66 -0
- statanim-0.1.0.dist-info/WHEEL +5 -0
- statanim-0.1.0.dist-info/licenses/LICENSE +21 -0
- statanim-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,2056 @@
|
|
|
1
|
+
"""
|
|
2
|
+
statanim/scenes/demo_bayes.py
|
|
3
|
+
==================================
|
|
4
|
+
BayesDemo — A complete, cinematic Bayes' theorem demonstration scene.
|
|
5
|
+
|
|
6
|
+
Story arc (7 acts)
|
|
7
|
+
------------------
|
|
8
|
+
Act 0 Cold open
|
|
9
|
+
A single urn emerges from darkness with a "?" above it.
|
|
10
|
+
Title card: "Which urn did this ball come from?"
|
|
11
|
+
|
|
12
|
+
Act 1 Setup — two urns
|
|
13
|
+
Urn A (3 red, 2 blue) and Urn B (1 red, 4 blue) appear side by side.
|
|
14
|
+
Ball counts animate in. "One urn was chosen at random."
|
|
15
|
+
|
|
16
|
+
Act 2 Prior
|
|
17
|
+
Prior probability bar: P(A) = 0.5 │ P(B) = 0.5
|
|
18
|
+
Animated split with labels and percentage badges.
|
|
19
|
+
Formula badge: "P(A) = P(B) = 1/2 (equal prior)"
|
|
20
|
+
|
|
21
|
+
Act 3 Draw — a red ball emerges
|
|
22
|
+
A red ball is drawn from an unseen urn (ShakeUrn + DrawBall).
|
|
23
|
+
"We drew a RED ball. Which urn is more likely?"
|
|
24
|
+
|
|
25
|
+
Act 4 Likelihood
|
|
26
|
+
Likelihood table animates in:
|
|
27
|
+
P(red | A) = 3/5 = 0.600
|
|
28
|
+
P(red | B) = 1/5 = 0.200
|
|
29
|
+
Each cell highlights as its value is spoken.
|
|
30
|
+
Marginal: P(red) = P(red|A)·P(A) + P(red|B)·P(B) = 0.400
|
|
31
|
+
|
|
32
|
+
Act 5 Bayes formula
|
|
33
|
+
Full formula types in one term at a time:
|
|
34
|
+
P(A | red) = P(red | A) · P(A) / P(red)
|
|
35
|
+
= 0.600 × 0.500 / 0.400
|
|
36
|
+
= 0.750
|
|
37
|
+
Each numeric substitution highlights in a matching colour.
|
|
38
|
+
|
|
39
|
+
Act 6 Posterior bar — first update
|
|
40
|
+
Prior bar morphs into posterior: P(A|red)=0.75 │ P(B|red)=0.25
|
|
41
|
+
Animated shift with counting number badges.
|
|
42
|
+
|
|
43
|
+
Act 7 Sequential updates — two more draws
|
|
44
|
+
Draw 2: another red ball → posterior shifts further toward A
|
|
45
|
+
Draw 3: a blue ball → posterior pulls back toward B
|
|
46
|
+
After each draw:
|
|
47
|
+
• Likelihood row highlights
|
|
48
|
+
• Formula re-substitutes
|
|
49
|
+
• Posterior bar morphs
|
|
50
|
+
• Posterior history trail shows all three bars stacked
|
|
51
|
+
|
|
52
|
+
Act 8 Beta distribution posterior
|
|
53
|
+
After the 3 draws (2 red, 1 blue from a Beta-Binomial model),
|
|
54
|
+
a Beta(α, β) curve replaces the discrete bar — showing the
|
|
55
|
+
continuous Bayesian posterior.
|
|
56
|
+
|
|
57
|
+
Act 9 Decision and summary
|
|
58
|
+
"Urn A is 3× more likely than Urn B."
|
|
59
|
+
Decision badge flashes on Urn A.
|
|
60
|
+
Posterior bar glows.
|
|
61
|
+
Summary panel: prior → likelihood → posterior, one line each.
|
|
62
|
+
|
|
63
|
+
Scene uses
|
|
64
|
+
----------
|
|
65
|
+
statanim.props.urn — Urn3D, Ball3D, DrawBall, ShakeUrn, FillUrn
|
|
66
|
+
(all other components built inline for self-contained demo)
|
|
67
|
+
|
|
68
|
+
Dependencies
|
|
69
|
+
------------
|
|
70
|
+
manim (CE or GL), numpy, scipy (optional)
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
from __future__ import annotations
|
|
74
|
+
|
|
75
|
+
import numpy as np
|
|
76
|
+
from typing import Optional
|
|
77
|
+
|
|
78
|
+
from manim import (
|
|
79
|
+
Scene, ThreeDScene,
|
|
80
|
+
VGroup,
|
|
81
|
+
Rectangle, RoundedRectangle, Square,
|
|
82
|
+
Circle, Annulus, Polygon, Line, DashedLine,
|
|
83
|
+
Arrow, DoubleArrow,
|
|
84
|
+
Text, MathTex, Tex,
|
|
85
|
+
Dot,
|
|
86
|
+
Animation, Succession, AnimationGroup,
|
|
87
|
+
ApplyMethod, FadeIn, FadeOut, GrowFromCenter,
|
|
88
|
+
Create, Write, Uncreate,
|
|
89
|
+
Indicate, Flash,
|
|
90
|
+
Rotate,
|
|
91
|
+
Transform, ReplacementTransform,
|
|
92
|
+
MoveToTarget,
|
|
93
|
+
ValueTracker,
|
|
94
|
+
interpolate_color, color_to_rgb,
|
|
95
|
+
always_redraw,
|
|
96
|
+
BLACK, WHITE,
|
|
97
|
+
GREY, GREY_A, GREY_B, GREY_C, GREY_D,
|
|
98
|
+
RED, RED_A, RED_B, RED_C, RED_D,
|
|
99
|
+
GREEN, GREEN_A, GREEN_B, GREEN_C, GREEN_D, GREEN_E,
|
|
100
|
+
BLUE, BLUE_A, BLUE_B, BLUE_C, BLUE_D, BLUE_E,
|
|
101
|
+
YELLOW, YELLOW_A, YELLOW_E,
|
|
102
|
+
ORANGE, TEAL, TEAL_A, TEAL_B, TEAL_C,
|
|
103
|
+
GOLD, GOLD_A, GOLD_B, GOLD_C, GOLD_D,
|
|
104
|
+
PURPLE_A, PURPLE_B, MAROON, PINK,
|
|
105
|
+
UP, DOWN, LEFT, RIGHT, OUT, IN, ORIGIN,
|
|
106
|
+
PI, TAU,
|
|
107
|
+
rate_functions,
|
|
108
|
+
ManimColor,
|
|
109
|
+
VMobject,
|
|
110
|
+
NumberLine,
|
|
111
|
+
DEGREES,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
115
|
+
# Optional urn import (graceful fallback to plain shapes if unavailable)
|
|
116
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
117
|
+
try:
|
|
118
|
+
import sys, os
|
|
119
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
120
|
+
from props.urn import Urn3D, Ball3D, DrawBall, ShakeUrn, FillUrn
|
|
121
|
+
from props.urn import get_packed_positions
|
|
122
|
+
_URN_AVAILABLE = True
|
|
123
|
+
except ImportError:
|
|
124
|
+
_URN_AVAILABLE = False
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
128
|
+
# Scene palette
|
|
129
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
130
|
+
|
|
131
|
+
P = {
|
|
132
|
+
# Urn colours
|
|
133
|
+
"urn_a": "#C1440E", # terracotta
|
|
134
|
+
"urn_b": "#1B3A8A", # cobalt
|
|
135
|
+
|
|
136
|
+
# Ball colours
|
|
137
|
+
"red_ball": "#E63946",
|
|
138
|
+
"blue_ball": "#457B9D",
|
|
139
|
+
|
|
140
|
+
# Prior / posterior bars
|
|
141
|
+
"bar_a": "#E07030", # warm orange for Urn A
|
|
142
|
+
"bar_b": "#3070C0", # cool blue for Urn B
|
|
143
|
+
"bar_bg": "#0C1018",
|
|
144
|
+
"bar_border": "#2A3848",
|
|
145
|
+
"bar_text": "#E8F0F8",
|
|
146
|
+
|
|
147
|
+
# Likelihood table
|
|
148
|
+
"tbl_bg": "#0E1420",
|
|
149
|
+
"tbl_header": "#1A2438",
|
|
150
|
+
"tbl_red_row": "#2A1010",
|
|
151
|
+
"tbl_blue_row": "#101828",
|
|
152
|
+
"tbl_border": "#2A3848",
|
|
153
|
+
"tbl_val_red": "#FF9090",
|
|
154
|
+
"tbl_val_blue": "#90C0FF",
|
|
155
|
+
"tbl_val_marg": "#D4AF37",
|
|
156
|
+
|
|
157
|
+
# Bayes formula
|
|
158
|
+
"formula_bg": "#080C14",
|
|
159
|
+
"term_prior": "#D4AF37", # gold
|
|
160
|
+
"term_like": "#E07030", # orange
|
|
161
|
+
"term_marg": "#70B060", # green
|
|
162
|
+
"term_post": "#C060E0", # purple
|
|
163
|
+
"term_eq": "#C8D8E8",
|
|
164
|
+
|
|
165
|
+
# Decision badge
|
|
166
|
+
"decision_bg": "#1A2410",
|
|
167
|
+
"decision_fg": "#60E060",
|
|
168
|
+
|
|
169
|
+
# Beta curve
|
|
170
|
+
"beta_fill": "#2A1040",
|
|
171
|
+
"beta_ridge": "#A060E0",
|
|
172
|
+
"beta_shade": "#6030A0",
|
|
173
|
+
|
|
174
|
+
# Title cards
|
|
175
|
+
"title_fg": "#D8F0FF",
|
|
176
|
+
"title_bg": "#050810",
|
|
177
|
+
"subtitle_fg": "#8090A8",
|
|
178
|
+
|
|
179
|
+
# Summary panel
|
|
180
|
+
"summary_bg": "#0C1018",
|
|
181
|
+
"summary_border":"#3A4A5A",
|
|
182
|
+
"summary_arrow":"#607080",
|
|
183
|
+
|
|
184
|
+
# General
|
|
185
|
+
"bg": "#080C12",
|
|
186
|
+
"glow": "#FFD060",
|
|
187
|
+
"axis": "#3A4A5A",
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
192
|
+
# Reusable scene-building helpers
|
|
193
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
194
|
+
|
|
195
|
+
def _title_card(
|
|
196
|
+
main: str,
|
|
197
|
+
subtitle: str = "",
|
|
198
|
+
main_fs: int = 44,
|
|
199
|
+
sub_fs: int = 22,
|
|
200
|
+
) -> VGroup:
|
|
201
|
+
"""Full-screen title card (dark bg, centred text)."""
|
|
202
|
+
g = VGroup()
|
|
203
|
+
bg = Rectangle(width=16, height=9,
|
|
204
|
+
fill_color=P["title_bg"],
|
|
205
|
+
fill_opacity=1.0, stroke_width=0)
|
|
206
|
+
g.add(bg)
|
|
207
|
+
ml = Text(main, font_size=main_fs, color=P["title_fg"],
|
|
208
|
+
font="sans-serif")
|
|
209
|
+
ml.move_to([0, 0.35 if subtitle else 0, 0])
|
|
210
|
+
g.add(ml)
|
|
211
|
+
if subtitle:
|
|
212
|
+
sl = Text(subtitle, font_size=sub_fs,
|
|
213
|
+
color=P["subtitle_fg"])
|
|
214
|
+
sl.move_to([0, -0.45, 0])
|
|
215
|
+
g.add(sl)
|
|
216
|
+
return g
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _section_label(text: str, color: str = None) -> Text:
|
|
220
|
+
"""Small act label in the corner."""
|
|
221
|
+
return Text(text, font_size=17,
|
|
222
|
+
color=color or P["subtitle_fg"])
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
226
|
+
# UrnSchematic — lightweight urn + ball counts for 2-D scene
|
|
227
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
228
|
+
|
|
229
|
+
class _UrnSchematic(VGroup):
|
|
230
|
+
"""
|
|
231
|
+
A clean 2-D schematic urn (for use in a flat Scene rather than
|
|
232
|
+
ThreeDScene), showing:
|
|
233
|
+
• Trapezoid body with coloured glaze fill
|
|
234
|
+
• Two handles (line arcs)
|
|
235
|
+
• Interior dots representing balls
|
|
236
|
+
• Label badge above (Urn A / Urn B)
|
|
237
|
+
• Prior / posterior probability badge below
|
|
238
|
+
|
|
239
|
+
Parameters
|
|
240
|
+
----------
|
|
241
|
+
label : str — "Urn A" / "Urn B"
|
|
242
|
+
n_red, n_blue: int — ball counts
|
|
243
|
+
fill_color : str — body colour
|
|
244
|
+
prior : float — prior probability (shown below)
|
|
245
|
+
"""
|
|
246
|
+
|
|
247
|
+
def __init__(
|
|
248
|
+
self,
|
|
249
|
+
label: str,
|
|
250
|
+
n_red: int,
|
|
251
|
+
n_blue: int,
|
|
252
|
+
fill_color: str,
|
|
253
|
+
prior: float = 0.5,
|
|
254
|
+
width: float = 1.60,
|
|
255
|
+
height: float = 2.10,
|
|
256
|
+
**kwargs,
|
|
257
|
+
):
|
|
258
|
+
super().__init__(**kwargs)
|
|
259
|
+
self._n_red = n_red
|
|
260
|
+
self._n_blue = n_blue
|
|
261
|
+
self._fill = fill_color
|
|
262
|
+
self._label = label
|
|
263
|
+
self._width = width
|
|
264
|
+
self._height = height
|
|
265
|
+
|
|
266
|
+
dark = interpolate_color(fill_color, BLACK, 0.45)
|
|
267
|
+
lite = interpolate_color(fill_color, WHITE, 0.28)
|
|
268
|
+
|
|
269
|
+
# ── Body (trapezoid) ──────────────────────────────────────────
|
|
270
|
+
bw = width
|
|
271
|
+
tw = width * 0.62 # top width (narrower neck)
|
|
272
|
+
h = height
|
|
273
|
+
body = Polygon(
|
|
274
|
+
[-bw/2, -h/2, 0],
|
|
275
|
+
[ bw/2, -h/2, 0],
|
|
276
|
+
[ tw/2, h/2, 0],
|
|
277
|
+
[-tw/2, h/2, 0],
|
|
278
|
+
fill_color=fill_color,
|
|
279
|
+
fill_opacity=1.0,
|
|
280
|
+
stroke_color=dark,
|
|
281
|
+
stroke_width=1.8,
|
|
282
|
+
)
|
|
283
|
+
self.add(body)
|
|
284
|
+
|
|
285
|
+
# Specular stripe
|
|
286
|
+
spec = Polygon(
|
|
287
|
+
[-tw/2 + tw*0.05, h/2, 0.001],
|
|
288
|
+
[-tw/2 + tw*0.28, h/2, 0.001],
|
|
289
|
+
[-bw/2 + bw*0.28, -h/2, 0.001],
|
|
290
|
+
[-bw/2 + bw*0.05, -h/2, 0.001],
|
|
291
|
+
fill_color=lite,
|
|
292
|
+
fill_opacity=0.25, stroke_width=0,
|
|
293
|
+
)
|
|
294
|
+
self.add(spec)
|
|
295
|
+
|
|
296
|
+
# Lip (top opening)
|
|
297
|
+
lip = Rectangle(
|
|
298
|
+
width=tw * 1.08, height=h * 0.055,
|
|
299
|
+
fill_color=lite,
|
|
300
|
+
fill_opacity=1.0, stroke_width=0,
|
|
301
|
+
)
|
|
302
|
+
lip.move_to([0, h/2 + lip.height/2, 0.001])
|
|
303
|
+
self.add(lip)
|
|
304
|
+
|
|
305
|
+
# Inner dark well
|
|
306
|
+
well = Polygon(
|
|
307
|
+
[-tw/2 + 0.05, h/2, 0.001],
|
|
308
|
+
[ tw/2 - 0.05, h/2, 0.001],
|
|
309
|
+
[ tw/2 - 0.05, h/2 - h*0.12, 0.001],
|
|
310
|
+
[-tw/2 + 0.05, h/2 - h*0.12, 0.001],
|
|
311
|
+
fill_color=interpolate_color(fill_color, BLACK, 0.70),
|
|
312
|
+
fill_opacity=1.0, stroke_width=0,
|
|
313
|
+
)
|
|
314
|
+
self.add(well)
|
|
315
|
+
|
|
316
|
+
# ── Handles ───────────────────────────────────────────────────
|
|
317
|
+
for side in [-1, 1]:
|
|
318
|
+
hx = side * bw / 2
|
|
319
|
+
handle_pts = []
|
|
320
|
+
n_pts = 10
|
|
321
|
+
for i in range(n_pts):
|
|
322
|
+
t = i / (n_pts - 1)
|
|
323
|
+
ang = PI * (0.30 + 0.40 * t) * side
|
|
324
|
+
r = bw * 0.28
|
|
325
|
+
cx = hx + side * r * 0.45
|
|
326
|
+
cy = -h * 0.05
|
|
327
|
+
handle_pts.append(
|
|
328
|
+
[cx + r * np.cos(ang),
|
|
329
|
+
cy + r * np.sin(ang) * 0.80, 0]
|
|
330
|
+
)
|
|
331
|
+
for i in range(len(handle_pts)-1):
|
|
332
|
+
self.add(Line(
|
|
333
|
+
handle_pts[i], handle_pts[i+1],
|
|
334
|
+
stroke_color=dark, stroke_width=3.5,
|
|
335
|
+
))
|
|
336
|
+
|
|
337
|
+
# ── Interior balls (dots) ─────────────────────────────────────
|
|
338
|
+
self._ball_dots = VGroup()
|
|
339
|
+
total = n_red + n_blue
|
|
340
|
+
cols = ([P["red_ball"]] * n_red
|
|
341
|
+
+ [P["blue_ball"]] * n_blue)
|
|
342
|
+
np.random.seed(hash(label) % (2**31))
|
|
343
|
+
np.random.shuffle(cols)
|
|
344
|
+
rows_n = max(1, int(np.ceil(np.sqrt(total * 0.8))))
|
|
345
|
+
dot_r = min(0.10, (bw * 0.70) / (2 * rows_n + 1))
|
|
346
|
+
row_h = (h * 0.68) / max(rows_n, 1)
|
|
347
|
+
|
|
348
|
+
placed = 0
|
|
349
|
+
for row in range(rows_n + 1):
|
|
350
|
+
y_dot = -h/2 + dot_r + row * row_h * 0.85
|
|
351
|
+
if y_dot > h/2 - dot_r * 2: break
|
|
352
|
+
n_in_row = min(rows_n + 1, total - placed)
|
|
353
|
+
row_w = n_in_row * dot_r * 2.2
|
|
354
|
+
for col in range(n_in_row):
|
|
355
|
+
if placed >= total: break
|
|
356
|
+
xd = -row_w/2 + col * dot_r * 2.2 + dot_r
|
|
357
|
+
d = Circle(
|
|
358
|
+
radius=dot_r,
|
|
359
|
+
fill_color=cols[placed],
|
|
360
|
+
fill_opacity=0.92,
|
|
361
|
+
stroke_color=interpolate_color(
|
|
362
|
+
cols[placed], BLACK, 0.25),
|
|
363
|
+
stroke_width=0.6,
|
|
364
|
+
)
|
|
365
|
+
d.move_to([xd, y_dot, 0.003])
|
|
366
|
+
# Specular highlight
|
|
367
|
+
sh = Circle(
|
|
368
|
+
radius=dot_r * 0.30,
|
|
369
|
+
fill_color=interpolate_color(
|
|
370
|
+
cols[placed], WHITE, 0.55),
|
|
371
|
+
fill_opacity=0.65, stroke_width=0,
|
|
372
|
+
)
|
|
373
|
+
sh.move_to([xd - dot_r*0.22,
|
|
374
|
+
y_dot + dot_r*0.25, 0.004])
|
|
375
|
+
self._ball_dots.add(d, sh)
|
|
376
|
+
placed += 1
|
|
377
|
+
self.add(self._ball_dots)
|
|
378
|
+
|
|
379
|
+
# ── Urn label ─────────────────────────────────────────────────
|
|
380
|
+
lbl = Text(label, font_size=22,
|
|
381
|
+
color=interpolate_color(fill_color, WHITE, 0.60))
|
|
382
|
+
lbl.move_to([0, h/2 + 0.42, 0.002])
|
|
383
|
+
self.add(lbl)
|
|
384
|
+
|
|
385
|
+
# Ball count badges
|
|
386
|
+
for count, col, sign in [
|
|
387
|
+
(n_red, P["red_ball"], -1),
|
|
388
|
+
(n_blue, P["blue_ball"], 1),
|
|
389
|
+
]:
|
|
390
|
+
ct = Text(f"●×{count}", font_size=14, color=col)
|
|
391
|
+
ct.move_to([sign * tw/2 * 0.55, -h/2 - 0.28, 0.002])
|
|
392
|
+
self.add(ct)
|
|
393
|
+
|
|
394
|
+
# Prior badge
|
|
395
|
+
self._prior_badge = self._make_prob_badge(prior, label)
|
|
396
|
+
self._prior_badge.move_to([0, -h/2 - 0.68, 0.002])
|
|
397
|
+
self.add(self._prior_badge)
|
|
398
|
+
|
|
399
|
+
def _make_prob_badge(self, prob: float, urn: str) -> VGroup:
|
|
400
|
+
g = VGroup()
|
|
401
|
+
col = P["bar_a"] if "A" in urn else P["bar_b"]
|
|
402
|
+
txt = Text(f"P({urn[-1]}) = {prob:.3f}",
|
|
403
|
+
font_size=15, color=col)
|
|
404
|
+
bg = RoundedRectangle(
|
|
405
|
+
width=txt.width + 0.22,
|
|
406
|
+
height=txt.height + 0.14,
|
|
407
|
+
corner_radius=0.06,
|
|
408
|
+
fill_color=P["bar_bg"],
|
|
409
|
+
fill_opacity=0.88,
|
|
410
|
+
stroke_color=col,
|
|
411
|
+
stroke_width=0.7,
|
|
412
|
+
)
|
|
413
|
+
bg.move_to(ORIGIN)
|
|
414
|
+
txt.move_to([0, 0, 0.001])
|
|
415
|
+
g.add(bg, txt)
|
|
416
|
+
return g
|
|
417
|
+
|
|
418
|
+
def update_prior_badge(self, new_prob: float) -> VGroup:
|
|
419
|
+
"""Replace the probability badge with a new value."""
|
|
420
|
+
old_pos = self._prior_badge.get_center()
|
|
421
|
+
new_badge = self._make_prob_badge(
|
|
422
|
+
new_prob, self._label[-1]
|
|
423
|
+
)
|
|
424
|
+
new_badge.move_to(old_pos)
|
|
425
|
+
if self._prior_badge in self.submobjects:
|
|
426
|
+
self.remove(self._prior_badge)
|
|
427
|
+
self._prior_badge = new_badge
|
|
428
|
+
self.add(new_badge)
|
|
429
|
+
return new_badge
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
433
|
+
# Probability bar
|
|
434
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
435
|
+
|
|
436
|
+
class _ProbBar(VGroup):
|
|
437
|
+
"""
|
|
438
|
+
Horizontal split probability bar showing P(A) and P(B).
|
|
439
|
+
|
|
440
|
+
Layers:
|
|
441
|
+
1. Background rect
|
|
442
|
+
2. Left segment (Urn A, P_a wide)
|
|
443
|
+
3. Right segment (Urn B, P_b wide)
|
|
444
|
+
4. Divider line
|
|
445
|
+
5. Labels above each segment
|
|
446
|
+
6. Percentage badges
|
|
447
|
+
|
|
448
|
+
Parameters
|
|
449
|
+
----------
|
|
450
|
+
p_a : float — P(Urn A)
|
|
451
|
+
width : float — total bar width
|
|
452
|
+
height : float — bar height
|
|
453
|
+
label_a : str
|
|
454
|
+
label_b : str
|
|
455
|
+
"""
|
|
456
|
+
|
|
457
|
+
# Class-level colour constants so morphing is possible
|
|
458
|
+
COLOR_A = P["bar_a"]
|
|
459
|
+
COLOR_B = P["bar_b"]
|
|
460
|
+
|
|
461
|
+
def __init__(
|
|
462
|
+
self,
|
|
463
|
+
p_a: float,
|
|
464
|
+
width: float = 6.0,
|
|
465
|
+
height: float = 0.55,
|
|
466
|
+
label_a: str = "Urn A",
|
|
467
|
+
label_b: str = "Urn B",
|
|
468
|
+
title: str = "Prior Probability",
|
|
469
|
+
**kwargs,
|
|
470
|
+
):
|
|
471
|
+
super().__init__(**kwargs)
|
|
472
|
+
self._p_a = p_a
|
|
473
|
+
self._width = width
|
|
474
|
+
self._height = height
|
|
475
|
+
|
|
476
|
+
p_b = 1 - p_a
|
|
477
|
+
w_a = p_a * width
|
|
478
|
+
w_b = p_b * width
|
|
479
|
+
z0 = 0.001
|
|
480
|
+
|
|
481
|
+
# ── Background ────────────────────────────────────────────────
|
|
482
|
+
bg = Rectangle(
|
|
483
|
+
width=width + 0.06,
|
|
484
|
+
height=height + 0.06,
|
|
485
|
+
fill_color=P["bar_bg"],
|
|
486
|
+
fill_opacity=1.0,
|
|
487
|
+
stroke_color=P["bar_border"],
|
|
488
|
+
stroke_width=0.8,
|
|
489
|
+
)
|
|
490
|
+
bg.move_to(ORIGIN)
|
|
491
|
+
self.add(bg)
|
|
492
|
+
|
|
493
|
+
# ── Segment A ─────────────────────────────────────────────────
|
|
494
|
+
seg_a = Rectangle(
|
|
495
|
+
width=max(w_a - 0.02, 0.01),
|
|
496
|
+
height=height,
|
|
497
|
+
fill_color=P["bar_a"],
|
|
498
|
+
fill_opacity=1.0,
|
|
499
|
+
stroke_width=0,
|
|
500
|
+
)
|
|
501
|
+
seg_a.move_to([-width/2 + w_a/2, 0, z0])
|
|
502
|
+
self.add(seg_a)
|
|
503
|
+
|
|
504
|
+
# Specular highlight on A
|
|
505
|
+
if w_a > 0.15:
|
|
506
|
+
hl_a = Rectangle(
|
|
507
|
+
width=min(w_a * 0.35, 0.60),
|
|
508
|
+
height=height * 0.28,
|
|
509
|
+
fill_color=interpolate_color(P["bar_a"], WHITE, 0.45),
|
|
510
|
+
fill_opacity=0.35, stroke_width=0,
|
|
511
|
+
)
|
|
512
|
+
hl_a.move_to([-width/2 + w_a * 0.30,
|
|
513
|
+
height * 0.22, z0+0.001])
|
|
514
|
+
self.add(hl_a)
|
|
515
|
+
|
|
516
|
+
# ── Segment B ─────────────────────────────────────────────────
|
|
517
|
+
seg_b = Rectangle(
|
|
518
|
+
width=max(w_b - 0.02, 0.01),
|
|
519
|
+
height=height,
|
|
520
|
+
fill_color=P["bar_b"],
|
|
521
|
+
fill_opacity=1.0,
|
|
522
|
+
stroke_width=0,
|
|
523
|
+
)
|
|
524
|
+
seg_b.move_to([width/2 - w_b/2, 0, z0])
|
|
525
|
+
self.add(seg_b)
|
|
526
|
+
|
|
527
|
+
if w_b > 0.15:
|
|
528
|
+
hl_b = Rectangle(
|
|
529
|
+
width=min(w_b * 0.35, 0.60),
|
|
530
|
+
height=height * 0.28,
|
|
531
|
+
fill_color=interpolate_color(P["bar_b"], WHITE, 0.45),
|
|
532
|
+
fill_opacity=0.35, stroke_width=0,
|
|
533
|
+
)
|
|
534
|
+
hl_b.move_to([-width/2 + w_a + w_b * 0.30,
|
|
535
|
+
height * 0.22, z0+0.001])
|
|
536
|
+
self.add(hl_b)
|
|
537
|
+
|
|
538
|
+
# ── Divider ───────────────────────────────────────────────────
|
|
539
|
+
div_x = -width/2 + w_a
|
|
540
|
+
self.add(Line(
|
|
541
|
+
[div_x, -height/2, z0+0.003],
|
|
542
|
+
[div_x, height/2, z0+0.003],
|
|
543
|
+
stroke_color=WHITE,
|
|
544
|
+
stroke_width=1.8,
|
|
545
|
+
))
|
|
546
|
+
|
|
547
|
+
# ── Labels above ──────────────────────────────────────────────
|
|
548
|
+
if w_a > 0.4:
|
|
549
|
+
la = Text(f"{label_a} {p_a:.1%}",
|
|
550
|
+
font_size=15, color=P["bar_text"])
|
|
551
|
+
la.move_to([-width/2 + w_a/2, height/2 + 0.28, z0+0.002])
|
|
552
|
+
self.add(la)
|
|
553
|
+
if w_b > 0.4:
|
|
554
|
+
lb = Text(f"{p_b:.1%} {label_b}",
|
|
555
|
+
font_size=15, color=P["bar_text"])
|
|
556
|
+
lb.move_to([width/2 - w_b/2, height/2 + 0.28, z0+0.002])
|
|
557
|
+
self.add(lb)
|
|
558
|
+
|
|
559
|
+
# ── Title ─────────────────────────────────────────────────────
|
|
560
|
+
tl = Text(title, font_size=18, color=P["subtitle_fg"])
|
|
561
|
+
tl.move_to([0, -height/2 - 0.32, z0+0.002])
|
|
562
|
+
self.add(tl)
|
|
563
|
+
|
|
564
|
+
|
|
565
|
+
def _morph_prob_bar(
|
|
566
|
+
scene: Scene,
|
|
567
|
+
old_bar: _ProbBar,
|
|
568
|
+
new_p_a: float,
|
|
569
|
+
run_time: float = 1.2,
|
|
570
|
+
**kwargs,
|
|
571
|
+
) -> _ProbBar:
|
|
572
|
+
"""
|
|
573
|
+
Create a new _ProbBar with updated probability and cross-fade to it.
|
|
574
|
+
Returns the new bar (added to scene in place of old).
|
|
575
|
+
"""
|
|
576
|
+
new_bar = _ProbBar(
|
|
577
|
+
p_a=new_p_a,
|
|
578
|
+
width=old_bar._width,
|
|
579
|
+
height=old_bar._height,
|
|
580
|
+
title="Posterior Probability",
|
|
581
|
+
)
|
|
582
|
+
new_bar.move_to(old_bar.get_center())
|
|
583
|
+
scene.play(
|
|
584
|
+
ReplacementTransform(old_bar, new_bar, run_time=run_time),
|
|
585
|
+
**kwargs,
|
|
586
|
+
)
|
|
587
|
+
return new_bar
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
591
|
+
# Likelihood table
|
|
592
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
593
|
+
|
|
594
|
+
class _LikelihoodTable(VGroup):
|
|
595
|
+
"""
|
|
596
|
+
Likelihood table showing P(obs | Urn A) and P(obs | Urn B).
|
|
597
|
+
|
|
598
|
+
Structure:
|
|
599
|
+
┌─────────────┬──────────────┬──────────────┐
|
|
600
|
+
│ │ Urn A │ Urn B │
|
|
601
|
+
├─────────────┼──────────────┼──────────────┤
|
|
602
|
+
│ P(red | ·) │ 3/5 = 0.600 │ 1/5 = 0.200 │
|
|
603
|
+
│ P(blue | ·) │ 2/5 = 0.400 │ 4/5 = 0.800 │
|
|
604
|
+
├─────────────┼──────────────┼──────────────┤
|
|
605
|
+
│ P(red) (marginal) = 0.400 │
|
|
606
|
+
└─────────────────────────────────────────────┘
|
|
607
|
+
|
|
608
|
+
Parameters
|
|
609
|
+
----------
|
|
610
|
+
p_red_a, p_red_b : likelihoods
|
|
611
|
+
p_a : current prior P(A)
|
|
612
|
+
"""
|
|
613
|
+
|
|
614
|
+
def __init__(
|
|
615
|
+
self,
|
|
616
|
+
p_red_a: float,
|
|
617
|
+
p_red_b: float,
|
|
618
|
+
p_a: float,
|
|
619
|
+
cell_w: float = 1.65,
|
|
620
|
+
cell_h: float = 0.60,
|
|
621
|
+
**kwargs,
|
|
622
|
+
):
|
|
623
|
+
super().__init__(**kwargs)
|
|
624
|
+
p_b = 1 - p_a
|
|
625
|
+
p_blue_a = 1 - p_red_a
|
|
626
|
+
p_blue_b = 1 - p_red_b
|
|
627
|
+
p_red_marg = p_red_a * p_a + p_red_b * p_b
|
|
628
|
+
p_blue_marg = p_blue_a * p_a + p_blue_b * p_b
|
|
629
|
+
|
|
630
|
+
row_w = 1.80 # row header width
|
|
631
|
+
n_cols = 2
|
|
632
|
+
total_w = row_w + n_cols * cell_w
|
|
633
|
+
total_h = cell_h * 4 # header + 2 data rows + marginal
|
|
634
|
+
|
|
635
|
+
z0 = 0.001
|
|
636
|
+
|
|
637
|
+
# ── Background ────────────────────────────────────────────────
|
|
638
|
+
bg = RoundedRectangle(
|
|
639
|
+
width=total_w + 0.24,
|
|
640
|
+
height=total_h + 0.24,
|
|
641
|
+
corner_radius=0.10,
|
|
642
|
+
fill_color=P["tbl_bg"],
|
|
643
|
+
fill_opacity=0.95,
|
|
644
|
+
stroke_color=P["tbl_border"],
|
|
645
|
+
stroke_width=0.9,
|
|
646
|
+
)
|
|
647
|
+
bg.move_to(ORIGIN)
|
|
648
|
+
self.add(bg)
|
|
649
|
+
|
|
650
|
+
x_cols = [
|
|
651
|
+
-total_w/2 + row_w/2,
|
|
652
|
+
-total_w/2 + row_w + cell_w/2,
|
|
653
|
+
-total_w/2 + row_w + cell_w * 1.5,
|
|
654
|
+
]
|
|
655
|
+
y_header = total_h/2 - cell_h/2
|
|
656
|
+
|
|
657
|
+
# ── Header row ────────────────────────────────────────────────
|
|
658
|
+
for xi, txt, col in [
|
|
659
|
+
(x_cols[0], "", P["tbl_header"]),
|
|
660
|
+
(x_cols[1], "Urn A", P["tbl_header"]),
|
|
661
|
+
(x_cols[2], "Urn B", P["tbl_header"]),
|
|
662
|
+
]:
|
|
663
|
+
self._add_cell(xi, y_header, cell_w if xi != x_cols[0] else row_w,
|
|
664
|
+
cell_h, col, txt, z0)
|
|
665
|
+
|
|
666
|
+
# ── Data rows ─────────────────────────────────────────────────
|
|
667
|
+
data_rows = [
|
|
668
|
+
("P(red | · )",
|
|
669
|
+
f"3/5 = {p_red_a:.3f}", P["tbl_val_red"],
|
|
670
|
+
f"1/5 = {p_red_b:.3f}", P["tbl_val_red"],
|
|
671
|
+
P["tbl_red_row"]),
|
|
672
|
+
("P(blue | · )",
|
|
673
|
+
f"2/5 = {p_blue_a:.3f}", P["tbl_val_blue"],
|
|
674
|
+
f"4/5 = {p_blue_b:.3f}", P["tbl_val_blue"],
|
|
675
|
+
P["tbl_blue_row"]),
|
|
676
|
+
]
|
|
677
|
+
for ri, (row_lbl, va, ca, vb, cb, rbg) in enumerate(data_rows):
|
|
678
|
+
yr = y_header - (ri + 1) * cell_h
|
|
679
|
+
self._add_cell(x_cols[0], yr, row_w, cell_h,
|
|
680
|
+
P["tbl_header"], row_lbl, z0)
|
|
681
|
+
self._add_cell(x_cols[1], yr, cell_w, cell_h, rbg, va, z0,
|
|
682
|
+
val_color=ca)
|
|
683
|
+
self._add_cell(x_cols[2], yr, cell_w, cell_h, rbg, vb, z0,
|
|
684
|
+
val_color=cb)
|
|
685
|
+
|
|
686
|
+
# ── Marginal row ──────────────────────────────────────────────
|
|
687
|
+
y_marg = y_header - 3 * cell_h
|
|
688
|
+
marg_str = (f"P(red) = P(red|A)·P(A) + P(red|B)·P(B)"
|
|
689
|
+
f" = {p_red_marg:.3f}")
|
|
690
|
+
marg_lbl = Text(marg_str, font_size=13,
|
|
691
|
+
color=P["tbl_val_marg"])
|
|
692
|
+
marg_bg = Rectangle(
|
|
693
|
+
width=total_w, height=cell_h,
|
|
694
|
+
fill_color=interpolate_color(P["tbl_bg"], P["tbl_val_marg"], 0.08),
|
|
695
|
+
fill_opacity=1.0, stroke_width=0,
|
|
696
|
+
)
|
|
697
|
+
marg_bg.move_to([0, y_marg, z0])
|
|
698
|
+
marg_lbl.move_to([0, y_marg, z0+0.002])
|
|
699
|
+
self.add(marg_bg, marg_lbl)
|
|
700
|
+
|
|
701
|
+
# ── Grid lines ────────────────────────────────────────────────
|
|
702
|
+
for xi in [-total_w/2 + row_w,
|
|
703
|
+
-total_w/2 + row_w + cell_w]:
|
|
704
|
+
self.add(Line(
|
|
705
|
+
[xi, -total_h/2, z0+0.002],
|
|
706
|
+
[xi, total_h/2, z0+0.002],
|
|
707
|
+
stroke_color=P["tbl_border"], stroke_width=0.7,
|
|
708
|
+
))
|
|
709
|
+
for yi in [y_header - cell_h*0.5,
|
|
710
|
+
y_header - cell_h*1.5,
|
|
711
|
+
y_header - cell_h*2.5]:
|
|
712
|
+
self.add(Line(
|
|
713
|
+
[-total_w/2, yi, z0+0.002],
|
|
714
|
+
[ total_w/2, yi, z0+0.002],
|
|
715
|
+
stroke_color=P["tbl_border"], stroke_width=0.7,
|
|
716
|
+
))
|
|
717
|
+
|
|
718
|
+
self._p_red_marg = p_red_marg
|
|
719
|
+
self._cell_h = cell_h
|
|
720
|
+
self._total_h = total_h
|
|
721
|
+
self._y_header = y_header
|
|
722
|
+
|
|
723
|
+
def _add_cell(
|
|
724
|
+
self,
|
|
725
|
+
x, y, w, h,
|
|
726
|
+
fill_col, text_str, z,
|
|
727
|
+
val_color=None,
|
|
728
|
+
):
|
|
729
|
+
rect = Rectangle(width=w, height=h,
|
|
730
|
+
fill_color=fill_col, fill_opacity=1.0,
|
|
731
|
+
stroke_width=0)
|
|
732
|
+
rect.move_to([x, y, z])
|
|
733
|
+
self.add(rect)
|
|
734
|
+
if text_str:
|
|
735
|
+
t = Text(text_str, font_size=13,
|
|
736
|
+
color=val_color or P["bar_text"])
|
|
737
|
+
t.move_to([x, y, z+0.002])
|
|
738
|
+
self.add(t)
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
742
|
+
# Bayes formula display
|
|
743
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
744
|
+
|
|
745
|
+
class _BayesFormula(VGroup):
|
|
746
|
+
"""
|
|
747
|
+
Multi-line Bayes formula with colour-coded terms and
|
|
748
|
+
numerical substitution below.
|
|
749
|
+
|
|
750
|
+
Structure:
|
|
751
|
+
Line 1 (symbolic):
|
|
752
|
+
P(A | red) = P(red | A) · P(A) / P(red)
|
|
753
|
+
───────────────────────────────
|
|
754
|
+
Line 2 (substituted):
|
|
755
|
+
P(A | red) = {p_red_a:.3f} × {p_a:.3f} / {p_red_marg:.3f}
|
|
756
|
+
Line 3 (result):
|
|
757
|
+
P(A | red) = {posterior_a:.3f}
|
|
758
|
+
|
|
759
|
+
Each term is a separate Text/MathTex so it can be highlighted
|
|
760
|
+
independently.
|
|
761
|
+
|
|
762
|
+
Parameters
|
|
763
|
+
----------
|
|
764
|
+
p_a : prior P(A)
|
|
765
|
+
p_red_a : P(red | A)
|
|
766
|
+
p_red_b : P(red | B)
|
|
767
|
+
"""
|
|
768
|
+
|
|
769
|
+
def __init__(
|
|
770
|
+
self,
|
|
771
|
+
p_a: float,
|
|
772
|
+
p_red_a: float,
|
|
773
|
+
p_red_b: float,
|
|
774
|
+
**kwargs,
|
|
775
|
+
):
|
|
776
|
+
super().__init__(**kwargs)
|
|
777
|
+
p_b = 1 - p_a
|
|
778
|
+
p_red_marg = p_red_a * p_a + p_red_b * p_b
|
|
779
|
+
posterior_a = (p_red_a * p_a) / p_red_marg
|
|
780
|
+
posterior_b = 1 - posterior_a
|
|
781
|
+
|
|
782
|
+
z0 = 0.001
|
|
783
|
+
fs_sym = 22
|
|
784
|
+
fs_num = 21
|
|
785
|
+
|
|
786
|
+
# ── Background panel ──────────────────────────────────────────
|
|
787
|
+
bg = RoundedRectangle(
|
|
788
|
+
width=8.20, height=3.40,
|
|
789
|
+
corner_radius=0.12,
|
|
790
|
+
fill_color=P["formula_bg"],
|
|
791
|
+
fill_opacity=0.95,
|
|
792
|
+
stroke_color=P["tbl_border"],
|
|
793
|
+
stroke_width=0.9,
|
|
794
|
+
)
|
|
795
|
+
bg.move_to(ORIGIN)
|
|
796
|
+
self.add(bg)
|
|
797
|
+
|
|
798
|
+
# Left border strip
|
|
799
|
+
strip = RoundedRectangle(
|
|
800
|
+
width=0.08, height=3.32,
|
|
801
|
+
corner_radius=0.04,
|
|
802
|
+
fill_color=P["term_post"],
|
|
803
|
+
fill_opacity=0.85, stroke_width=0,
|
|
804
|
+
)
|
|
805
|
+
strip.move_to([-4.05, 0, z0+0.001])
|
|
806
|
+
self.add(strip)
|
|
807
|
+
|
|
808
|
+
# ── Title ─────────────────────────────────────────────────────
|
|
809
|
+
title = Text("Bayes' Theorem — Numerical Update",
|
|
810
|
+
font_size=17, color=P["subtitle_fg"])
|
|
811
|
+
title.move_to([0, 1.45, z0+0.002])
|
|
812
|
+
self.add(title)
|
|
813
|
+
|
|
814
|
+
# ── Symbolic line ─────────────────────────────────────────────
|
|
815
|
+
# Build as VGroup of colour-coded pieces
|
|
816
|
+
sym_line = VGroup()
|
|
817
|
+
pieces_sym = [
|
|
818
|
+
("P(A | red)", P["term_post"]),
|
|
819
|
+
(" = ", P["term_eq"]),
|
|
820
|
+
("P(red | A)", P["term_like"]),
|
|
821
|
+
(" · ", P["term_eq"]),
|
|
822
|
+
("P(A)", P["term_prior"]),
|
|
823
|
+
(" / ", P["term_eq"]),
|
|
824
|
+
("P(red)", P["term_marg"]),
|
|
825
|
+
]
|
|
826
|
+
x_cur = -3.60
|
|
827
|
+
for txt, col in pieces_sym:
|
|
828
|
+
m = Text(txt, font_size=fs_sym, color=col)
|
|
829
|
+
m.move_to([x_cur + m.width/2, 0.72, z0+0.003])
|
|
830
|
+
sym_line.add(m)
|
|
831
|
+
x_cur += m.width + 0.02
|
|
832
|
+
self.sym_pieces = sym_line
|
|
833
|
+
self.add(sym_line)
|
|
834
|
+
|
|
835
|
+
# ── Numeric substitution line ─────────────────────────────────
|
|
836
|
+
num_line = VGroup()
|
|
837
|
+
pieces_num = [
|
|
838
|
+
("P(A | red)", P["term_post"]),
|
|
839
|
+
(" = ", P["term_eq"]),
|
|
840
|
+
(f"{p_red_a:.3f}", P["term_like"]),
|
|
841
|
+
(" × ", P["term_eq"]),
|
|
842
|
+
(f"{p_a:.3f}", P["term_prior"]),
|
|
843
|
+
(" / ", P["term_eq"]),
|
|
844
|
+
(f"{p_red_marg:.3f}", P["term_marg"]),
|
|
845
|
+
]
|
|
846
|
+
x_cur = -3.60
|
|
847
|
+
for txt, col in pieces_num:
|
|
848
|
+
m = Text(txt, font_size=fs_num, color=col,
|
|
849
|
+
font="monospace")
|
|
850
|
+
m.move_to([x_cur + m.width/2, 0.10, z0+0.003])
|
|
851
|
+
num_line.add(m)
|
|
852
|
+
x_cur += m.width + 0.02
|
|
853
|
+
self.num_pieces = num_line
|
|
854
|
+
self.add(num_line)
|
|
855
|
+
|
|
856
|
+
# Divider
|
|
857
|
+
self.add(Line(
|
|
858
|
+
[-3.80, -0.22, z0+0.002],
|
|
859
|
+
[ 3.80, -0.22, z0+0.002],
|
|
860
|
+
stroke_color=P["tbl_border"], stroke_width=0.7,
|
|
861
|
+
))
|
|
862
|
+
|
|
863
|
+
# ── Result line ───────────────────────────────────────────────
|
|
864
|
+
result_pieces = [
|
|
865
|
+
("P(A | red)", P["term_post"]),
|
|
866
|
+
(" = ", P["term_eq"]),
|
|
867
|
+
(f"{posterior_a:.4f}", P["term_post"]),
|
|
868
|
+
(" ", P["term_eq"]),
|
|
869
|
+
("P(B | red)", P["term_b"] if hasattr(P,"term_b") else P["bar_b"]),
|
|
870
|
+
(" = ", P["term_eq"]),
|
|
871
|
+
(f"{posterior_b:.4f}", P["bar_b"]),
|
|
872
|
+
]
|
|
873
|
+
res_line = VGroup()
|
|
874
|
+
x_cur = -3.80
|
|
875
|
+
for txt, col in result_pieces:
|
|
876
|
+
m = Text(txt, font_size=fs_num + 2,
|
|
877
|
+
color=col,
|
|
878
|
+
font="monospace")
|
|
879
|
+
m.move_to([x_cur + m.width/2, -0.62, z0+0.003])
|
|
880
|
+
res_line.add(m)
|
|
881
|
+
x_cur += m.width + 0.02
|
|
882
|
+
self.result_pieces = res_line
|
|
883
|
+
self.add(res_line)
|
|
884
|
+
|
|
885
|
+
# Odds line
|
|
886
|
+
odds = posterior_a / max(posterior_b, 1e-9)
|
|
887
|
+
odds_str = f"Posterior odds: P(A|red) / P(B|red) = {odds:.2f}×"
|
|
888
|
+
odds_lbl = Text(odds_str, font_size=16,
|
|
889
|
+
color=P["term_marg"])
|
|
890
|
+
odds_lbl.move_to([0, -1.20, z0+0.003])
|
|
891
|
+
self.add(odds_lbl)
|
|
892
|
+
|
|
893
|
+
self._posterior_a = posterior_a
|
|
894
|
+
self._posterior_b = posterior_b
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
898
|
+
# Beta distribution curve
|
|
899
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
900
|
+
|
|
901
|
+
class _BetaCurve(VGroup):
|
|
902
|
+
"""
|
|
903
|
+
Beta(α, β) posterior distribution curve.
|
|
904
|
+
|
|
905
|
+
Shown as a filled polygon over a [0,1] axis,
|
|
906
|
+
representing the posterior belief about P(red | chosen urn).
|
|
907
|
+
|
|
908
|
+
Parameters
|
|
909
|
+
----------
|
|
910
|
+
alpha_param : float — Beta α (successes + 1)
|
|
911
|
+
beta_param : float — Beta β (failures + 1)
|
|
912
|
+
width : float — plot width
|
|
913
|
+
height : float — max curve height
|
|
914
|
+
"""
|
|
915
|
+
|
|
916
|
+
def __init__(
|
|
917
|
+
self,
|
|
918
|
+
alpha_param: float,
|
|
919
|
+
beta_param: float,
|
|
920
|
+
width: float = 5.0,
|
|
921
|
+
height: float = 2.8,
|
|
922
|
+
**kwargs,
|
|
923
|
+
):
|
|
924
|
+
super().__init__(**kwargs)
|
|
925
|
+
try:
|
|
926
|
+
from scipy.stats import beta as _beta_dist
|
|
927
|
+
x_raw = np.linspace(0.001, 0.999, 400)
|
|
928
|
+
y_raw = _beta_dist.pdf(x_raw, alpha_param, beta_param)
|
|
929
|
+
except ImportError:
|
|
930
|
+
# Manual Beta PDF (simplified)
|
|
931
|
+
import math
|
|
932
|
+
def beta_pdf(x, a, b):
|
|
933
|
+
with np.errstate(all="ignore"):
|
|
934
|
+
v = (x**(a-1) * (1-x)**(b-1))
|
|
935
|
+
try:
|
|
936
|
+
norm = (math.gamma(a) * math.gamma(b)
|
|
937
|
+
/ math.gamma(a + b))
|
|
938
|
+
except Exception:
|
|
939
|
+
norm = 1.0
|
|
940
|
+
return np.where((x>0)&(x<1), v/norm, 0.0)
|
|
941
|
+
x_raw = np.linspace(0.001, 0.999, 400)
|
|
942
|
+
y_raw = beta_pdf(x_raw, alpha_param, beta_param)
|
|
943
|
+
|
|
944
|
+
y_raw = np.where(np.isfinite(y_raw), y_raw, 0.0)
|
|
945
|
+
peak = float(y_raw.max()) if y_raw.max() > 1e-12 else 1.0
|
|
946
|
+
scale = height / peak
|
|
947
|
+
x_px = (x_raw - 0.5) * width
|
|
948
|
+
y_px = y_raw * scale
|
|
949
|
+
z0 = 0.001
|
|
950
|
+
baseline = 0.0
|
|
951
|
+
|
|
952
|
+
# ── Fill polygon ──────────────────────────────────────────────
|
|
953
|
+
verts = (
|
|
954
|
+
[[float(x_px[0]), baseline, 0]]
|
|
955
|
+
+ [[float(x), float(y), 0] for x,y in zip(x_px,y_px)]
|
|
956
|
+
+ [[float(x_px[-1]), baseline, 0]]
|
|
957
|
+
)
|
|
958
|
+
if len(verts) >= 3:
|
|
959
|
+
fill = Polygon(*verts,
|
|
960
|
+
fill_color=P["beta_fill"],
|
|
961
|
+
fill_opacity=0.75, stroke_width=0)
|
|
962
|
+
fill.shift([0, 0, z0])
|
|
963
|
+
self.add(fill)
|
|
964
|
+
|
|
965
|
+
# ── Ridge spine ───────────────────────────────────────────────
|
|
966
|
+
step = max(1, len(x_px)//80)
|
|
967
|
+
pts3d = [np.array([float(x_px[i]), float(y_px[i]), z0+0.004])
|
|
968
|
+
for i in range(0, len(x_px), step) if y_px[i] > 0.01]
|
|
969
|
+
if len(pts3d) >= 2:
|
|
970
|
+
spine = VMobject(stroke_color=P["beta_ridge"],
|
|
971
|
+
stroke_width=2.2, stroke_opacity=0.92)
|
|
972
|
+
spine.set_points_smoothly(pts3d)
|
|
973
|
+
self.add(spine)
|
|
974
|
+
|
|
975
|
+
# ── AO tail darkening ─────────────────────────────────────────
|
|
976
|
+
ao_frac = 0.08
|
|
977
|
+
for x_e, x_i in [(x_px[0], x_px[0]+width*ao_frac),
|
|
978
|
+
(x_px[-1], x_px[-1]-width*ao_frac)]:
|
|
979
|
+
xl,xr = min(x_e,x_i), max(x_e,x_i)
|
|
980
|
+
mask = (x_px>=xl)&(x_px<=xr)
|
|
981
|
+
xs,ys = x_px[mask], y_px[mask]
|
|
982
|
+
if len(xs)>=2:
|
|
983
|
+
av = ([[xl,baseline,0]]
|
|
984
|
+
+[[float(x),float(y),0] for x,y in zip(xs,ys)]
|
|
985
|
+
+[[xr,baseline,0]])
|
|
986
|
+
self.add(Polygon(*av,
|
|
987
|
+
fill_color=BLACK,
|
|
988
|
+
fill_opacity=0.45, stroke_width=0))
|
|
989
|
+
|
|
990
|
+
# ── Axis ──────────────────────────────────────────────────────
|
|
991
|
+
ax = Line([-width/2-0.2, baseline, z0+0.001],
|
|
992
|
+
[ width/2+0.2, baseline, z0+0.001],
|
|
993
|
+
stroke_color=P["axis"], stroke_width=1.5)
|
|
994
|
+
self.add(ax)
|
|
995
|
+
|
|
996
|
+
# Ticks at 0, 0.2, 0.4, 0.6, 0.8, 1.0
|
|
997
|
+
for rv in [0, 0.2, 0.4, 0.6, 0.8, 1.0]:
|
|
998
|
+
xp = (rv - 0.5) * width
|
|
999
|
+
self.add(Line([xp, -0.07, z0+0.001],
|
|
1000
|
+
[xp, 0.07, z0+0.001],
|
|
1001
|
+
stroke_color=P["axis"], stroke_width=1.0))
|
|
1002
|
+
self.add(Text(f"{rv:.1f}", font_size=12,
|
|
1003
|
+
color=P["subtitle_fg"]).move_to(
|
|
1004
|
+
[xp, -0.28, z0+0.001]))
|
|
1005
|
+
|
|
1006
|
+
# ── Labels ────────────────────────────────────────────────────
|
|
1007
|
+
mode = (alpha_param - 1) / max(alpha_param + beta_param - 2, 1e-6)
|
|
1008
|
+
mode = float(np.clip(mode, 0.05, 0.95))
|
|
1009
|
+
peak_px = (mode - 0.5) * width
|
|
1010
|
+
peak_py = float(y_px[np.argmin(np.abs(x_raw - mode))])
|
|
1011
|
+
|
|
1012
|
+
try:
|
|
1013
|
+
title_lbl = MathTex(
|
|
1014
|
+
rf"\text{{Beta}}(\alpha={alpha_param:.1f},\;"
|
|
1015
|
+
rf"\beta={beta_param:.1f})",
|
|
1016
|
+
font_size=22, color=P["beta_ridge"],
|
|
1017
|
+
)
|
|
1018
|
+
except Exception:
|
|
1019
|
+
title_lbl = Text(
|
|
1020
|
+
f"Beta(α={alpha_param:.1f}, β={beta_param:.1f})",
|
|
1021
|
+
font_size=18, color=P["beta_ridge"],
|
|
1022
|
+
)
|
|
1023
|
+
title_lbl.move_to([0, peak_py + 0.45, z0+0.005])
|
|
1024
|
+
self.add(title_lbl)
|
|
1025
|
+
|
|
1026
|
+
x_label = Text("θ = P(red ball from chosen urn)",
|
|
1027
|
+
font_size=14, color=P["subtitle_fg"])
|
|
1028
|
+
x_label.move_to([0, baseline - 0.55, z0+0.002])
|
|
1029
|
+
self.add(x_label)
|
|
1030
|
+
|
|
1031
|
+
|
|
1032
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1033
|
+
# Posterior history trail
|
|
1034
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1035
|
+
|
|
1036
|
+
class _PosteriorTrail(VGroup):
|
|
1037
|
+
"""
|
|
1038
|
+
A vertical stack of small probability bars showing the evolution
|
|
1039
|
+
of the posterior after each draw.
|
|
1040
|
+
|
|
1041
|
+
Each row: draw index, observation icon, posterior bar.
|
|
1042
|
+
"""
|
|
1043
|
+
|
|
1044
|
+
def __init__(
|
|
1045
|
+
self,
|
|
1046
|
+
history: list[tuple[str, float]], # [(obs_label, p_a), ...]
|
|
1047
|
+
bar_w: float = 3.2,
|
|
1048
|
+
bar_h: float = 0.30,
|
|
1049
|
+
spacing: float = 0.48,
|
|
1050
|
+
**kwargs,
|
|
1051
|
+
):
|
|
1052
|
+
super().__init__(**kwargs)
|
|
1053
|
+
z0 = 0.001
|
|
1054
|
+
|
|
1055
|
+
for i, (obs_lbl, p_a) in enumerate(history):
|
|
1056
|
+
y_row = -i * spacing
|
|
1057
|
+
p_b = 1 - p_a
|
|
1058
|
+
|
|
1059
|
+
# Row label
|
|
1060
|
+
lbl = Text(f"Draw {i+1}: {obs_lbl}",
|
|
1061
|
+
font_size=13, color=P["subtitle_fg"])
|
|
1062
|
+
lbl.move_to([-bar_w/2 - 0.88, y_row, z0+0.002])
|
|
1063
|
+
self.add(lbl)
|
|
1064
|
+
|
|
1065
|
+
# Mini prob bar
|
|
1066
|
+
w_a = p_a * bar_w
|
|
1067
|
+
w_b = p_b * bar_w
|
|
1068
|
+
|
|
1069
|
+
bg = Rectangle(width=bar_w+0.04, height=bar_h+0.04,
|
|
1070
|
+
fill_color=P["bar_bg"],
|
|
1071
|
+
fill_opacity=1.0, stroke_width=0)
|
|
1072
|
+
bg.move_to([0, y_row, z0])
|
|
1073
|
+
self.add(bg)
|
|
1074
|
+
|
|
1075
|
+
if w_a > 0.01:
|
|
1076
|
+
sa = Rectangle(width=w_a-0.01, height=bar_h,
|
|
1077
|
+
fill_color=P["bar_a"],
|
|
1078
|
+
fill_opacity=0.90, stroke_width=0)
|
|
1079
|
+
sa.move_to([-bar_w/2 + w_a/2, y_row, z0+0.001])
|
|
1080
|
+
self.add(sa)
|
|
1081
|
+
if w_b > 0.01:
|
|
1082
|
+
sb = Rectangle(width=w_b-0.01, height=bar_h,
|
|
1083
|
+
fill_color=P["bar_b"],
|
|
1084
|
+
fill_opacity=0.90, stroke_width=0)
|
|
1085
|
+
sb.move_to([bar_w/2 - w_b/2, y_row, z0+0.001])
|
|
1086
|
+
self.add(sb)
|
|
1087
|
+
|
|
1088
|
+
# Probability labels
|
|
1089
|
+
ta = Text(f"A:{p_a:.2f}", font_size=11, color=P["bar_text"])
|
|
1090
|
+
tb = Text(f"B:{p_b:.2f}", font_size=11, color=P["bar_text"])
|
|
1091
|
+
ta.move_to([-bar_w/2 + w_a/2, y_row, z0+0.003])
|
|
1092
|
+
tb.move_to([bar_w/2 - w_b/2, y_row, z0+0.003])
|
|
1093
|
+
self.add(ta, tb)
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1097
|
+
# Summary panel
|
|
1098
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1099
|
+
|
|
1100
|
+
class _SummaryPanel(VGroup):
|
|
1101
|
+
"""
|
|
1102
|
+
Final summary: Prior → Likelihood → Posterior, one row each,
|
|
1103
|
+
with colour-coded term labels and a compact visual.
|
|
1104
|
+
"""
|
|
1105
|
+
|
|
1106
|
+
def __init__(
|
|
1107
|
+
self,
|
|
1108
|
+
p_a_prior: float,
|
|
1109
|
+
p_red_a: float,
|
|
1110
|
+
p_red_b: float,
|
|
1111
|
+
p_a_posterior: float,
|
|
1112
|
+
panel_w: float = 7.50,
|
|
1113
|
+
**kwargs,
|
|
1114
|
+
):
|
|
1115
|
+
super().__init__(**kwargs)
|
|
1116
|
+
p_b_prior = 1 - p_a_prior
|
|
1117
|
+
p_red_marg = p_red_a*p_a_prior + p_red_b*p_b_prior
|
|
1118
|
+
p_b_post = 1 - p_a_posterior
|
|
1119
|
+
z0 = 0.001
|
|
1120
|
+
|
|
1121
|
+
rows = [
|
|
1122
|
+
("Prior", f"P(A) = {p_a_prior:.3f}",
|
|
1123
|
+
f"P(B) = {p_b_prior:.3f}",
|
|
1124
|
+
P["term_prior"]),
|
|
1125
|
+
("Likelihood", f"P(red|A) = {p_red_a:.3f}",
|
|
1126
|
+
f"P(red|B) = {p_red_b:.3f}",
|
|
1127
|
+
P["term_like"]),
|
|
1128
|
+
("Marginal", f"P(red) = {p_red_marg:.3f}",
|
|
1129
|
+
"",
|
|
1130
|
+
P["term_marg"]),
|
|
1131
|
+
("Posterior", f"P(A|red) = {p_a_posterior:.3f}",
|
|
1132
|
+
f"P(B|red) = {p_b_post:.3f}",
|
|
1133
|
+
P["term_post"]),
|
|
1134
|
+
]
|
|
1135
|
+
row_h = 0.62
|
|
1136
|
+
total_h = len(rows) * row_h + 0.60
|
|
1137
|
+
|
|
1138
|
+
bg = RoundedRectangle(
|
|
1139
|
+
width=panel_w+0.24, height=total_h+0.24,
|
|
1140
|
+
corner_radius=0.12,
|
|
1141
|
+
fill_color=P["summary_bg"],
|
|
1142
|
+
fill_opacity=0.95,
|
|
1143
|
+
stroke_color=P["summary_border"],
|
|
1144
|
+
stroke_width=0.9,
|
|
1145
|
+
)
|
|
1146
|
+
bg.move_to(ORIGIN)
|
|
1147
|
+
self.add(bg)
|
|
1148
|
+
|
|
1149
|
+
title = Text("Bayesian Update — Summary",
|
|
1150
|
+
font_size=20, color=P["title_fg"])
|
|
1151
|
+
title.move_to([0, total_h/2 - 0.30, z0+0.002])
|
|
1152
|
+
self.add(title)
|
|
1153
|
+
|
|
1154
|
+
self.add(Line(
|
|
1155
|
+
[-panel_w/2+0.10, total_h/2-0.55, z0+0.002],
|
|
1156
|
+
[ panel_w/2-0.10, total_h/2-0.55, z0+0.002],
|
|
1157
|
+
stroke_color=P["summary_border"], stroke_width=0.7,
|
|
1158
|
+
))
|
|
1159
|
+
|
|
1160
|
+
for i, (term, val_a, val_b, col) in enumerate(rows):
|
|
1161
|
+
yr = total_h/2 - 0.78 - i * row_h
|
|
1162
|
+
|
|
1163
|
+
# Left border dot
|
|
1164
|
+
dot = Circle(radius=0.06,
|
|
1165
|
+
fill_color=col,
|
|
1166
|
+
fill_opacity=1.0, stroke_width=0)
|
|
1167
|
+
dot.move_to([-panel_w/2 + 0.22, yr, z0+0.003])
|
|
1168
|
+
self.add(dot)
|
|
1169
|
+
|
|
1170
|
+
term_lbl = Text(term, font_size=15, color=col)
|
|
1171
|
+
term_lbl.move_to([-panel_w/2 + 0.55 + term_lbl.width/2,
|
|
1172
|
+
yr, z0+0.003])
|
|
1173
|
+
self.add(term_lbl)
|
|
1174
|
+
|
|
1175
|
+
val_a_lbl = Text(val_a, font_size=14,
|
|
1176
|
+
color=P["bar_a"],
|
|
1177
|
+
font="monospace")
|
|
1178
|
+
val_a_lbl.move_to([0.50, yr, z0+0.003])
|
|
1179
|
+
self.add(val_a_lbl)
|
|
1180
|
+
|
|
1181
|
+
if val_b:
|
|
1182
|
+
val_b_lbl = Text(val_b, font_size=14,
|
|
1183
|
+
color=P["bar_b"],
|
|
1184
|
+
font="monospace")
|
|
1185
|
+
val_b_lbl.move_to([panel_w/2 - 0.10 - val_b_lbl.width/2,
|
|
1186
|
+
yr, z0+0.003])
|
|
1187
|
+
self.add(val_b_lbl)
|
|
1188
|
+
|
|
1189
|
+
# Arrow to next row
|
|
1190
|
+
if i < len(rows) - 1:
|
|
1191
|
+
arrow = Arrow(
|
|
1192
|
+
start=[panel_w/2 - 0.85, yr - 0.18, z0+0.002],
|
|
1193
|
+
end =[panel_w/2 - 0.85, yr - row_h + 0.18,
|
|
1194
|
+
z0+0.002],
|
|
1195
|
+
stroke_color=P["summary_arrow"],
|
|
1196
|
+
stroke_width=1.2,
|
|
1197
|
+
tip_length=0.12, buff=0,
|
|
1198
|
+
)
|
|
1199
|
+
self.add(arrow)
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1203
|
+
# Drawn ball indicator
|
|
1204
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1205
|
+
|
|
1206
|
+
def _drawn_ball_display(
|
|
1207
|
+
color: str,
|
|
1208
|
+
label: str = "RED",
|
|
1209
|
+
radius: float = 0.45,
|
|
1210
|
+
) -> VGroup:
|
|
1211
|
+
"""
|
|
1212
|
+
Large ball display for "we drew a {color} ball" reveal.
|
|
1213
|
+
Layers: shadow, body, specular, label.
|
|
1214
|
+
"""
|
|
1215
|
+
g = VGroup()
|
|
1216
|
+
dark_col = interpolate_color(color, BLACK, 0.55)
|
|
1217
|
+
lite_col = interpolate_color(color, WHITE, 0.50)
|
|
1218
|
+
|
|
1219
|
+
shadow = Circle(radius=radius*1.08,
|
|
1220
|
+
fill_color=dark_col,
|
|
1221
|
+
fill_opacity=0.45, stroke_width=0)
|
|
1222
|
+
shadow.shift([radius*0.06, -radius*0.06, -0.001])
|
|
1223
|
+
g.add(shadow)
|
|
1224
|
+
|
|
1225
|
+
body = Circle(radius=radius,
|
|
1226
|
+
fill_color=color,
|
|
1227
|
+
fill_opacity=1.0, stroke_width=0)
|
|
1228
|
+
g.add(body)
|
|
1229
|
+
|
|
1230
|
+
spec = Circle(radius=radius*0.32,
|
|
1231
|
+
fill_color=lite_col,
|
|
1232
|
+
fill_opacity=0.72, stroke_width=0)
|
|
1233
|
+
spec.scale([1.35, 0.75, 1])
|
|
1234
|
+
spec.move_to([-radius*0.26, radius*0.28, 0.003])
|
|
1235
|
+
g.add(spec)
|
|
1236
|
+
|
|
1237
|
+
rim = Annulus(inner_radius=radius*0.28,
|
|
1238
|
+
outer_radius=radius*0.40,
|
|
1239
|
+
fill_color=lite_col,
|
|
1240
|
+
fill_opacity=0.38, stroke_width=0)
|
|
1241
|
+
rim.move_to([-radius*0.24, radius*0.26, 0.002])
|
|
1242
|
+
g.add(rim)
|
|
1243
|
+
|
|
1244
|
+
lbl = Text(label, font_size=int(radius*52),
|
|
1245
|
+
color=WHITE if _perceived_lum(color) < 0.5 else BLACK)
|
|
1246
|
+
lbl.move_to([0, 0, 0.004])
|
|
1247
|
+
g.add(lbl)
|
|
1248
|
+
|
|
1249
|
+
return g
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
def _perceived_lum(hex_col: str) -> float:
|
|
1253
|
+
try:
|
|
1254
|
+
rgb = np.array(color_to_rgb(hex_col))
|
|
1255
|
+
return 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2]
|
|
1256
|
+
except Exception:
|
|
1257
|
+
return 0.5
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1261
|
+
# Counting animation helper
|
|
1262
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1263
|
+
|
|
1264
|
+
class _CountUp(Animation):
|
|
1265
|
+
"""
|
|
1266
|
+
Animates a Text label counting from old_val to new_val.
|
|
1267
|
+
Replaces the text in place.
|
|
1268
|
+
|
|
1269
|
+
Parameters
|
|
1270
|
+
----------
|
|
1271
|
+
mob : Text mob to update
|
|
1272
|
+
old_val : float
|
|
1273
|
+
new_val : float
|
|
1274
|
+
fmt : format string (default "{:.3f}")
|
|
1275
|
+
"""
|
|
1276
|
+
|
|
1277
|
+
def __init__(
|
|
1278
|
+
self,
|
|
1279
|
+
mob: Text,
|
|
1280
|
+
old_val: float,
|
|
1281
|
+
new_val: float,
|
|
1282
|
+
fmt: str = "{:.3f}",
|
|
1283
|
+
**kwargs,
|
|
1284
|
+
):
|
|
1285
|
+
self.old_val = old_val
|
|
1286
|
+
self.new_val = new_val
|
|
1287
|
+
self.fmt = fmt
|
|
1288
|
+
kwargs.setdefault("run_time", 0.80)
|
|
1289
|
+
kwargs.setdefault("rate_func", rate_functions.ease_in_out_cubic)
|
|
1290
|
+
super().__init__(mob, **kwargs)
|
|
1291
|
+
|
|
1292
|
+
def interpolate_mobject(self, alpha: float):
|
|
1293
|
+
val = self.old_val + (self.new_val - self.old_val) * alpha
|
|
1294
|
+
self.mobject.become(
|
|
1295
|
+
Text(self.fmt.format(val),
|
|
1296
|
+
font_size=self.mobject.font_size,
|
|
1297
|
+
color=self.mobject.get_color(),
|
|
1298
|
+
font="monospace")
|
|
1299
|
+
.move_to(self.mobject.get_center())
|
|
1300
|
+
)
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1304
|
+
# The full demo scene
|
|
1305
|
+
# ─────────────────────────────────────────────────────────────────────────────
|
|
1306
|
+
|
|
1307
|
+
class BayesDemo(Scene):
|
|
1308
|
+
"""
|
|
1309
|
+
Full cinematic Bayes' theorem demonstration.
|
|
1310
|
+
|
|
1311
|
+
Run with:
|
|
1312
|
+
manim -pql demo_bayes.py BayesDemo
|
|
1313
|
+
|
|
1314
|
+
For high quality:
|
|
1315
|
+
manim -pqh demo_bayes.py BayesDemo
|
|
1316
|
+
"""
|
|
1317
|
+
|
|
1318
|
+
# ── Problem parameters ────────────────────────────────────────────
|
|
1319
|
+
N_RED_A = 3
|
|
1320
|
+
N_BLUE_A = 2
|
|
1321
|
+
N_RED_B = 1
|
|
1322
|
+
N_BLUE_B = 4
|
|
1323
|
+
P_PRIOR_A = 0.5
|
|
1324
|
+
P_RED_A = 3 / 5 # 0.600
|
|
1325
|
+
P_RED_B = 1 / 5 # 0.200
|
|
1326
|
+
|
|
1327
|
+
def construct(self):
|
|
1328
|
+
|
|
1329
|
+
# ── ACT 0: Cold open ──────────────────────────────────────────
|
|
1330
|
+
self._act0_cold_open()
|
|
1331
|
+
|
|
1332
|
+
# ── ACT 1: Two urns setup ─────────────────────────────────────
|
|
1333
|
+
urn_a, urn_b = self._act1_setup()
|
|
1334
|
+
|
|
1335
|
+
# ── ACT 2: Prior probability bar ──────────────────────────────
|
|
1336
|
+
prob_bar = self._act2_prior(urn_a, urn_b)
|
|
1337
|
+
|
|
1338
|
+
# ── ACT 3: Draw a red ball ────────────────────────────────────
|
|
1339
|
+
ball_display = self._act3_draw(urn_a, urn_b, prob_bar)
|
|
1340
|
+
|
|
1341
|
+
# ── ACT 4: Likelihood table ───────────────────────────────────
|
|
1342
|
+
like_table = self._act4_likelihood(urn_a, urn_b,
|
|
1343
|
+
prob_bar, ball_display)
|
|
1344
|
+
|
|
1345
|
+
# ── ACT 5: Bayes formula — first update ───────────────────────
|
|
1346
|
+
prob_bar, formula = self._act5_bayes_formula(
|
|
1347
|
+
urn_a, urn_b, prob_bar, like_table, ball_display
|
|
1348
|
+
)
|
|
1349
|
+
|
|
1350
|
+
# ── ACT 6: Posterior bar ──────────────────────────────────────
|
|
1351
|
+
prob_bar = self._act6_posterior_bar(
|
|
1352
|
+
urn_a, urn_b, prob_bar, formula
|
|
1353
|
+
)
|
|
1354
|
+
|
|
1355
|
+
# ── ACT 7: Sequential updates ─────────────────────────────────
|
|
1356
|
+
prob_bar, trail = self._act7_sequential(
|
|
1357
|
+
urn_a, urn_b, prob_bar, like_table, ball_display
|
|
1358
|
+
)
|
|
1359
|
+
|
|
1360
|
+
# ── ACT 8: Beta distribution ──────────────────────────────────
|
|
1361
|
+
self._act8_beta_curve(urn_a, urn_b, prob_bar, trail)
|
|
1362
|
+
|
|
1363
|
+
# ── ACT 9: Decision and summary ───────────────────────────────
|
|
1364
|
+
self._act9_decision(urn_a, urn_b)
|
|
1365
|
+
|
|
1366
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1367
|
+
# Act 0 — Cold open
|
|
1368
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1369
|
+
|
|
1370
|
+
def _act0_cold_open(self):
|
|
1371
|
+
# Full-screen dark background
|
|
1372
|
+
bg = Rectangle(width=16, height=9,
|
|
1373
|
+
fill_color=P["bg"],
|
|
1374
|
+
fill_opacity=1.0, stroke_width=0)
|
|
1375
|
+
self.add(bg)
|
|
1376
|
+
|
|
1377
|
+
# Mystery urn in darkness
|
|
1378
|
+
mystery_urn = _UrnSchematic(
|
|
1379
|
+
label="?", n_red=0, n_blue=0,
|
|
1380
|
+
fill_color="#303038",
|
|
1381
|
+
width=1.4, height=2.0,
|
|
1382
|
+
)
|
|
1383
|
+
mystery_urn.move_to([0, 0.2, 0])
|
|
1384
|
+
|
|
1385
|
+
q_mark = Text("?", font_size=68,
|
|
1386
|
+
color=P["subtitle_fg"])
|
|
1387
|
+
q_mark.move_to([0, 1.70, 0])
|
|
1388
|
+
|
|
1389
|
+
self.play(FadeIn(mystery_urn, shift=UP*0.3), run_time=1.2)
|
|
1390
|
+
self.play(FadeIn(q_mark, shift=UP*0.15,
|
|
1391
|
+
rate_func=rate_functions.ease_out_back),
|
|
1392
|
+
run_time=0.9)
|
|
1393
|
+
self.wait(0.4)
|
|
1394
|
+
|
|
1395
|
+
# Title question
|
|
1396
|
+
question = Text(
|
|
1397
|
+
"Which urn did this ball come from?",
|
|
1398
|
+
font_size=32, color=P["title_fg"],
|
|
1399
|
+
)
|
|
1400
|
+
question.move_to([0, -1.60, 0])
|
|
1401
|
+
self.play(Write(question, run_time=1.4))
|
|
1402
|
+
self.wait(0.8)
|
|
1403
|
+
|
|
1404
|
+
# Drawn ball floats up
|
|
1405
|
+
ball = _drawn_ball_display(P["red_ball"], "RED", radius=0.32)
|
|
1406
|
+
ball.move_to([0, -0.50, 0])
|
|
1407
|
+
ball.set_opacity(0)
|
|
1408
|
+
self.play(FadeIn(ball, shift=UP*0.30, run_time=0.8))
|
|
1409
|
+
self.wait(0.6)
|
|
1410
|
+
|
|
1411
|
+
self.play(
|
|
1412
|
+
FadeOut(mystery_urn),
|
|
1413
|
+
FadeOut(q_mark),
|
|
1414
|
+
FadeOut(question),
|
|
1415
|
+
FadeOut(ball),
|
|
1416
|
+
FadeOut(bg),
|
|
1417
|
+
run_time=0.8,
|
|
1418
|
+
)
|
|
1419
|
+
|
|
1420
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1421
|
+
# Act 1 — Two urns setup
|
|
1422
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1423
|
+
|
|
1424
|
+
def _act1_setup(self):
|
|
1425
|
+
# Section label
|
|
1426
|
+
act_lbl = _section_label("Act 1 — The Setup")
|
|
1427
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1428
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1429
|
+
|
|
1430
|
+
# Urn A — terracotta
|
|
1431
|
+
urn_a = _UrnSchematic(
|
|
1432
|
+
label="Urn A",
|
|
1433
|
+
n_red=self.N_RED_A, n_blue=self.N_BLUE_A,
|
|
1434
|
+
fill_color=P["urn_a"],
|
|
1435
|
+
prior=self.P_PRIOR_A,
|
|
1436
|
+
)
|
|
1437
|
+
urn_a.move_to([-3.40, 0.40, 0])
|
|
1438
|
+
|
|
1439
|
+
# Urn B — cobalt
|
|
1440
|
+
urn_b = _UrnSchematic(
|
|
1441
|
+
label="Urn B",
|
|
1442
|
+
n_red=self.N_RED_B, n_blue=self.N_BLUE_B,
|
|
1443
|
+
fill_color=P["urn_b"],
|
|
1444
|
+
prior=1 - self.P_PRIOR_A,
|
|
1445
|
+
)
|
|
1446
|
+
urn_b.move_to([3.40, 0.40, 0])
|
|
1447
|
+
|
|
1448
|
+
self.play(
|
|
1449
|
+
FadeIn(urn_a, shift=LEFT*0.4),
|
|
1450
|
+
FadeIn(urn_b, shift=RIGHT*0.4),
|
|
1451
|
+
run_time=1.2,
|
|
1452
|
+
)
|
|
1453
|
+
|
|
1454
|
+
# Description labels
|
|
1455
|
+
desc_a = Text(
|
|
1456
|
+
f"{self.N_RED_A} red, {self.N_BLUE_A} blue",
|
|
1457
|
+
font_size=18, color=P["subtitle_fg"],
|
|
1458
|
+
)
|
|
1459
|
+
desc_b = Text(
|
|
1460
|
+
f"{self.N_RED_B} red, {self.N_BLUE_B} blue",
|
|
1461
|
+
font_size=18, color=P["subtitle_fg"],
|
|
1462
|
+
)
|
|
1463
|
+
desc_a.next_to(urn_a, DOWN, buff=1.10)
|
|
1464
|
+
desc_b.next_to(urn_b, DOWN, buff=1.10)
|
|
1465
|
+
self.play(FadeIn(desc_a), FadeIn(desc_b), run_time=0.7)
|
|
1466
|
+
|
|
1467
|
+
# "One urn chosen at random" label
|
|
1468
|
+
chosen_lbl = Text(
|
|
1469
|
+
"One urn was chosen at random. We don't know which.",
|
|
1470
|
+
font_size=22, color=P["title_fg"],
|
|
1471
|
+
)
|
|
1472
|
+
chosen_lbl.move_to([0, -2.60, 0])
|
|
1473
|
+
self.play(Write(chosen_lbl, run_time=1.5))
|
|
1474
|
+
self.wait(0.8)
|
|
1475
|
+
|
|
1476
|
+
self.play(FadeOut(act_lbl), FadeOut(chosen_lbl),
|
|
1477
|
+
FadeOut(desc_a), FadeOut(desc_b), run_time=0.5)
|
|
1478
|
+
|
|
1479
|
+
self._urn_a = urn_a
|
|
1480
|
+
self._urn_b = urn_b
|
|
1481
|
+
return urn_a, urn_b
|
|
1482
|
+
|
|
1483
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1484
|
+
# Act 2 — Prior
|
|
1485
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1486
|
+
|
|
1487
|
+
def _act2_prior(self, urn_a, urn_b):
|
|
1488
|
+
act_lbl = _section_label("Act 2 — Prior Probability")
|
|
1489
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1490
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1491
|
+
|
|
1492
|
+
# Shrink urns to top
|
|
1493
|
+
self.play(
|
|
1494
|
+
urn_a.animate.scale(0.72).move_to([-4.80, 1.60, 0]),
|
|
1495
|
+
urn_b.animate.scale(0.72).move_to([ 4.80, 1.60, 0]),
|
|
1496
|
+
run_time=0.90,
|
|
1497
|
+
)
|
|
1498
|
+
|
|
1499
|
+
# Prior bar
|
|
1500
|
+
prob_bar = _ProbBar(
|
|
1501
|
+
p_a=self.P_PRIOR_A,
|
|
1502
|
+
width=6.0, height=0.55,
|
|
1503
|
+
title="Prior: P(A) = P(B) = 0.500",
|
|
1504
|
+
)
|
|
1505
|
+
prob_bar.move_to([0, 0.20, 0])
|
|
1506
|
+
prob_bar.set_opacity(0)
|
|
1507
|
+
|
|
1508
|
+
self.play(FadeIn(prob_bar, shift=UP*0.15,
|
|
1509
|
+
run_time=1.0))
|
|
1510
|
+
|
|
1511
|
+
# Formula badge
|
|
1512
|
+
try:
|
|
1513
|
+
prior_form = MathTex(
|
|
1514
|
+
r"P(A) = P(B) = \frac{1}{2} = 0.500"
|
|
1515
|
+
r"\quad\text{(equal prior)}",
|
|
1516
|
+
font_size=24, color=P["term_prior"],
|
|
1517
|
+
)
|
|
1518
|
+
except Exception:
|
|
1519
|
+
prior_form = Text(
|
|
1520
|
+
"P(A) = P(B) = 1/2 = 0.500 (equal prior)",
|
|
1521
|
+
font_size=20, color=P["term_prior"],
|
|
1522
|
+
)
|
|
1523
|
+
prior_form.move_to([0, -0.70, 0])
|
|
1524
|
+
self.play(Write(prior_form, run_time=1.2))
|
|
1525
|
+
self.wait(0.7)
|
|
1526
|
+
|
|
1527
|
+
# Indicate both halves
|
|
1528
|
+
self.play(
|
|
1529
|
+
Indicate(prob_bar, color=P["term_prior"],
|
|
1530
|
+
scale_factor=1.04, run_time=0.7)
|
|
1531
|
+
)
|
|
1532
|
+
self.wait(0.3)
|
|
1533
|
+
|
|
1534
|
+
self.play(FadeOut(act_lbl), FadeOut(prior_form), run_time=0.5)
|
|
1535
|
+
return prob_bar
|
|
1536
|
+
|
|
1537
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1538
|
+
# Act 3 — Draw a red ball
|
|
1539
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1540
|
+
|
|
1541
|
+
def _act3_draw(self, urn_a, urn_b, prob_bar):
|
|
1542
|
+
act_lbl = _section_label("Act 3 — The Draw")
|
|
1543
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1544
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1545
|
+
|
|
1546
|
+
# Rumble effect on both urns (we don't know which one)
|
|
1547
|
+
self.play(
|
|
1548
|
+
urn_a.animate.shift(LEFT*0.08),
|
|
1549
|
+
run_time=0.12,
|
|
1550
|
+
)
|
|
1551
|
+
self.play(
|
|
1552
|
+
urn_a.animate.shift(RIGHT*0.16),
|
|
1553
|
+
run_time=0.12,
|
|
1554
|
+
)
|
|
1555
|
+
self.play(
|
|
1556
|
+
urn_a.animate.shift(LEFT*0.08),
|
|
1557
|
+
run_time=0.10,
|
|
1558
|
+
)
|
|
1559
|
+
|
|
1560
|
+
# Red ball emerges from bottom-centre
|
|
1561
|
+
ball_display = _drawn_ball_display(
|
|
1562
|
+
P["red_ball"], "RED", radius=0.42
|
|
1563
|
+
)
|
|
1564
|
+
ball_display.move_to([0, -0.50, 0])
|
|
1565
|
+
ball_display.set_opacity(0)
|
|
1566
|
+
|
|
1567
|
+
self.play(
|
|
1568
|
+
FadeIn(ball_display, shift=UP*0.40,
|
|
1569
|
+
rate_func=rate_functions.ease_out_back),
|
|
1570
|
+
run_time=1.0,
|
|
1571
|
+
)
|
|
1572
|
+
|
|
1573
|
+
reveal_lbl = Text(
|
|
1574
|
+
"We drew a RED ball.",
|
|
1575
|
+
font_size=28, color=P["red_ball"],
|
|
1576
|
+
)
|
|
1577
|
+
reveal_lbl.move_to([0, -1.45, 0])
|
|
1578
|
+
self.play(Write(reveal_lbl, run_time=1.0))
|
|
1579
|
+
|
|
1580
|
+
question = Text(
|
|
1581
|
+
"Which urn is more likely to have produced it?",
|
|
1582
|
+
font_size=20, color=P["title_fg"],
|
|
1583
|
+
)
|
|
1584
|
+
question.move_to([0, -2.10, 0])
|
|
1585
|
+
self.play(FadeIn(question, shift=UP*0.10,
|
|
1586
|
+
run_time=0.8))
|
|
1587
|
+
self.wait(1.0)
|
|
1588
|
+
|
|
1589
|
+
self.play(
|
|
1590
|
+
FadeOut(act_lbl),
|
|
1591
|
+
FadeOut(reveal_lbl),
|
|
1592
|
+
FadeOut(question),
|
|
1593
|
+
run_time=0.5,
|
|
1594
|
+
)
|
|
1595
|
+
return ball_display
|
|
1596
|
+
|
|
1597
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1598
|
+
# Act 4 — Likelihood table
|
|
1599
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1600
|
+
|
|
1601
|
+
def _act4_likelihood(self, urn_a, urn_b, prob_bar, ball_display):
|
|
1602
|
+
act_lbl = _section_label("Act 4 — Likelihood")
|
|
1603
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1604
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1605
|
+
|
|
1606
|
+
# Move ball to side
|
|
1607
|
+
self.play(
|
|
1608
|
+
ball_display.animate.scale(0.65).move_to([4.60, -0.20, 0]),
|
|
1609
|
+
run_time=0.7,
|
|
1610
|
+
)
|
|
1611
|
+
|
|
1612
|
+
# Build likelihood table
|
|
1613
|
+
like_table = _LikelihoodTable(
|
|
1614
|
+
p_red_a=self.P_RED_A,
|
|
1615
|
+
p_red_b=self.P_RED_B,
|
|
1616
|
+
p_a=self.P_PRIOR_A,
|
|
1617
|
+
)
|
|
1618
|
+
like_table.scale(0.88)
|
|
1619
|
+
like_table.move_to([0, -0.90, 0])
|
|
1620
|
+
like_table.set_opacity(0)
|
|
1621
|
+
|
|
1622
|
+
self.play(FadeIn(like_table, shift=UP*0.15,
|
|
1623
|
+
run_time=1.1))
|
|
1624
|
+
|
|
1625
|
+
# Highlight the red row
|
|
1626
|
+
self.wait(0.4)
|
|
1627
|
+
red_row_hint = Text(
|
|
1628
|
+
"← P(red | Urn A) is 3× larger than P(red | Urn B)",
|
|
1629
|
+
font_size=17, color=P["tbl_val_red"],
|
|
1630
|
+
)
|
|
1631
|
+
red_row_hint.move_to([0, 0.95, 0])
|
|
1632
|
+
self.play(Write(red_row_hint, run_time=1.0))
|
|
1633
|
+
self.wait(0.8)
|
|
1634
|
+
|
|
1635
|
+
# Highlight marginal
|
|
1636
|
+
marg_hint = Text(
|
|
1637
|
+
"Marginal P(red) = 0.600×0.5 + 0.200×0.5 = 0.400",
|
|
1638
|
+
font_size=16, color=P["tbl_val_marg"],
|
|
1639
|
+
)
|
|
1640
|
+
marg_hint.move_to([0, 1.38, 0])
|
|
1641
|
+
self.play(FadeIn(marg_hint, shift=UP*0.08,
|
|
1642
|
+
run_time=0.8))
|
|
1643
|
+
self.wait(0.8)
|
|
1644
|
+
|
|
1645
|
+
self.play(
|
|
1646
|
+
FadeOut(act_lbl),
|
|
1647
|
+
FadeOut(red_row_hint),
|
|
1648
|
+
FadeOut(marg_hint),
|
|
1649
|
+
run_time=0.5,
|
|
1650
|
+
)
|
|
1651
|
+
return like_table
|
|
1652
|
+
|
|
1653
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1654
|
+
# Act 5 — Bayes formula
|
|
1655
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1656
|
+
|
|
1657
|
+
def _act5_bayes_formula(
|
|
1658
|
+
self, urn_a, urn_b, prob_bar, like_table, ball_display
|
|
1659
|
+
):
|
|
1660
|
+
act_lbl = _section_label("Act 5 — Bayes' Theorem")
|
|
1661
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1662
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1663
|
+
|
|
1664
|
+
# Slide everything up to make room
|
|
1665
|
+
self.play(
|
|
1666
|
+
like_table.animate.scale(0.75).move_to([-3.60, -2.30, 0]),
|
|
1667
|
+
prob_bar.animate.move_to([0, 2.15, 0]),
|
|
1668
|
+
urn_a.animate.move_to([-5.80, 2.00, 0]),
|
|
1669
|
+
urn_b.animate.move_to([ 5.80, 2.00, 0]),
|
|
1670
|
+
run_time=0.80,
|
|
1671
|
+
)
|
|
1672
|
+
|
|
1673
|
+
# Build Bayes formula
|
|
1674
|
+
formula = _BayesFormula(
|
|
1675
|
+
p_a=self.P_PRIOR_A,
|
|
1676
|
+
p_red_a=self.P_RED_A,
|
|
1677
|
+
p_red_b=self.P_RED_B,
|
|
1678
|
+
)
|
|
1679
|
+
formula.scale(0.88)
|
|
1680
|
+
formula.move_to([0.80, -0.10, 0])
|
|
1681
|
+
formula.set_opacity(0)
|
|
1682
|
+
|
|
1683
|
+
self.play(FadeIn(formula, shift=UP*0.15,
|
|
1684
|
+
run_time=1.2))
|
|
1685
|
+
self.wait(0.5)
|
|
1686
|
+
|
|
1687
|
+
# Highlight terms one at a time
|
|
1688
|
+
self.play(
|
|
1689
|
+
Indicate(formula.sym_pieces[2], # P(red|A)
|
|
1690
|
+
color=P["term_like"],
|
|
1691
|
+
scale_factor=1.15, run_time=0.7),
|
|
1692
|
+
)
|
|
1693
|
+
self.wait(0.2)
|
|
1694
|
+
self.play(
|
|
1695
|
+
Indicate(formula.sym_pieces[4], # P(A)
|
|
1696
|
+
color=P["term_prior"],
|
|
1697
|
+
scale_factor=1.15, run_time=0.7),
|
|
1698
|
+
)
|
|
1699
|
+
self.wait(0.2)
|
|
1700
|
+
self.play(
|
|
1701
|
+
Indicate(formula.sym_pieces[6], # P(red)
|
|
1702
|
+
color=P["term_marg"],
|
|
1703
|
+
scale_factor=1.15, run_time=0.7),
|
|
1704
|
+
)
|
|
1705
|
+
self.wait(0.3)
|
|
1706
|
+
|
|
1707
|
+
# Highlight the result
|
|
1708
|
+
self.play(
|
|
1709
|
+
Flash(formula.result_pieces[2].get_center(),
|
|
1710
|
+
color=P["term_post"],
|
|
1711
|
+
line_length=0.16, num_lines=10,
|
|
1712
|
+
run_time=0.7),
|
|
1713
|
+
Indicate(formula.result_pieces[2],
|
|
1714
|
+
color=P["term_post"],
|
|
1715
|
+
scale_factor=1.20, run_time=0.7),
|
|
1716
|
+
)
|
|
1717
|
+
self.wait(0.7)
|
|
1718
|
+
|
|
1719
|
+
# Arrow from formula result to prob bar
|
|
1720
|
+
post_val = formula._posterior_a
|
|
1721
|
+
arr = Arrow(
|
|
1722
|
+
start=formula.result_pieces[2].get_center()
|
|
1723
|
+
+ np.array([0, 0.30, 0]),
|
|
1724
|
+
end =prob_bar.get_center() + np.array([0, -0.40, 0]),
|
|
1725
|
+
stroke_color=P["term_post"],
|
|
1726
|
+
stroke_width=1.8, tip_length=0.16, buff=0.08,
|
|
1727
|
+
)
|
|
1728
|
+
self.play(Create(arr, run_time=0.6))
|
|
1729
|
+
self.wait(0.3)
|
|
1730
|
+
|
|
1731
|
+
self.play(FadeOut(act_lbl), FadeOut(arr), run_time=0.5)
|
|
1732
|
+
return prob_bar, formula
|
|
1733
|
+
|
|
1734
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1735
|
+
# Act 6 — Posterior bar (first update)
|
|
1736
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1737
|
+
|
|
1738
|
+
def _act6_posterior_bar(self, urn_a, urn_b, prob_bar, formula):
|
|
1739
|
+
act_lbl = _section_label("Act 6 — Posterior Update")
|
|
1740
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1741
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1742
|
+
|
|
1743
|
+
post_a = formula._posterior_a # 0.750
|
|
1744
|
+
|
|
1745
|
+
# Morph the prior bar into posterior
|
|
1746
|
+
new_bar = _ProbBar(
|
|
1747
|
+
p_a=post_a,
|
|
1748
|
+
width=6.0, height=0.55,
|
|
1749
|
+
title=f"Posterior: P(A|red) = {post_a:.3f} P(B|red) = {1-post_a:.3f}",
|
|
1750
|
+
)
|
|
1751
|
+
new_bar.move_to(prob_bar.get_center())
|
|
1752
|
+
|
|
1753
|
+
self.play(
|
|
1754
|
+
ReplacementTransform(prob_bar, new_bar, run_time=1.4)
|
|
1755
|
+
)
|
|
1756
|
+
|
|
1757
|
+
# Flash the dominant side
|
|
1758
|
+
self.play(
|
|
1759
|
+
Indicate(new_bar, color=P["bar_a"],
|
|
1760
|
+
scale_factor=1.04, run_time=0.7)
|
|
1761
|
+
)
|
|
1762
|
+
self.wait(0.4)
|
|
1763
|
+
|
|
1764
|
+
# Update urn probability badges
|
|
1765
|
+
new_badge_a = urn_a.update_prior_badge(post_a)
|
|
1766
|
+
new_badge_b = urn_b.update_prior_badge(1 - post_a)
|
|
1767
|
+
self.play(
|
|
1768
|
+
FadeIn(new_badge_a, shift=UP*0.06),
|
|
1769
|
+
FadeIn(new_badge_b, shift=UP*0.06),
|
|
1770
|
+
run_time=0.6,
|
|
1771
|
+
)
|
|
1772
|
+
self.wait(0.5)
|
|
1773
|
+
|
|
1774
|
+
self.play(FadeOut(act_lbl), run_time=0.4)
|
|
1775
|
+
return new_bar
|
|
1776
|
+
|
|
1777
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1778
|
+
# Act 7 — Sequential updates
|
|
1779
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1780
|
+
|
|
1781
|
+
def _act7_sequential(
|
|
1782
|
+
self, urn_a, urn_b, prob_bar, like_table, ball_display
|
|
1783
|
+
):
|
|
1784
|
+
act_lbl = _section_label("Act 7 — Sequential Updates")
|
|
1785
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1786
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1787
|
+
|
|
1788
|
+
# Starting posterior after draw 1 (red) was P(A|red) = 0.75
|
|
1789
|
+
p_red_a = self.P_RED_A
|
|
1790
|
+
p_red_b = self.P_RED_B
|
|
1791
|
+
|
|
1792
|
+
history = [("red ←", 0.750)] # after draw 1
|
|
1793
|
+
|
|
1794
|
+
# We'll track P(A) as the running posterior
|
|
1795
|
+
p_a_current = 0.750
|
|
1796
|
+
|
|
1797
|
+
draw_configs = [
|
|
1798
|
+
# (ball_color, ball_label, is_red)
|
|
1799
|
+
(P["red_ball"], "RED", True),
|
|
1800
|
+
(P["blue_ball"], "BLUE", False),
|
|
1801
|
+
]
|
|
1802
|
+
|
|
1803
|
+
for draw_num, (bcol, blbl, is_red) in enumerate(draw_configs,
|
|
1804
|
+
start=2):
|
|
1805
|
+
# Show drawn ball
|
|
1806
|
+
ball = _drawn_ball_display(bcol, blbl, radius=0.30)
|
|
1807
|
+
ball.move_to([4.80, -0.20, 0])
|
|
1808
|
+
self.play(FadeIn(ball, shift=LEFT*0.20, run_time=0.6))
|
|
1809
|
+
|
|
1810
|
+
# Compute updated posterior
|
|
1811
|
+
p_like_a = p_red_a if is_red else (1 - p_red_a)
|
|
1812
|
+
p_like_b = p_red_b if is_red else (1 - p_red_b)
|
|
1813
|
+
p_marg = p_like_a * p_a_current + p_like_b * (1 - p_a_current)
|
|
1814
|
+
p_a_new = (p_like_a * p_a_current) / max(p_marg, 1e-9)
|
|
1815
|
+
|
|
1816
|
+
# Highlight relevant likelihood row in table
|
|
1817
|
+
self.play(
|
|
1818
|
+
Indicate(like_table,
|
|
1819
|
+
color=bcol,
|
|
1820
|
+
scale_factor=1.03,
|
|
1821
|
+
run_time=0.6),
|
|
1822
|
+
)
|
|
1823
|
+
|
|
1824
|
+
# Morph probability bar
|
|
1825
|
+
obs_str = blbl.lower()
|
|
1826
|
+
bar_title = (
|
|
1827
|
+
f"After draw {draw_num} ({blbl}): "
|
|
1828
|
+
f"P(A) = {p_a_new:.3f} "
|
|
1829
|
+
f"P(B) = {1-p_a_new:.3f}"
|
|
1830
|
+
)
|
|
1831
|
+
new_bar = _ProbBar(
|
|
1832
|
+
p_a=p_a_new,
|
|
1833
|
+
width=6.0, height=0.55,
|
|
1834
|
+
title=bar_title,
|
|
1835
|
+
)
|
|
1836
|
+
new_bar.move_to(prob_bar.get_center())
|
|
1837
|
+
self.play(
|
|
1838
|
+
ReplacementTransform(prob_bar, new_bar, run_time=1.1)
|
|
1839
|
+
)
|
|
1840
|
+
prob_bar = new_bar
|
|
1841
|
+
|
|
1842
|
+
history.append((f"{blbl.lower()} ←", p_a_new))
|
|
1843
|
+
p_a_current = p_a_new
|
|
1844
|
+
|
|
1845
|
+
self.play(FadeOut(ball), run_time=0.35)
|
|
1846
|
+
self.wait(0.35)
|
|
1847
|
+
|
|
1848
|
+
# Build posterior trail
|
|
1849
|
+
trail = _PosteriorTrail(
|
|
1850
|
+
history=history,
|
|
1851
|
+
bar_w=3.0, bar_h=0.28, spacing=0.46,
|
|
1852
|
+
)
|
|
1853
|
+
trail.move_to([-1.50, -1.70, 0])
|
|
1854
|
+
trail.set_opacity(0)
|
|
1855
|
+
self.play(FadeIn(trail, shift=UP*0.10, run_time=0.9))
|
|
1856
|
+
self.wait(0.7)
|
|
1857
|
+
|
|
1858
|
+
self.play(FadeOut(act_lbl), run_time=0.4)
|
|
1859
|
+
return prob_bar, trail
|
|
1860
|
+
|
|
1861
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1862
|
+
# Act 8 — Beta distribution posterior
|
|
1863
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1864
|
+
|
|
1865
|
+
def _act8_beta_curve(self, urn_a, urn_b, prob_bar, trail):
|
|
1866
|
+
act_lbl = _section_label("Act 8 — Continuous Posterior")
|
|
1867
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1868
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1869
|
+
|
|
1870
|
+
# Transition: move prob bar + trail out
|
|
1871
|
+
self.play(
|
|
1872
|
+
prob_bar.animate.shift(DOWN*5).set_opacity(0),
|
|
1873
|
+
trail.animate.shift(DOWN*5).set_opacity(0),
|
|
1874
|
+
run_time=0.7,
|
|
1875
|
+
)
|
|
1876
|
+
self.remove(prob_bar, trail)
|
|
1877
|
+
|
|
1878
|
+
# Beta-Binomial model:
|
|
1879
|
+
# Prior: Beta(1, 1) = Uniform
|
|
1880
|
+
# After 2 reds and 1 blue from chosen urn: Beta(3, 2)
|
|
1881
|
+
# (counts: successes = 2 red, failures = 1 blue → α=1+2=3, β=1+1=2)
|
|
1882
|
+
alpha_post = 3.0 # prior_α + successes
|
|
1883
|
+
beta_post = 2.0 # prior_β + failures
|
|
1884
|
+
|
|
1885
|
+
intro_lbl = Text(
|
|
1886
|
+
"Beta-Binomial model: 3 draws (2 red, 1 blue)\n"
|
|
1887
|
+
"Prior: Beta(1,1) → Posterior: Beta(3,2)",
|
|
1888
|
+
font_size=20, color=P["subtitle_fg"],
|
|
1889
|
+
)
|
|
1890
|
+
intro_lbl.move_to([0, 2.20, 0])
|
|
1891
|
+
self.play(Write(intro_lbl, run_time=1.2))
|
|
1892
|
+
|
|
1893
|
+
beta_curve = _BetaCurve(
|
|
1894
|
+
alpha_param=alpha_post,
|
|
1895
|
+
beta_param=beta_post,
|
|
1896
|
+
width=6.0, height=3.0,
|
|
1897
|
+
)
|
|
1898
|
+
beta_curve.move_to([0, -0.20, 0])
|
|
1899
|
+
beta_curve.set_opacity(0)
|
|
1900
|
+
|
|
1901
|
+
self.play(FadeIn(beta_curve, shift=UP*0.15, run_time=1.3))
|
|
1902
|
+
self.wait(0.5)
|
|
1903
|
+
|
|
1904
|
+
# Mark the mode
|
|
1905
|
+
mode = (alpha_post - 1) / (alpha_post + beta_post - 2)
|
|
1906
|
+
mode_px = (mode - 0.5) * 6.0
|
|
1907
|
+
mode_line = DashedLine(
|
|
1908
|
+
start=[mode_px, -1.65, 0.010],
|
|
1909
|
+
end =[mode_px, 1.20, 0.010],
|
|
1910
|
+
stroke_color=P["term_post"],
|
|
1911
|
+
stroke_width=2.0,
|
|
1912
|
+
dash_length=0.11,
|
|
1913
|
+
)
|
|
1914
|
+
mode_lbl = Text(
|
|
1915
|
+
f"Mode = {mode:.2f}",
|
|
1916
|
+
font_size=17, color=P["term_post"],
|
|
1917
|
+
)
|
|
1918
|
+
mode_lbl.move_to([mode_px + 0.90, 1.42, 0.010])
|
|
1919
|
+
self.play(
|
|
1920
|
+
Create(mode_line, run_time=0.6),
|
|
1921
|
+
FadeIn(mode_lbl, run_time=0.5),
|
|
1922
|
+
)
|
|
1923
|
+
self.wait(0.8)
|
|
1924
|
+
|
|
1925
|
+
self.play(
|
|
1926
|
+
FadeOut(act_lbl),
|
|
1927
|
+
FadeOut(intro_lbl),
|
|
1928
|
+
FadeOut(mode_line),
|
|
1929
|
+
FadeOut(mode_lbl),
|
|
1930
|
+
run_time=0.5,
|
|
1931
|
+
)
|
|
1932
|
+
|
|
1933
|
+
# Fade out beta curve for act 9
|
|
1934
|
+
self.play(FadeOut(beta_curve), run_time=0.5)
|
|
1935
|
+
|
|
1936
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1937
|
+
# Act 9 — Decision and summary
|
|
1938
|
+
# ─────────────────────────────────────────────────────────────────
|
|
1939
|
+
|
|
1940
|
+
def _act9_decision(self, urn_a, urn_b):
|
|
1941
|
+
act_lbl = _section_label("Act 9 — Decision")
|
|
1942
|
+
act_lbl.to_corner(LEFT + UP, buff=0.22)
|
|
1943
|
+
self.play(FadeIn(act_lbl, run_time=0.4))
|
|
1944
|
+
|
|
1945
|
+
# Bring urns back centre-stage
|
|
1946
|
+
self.play(
|
|
1947
|
+
urn_a.animate.scale(1.0 / 0.72).move_to([-3.40, 0.50, 0]),
|
|
1948
|
+
urn_b.animate.scale(1.0 / 0.72).move_to([ 3.40, 0.50, 0]),
|
|
1949
|
+
run_time=0.80,
|
|
1950
|
+
)
|
|
1951
|
+
|
|
1952
|
+
# Final posterior bar
|
|
1953
|
+
# After 2 red + 1 blue draws:
|
|
1954
|
+
# Draw 1 red: P(A)=0.5 → P(A|r1) = 0.600×0.5/0.4 = 0.750
|
|
1955
|
+
# Draw 2 red: P(A)=0.75 → P(A|r2) = 0.600×0.75/0.500 ≈ 0.900
|
|
1956
|
+
# Draw 3 blue: P(A)=0.9 → P(A|b3) = 0.400×0.9/0.380 ≈ 0.947
|
|
1957
|
+
final_p_a = (self.P_RED_A**2 * (1-self.P_RED_A)
|
|
1958
|
+
* self.P_PRIOR_A)
|
|
1959
|
+
final_p_b = (self.P_RED_B**2 * (1-self.P_RED_B)
|
|
1960
|
+
* (1-self.P_PRIOR_A))
|
|
1961
|
+
norm = final_p_a + final_p_b
|
|
1962
|
+
final_p_a /= norm
|
|
1963
|
+
final_p_b = 1 - final_p_a
|
|
1964
|
+
|
|
1965
|
+
final_bar = _ProbBar(
|
|
1966
|
+
p_a=final_p_a, width=5.0, height=0.50,
|
|
1967
|
+
title=(f"Final posterior: "
|
|
1968
|
+
f"P(A|data) = {final_p_a:.3f} "
|
|
1969
|
+
f"P(B|data) = {final_p_b:.3f}"),
|
|
1970
|
+
)
|
|
1971
|
+
final_bar.move_to([0, -1.60, 0])
|
|
1972
|
+
self.play(FadeIn(final_bar, shift=UP*0.12, run_time=0.9))
|
|
1973
|
+
self.wait(0.4)
|
|
1974
|
+
|
|
1975
|
+
# Odds statement
|
|
1976
|
+
odds = final_p_a / max(final_p_b, 1e-9)
|
|
1977
|
+
verdict = Text(
|
|
1978
|
+
f"Urn A is {odds:.1f}× more likely than Urn B.",
|
|
1979
|
+
font_size=27, color=P["decision_fg"],
|
|
1980
|
+
)
|
|
1981
|
+
verdict.move_to([0, -2.55, 0])
|
|
1982
|
+
self.play(Write(verdict, run_time=1.1))
|
|
1983
|
+
|
|
1984
|
+
# Flash Urn A
|
|
1985
|
+
self.play(
|
|
1986
|
+
Indicate(urn_a, color=P["decision_fg"],
|
|
1987
|
+
scale_factor=1.07, run_time=0.80),
|
|
1988
|
+
Flash(urn_a.get_center(),
|
|
1989
|
+
color=P["decision_fg"],
|
|
1990
|
+
line_length=0.22,
|
|
1991
|
+
num_lines=12,
|
|
1992
|
+
run_time=0.80),
|
|
1993
|
+
)
|
|
1994
|
+
|
|
1995
|
+
# Decision badge on Urn A
|
|
1996
|
+
badge_bg = RoundedRectangle(
|
|
1997
|
+
width=2.60, height=0.55,
|
|
1998
|
+
corner_radius=0.10,
|
|
1999
|
+
fill_color=P["decision_bg"],
|
|
2000
|
+
fill_opacity=0.92,
|
|
2001
|
+
stroke_color=P["decision_fg"],
|
|
2002
|
+
stroke_width=1.8,
|
|
2003
|
+
)
|
|
2004
|
+
badge_bg.move_to([-3.40, 1.92, 0])
|
|
2005
|
+
badge_txt = Text("MOST LIKELY", font_size=22,
|
|
2006
|
+
color=P["decision_fg"])
|
|
2007
|
+
badge_txt.move_to(badge_bg.get_center() + np.array([0,0,0.001]))
|
|
2008
|
+
badge = VGroup(badge_bg, badge_txt)
|
|
2009
|
+
badge.rotate(0.06)
|
|
2010
|
+
self.play(
|
|
2011
|
+
GrowFromCenter(badge,
|
|
2012
|
+
rate_func=rate_functions.ease_out_back,
|
|
2013
|
+
run_time=0.75),
|
|
2014
|
+
)
|
|
2015
|
+
self.wait(0.6)
|
|
2016
|
+
|
|
2017
|
+
# Summary panel
|
|
2018
|
+
self.play(
|
|
2019
|
+
FadeOut(act_lbl),
|
|
2020
|
+
FadeOut(verdict),
|
|
2021
|
+
FadeOut(final_bar),
|
|
2022
|
+
FadeOut(badge),
|
|
2023
|
+
urn_a.animate.scale(0.60).move_to([-5.50, 1.80, 0]),
|
|
2024
|
+
urn_b.animate.scale(0.60).move_to([ 5.50, 1.80, 0]),
|
|
2025
|
+
run_time=0.70,
|
|
2026
|
+
)
|
|
2027
|
+
|
|
2028
|
+
summary = _SummaryPanel(
|
|
2029
|
+
p_a_prior=self.P_PRIOR_A,
|
|
2030
|
+
p_red_a=self.P_RED_A,
|
|
2031
|
+
p_red_b=self.P_RED_B,
|
|
2032
|
+
p_a_posterior=final_p_a,
|
|
2033
|
+
panel_w=7.50,
|
|
2034
|
+
)
|
|
2035
|
+
summary.move_to([0, -0.20, 0])
|
|
2036
|
+
summary.set_opacity(0)
|
|
2037
|
+
self.play(FadeIn(summary, shift=UP*0.15, run_time=1.2))
|
|
2038
|
+
self.wait(2.5)
|
|
2039
|
+
|
|
2040
|
+
# Closing fade
|
|
2041
|
+
self.play(
|
|
2042
|
+
FadeOut(summary),
|
|
2043
|
+
FadeOut(urn_a),
|
|
2044
|
+
FadeOut(urn_b),
|
|
2045
|
+
run_time=1.0,
|
|
2046
|
+
)
|
|
2047
|
+
|
|
2048
|
+
# End card
|
|
2049
|
+
end_card = _title_card(
|
|
2050
|
+
"Bayes' Theorem",
|
|
2051
|
+
"Prior × Likelihood → Posterior",
|
|
2052
|
+
main_fs=42, sub_fs=24,
|
|
2053
|
+
)
|
|
2054
|
+
self.play(FadeIn(end_card, run_time=1.0))
|
|
2055
|
+
self.wait(2.0)
|
|
2056
|
+
self.play(FadeOut(end_card, run_time=0.8))
|