# -*- coding: utf-8 -*- import sys, os sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from manim import * from tiktok import * # real data (Charig et al., 1986): treatment A vs B for kidney stones LEVER = (("A", 81, 87), ("B", 234, 270)) GRAV_FIELD = (("A", 192, 263), ("B", 55, 80)) TOT = (("A", 81 + 192, 87 + 263), ("B", 234 + 55, 270 + 80)) BARW = 4.05 # bar width at 100% XL = -1.55 # left edge of the bars class Simpson(TikTok): NAME = "simpson" def bar_m(self, letter, num, den, y, wins): pct = 100.0 * num / den col = C_GREEN if wins else C_GRAY lab = Text(letter, font=FONT, font_size=50, weight=BOLD, color=C_SKY if letter == "A" else C_PINK) lab.move_to([-3.65, y, 0]) track = RoundedRectangle(corner_radius=0.12, width=BARW, height=0.62, stroke_width=0, fill_color="#1B2233", fill_opacity=1) track.move_to([XL, y, 0], LEFT) fill = RoundedRectangle(corner_radius=0.12, width=max(pct / 100 * BARW, 0.2), height=0.62, stroke_width=0, fill_color=col, fill_opacity=1) fill.move_to([XL, y, 0], LEFT) val = Text(f"{pct:.0f}%", font=FONT, font_size=42, weight=BOLD, color=col) val.next_to(track, RIGHT, buff=0.22) return VGroup(lab, track, fill, val) def block_obj(self, title, dataset, ytop, winner): t = Text(title, font=FONT, font_size=40, weight=BOLD, color=WHITE) t.move_to([-3.85, ytop, 0], LEFT) b1 = self.bar_m(dataset[0][0], dataset[0][1], dataset[0][2], ytop - 0.85, dataset[0][0] == winner) b2 = self.bar_m(dataset[1][0], dataset[1][1], dataset[1][2], ytop - 1.70, dataset[1][0] == winner) return VGroup(t, b1, b2) def construct(self): self.prepare() chip = self.chip("Estadística que engaña", C_PINK) with self.beat(0) as b: b.play(FadeIn(chip, shift=DOWN * 0.3), run_time=0.45) header = VGroup( self.fit(Text("dos tratamientos", font=FONT, font_size=52, weight=BOLD, color=WHITE), 8.2), VGroup( Text("A", font=FONT, font_size=90, weight=BOLD, color=C_SKY), Text("vs", font=FONT, font_size=44, color=C_GRAY), Text("B", font=FONT, font_size=90, weight=BOLD, color=C_PINK), ).arrange(RIGHT, buff=0.7), ).arrange(DOWN, buff=0.45).move_to(UP * 3.0) b.play(FadeIn(header[0], shift=UP * 0.15), run_time=0.6) b.play(FadeIn(header[1][0], scale=0.6), run_time=0.35) b.play(FadeIn(header[1][1]), run_time=0.2) b.play(FadeIn(header[1][2], scale=0.6), run_time=0.35) lever = self.block_obj("casos LEVES", LEVER, 2.55, "A") with self.beat(1) as b: b.play(header.animate.scale(0.5).move_to(UP * 5.4), run_time=0.5) b.play(FadeIn(lever[0], shift=RIGHT * 0.2), run_time=0.4) for bb in (lever[1], lever[2]): b.play(FadeIn(bb[0]), FadeIn(bb[1]), GrowFromEdge(bb[2], LEFT), FadeIn(bb[3]), run_time=0.55) grav = self.block_obj("casos GRAVES", GRAV_FIELD, -0.35, "A") with self.beat(2) as b: b.play(FadeIn(grav[0], shift=RIGHT * 0.2), run_time=0.4) for bb in (grav[1], grav[2]): b.play(FadeIn(bb[0]), FadeIn(bb[1]), GrowFromEdge(bb[2], LEFT), FadeIn(bb[3]), run_time=0.55) def tilde(col=C_GREEN): m = VMobject(stroke_color=col, stroke_width=9) m.set_points_as_corners([[-0.20, 0.02, 0], [-0.05, -0.18, 0], [0.24, 0.26, 0]]) return m with self.beat(3) as b: tick = VGroup(*[tilde() for _ in range(2)]) tick[0].next_to(lever[1][3], RIGHT, buff=0.25) tick[1].next_to(grav[1][3], RIGHT, buff=0.25) b.play(FadeIn(tick[0], scale=0.5), run_time=0.4) b.play(FadeIn(tick[1], scale=0.5), run_time=0.4) pre_frames = self.fit(Text("A gana 2 de 2", font=FONT, font_size=48, weight=BOLD, color=C_SKY), 8.0).move_to(DOWN * 2.80) b.play(FadeIn(pre_frames, shift=UP * 0.15), run_time=0.5) tot = self.block_obj("TODOS juntos", TOT, 2.55, "B") with self.beat(4) as b: b.play(FadeOut(VGroup(lever, grav, tick, pre_frames)), run_time=0.45) b.play(FadeIn(tot[0], shift=RIGHT * 0.2), run_time=0.4) for bb in (tot[1], tot[2]): b.play(FadeIn(bb[0]), FadeIn(bb[1]), GrowFromEdge(bb[2], LEFT), FadeIn(bb[3]), run_time=0.6) with self.beat(5) as b: b.play(Circumscribe(tot[2], color=C_GREEN, buff=0.18, stroke_width=6), run_time=0.8) g = self.fit(Text("gana B", font=FONT, font_size=70, weight=BOLD, color=C_PINK), 8.0).move_to(DOWN * 0.75) ne = self.fit(Text("y no hay ningún error", font=FONT, font_size=44, weight=BOLD, color=WHITE), 8.0).next_to(g, DOWN, buff=0.4) b.play(FadeIn(g, scale=0.7), run_time=0.6) b.play(FadeIn(ne, shift=UP * 0.15), run_time=0.5) with self.beat(6) as b: b.play(FadeOut(VGroup(tot, g, ne, header)), run_time=0.4) nm_key = VGroup( self.fit(Text("PARADOJA", font=FONT, font_size=72, weight=BOLD, color=AMBER), 8.2), self.fit(Text("DE SIMPSON", font=FONT, font_size=72, weight=BOLD, color=AMBER), 8.2), ).arrange(DOWN, buff=0.2).move_to(UP * 2.6) b.play(FadeIn(nm_key, scale=0.75), run_time=0.8) b.play(Flash(nm_key, color=AMBER, line_length=0.6, num_lines=20, flash_radius=3.0), run_time=0.7) def split_up(letter, col, minor, graves, y): lab = Text(letter, font=FONT, font_size=50, weight=BOLD, color=col) lab.move_to([-3.65, y, 0]) n = minor + graves wl = minor / n * BARW a = Rectangle(width=max(wl, 0.05), height=0.7, stroke_width=0, fill_color=C_GREEN, fill_opacity=1).move_to([XL, y, 0], LEFT) c = Rectangle(width=max(BARW - wl, 0.05), height=0.7, stroke_width=0, fill_color=C_PINK, fill_opacity=1).next_to(a, RIGHT, buff=0) nl = Text(str(minor), font=FONT, font_size=30, weight=BOLD, color="#0B0F1A").move_to(a) ng = Text(str(graves), font=FONT, font_size=30, weight=BOLD, color="#0B0F1A").move_to(c) return VGroup(lab, a, c, nl, ng) legend = VGroup( VGroup(Square(0.34, stroke_width=0, fill_color=C_GREEN, fill_opacity=1), Text("leves", font=FONT, font_size=32, color=WHITE)).arrange(RIGHT, buff=0.18), VGroup(Square(0.34, stroke_width=0, fill_color=C_PINK, fill_opacity=1), Text("graves", font=FONT, font_size=32, color=WHITE)).arrange(RIGHT, buff=0.18), ).arrange(RIGHT, buff=0.8).move_to(UP * 0.55) rA = split_up("A", C_SKY, 87, 263, -0.65) rB = split_up("B", C_PINK, 270, 80, -1.75) with self.beat(7) as b: b.play(nm_key.animate.scale(0.55).move_to(UP * 5.3), run_time=0.5) b.play(FadeIn(legend), run_time=0.4) b.play(FadeIn(rA[0]), GrowFromEdge(VGroup(rA[1], rA[2]), LEFT), FadeIn(VGroup(rA[3], rA[4])), run_time=0.8) b.play(Circumscribe(rA[2], color=C_PINK, buff=0.1, stroke_width=5), run_time=0.7) with self.beat(8) as b: b.play(FadeIn(rB[0]), GrowFromEdge(VGroup(rB[1], rB[2]), LEFT), FadeIn(VGroup(rB[3], rB[4])), run_time=0.8) b.play(Circumscribe(rB[1], color=C_GREEN, buff=0.1, stroke_width=5), run_time=0.7) with self.beat(9) as b: b.play(FadeOut(VGroup(rA, rB, legend)), run_time=0.4) t = VGroup( self.fit(Text("el promedio mezcla", font=FONT, font_size=48, weight=BOLD, color=WHITE), 8.2), self.fit(Text("dos grupos distintos", font=FONT, font_size=48, weight=BOLD, color=AMBER), 8.2), self.fit(Text("(datos de un estudio real)", font=FONT, font_size=34, color=C_GRAY), 8.2), ).arrange(DOWN, buff=0.35).move_to(UP * 1.6) b.play(FadeIn(t[0], shift=UP * 0.15), run_time=0.55) b.play(FadeIn(t[1], shift=UP * 0.15), run_time=0.55) b.play(FadeIn(t[2]), run_time=0.45) with self.beat(10) as b: b.play(FadeOut(t), run_time=0.35) count_txt = VGroup( self.fit(Text("cuando te muestren", font=FONT, font_size=44, weight=BOLD, color=WHITE), 8.0), self.fit(Text("un promedio, preguntá:", font=FONT, font_size=44, weight=BOLD, color=WHITE), 8.0), self.fit(Text("¿de quiénes?", font=FONT, font_size=64, weight=BOLD, color=AMBER), 8.0), ).arrange(DOWN, buff=0.3).move_to(UP * 1.4) b.play(FadeIn(count_txt[0], shift=UP * 0.12), run_time=0.45) b.play(FadeIn(count_txt[1], shift=UP * 0.12), run_time=0.45) b.play(Write(count_txt[2]), run_time=0.8) b.play(Circumscribe(count_txt[2], color=AMBER, buff=0.25, stroke_width=6), run_time=0.8) self.finish()