Exemple #1
0
    def show_probability(self):
        title = TexMobject(
            "P(\\text{triangle contains the center})",
            "=", "1/4"
        )
        title.to_edge(UP, buff = MED_SMALL_BUFF)
        title.highlight_by_tex("1/4", BLUE)
        four = title[-1][-1]
        four_circle = Circle(color = YELLOW)
        four_circle.replace(four, dim_to_match = 1)
        four_circle.scale_in_place(1.2)

        self.n_in = 0
        self.n_out = 0
        frac = TexMobject(
            "{0", "\\over", "\\quad 0", "+", "0 \\quad}", "="
        )
        placeholders = frac.get_parts_by_tex("0")
        positions = [ORIGIN, RIGHT, LEFT]
        frac.next_to(self.circle, RIGHT, 1.5*LARGE_BUFF)

        def place_random_triangles(n, dither_time):
            for x in range(n):
                self.change_point_mobs_randomly(run_time = 0)
                contain_center = self.points_contain_center(
                    [pm.get_center() for pm in self.point_mobs]
                )
                if contain_center:
                    self.n_in += 1
                else:
                    self.n_out += 1
                nums = map(Integer, [self.n_in, self.n_in, self.n_out])
                VGroup(*nums[:2]).highlight(self.positive_triangle_color)
                VGroup(*nums[2:]).highlight(self.negative_triangle_color)
                for num, placeholder, position in zip(nums, placeholders, positions):
                    num.move_to(placeholder, position)
                decimal = DecimalNumber(float(self.n_in)/(self.n_in + self.n_out))
                decimal.next_to(frac, RIGHT, SMALL_BUFF)

                self.add(decimal, *nums)
                self.dither(dither_time)
                self.remove(decimal, *nums)
            return VGroup(decimal, *nums)


        self.play(Write(title))
        self.add(frac)
        self.remove(*placeholders)
        place_random_triangles(10, 0.25)
        nums = place_random_triangles(self.n_random_trials, 0.05)
        self.add(nums)
        self.dither()
        self.play(*map(FadeOut, [frac, nums, title]))
    def construct(self):
        scale_factor = 0.7
        sick = "\\text{sick}"
        positive = "\\text{positive}"
        formula = TexMobject("P(", sick, "\\,|\\,", positive, ")", "=",
                             "{\\quad P(", sick, "\\text{ and }", positive,
                             ") \\quad", "\\over", "P(", positive, ")}", "=",
                             "{1/1{,}000", "\\over", "1/1{,}000", "+",
                             "(999/1{,}000)(0.01)}")
        formula.scale(scale_factor)
        formula.next_to(self.pi_creatures, UP, LARGE_BUFF)
        formula.shift_onto_screen(buff=MED_LARGE_BUFF)
        equals_group = formula.get_parts_by_tex("=")
        equals_indices = [
            formula.index_of_part(equals) for equals in equals_group
        ]

        lhs = VGroup(*formula[:equals_indices[0]])
        initial_formula = VGroup(*formula[:equals_indices[1]])
        initial_formula.save_state()
        initial_formula.shift(3 * RIGHT)

        over = formula.get_part_by_tex("\\over")
        num_start_index = equals_indices[0] + 1
        num_end_index = formula.index_of_part(over)
        numerator = VGroup(*formula[num_start_index:num_end_index])
        numerator_rect = SurroundingRectangle(numerator)

        alt_numerator = TexMobject("P(", sick, ")", "P(", positive, "\\,|\\,",
                                   sick, ")")
        alt_numerator.scale(scale_factor)
        alt_numerator.move_to(numerator)

        number_fraction = VGroup(*formula[equals_indices[-1]:])

        rhs = TexMobject("\\approx 0.09")
        rhs.scale(scale_factor)
        rhs.move_to(equals_group[-1], LEFT)
        rhs.highlight(YELLOW)

        for mob in formula, alt_numerator:
            mob.highlight_by_tex(sick, SICKLY_GREEN)
            mob.highlight_by_tex(positive, YELLOW)

        #Ask question
        self.student_says("What does the \\\\ formula look like here?")
        self.play(self.teacher.change, "happy")
        self.dither()
        self.play(
            Write(lhs),
            RemovePiCreatureBubble(
                self.students[1],
                target_mode="pondering",
            ),
            self.teacher.change,
            "raise_right_hand",
            self.students[0].change,
            "pondering",
            self.students[2].change,
            "pondering",
        )
        self.dither()

        #Show initial formula
        lhs_copy = lhs.copy()
        self.play(
            LaggedStart(FadeIn, initial_formula, lag_ratio=0.7),
            Animation(lhs_copy, remover=True),
        )
        self.dither(2)
        self.play(ShowCreation(numerator_rect))
        self.play(FadeOut(numerator_rect))
        self.dither()
        self.play(Transform(numerator, alt_numerator))
        initial_formula.add(*numerator)
        formula.add(*numerator)
        self.dither(3)

        #Show number_fraction
        self.play(initial_formula.move_to, initial_formula.saved_state,
                  FadeIn(VGroup(*number_fraction[:3])))
        self.dither(2)
        self.play(
            LaggedStart(FadeIn,
                        VGroup(*number_fraction[3:]),
                        run_time=3,
                        lag_ratio=0.7))
        self.dither(2)

        #Show rhs
        self.play(formula.shift, UP)
        self.play(Write(rhs))
        self.change_student_modes(*["happy"] * 3)
        self.look_at(rhs)
        self.dither(2)
