improved area model examples 1 and 2

This commit is contained in:
Ben Hambrecht 2018-04-12 15:17:47 +02:00
parent 8e7f33c65a
commit dca4400b09

View file

@ -1,6 +1,8 @@
from big_ol_pile_of_manim_imports import * from big_ol_pile_of_manim_imports import *
from old_projects.eoc.chapter8 import * from old_projects.eoc.chapter8 import *
import scipy.special
COIN_RADIUS = 0.3 COIN_RADIUS = 0.3
COIN_THICKNESS = 0.4 * COIN_RADIUS COIN_THICKNESS = 0.4 * COIN_RADIUS
COIN_FORESHORTENING = 0.3 COIN_FORESHORTENING = 0.3
@ -665,19 +667,35 @@ class IllustrateAreaModel1(Scene):
label_B_knowing_A = label_B label_B_knowing_A = label_B
self.play(FadeOut(label_B_copy)) 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() label_B_knowing_A_copy = label_B_knowing_A.copy()
self.add(label_B_knowing_A_copy) self.add(label_B_knowing_A_copy)
self.play( 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.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): # def show_independent_events(self):
# sample_space = SampleSpace( # sample_space = SampleSpace(
@ -747,63 +765,108 @@ class IllustrateAreaModel1(Scene):
def color_label(self, label): # def color_label(self, label):
label.set_color_by_tex("B", RED) # label.set_color_by_tex("B", RED)
label.set_color_by_tex("I", GREEN) # label.set_color_by_tex("I", GREEN)
class IllustrateAreaModel2(GraphScene):
class IllustrateAreaModel2(AreaIsDerivative):
CONFIG = { CONFIG = {
"y_max" : 4, "x_min" : -5,
"y_min" : -4, "x_max" : 5,
"num_iterations" : 7, "y_min" : -0,
"y_max" : 0.6,
"graph_origin": 3*DOWN,
"num_rects": 20,
"y_axis_label" : "", "y_axis_label" : "",
"num_rects" : 50, "x_axis_label" : "",
"dT" : 0.25, "variable_point_label" : "x",
"variable_point_label" : "T", "y_axis_height" : 4
"area_opacity" : 0.8,
} }
def construct(self): 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.setup_axes()
self.introduce_variable_area() graph = self.get_graph(lambda x: np.exp(-x**2) / ((0.5 * TAU) ** 0.5))
return self.add(graph)
graph, label = self.get_v_graph_and_label()
rect_list = self.get_riemann_rectangles_list( cdf_formula = TexMobject("P(|X-\mu| < x) = \int_{-x}^x {\exp(-{1\over 2}({t\over \sigma})^2) \over \sigma\sqrt{2\pi}} dt")
graph, self.num_iterations cdf_formula.set_color_by_tex("x", YELLOW)
) cdf_formula.next_to(graph, UP, buff = 1)
VGroup(*rect_list).set_fill(opacity = 0.8) self.add(cdf_formula)
rects = rect_list[0]
self.play(ShowCreation(graph)) self.v_graph = graph
self.play(Write(rects)) self.add_T_label(x_min_1, color = YELLOW, animated = False)
for new_rects in rect_list[1:]: self.remove(self.T_label_group, self.right_v_line)
rects.align_submobjects(new_rects) #self.T_label_group[0].set_fill(opacity = 0).set_stroke(width = 0)
for every_other_rect in rects[::2]: #self.T_label_group[1].set_fill(opacity = 0).set_stroke(width = 0)
every_other_rect.set_fill(opacity = 0) #self.right_v_line.set_fill(opacity = 0).set_stroke(width = 0)
self.play(Transform(
rects, new_rects,
run_time = 2,
submobject_mode = "lagged_start"
))
self.wait()
# self.play(FadeOut(self.x_axis.numbers)) #self.add(self.T_label_group)
self.add_T_label(6) area = self.area = self.get_area(graph, x_min_1, x_max_1)
self.change_area_bounds(
new_t_max = 4, right_bound_label = TexMobject("x", color = YELLOW)
rate_func = there_and_back, right_bound_label.next_to(self.coords_to_point(0,0), DOWN)
run_time = 2 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)
# 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): 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)