Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

improved area model examples 1 and 2

Ben Hambrechtdca4400b09a7
diff --git a/active_projects/eop/chapter1.py b/active_projects/eop/chapter1.py
index 54439e7a0..a18c0c7bb 100644
--- a/active_projects/eop/chapter1.py
+++ b/active_projects/eop/chapter1.py
@@ -1,6 +1,8 @@
from big_ol_pile_of_manim_imports import *
from old_projects.eoc.chapter8 import *
+import scipy.special
+
COIN_RADIUS = 0.3
COIN_THICKNESS = 0.4 * COIN_RADIUS
COIN_FORESHORTENING = 0.3
@@ -665,19 +667,35 @@ class IllustrateAreaModel1(Scene):
label_B_knowing_A = label_B
self.play(FadeOut(label_B_copy))
+ self.remove(indep_formula.get_part_by_tex("P(B)"))
label_B_knowing_A_copy = label_B_knowing_A.copy()
self.add(label_B_knowing_A_copy)
self.play(
- label_B_knowing_A_copy.next_to, indep_formula[-2], RIGHT
+ label_B_knowing_A_copy.next_to, indep_formula.get_part_by_tex("\cdot"), RIGHT,
)
+ # solve formula for P(B|A)
-
-
+ rearranged_formula = TexMobject(["P(B\mid A)", "=", "{P(A\\text{ and }B) \over P(A)}"])
+ rearranged_formula.move_to(indep_formula)
self.wait()
+ self.play(
+ # in some places get_part_by_tex does not find the correct part
+ # so I picked out fitting indices
+ label_B_knowing_A_copy.move_to, rearranged_formula.get_part_by_tex("P(B\mid A)"),
+ label_A_copy.move_to, rearranged_formula[-1][10],
+ label_A_and_B_copy.move_to, rearranged_formula[-1][3],
+ indep_formula.get_part_by_tex("=").move_to, rearranged_formula.get_part_by_tex("="),
+ Transform(indep_formula.get_part_by_tex("\cdot"), rearranged_formula[-1][8]),
+ )
+
+
+# # # # # # # # # # # # # # # # #
+# Old version with SampleSpace #
+# # # # # # # # # # # # # # # # #
# def show_independent_events(self):
# sample_space = SampleSpace(
@@ -747,63 +765,108 @@ class IllustrateAreaModel1(Scene):
- def color_label(self, label):
- label.set_color_by_tex("B", RED)
- label.set_color_by_tex("I", GREEN)
+ # def color_label(self, label):
+ # label.set_color_by_tex("B", RED)
+ # label.set_color_by_tex("I", GREEN)
-
-class IllustrateAreaModel2(AreaIsDerivative):
-
+class IllustrateAreaModel2(GraphScene):
CONFIG = {
- "y_max" : 4,
- "y_min" : -4,
- "num_iterations" : 7,
+ "x_min" : -5,
+ "x_max" : 5,
+ "y_min" : -0,
+ "y_max" : 0.6,
+ "graph_origin": 3*DOWN,
+ "num_rects": 20,
"y_axis_label" : "",
- "num_rects" : 50,
- "dT" : 0.25,
- "variable_point_label" : "T",
- "area_opacity" : 0.8,
+ "x_axis_label" : "",
+ "variable_point_label" : "x",
+ "y_axis_height" : 4
}
+
def construct(self):
+ x_max_1 = 0
+ x_min_1 = -x_max_1
+
+ x_max_2 = 5
+ x_min_2 = -x_max_2
+
+
self.setup_axes()
- self.introduce_variable_area()
- return
+ graph = self.get_graph(lambda x: np.exp(-x**2) / ((0.5 * TAU) ** 0.5))
+ self.add(graph)
- graph, label = self.get_v_graph_and_label()
- rect_list = self.get_riemann_rectangles_list(
- graph, self.num_iterations
- )
- VGroup(*rect_list).set_fill(opacity = 0.8)
- rects = rect_list[0]
-
- self.play(ShowCreation(graph))
- self.play(Write(rects))
- for new_rects in rect_list[1:]:
- rects.align_submobjects(new_rects)
- for every_other_rect in rects[::2]:
- every_other_rect.set_fill(opacity = 0)
- self.play(Transform(
- rects, new_rects,
- run_time = 2,
- submobject_mode = "lagged_start"
- ))
- self.wait()
+ cdf_formula = TexMobject("P(|X-\mu| < x) = \int_{-x}^x {\exp(-{1\over 2}({t\over \sigma})^2) \over \sigma\sqrt{2\pi}} dt")
+ cdf_formula.set_color_by_tex("x", YELLOW)
+ cdf_formula.next_to(graph, UP, buff = 1)
+ self.add(cdf_formula)
+
+
+ self.v_graph = graph
+ self.add_T_label(x_min_1, color = YELLOW, animated = False)
+ self.remove(self.T_label_group, self.right_v_line)
+ #self.T_label_group[0].set_fill(opacity = 0).set_stroke(width = 0)
+ #self.T_label_group[1].set_fill(opacity = 0).set_stroke(width = 0)
+ #self.right_v_line.set_fill(opacity = 0).set_stroke(width = 0)
+
+ #self.add(self.T_label_group)
+ area = self.area = self.get_area(graph, x_min_1, x_max_1)
+
+ right_bound_label = TexMobject("x", color = YELLOW)
+ right_bound_label.next_to(self.coords_to_point(0,0), DOWN)
+ right_bound_label.target = right_bound_label.copy().next_to(self.coords_to_point(self.x_max,0), DOWN)
+ right_bound_label.set_fill(opacity = 0).set_stroke(width = 0)
+
+ left_bound_label = TexMobject("-x", color = YELLOW)
+ left_bound_label.next_to(self.coords_to_point(0,0), DOWN)
+ left_bound_label.target = right_bound_label.copy().next_to(self.coords_to_point(self.x_min,0), DOWN)
+ left_bound_label.set_fill(opacity = 0).set_stroke(width = 0)
+
+ #integral = self.get_riemann_rectangles(
+ #graph,x_min = self.x_min, x_max = x_max_1)
+ self.add(area)
+
+ def integral_update_func(t):
+ return 100 * scipy.special.erf(
+ self.point_to_coords(self.right_v_line.get_center())[0]
+ )
+
+ cdf_value = DecimalNumber(0, unit = "\%")
+ cdf_value.move_to(self.coords_to_point(0,0.2))
+ self.add_foreground_mobject(cdf_value)
+
+ self.add(ContinualChangingDecimal(
+ decimal_number_mobject = cdf_value,
+ number_update_func = integral_update_func,
+ num_decimal_points = 1
+ ))
+
+ anim = self.get_animation_integral_bounds_change(
+ graph, x_min_2, x_max_2, run_time = 3)
-# self.play(FadeOut(self.x_axis.numbers))
- self.add_T_label(6)
- self.change_area_bounds(
- new_t_max = 4,
- rate_func = there_and_back,
- run_time = 2
+ # changing_cdf_value = ChangingDecimal(
+ # decimal_number_mobject = cdf_value,
+ # number_update_func = integral_update_func,
+ # num_decimal_points = 1
+ # )
+
+ self.play(
+ anim
)
- def func(self, x):
- return np.exp(-x**2/2)
+
+
+
+
+
+
+
+
+
class AreaSplitting(Scene):
@@ -936,36 +999,6 @@ class AreaSplitting(Scene):
-class Test(GraphScene):
- CONFIG = {
- "x_min" : -3,
- "x_max" : 3,
- "y_min" : -0.2,
- "y_max" : 2.0,
- "graph_origin": 3*DOWN,
- "num_rects": 100
- }
-
- def construct(self):
-
- x_max_1 = -2.99
- x_max_2 = 3.0
-
- self.setup_axes()
- graph = self.get_graph(lambda x: np.exp(-x**2/2))
- self.add(graph)
- area = self.area = self.get_area(graph,self.x_min, x_max_1)
-
- integral = self.get_riemann_rectangles(
- graph,x_min = self.x_min, x_max = x_max_1)
- self.add(integral)
-
- anim = self.get_animation_integral_bounds_change(
- graph, self.x_min, x_max_2, run_time = 3)
-
- self.play(anim)
-
-