# Was sind eigentlich stetige Zufallsvariablen? # # von # # Jonas Alexander Lache # Daniel Meißner # # Dieses Werk ist lizenziert unter einer Creative Commons # Namensnennung-Weitergabe unter gleichen Bedingungen 4.0 International # Lizenz. Um eine Kopie der Lizenz zu erhalten, besuchen Sie # http://creativecommons.org/licenses/by-sa/4.0/. # # SPDX-License-Identifier: CC-BY-SA-4.0 from manim import * import numpy as np import matplotlib.pyplot as plt ORCA_BLUE = "#002b44" ORCA_RED = "#e61300" ORCA_GREEN = "#4cb011" ORCA_GREY = "#f5f5f5" ORCA_WHITE = "#ffffff" config.background_color = ORCA_WHITE def histogram_to_file(data): filename = "histogram.svg" fig, ax = plt.subplots() ax.hist(data, color=ORCA_BLUE, density=True, bins="sturges") x = np.linspace(45, 48) ax.set_xlabel("Gewicht in Gramm") fig.savefig(filename, bbox_inches="tight", pad_inches=0.05) return filename def scene_heading(title): heading = Tex( r"\textsf{{\bfseries {}}}".format(title), color=ORCA_BLUE, font_size=56, tex_environment="flushleft" ) heading.to_edge(UL) return heading class Definition(Scene): def construct(self): heading = scene_heading("Stetige Zufallsvariablen") self.play(Create(heading)) self.wait(5.5) formulas = MathTex( "P(X = 2) = 0", r"P(X \in [2 - \epsilon, 2 + \epsilon])", r"= \int_{2-\epsilon}^{2+\epsilon} f(x) \:dx", color=ORCA_BLUE ) formulas.arrange(DOWN) self.play(Create(formulas[0]), run_time=1) self.wait(6) formulas[1].next_to(formulas[0], DOWN) self.play(Create(formulas[1]), run_time=1) self.wait(4) formulas[2].next_to(formulas[1], RIGHT) self.play(Create(formulas[2])) self.wait(3) # Dichtefunktion dichte = Tex( r"{\sffamily \(f\) nennen wir \textbf{Dichtefunktion}}", color=ORCA_BLUE ) dichte.next_to(formulas[1], 3*DOWN) self.play(Create(dichte), run_time=1) self.wait(0.5) self.play(FadeOut(formulas, dichte)) definition = Tex( r"{\sffamily Eine Zufallsvariable \(X\) hat eine stetige Verteilung,}\\", r"{\sffamily falls eine messbare, nichtnegative Funktion \(f\) existiert,}\\", r"{\sffamily sodass\\}", color=ORCA_BLUE, tex_environment="flushleft" ) interval_probability = MathTex( r"{{ P(a \le X \le b) }} = {{ \int_a^b f(x) \:dx }}", color = ORCA_BLUE ) interval_probability.next_to(definition, DOWN) self.play(Create(definition[:2]), run_time=5) self.wait(1.5) self.play(Create(definition[2]), run_time=0.5) # self.wait() self.play(Create(interval_probability[0]), run_time=2) self.wait(2) self.play(Create(interval_probability[1:]), run_time=1) self.wait(8) self.play(FadeOut(definition, interval_probability, heading)) self.wait(0.5) class Modelling(Scene): def construct(self): heading = scene_heading("Verteilung der Packungsgewichte") self.play(Create(heading)) subheading = Tex(r"\textsf{Modellbildung}", color=ORCA_BLUE) subheading.next_to(heading, DOWN).align_to(heading, LEFT) self.play(Create(subheading)) self.wait(5.5) data = np.array([47.84, 45.09, 46.46, 45.66, 46.38, 45.73, 45.80, 46.47, 46.17, 46.65, 45.99, 46.32, 46.10, 46.47, 48.01, 46.87, 46.56, 46.24, 46.82, 46.32, 46.16, 46.46, 46.74, 47.24]) histogram = SVGMobject(histogram_to_file(data), height=5) (histogram.next_to(subheading, DOWN, buff=0.3) .shift(-histogram.get_center() * np.array([1, 0, 0]))) self.play(Create(histogram)) self.wait(1) self.play(histogram.animate.scale_to_fit_width(5)) self.play(histogram.animate.to_edge(RIGHT, buff=2)) model = Tex( r"\textsf{Modellannahme}\\", r"\(X \sim \mathcal{N}(\mu, \sigma^2)\)", color=ORCA_BLUE, tex_environment="flushleft" ) model.to_edge(LEFT, buff=2) model.align_to(histogram, UP) self.play(Create(model)) self.wait(11.5) estimated_parameters = Tex( r"\textsf{Geschätzte Parameter}\\", r"""\(\mu = {0:.1f}\)\\ \(\sigma = {1:.2f}\)""".format(data.mean(), data.std()), color=ORCA_BLUE, tex_environment="flushleft" ) estimated_parameters.next_to(model, DOWN, buff=1) estimated_parameters.align_to(model, LEFT) self.play(Create(estimated_parameters)) self.wait(2) self.play(FadeOut(histogram)) self.play(estimated_parameters.animate.to_edge(RIGHT, buff=2)) self.play(estimated_parameters.animate.align_to(model, UP)) model_probability = MathTex( r"P(44 \le X \le 45) =", r"""\int_{44}^{45} \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x - \mu)^2}{2\sigma^2}} dx""", color=ORCA_BLUE ) model_probability.next_to(estimated_parameters, DOWN, buff=1) model_probability.align_to(model, LEFT) self.play(Create(model_probability[0], run_time=3)) self.wait(3.5) self.play(Create(model_probability[1], run_time=2)) self.wait(9.5) self.play(FadeOut(subheading,model,estimated_parameters,model_probability)) self.wait(0.5) class DensityProperties(Scene): def construct(self): heading = scene_heading("Eigenschaften der Dichtefunktion") self.play(Create(heading)) text = Tex( r"""\sffamily {Eine Dichtefunktion \(f\colon\mathbb{R}\to \mathbb{R}\) erfüllt zwei Eigenschaften:}""", color=ORCA_BLUE, tex_environment="flushleft" ) formulas = MathTex( r"f(x) \ge 0", r"\int_{-\infty}^\infty f(x) \:dx = 1", color=ORCA_BLUE) formulas.center() formulas.arrange(DOWN) formulas[1].next_to(formulas[0], DOWN) text.next_to(formulas, UP, buff=1) text.align_to(heading, LEFT) self.play(Create(text)) self.wait(2) self.play(Create(formulas[0]), run_time=1) self.wait(1.5) self.play(Create(formulas[1]), run_time=1) self.wait(1.2) self.play(FadeOut(heading,text,formulas)) class DistributionFunction(Scene): def construct(self): heading = scene_heading("Verteilung der Packungsgewichte") self.add(heading) self.wait(7) subheading = Tex(r"\textsf{Die Verteilungsfunktion}", color=ORCA_BLUE) subheading.next_to(heading, DOWN).align_to(heading, LEFT) self.play(Create(subheading)) self.wait(3) text = Tex( r"""{\sffamily Sei \(X\) eine stetige oder diskrete Zufallsvariable.}\\""", color=ORCA_BLUE ) formula = MathTex(r"F(x) := P(X \le x)", color=ORCA_BLUE) (formula.next_to(text, DOWN) .shift(-formula.get_center() * np.array([1, 0, 0]))) self.play(Create(text)) self.wait(5) self.play(Create(formula), run_time=1) self.wait(6.5) self.play(FadeOut(text, formula)) probability = Tex( r"""{\sffamily Wie wahrscheinlich ist es,\\ dass eine Packung weniger als 45 Gramm wiegt?}\\\vspace{0.5ex}""", r"\textsf{Antwort:}\\", r"\(P(X \le 45) = F(45) \approx 4.6\%\)", color=ORCA_BLUE, tex_environment="flushleft" ) self.play(FadeIn(probability[0])) self.wait(4) ( probability[1].next_to(probability[0], DOWN, buff=1) .align_to(probability[0], LEFT) ) self.play(FadeIn(probability[1])) self.wait() self.play(Create( probability[2].next_to(probability[1], DOWN).shift(5*RIGHT) )) self.wait(3.5) self.play(FadeOut(heading, subheading, probability)) self.wait(0.5) class FromDistributionToDensity(Scene): def construct(self): heading = scene_heading( "Zusammenhang Dichte und Verteilungsfunktion" ) self.play(Create(heading)) self.wait(9) texts = Tex( r"\textsf{Von Dichtefunktion zur Verteilungsfunktion}", r"\textsf{und umgekehrt}", color=ORCA_BLUE ) formulas = MathTex( r"F(x) = P(X \le x) = \int_{-\infty}^x f(u) du", r"f(x) = F'(x)", color=ORCA_BLUE ) self.play(Create(texts[0].next_to(heading, DOWN, buff=0.8))) self.wait(3) self.play(Create( formulas[0].next_to(texts[0], DOWN) .shift(-formulas[0].get_center() * np.array([1, 0, 0]))) ) self.wait(2) self.play(Create( texts[1].next_to(formulas[0], DOWN) .align_to(texts[0], LEFT) )) self.wait(2) self.play(Create( formulas[1].next_to(texts[1], 2*DOWN) .shift(-formulas[1].get_center() * np.array([1, 0, 0]))) ) self.wait()