finished area model scene 2 (normal dist)

This commit is contained in:
Ben Hambrecht 2018-04-19 23:03:29 +02:00
parent 83bebfb109
commit ad6b1fee20

View file

@ -740,95 +740,115 @@ class IllustrateAreaModel1(Scene):
class IllustrateAreaModel2(GraphScene):
CONFIG = {
"x_min" : -3.5,
"x_max" : 3.5,
"y_min" : -0,
"y_max" : 0.6,
"graph_origin": 3*DOWN,
"num_rects": 20,
"x_min" : -3.0,
"x_max" : 3.0,
"y_min" : 0,
"y_max" : 1.0,
"num_rects": 400,
"y_axis_label" : "",
"x_axis_label" : "",
"variable_point_label" : "x",
"y_axis_height" : 4,
"graph_origin": 2.5 * DOWN + 3 * LEFT,
"variable_point_label" : "a",
"graph_origin": 2.5 * DOWN + 4 * RIGHT,
"x_axis_width": 5,
"y_axis_height": 3
"y_axis_height": 5
}
def construct(self):
x_max_1 = 0
x_min_1 = -x_max_1
x_max_2 = 3.5
x_min_2 = -x_max_2
# integral bounds
x_min_1 = -0.0001
x_max_1 = 0.0001
x_min_2 = self.x_min
x_max_2 = self.x_max
self.setup_axes()
graph = self.get_graph(lambda x: np.exp(-x**2) / ((0.5 * TAU) ** 0.5))
self.remove(self.x_axis, self.y_axis)
graph = self.get_graph(lambda x: np.exp(-x**2) * 2.0 / TAU ** 0.5)
area = self.area = self.get_area(graph, x_min_1, x_max_1)
self.add(graph)
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, RIGHT, buff = -1)
self.add(cdf_formula)
pdf_formula = TexMobject("p(x) = {1\over \sigma\sqrt{2\pi}}e^{-{1\over 2}({x\over\sigma})^2}")
pdf_formula.set_color(graph.color)
cdf_formula = TexMobject("P(|X| < ", "a", ") = \int", "_{-a}", "^a", "p(x) dx")
cdf_formula.set_color_by_tex("a", YELLOW)
cdf_formula.next_to(graph, LEFT, buff = 2)
pdf_formula.next_to(cdf_formula, UP)
formulas = VGroup(pdf_formula, cdf_formula)
self.play(Write(pdf_formula))
self.play(Write(cdf_formula))
self.wait()
self.play(ShowCreation(self.x_axis))
self.play(ShowCreation(graph))
self.play(FadeIn(area))
self.v_graph = graph
self.add_T_label(x_min_1, color = YELLOW, animated = False)
self.add_T_label(
x_min_1,
label = "-a",
side = LEFT,
color = YELLOW,
animated = False
)
self.add_T_label(
x_max_1,
label = "a",
side = RIGHT,
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(
return 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))
def integral_update_func_percent(t):
return 100 * integral_update_func(t)
equals_sign = TexMobject("=").next_to(cdf_formula, buff = MED_LARGE_BUFF)
cdf_value = DecimalNumber(0, color = graph.color, num_decimal_points = 3)
cdf_value.next_to(equals_sign)
self.play(
FadeIn(equals_sign),
FadeIn(cdf_value)
)
self.add_foreground_mobject(cdf_value)
cdf_percentage = DecimalNumber(0, unit = "\%")
cdf_percentage.move_to(self.coords_to_point(0,0.2))
self.add_foreground_mobject(cdf_percentage)
self.add(ContinualChangingDecimal(
decimal_number_mobject = cdf_value,
number_update_func = integral_update_func,
num_decimal_points = 3
))
self.add(ContinualChangingDecimal(
decimal_number_mobject = cdf_percentage,
number_update_func = integral_update_func_percent,
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
# )
anim = self.get_animation_integral_bounds_change(
graph, x_min_2, x_max_2,
run_time = 3)
self.play(
anim
)
rect = SurroundingRectangle(formulas, buff = 0.5 * MED_LARGE_BUFF)
self.play(ShowCreation(rect))