diff --git a/active_projects/eop/chapter1/area_model_bayes.py b/active_projects/eop/chapter1/area_model_bayes.py index 1dae8d35..fe6a284d 100644 --- a/active_projects/eop/chapter1/area_model_bayes.py +++ b/active_projects/eop/chapter1/area_model_bayes.py @@ -203,7 +203,7 @@ class IllustrateAreaModelBayes(Scene): # solve formula for P(B|A) - rearranged_formula = TexMobject(["P(B\mid A)", "=", "{P(A\\text{ and }B) \over P(A)}"]) + rearranged_formula = TexMobject("P(B\mid A)", "=", "{P(A\\text{ and }B) \over P(A)}") rearranged_formula.move_to(indep_formula) self.wait() diff --git a/active_projects/eop/chapter1/brick_row_scene.py b/active_projects/eop/chapter1/brick_row_scene.py index 8782db89..a4c0b19e 100644 --- a/active_projects/eop/chapter1/brick_row_scene.py +++ b/active_projects/eop/chapter1/brick_row_scene.py @@ -714,16 +714,16 @@ class BrickRowScene(PiCreatureScene): run_time = 5)) self.wait() - brace1 = Brace(self.row.rects[2], UP) - brace2 = Brace(self.row.rects[3], UP) - p1 = TexMobject("{3\over 8}").next_to(brace1, UP) - p2 = TexMobject("{1\over 8}").next_to(brace2, UP) + braces = VGroup(*[Brace(rect, UP) for rect in self.row.rects]) + counts = [choose(3, i) for i in range(4)] + probs = VGroup(*[TexMobject("{" + str(k) + "\over 8}") for k in counts]) + for (brace, prob) in zip(braces, probs): + prob.next_to(brace, UP) + self.play( - ShowCreation(brace1), - ShowCreation(brace2), - Write(p1), - Write(p2), + LaggedStart(ShowCreation, braces), + LaggedStart(Write, probs) ) self.wait() self.play(LaggedStart( @@ -737,13 +737,11 @@ class BrickRowScene(PiCreatureScene): self.wait() self.play( - FadeOut(brace1), - FadeOut(brace2), - FadeOut(p1), - FadeOut(p2), + FadeOut(braces), + FadeOut(probs) ) self.wait() - + # put visuals for other probability distribtuions here diff --git a/active_projects/eop/chapter1/intro.py b/active_projects/eop/chapter1/intro.py index 39a924d3..9689fa51 100644 --- a/active_projects/eop/chapter1/intro.py +++ b/active_projects/eop/chapter1/intro.py @@ -42,8 +42,9 @@ class Introduction(TeacherStudentsScene): self.wait() - - self.get_teacher().change_mode("raise_right_hand") + self.play( + self.get_teacher().change_mode,"raise_right_hand" + ) self.wait() self.wait(30) diff --git a/active_projects/eop/chapter1/prob_dist_visuals.py b/active_projects/eop/chapter1/prob_dist_visuals.py index 1292d7b4..24302403 100644 --- a/active_projects/eop/chapter1/prob_dist_visuals.py +++ b/active_projects/eop/chapter1/prob_dist_visuals.py @@ -17,19 +17,9 @@ class ProbabilityDistributions(PiCreatureScene): run_time = 3 - text = TextMobject("Probability distributions", color = YELLOW) - text.to_edge(UP) - text_rect = SurroundingRectangle(text, buff = MED_SMALL_BUFF) - - self.play( - FadeIn(text), - ShowCreation(text_rect) - ) - - # WEATHER FORECAST - + unit_rect = Rectangle( height = 3, width = 3 ).shift(DOWN) @@ -48,7 +38,7 @@ class ProbabilityDistributions(PiCreatureScene): sun_rect.set_fill(color = YELLOW, opacity = opacity) sun_rect.set_stroke(width = 0) - self.play(FadeIn(VGroup(unit_rect, rain_rect, sun_rect))) + self.add(unit_rect, rain_rect, sun_rect) rain = SVGMobject(file_name = "rain").scale(0.35) sun = SVGMobject(file_name = "sun").scale(0.35) @@ -58,6 +48,7 @@ class ProbabilityDistributions(PiCreatureScene): self.add(rain, sun) + text_scale = 0.7 brace_rain = Brace(rain_rect, UP) @@ -76,72 +67,73 @@ class ProbabilityDistributions(PiCreatureScene): self.add(brace_rain, p_rain_whole_label, brace_sun, p_sun_whole_label) + self.wait(6) - new_p_rain = 0.68 - new_p_sun = 1 - new_p_rain + # new_p_rain = 0.68 + # new_p_sun = 1 - new_p_rain - new_rain_rect = unit_rect.copy().stretch(new_p_rain, 0) - new_rain_rect.align_to(unit_rect, LEFT) - new_rain_rect.set_fill(color = BLUE, opacity = opacity) - new_rain_rect.set_stroke(width = 0) + # new_rain_rect = unit_rect.copy().stretch(new_p_rain, 0) + # new_rain_rect.align_to(unit_rect, LEFT) + # new_rain_rect.set_fill(color = BLUE, opacity = opacity) + # new_rain_rect.set_stroke(width = 0) - new_sun_rect = unit_rect.copy().stretch(new_p_sun, 0) - new_sun_rect.next_to(new_rain_rect, RIGHT, buff = 0) - new_sun_rect.set_fill(color = YELLOW, opacity = opacity) - new_sun_rect.set_stroke(width = 0) + # new_sun_rect = unit_rect.copy().stretch(new_p_sun, 0) + # new_sun_rect.next_to(new_rain_rect, RIGHT, buff = 0) + # new_sun_rect.set_fill(color = YELLOW, opacity = opacity) + # new_sun_rect.set_stroke(width = 0) - new_rain = SVGMobject(file_name = "rain").scale(0.35) - new_sun = SVGMobject(file_name = "sun").scale(0.35) + # new_rain = SVGMobject(file_name = "rain").scale(0.35) + # new_sun = SVGMobject(file_name = "sun").scale(0.35) - new_rain.flip().move_to(new_rain_rect) - new_sun.move_to(new_sun_rect) + # new_rain.flip().move_to(new_rain_rect) + # new_sun.move_to(new_sun_rect) - new_brace_rain = Brace(new_rain_rect, UP) - new_p_rain_label = TextMobject("$P($rain$)=$").scale(text_scale) - new_p_rain_decimal = DecimalNumber(new_p_rain).scale(text_scale) - new_p_rain_decimal.next_to(new_p_rain_label) - new_p_rain_whole_label = VGroup(new_p_rain_label, new_p_rain_decimal) - new_p_rain_whole_label.next_to(new_brace_rain, UP) + # new_brace_rain = Brace(new_rain_rect, UP) + # new_p_rain_label = TextMobject("$P($rain$)=$").scale(text_scale) + # new_p_rain_decimal = DecimalNumber(new_p_rain).scale(text_scale) + # new_p_rain_decimal.next_to(new_p_rain_label) + # new_p_rain_whole_label = VGroup(new_p_rain_label, new_p_rain_decimal) + # new_p_rain_whole_label.next_to(new_brace_rain, UP) - new_brace_sun = Brace(new_sun_rect, DOWN) - new_p_sun_label = TextMobject("$P($sunshine$)=$").scale(text_scale) - new_p_sun_decimal = DecimalNumber(new_p_sun).scale(text_scale) - new_p_sun_decimal.next_to(new_p_sun_label) - new_p_sun_whole_label = VGroup(new_p_sun_label, new_p_sun_decimal) - new_p_sun_whole_label.next_to(new_brace_sun, DOWN) + # new_brace_sun = Brace(new_sun_rect, DOWN) + # new_p_sun_label = TextMobject("$P($sunshine$)=$").scale(text_scale) + # new_p_sun_decimal = DecimalNumber(new_p_sun).scale(text_scale) + # new_p_sun_decimal.next_to(new_p_sun_label) + # new_p_sun_whole_label = VGroup(new_p_sun_label, new_p_sun_decimal) + # new_p_sun_whole_label.next_to(new_brace_sun, DOWN) - def rain_update_func(alpha): - return alpha * new_p_rain + (1 - alpha) * p_rain + # def rain_update_func(alpha): + # return alpha * new_p_rain + (1 - alpha) * p_rain - def sun_update_func(alpha): - return 1 - rain_update_func(alpha) + # def sun_update_func(alpha): + # return 1 - rain_update_func(alpha) - update_p_rain = ChangingDecimal( - p_rain_decimal, rain_update_func, - tracked_mobject = p_rain_label, - run_time = run_time - ) - update_p_sun = ChangingDecimal( - p_sun_decimal, sun_update_func, - tracked_mobject = p_sun_label, - run_time = run_time - ) + # update_p_rain = ChangingDecimal( + # p_rain_decimal, rain_update_func, + # tracked_mobject = p_rain_label, + # run_time = run_time + # ) + # update_p_sun = ChangingDecimal( + # p_sun_decimal, sun_update_func, + # tracked_mobject = p_sun_label, + # run_time = run_time + # ) - self.play( - Transform(rain_rect, new_rain_rect, run_time = run_time), - Transform(sun_rect, new_sun_rect, run_time = run_time), - Transform(rain, new_rain, run_time = run_time), - Transform(sun, new_sun, run_time = run_time), - Transform(brace_rain, new_brace_rain, run_time = run_time), - Transform(brace_sun, new_brace_sun, run_time = run_time), - Transform(p_rain_label, new_p_rain_label, run_time = run_time), - Transform(p_sun_label, new_p_sun_label, run_time = run_time), - update_p_rain, - update_p_sun - ) + # self.play( + # Transform(rain_rect, new_rain_rect, run_time = run_time), + # Transform(sun_rect, new_sun_rect, run_time = run_time), + # Transform(rain, new_rain, run_time = run_time), + # Transform(sun, new_sun, run_time = run_time), + # Transform(brace_rain, new_brace_rain, run_time = run_time), + # Transform(brace_sun, new_brace_sun, run_time = run_time), + # Transform(p_rain_label, new_p_rain_label, run_time = run_time), + # Transform(p_sun_label, new_p_sun_label, run_time = run_time), + # update_p_rain, + # update_p_sun + # ) @@ -167,50 +159,6 @@ class ProbabilityDistributions(PiCreatureScene): - -# COIN FLIP - - - coin_flip_rect = BrickRow(3, height = 2, width = 10) - - for (i, brick) in enumerate(coin_flip_rect.rects): - tally = TallyStack(3 - i, i) - tally.move_to(brick) - coin_flip_rect.add(tally) - - coin_flip_rect.scale(0.8).shift(2*RIGHT) - self.play(FadeIn(coin_flip_rect)) - - counts = [1, 3, 3, 1] - braces = VGroup() - labels = VGroup() - for (rect, count) in zip(coin_flip_rect.rects, counts): - label = TexMobject("{" + str(count) + "\over 8}").scale(0.5) - brace = Brace(rect, DOWN) - label.next_to(brace, DOWN) - braces.add(brace) - labels.add(label) - - self.play( - LaggedStart(ShowCreation, braces, lag_ratio = lag_ratio, run_time = run_time), - LaggedStart(Write, labels, lag_ratio = lag_ratio, run_time = run_time) - ) - - coin_flip_rect.add(braces, labels) - - coin_flip_rect.target = coin_flip_rect.copy().scale(0.6) - coin_flip_rect.target.to_corner(UR, buff = MED_LARGE_BUFF) - coin_flip_rect.target.shift(DOWN) - - self.play( - MoveToTarget(coin_flip_rect) - ) - self.play( - FadeOut(braces), - FadeOut(labels) - ) - - # DOUBLE DICE THROW cell_size = 0.5 @@ -241,24 +189,22 @@ class ProbabilityDistributions(PiCreatureScene): for k in range(5 - i) ])) - self.play( - FadeIn(dice_unit_rect), - FadeIn(dice_table.rows) - ) + # self.play( + # FadeIn(dice_unit_rect), + # FadeIn(dice_table.rows) + # ) + self.add(dice_unit_rect, dice_table_rows) for (cell, label) in zip(dice_table.cells, dice_table.labels): cell.add(label) - self.play( - LaggedStart(FadeIn, dice_table_grouped_cells, - lag_ratio = lag_ratio, run_time = run_time) - ) + # self.play( + # LaggedStart(FadeIn, dice_table_grouped_cells, + # lag_ratio = lag_ratio, run_time = run_time) + # ) + self.add(dice_table_grouped_cells) self.wait() - self.play( - FadeOut(dice_table.rows), - FadeOut(dice_unit_rect), - ) self.play( @@ -266,3 +212,103 @@ class ProbabilityDistributions(PiCreatureScene): rate_func=there_and_back_with_pause, run_time=run_time ) + + self.play( + FadeOut(dice_table.rows), + FadeOut(dice_unit_rect), + ) + + dice_table_target = dice_table.copy() + dice_table_target.scale(0.5).to_corner(UR, buff = MED_LARGE_BUFF) + + self.play(Transform(dice_table, dice_table_target)) + + self.wait() + +# TITLE + + text = TextMobject("Probability distributions", color = YELLOW) + text.to_edge(UP) + text_rect = SurroundingRectangle(text, buff = MED_SMALL_BUFF) + + self.play( + FadeIn(text), + ShowCreation(text_rect) + ) + + self.wait() + + +# COIN FLIP + + + coin_flip_rect = BrickRow(3, height = 2, width = 10) + + tallies = VGroup() + for (i, brick) in enumerate(coin_flip_rect.rects): + tally = TallyStack(3 - i, i) + tally.move_to(brick) + tallies.add(tally) + coin_flip_rect.add(tallies) + + coin_flip_rect.scale(0.65).shift(RIGHT) + self.play(FadeIn(coin_flip_rect)) + + counts = [1, 3, 3, 1] + braces = VGroup() + labels = VGroup() + for (rect, count) in zip(coin_flip_rect.rects, counts): + label = TexMobject("{" + str(count) + "\over 8}").scale(0.5) + brace = Brace(rect, DOWN) + label.next_to(brace, DOWN) + braces.add(brace) + labels.add(label) + + self.play( + FadeIn(braces), + FadeIn(labels) + ) + + coin_flip_rect.add(braces, labels) + + coin_flip_rect.target = coin_flip_rect.copy().scale(0.6) + coin_flip_rect.target.to_corner(UR, buff = MED_LARGE_BUFF) + coin_flip_rect.target.shift(DOWN) + + + self.wait() + + outcomes = coin_flip_rect.get_outcome_rects_for_level(3, with_labels = True, + inset = True) + outcome_braces = VGroup(*[ + Brace(outcome, DOWN) for outcome in outcomes + ]) + outcome_labels = VGroup(*[ + TexMobject("{1\over 8}").scale(0.5).next_to(brace, DOWN) + for brace in outcome_braces + ]) + + self.play( + FadeOut(tallies), + FadeIn(outcomes), + FadeOut(braces), + FadeOut(labels), + FadeIn(outcome_braces), + FadeIn(outcome_labels) + ) + + self.wait() + + + + + + + + + + + + + + diff --git a/active_projects/eop/chapter1/show_proportion.py b/active_projects/eop/chapter1/show_proportion.py new file mode 100644 index 00000000..f891dae4 --- /dev/null +++ b/active_projects/eop/chapter1/show_proportion.py @@ -0,0 +1,145 @@ +from big_ol_pile_of_manim_imports import * + + +class ProbabilityRect(VMobject): + CONFIG = { + "unit_width" : 2, + "unit_height" : 2, + "alignment" : LEFT, + "color": YELLOW, + "opacity": 1.0, + "num_decimal_places": 2, + "use_percent" : False + } + + def __init__(self, p0, **kwargs): + + VMobject.__init__(self, **kwargs) + self.unit_rect = Rectangle( + width = self.unit_width, + height = self.unit_height, + stroke_color = self.color + ) + self.p = p0 + self.prob_rect = self.create_prob_rect(p0) + self.prob_label = self.create_prob_label(p0) + + self.add(self.unit_rect, self.prob_rect, self.prob_label) + + + def create_prob_rect(self, p): + + prob_width, prob_height = self.unit_width, self.unit_height + + if self.alignment in [LEFT, RIGHT]: + prob_width *= p + elif self.alignment in [UP, DOWN]: + prob_height *= p + else: + raise Exception("Aligment must be LEFT, RIGHT, UP or DOWN") + + prob_rect = Rectangle( + width = prob_width, + height = prob_height, + fill_color = self.color, + fill_opacity = self.opacity, + stroke_color = self.color + ) + + prob_rect.align_to(self.unit_rect, direction = self.alignment) + return prob_rect + + + def create_prob_label(self, p): + + if self.use_percent: + prob_label = DecimalNumber( + p * 100, + color = BLACK, + num_decimal_places = self.num_decimal_places, + unit = "\%" + ) + else: + prob_label = DecimalNumber( + p, + color = BLACK, + num_decimal_places = self.num_decimal_places, + ) + + prob_label.move_to(self.prob_rect) + + return prob_label + + +class ChangeProbability(Animation): + + def __init__(self, prob_mob, p1, **kwargs): + + if not isinstance(prob_mob, ProbabilityRect): + raise Exception("ChangeProportion's mobject must be a ProbabilityRect") + + self.p1 = p1 + self.p0 = prob_mob.p + Animation.__init__(self, prob_mob, **kwargs) + + + def update_mobject(self, alpha): + + p = (1 - alpha) * self.p0 + alpha * self.p1 + self.mobject.remove(self.mobject.prob_rect, self.mobject.prob_label) + self.mobject.prob_rect = self.mobject.create_prob_rect(p) + self.mobject.prob_label = self.mobject.create_prob_label(p) + self.mobject.add(self.mobject.prob_rect, self.mobject.prob_label) + + + def clean_up(self, surrounding_scene=None): + self.mobject.p = self.p1 + super(ChangeProbability, self).clean_up(surrounding_scene = surrounding_scene) + + + + + +class ShowProbAsProportion(Scene): + + def construct(self): + + p0 = 0.3 + p1 = 1 + p2 = 0.18 + p3 = 0.64 + + prob_mob = ProbabilityRect(p0, + unit_width = 4, + unit_height = 2, + use_percent = False, + num_decimal_places = 2 + ) + + self.add(prob_mob) + self.wait() + self.play( + ChangeProbability(prob_mob, p1, + run_time = 3) + ) + self.wait(0.5) + self.play( + ChangeProbability(prob_mob, p2, + run_time = 3) + ) + self.wait(0.5) + self.play( + ChangeProbability(prob_mob, p3, + run_time = 3) + ) + + + + + + + + + + +