Exemple #3
0
    def construct(self):
        clunky_deriv = TexMobject(
            "{d", "\\big(", "{df", "\\over", "dx}", "\\big)",
            "\\over", "dx }"
        )
        over_index = clunky_deriv.index_of_part(
            clunky_deriv.get_parts_by_tex("\\over")[1]
        )
        numerator = VGroup(*clunky_deriv[:over_index])
        denominator = VGroup(*clunky_deriv[over_index+1:])


        rp = clunky_deriv.get_part_by_tex("(")
        lp = clunky_deriv.get_part_by_tex(")")
        dfs, overs, dxs = map(clunky_deriv.get_parts_by_tex, [
            "df", "over", "dx"
        ])
        df_over_dx = VGroup(dfs[0], overs[0], dxs[0])
        d = clunky_deriv.get_part_by_tex("d")
        d_over_dx = VGroup(d, overs[1], dxs[1])

        d2f_over_dx2 = TexMobject("{d^2 f", "\\over", "dx", "^2}")
        d2f_over_dx2.highlight_by_tex("dx", YELLOW)

        for mob in clunky_deriv, d2f_over_dx2:
            mob.next_to(self.teacher, UP+LEFT)

        for mob in numerator, denominator:
            circle = Circle(color = YELLOW)
            circle.replace(mob, stretch = True)
            circle.scale_in_place(1.3)
            mob.circle = circle
        dx_to_zero = TexMobject("dx \\to 0")
        dx_to_zero.highlight(YELLOW)
        dx_to_zero.next_to(clunky_deriv, UP+LEFT)

        self.student_says(
            "What's that notation?",
            target_mode = "raise_left_hand"
        )
        self.change_student_modes("confused", "raise_left_hand", "confused")
        self.play(
            FadeIn(
                clunky_deriv,
                run_time = 2,
                submobject_mode = "lagged_start"
            ),
            RemovePiCreatureBubble(self.get_students()[1]),
            self.teacher.change_mode, "raise_right_hand"
        )
        self.dither()
        self.play(ShowCreation(numerator.circle))
        self.dither()
        self.play(ReplacementTransform(
            numerator.circle,
            denominator.circle,
        ))
        self.dither()
        self.play(
            FadeOut(denominator.circle),
            Write(dx_to_zero),
            dxs.highlight, YELLOW
        )
        self.dither()
        self.play(
            FadeOut(dx_to_zero),
            *[ApplyMethod(pi.change, "plain") for pi in self.get_pi_creatures()]
        )
        self.play(
            df_over_dx.scale, dxs[1].get_height()/dxs[0].get_height(),
            df_over_dx.move_to, d_over_dx, RIGHT,
            FadeOut(VGroup(lp, rp)),
            d_over_dx.shift, 0.8*LEFT + 0.05*UP,
        )
        self.dither()
        self.play(*[
            ReplacementTransform(
                group,
                VGroup(d2f_over_dx2.get_part_by_tex(tex))
            )
            for group, tex in [
                (VGroup(d, dfs[0]), "d^2"),
                (overs, "over"),
                (dxs, "dx"),
                (VGroup(dxs[1].copy()), "^2}"),
            ]
        ])
        self.dither(2)
        self.student_says(
            "How does one... \\\\ read that?",
            student_index = 0,
        )
        self.play(self.teacher.change, "happy")
        self.dither(2)
Exemple #4
0
    def construct(self):
        clunky_deriv = TexMobject(
            "{d", "\\big(", "{df", "\\over", "dx}", "\\big)",
            "\\over", "dx }"
        )
        over_index = clunky_deriv.index_of_part(
            clunky_deriv.get_parts_by_tex("\\over")[1]
        )
        numerator = VGroup(*clunky_deriv[:over_index])
        denominator = VGroup(*clunky_deriv[over_index+1:])


        rp = clunky_deriv.get_part_by_tex("(")
        lp = clunky_deriv.get_part_by_tex(")")
        dfs, overs, dxs = map(clunky_deriv.get_parts_by_tex, [
            "df", "over", "dx"
        ])
        df_over_dx = VGroup(dfs[0], overs[0], dxs[0])
        d = clunky_deriv.get_part_by_tex("d")
        d_over_dx = VGroup(d, overs[1], dxs[1])

        d2f_over_dx2 = TexMobject("{d^2 f", "\\over", "dx", "^2}")
        d2f_over_dx2.highlight_by_tex("dx", YELLOW)

        for mob in clunky_deriv, d2f_over_dx2:
            mob.next_to(self.teacher, UP+LEFT)

        for mob in numerator, denominator:
            circle = Circle(color = YELLOW)
            circle.replace(mob, stretch = True)
            circle.scale_in_place(1.3)
            mob.circle = circle
        dx_to_zero = TexMobject("dx \\to 0")
        dx_to_zero.highlight(YELLOW)
        dx_to_zero.next_to(clunky_deriv, UP+LEFT)

        self.student_says(
            "What's that notation?",
            target_mode = "raise_left_hand"
        )
        self.change_student_modes("confused", "raise_left_hand", "confused")
        self.play(
            FadeIn(
                clunky_deriv,
                run_time = 2,
                submobject_mode = "lagged_start"
            ),
            RemovePiCreatureBubble(self.get_students()[1]),
            self.teacher.change_mode, "raise_right_hand"
        )
        self.wait()
        self.play(ShowCreation(numerator.circle))
        self.wait()
        self.play(ReplacementTransform(
            numerator.circle,
            denominator.circle,
        ))
        self.wait()
        self.play(
            FadeOut(denominator.circle),
            Write(dx_to_zero),
            dxs.highlight, YELLOW
        )
        self.wait()
        self.play(
            FadeOut(dx_to_zero),
            *[ApplyMethod(pi.change, "plain") for pi in self.get_pi_creatures()]
        )
        self.play(
            df_over_dx.scale, dxs[1].get_height()/dxs[0].get_height(),
            df_over_dx.move_to, d_over_dx, RIGHT,
            FadeOut(VGroup(lp, rp)),
            d_over_dx.shift, 0.8*LEFT + 0.05*UP,
        )
        self.wait()
        self.play(*[
            ReplacementTransform(
                group,
                VGroup(d2f_over_dx2.get_part_by_tex(tex))
            )
            for group, tex in [
                (VGroup(d, dfs[0]), "d^2"),
                (overs, "over"),
                (dxs, "dx"),
                (VGroup(dxs[1].copy()), "^2}"),
            ]
        ])
        self.wait(2)
        self.student_says(
            "How does one... \\\\ read that?",
            student_index = 0,
        )
        self.play(self.teacher.change, "happy")
        self.wait(2)
Exemple #5
0
    def reminder_of_weights_and_bias(self):
        neuron = self.neuron
        layer0 = self.network_mob.layers[0]
        active_layer0 = self.network_mob.get_active_layer(
            0, np.random.random(len(layer0.neurons)))
        prev_neurons = layer0.neurons

        weights = 4 * (np.random.random(len(neuron.edges_in)) - 0.5)
        weighted_edges = VGroup(*[
            edge.copy().set_stroke(color=GREEN if w > 0 else RED, width=abs(w))
            for w, edge in zip(weights, neuron.edges_in)
        ])

        formula = TexMobject("=", "\\sigma(", "w_1", "a_1", "+", "w_2", "a_2",
                             "+", "\\cdots", "+", "w_n", "a_n", "+", "b", ")")
        w_labels = formula.get_parts_by_tex("w_")
        a_labels = formula.get_parts_by_tex("a_")
        b = formula.get_part_by_tex("b")
        sigma = VGroup(
            formula.get_part_by_tex("\\sigma"),
            formula.get_part_by_tex(")"),
        )
        symbols = VGroup(
            *[formula.get_parts_by_tex(tex) for tex in "=", "+", "dots"])

        w_labels.highlight(GREEN)
        b.highlight(BLUE)
        sigma.highlight(YELLOW)
        # formula.to_edge(UP)
        formula.next_to(neuron, RIGHT)

        weights_word = TextMobject("Weights")
        weights_word.next_to(neuron.edges_in, RIGHT, aligned_edge=UP)
        weights_word.highlight(GREEN)
        weights_arrow = Arrow(weights_word.get_bottom(),
                              neuron.edges_in[0].get_center(),
                              color=GREEN)

        alt_weights_arrows = VGroup(*[
            Arrow(weights_word.get_bottom(), w_label.get_top(), color=GREEN)
            for w_label in w_labels
        ])

        bias_word = TextMobject("Bias")
        bias_arrow = Vector(DOWN, color=BLUE)
        bias_arrow.next_to(b, UP, SMALL_BUFF)
        bias_word.next_to(bias_arrow, UP, SMALL_BUFF)
        bias_word.highlight(BLUE)

        self.revert_to_original_skipping_status()
        self.play(Transform(layer0, active_layer0),
                  FadeIn(a_labels),
                  FadeIn(symbols),
                  run_time=2,
                  submobject_mode="lagged_start")
        self.play(
            Write(weights_word),
            GrowArrow(weights_arrow),
            Transform(neuron.edges_in, weighted_edges),
            run_time=1,
        )
        self.dither()
        self.play(
            ReplacementTransform(
                weighted_edges.copy(),
                w_labels,
            ), ReplacementTransform(VGroup(weights_arrow), alt_weights_arrows))
        self.dither()
        self.play(Write(b),
                  Write(bias_word),
                  GrowArrow(bias_arrow),
                  run_time=1)
        self.play(Write(sigma))
        self.dither(2)