Created
November 15, 2022 21:44
-
-
Save xvzftube/976026924f38d884783fccba37cec814 to your computer and use it in GitHub Desktop.
distributions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from manim import * | |
from scipy.stats import uniform, beta, norm, cauchy | |
import numpy as np | |
black_color = '#292c35' | |
black = "#292c35" | |
red = "#ee1d52" | |
yellow = "#f2d803" | |
blue = "#63a3be" | |
class Uniform(Scene): | |
''' | |
Scene to observe how adjustments to the mean of a normal distrubtion | |
influences the shape of its probability density function | |
''' | |
def construct(self): | |
ax = Axes( | |
x_range = [0, 1, .1], | |
y_range = [0, 2, 2], | |
y_length = 5, | |
color = black_color, | |
axis_config = {'color': black_color}, | |
x_axis_config = {'include_numbers':True,"decimal_number_config": {"color":black_color}}, | |
y_axis_config = {'include_numbers':False, 'exclude_origin_tick': True,"decimal_number_config": {"color":black_color}} | |
) | |
self.camera.background_color = '#f2d803' | |
# Text to display mean | |
mu_text = Text(r'Uniform Density', font = 'SauceCode Pro Nerd Font').next_to(ax, UP, buff=0.2).set_color(black_color) | |
# Define PDF curve, always redraw for each frame | |
curve = always_redraw( | |
lambda: ax.plot( | |
lambda x: uniform.pdf(x), color=black_color) | |
) | |
def get_dots(samples): | |
dot = Dot(ax.coords_to_point(samples, 1), color=black_color) | |
return dot | |
def get_lines(samples): | |
line = ax.get_lines_to_point(ax.c2p(samples,1), color=black_color)[1] | |
return line | |
samples1 = [0.79, 0.47, 0.85, 0.57, 0.34, 0.99, 0.36, 0.81, 0.85, 0.65, | |
0.68, 0.02, 0.65, 0.45, 0.16, 0.11, 0.31, 0.15, 0.4, 0.1, 0.87, | |
0.69, 0.09, 0.77, 0.39, 0.21, 0.36, 0.77, 0.29, 0.22] | |
samples2 = [0.15, 0.63, 0.44, 0.8, 0.74, 0.02, 0.21, 0.93, 0.91, 0.4, 0.05, | |
0.77, 0.93, 0.25, 0.38, 0.81, 0.56, 0.27, 0.9, 0.5, 0.65, 0.71, | |
0.57, 0.77, 0.12, 0.8, 0.95, 0.64, 0.43, 0.13] | |
# uniform 1 | |
mean = np.round(np.average(samples1),3) | |
below = MathTex(r'\bar{x}=').next_to(ax, DOWN, buff=0.2).set_color(black_color) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
lines = list(map(get_lines, samples1)) | |
group_lines = Group(*lines) | |
ax.set_color(black_color) | |
# Start animation | |
self.add(ax, mu_text) | |
self.play(Create(curve)) | |
self.wait() | |
self.play(ShowIncreasingSubsets(group_lines)) | |
self.play(Write(below)) | |
self.play(Write(value)) | |
self.wait(duration = 2) | |
self.play(FadeOut(group_lines)) | |
lines = list(map(get_lines, samples2)) | |
group_lines = Group(*lines) | |
self.play(ShowIncreasingSubsets(group_lines)) | |
self.play(Unwrite(value)) | |
mean = np.round(np.average(samples2),3) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
self.play(Write(value)) | |
self.wait(duration = 2) | |
class Normal(Scene): | |
''' | |
Scene to observe how adjustments to the mean of a normal distrubtion | |
influences the shape of its probability density function | |
''' | |
def construct(self): | |
ax = Axes( | |
x_range = [-3, 3, .5], | |
y_range = [0, .5, .1], | |
y_length = 5, | |
color = black_color, | |
axis_config = {'color': black_color}, | |
x_axis_config = {'include_numbers':True,"decimal_number_config": {"color":black_color}}, | |
y_axis_config = {'include_numbers':False, 'exclude_origin_tick': True,"decimal_number_config": {"color":black_color}} | |
) | |
self.camera.background_color = '#f2d803' | |
# Text to display mean | |
mu_text = Text(r'Normal', font = 'SauceCode Pro Nerd Font').next_to(ax, UP, buff=0.2).set_color(black_color) | |
# Define PDF curve, always redraw for each frame | |
curve = always_redraw( | |
lambda: ax.plot( | |
lambda x: norm.pdf(x), color=black_color) | |
) | |
def get_lines(samples): | |
line = ax.get_lines_to_point(ax.c2p(samples,norm.pdf(samples)), color=black_color)[1] | |
return line | |
samples1 = [-0.77, -0.48, 1.88, 2.24, -1.19, -0.9, -0.24, -1.32, 0.85, | |
0.35, 0.85, 0.82, -1.32, 1.2, -0.79, 0.72, -0.02, -0.65, 0.71, | |
-0.78, -0.06, -1.47, -0.63, -0.7, -0.23, 0.85, 0.58, 1.3, 0.14, | |
0.94] | |
samples2 = [1.69, 1.07, -0.48, -1.87, 0.24, 0.08, -1.06, -1.5, 1.81, 1.1, | |
-1.19, -0.5, -0.54, -0.25, -1.35, -0.25, -0.53, -1.02, -1.84, | |
-1.72, -0.73, -0.37, 0.07, -0.86, 2.09, -1.66, 1.17, 0.25, 0.11, | |
1.33] | |
mean = np.round(np.average(samples1),3) | |
below = MathTex(r'\bar{x}=').next_to(ax, DOWN, buff=0.2).set_color(black_color) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
lines = list(map(get_lines, samples1)) | |
group_lines = Group(*lines) | |
ax.set_color(black_color) | |
# Start animation | |
self.add(ax.remove(ax.get_axis(1)), mu_text) | |
self.play(Create(curve)) | |
self.wait() | |
self.play(ShowIncreasingSubsets(group_lines)) | |
self.play(Write(below)) | |
self.play(Write(value)) | |
self.wait(duration = 2) | |
self.play(FadeOut(group_lines)) | |
lines = list(map(get_lines, samples2)) | |
group_lines = Group(*lines) | |
self.play(ShowIncreasingSubsets(group_lines.set_color(black_color))) | |
self.play(Unwrite(value)) | |
mean = np.round(np.average(samples2),3) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
self.play(Write(value.set_color(black_color))) | |
self.wait(duration = 2) | |
class Beta(Scene): | |
''' | |
Scene to observe how adjustments to the mean of a normal distrubtion | |
influences the shape of its probability density function | |
''' | |
def construct(self): | |
ax = Axes( | |
x_range = [0.01, 0.99, .1], | |
y_range = [0, 3, 1], | |
y_length = 5, | |
color = black_color, | |
axis_config = {'color': black_color}, | |
x_axis_config = {'include_numbers':True,"decimal_number_config": {"color":black_color}}, | |
y_axis_config = {'include_numbers':False, 'exclude_origin_tick': True,"decimal_number_config": {"color":black_color}} | |
) | |
self.camera.background_color = '#f2d803' | |
# Text to display mean | |
mu_text = Text(r'Beta(0.5, 0.5)', font = 'SauceCode Pro Nerd Font').next_to(ax, UP, buff=0.2).set_color(black_color) | |
# Define PDF curve, always redraw for each frame | |
curve = always_redraw( | |
lambda: ax.plot( | |
lambda x: beta.pdf(x, a = .5, b = .5), color=black_color) | |
) | |
def get_lines(samples): | |
line = ax.get_lines_to_point(ax.c2p(samples,beta.pdf(samples, a = .5, b = .5)), color=black_color)[1] | |
return line | |
samples1 = [0.03, 0.57, 0.64, 0.11, 0.45, 0.39, 0.98, 0.88, 0.79, 0.99, | |
0.99, 0.98, 0.8, 0.06, 0.67, 0.03, .98, 0.65, 0.89, 0.14, 0.79, | |
.98, 0.76, 0.99, 0.69, 0.38, 0.01, .98, 0.99, 0.33] | |
samples2 = [0.66, 0.17, 0.07, 0.99, 0.61, 0.01, 0.63, 0.73, 0.01, 0.88, 0.01, 0.49, | |
0.82, 0.53, 0.07, 0.01, 0.01, 0.52, 0.51, 0.62, 0.98, .98, 0.85, 0.2, | |
0.03, 0.01, 0.85, 0.13, 0.25, 0.04] | |
mean = np.round(np.average(samples1),3) | |
below = MathTex(r'\bar{x}=').next_to(ax, DOWN, buff=0.2).set_color(black_color) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
lines = list(map(get_lines, samples1)) | |
group_lines = Group(*lines) | |
ax.set_color(black_color) | |
# Start animation | |
self.add(ax, mu_text) | |
self.play(Create(curve)) | |
self.wait() | |
self.play(ShowIncreasingSubsets(group_lines)) | |
self.play(Write(below)) | |
self.play(Write(value)) | |
self.wait(duration = 2) | |
self.play(FadeOut(group_lines)) | |
lines = list(map(get_lines, samples2)) | |
group_lines = Group(*lines) | |
self.play(ShowIncreasingSubsets(group_lines.set_color(black_color))) | |
self.play(Unwrite(value)) | |
mean = np.round(np.average(samples2),3) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
self.play(Write(value.set_color(black_color))) | |
self.wait(duration = 2) | |
class Cauchy(Scene): | |
''' | |
Scene to observe how adjustments to the mean of a normal distrubtion | |
influences the shape of its probability density function | |
''' | |
def construct(self): | |
ax = Axes( | |
x_range = [-3, 3, .5], | |
y_range = [0, .5, .1], | |
y_length = 5, | |
color = black_color, | |
axis_config = {'color': black_color}, | |
x_axis_config = {'include_numbers':True,"decimal_number_config": {"color":black_color}}, | |
y_axis_config = {'include_numbers':False, 'exclude_origin_tick': True,"decimal_number_config": {"color":black_color}} | |
) | |
self.camera.background_color = '#f2d803' | |
# Text to display mean | |
mu_text = Text(r'Cauchy', font = 'SauceCode Pro Nerd Font').next_to(ax, UP, buff=0.2).set_color(black_color) | |
# Define PDF curve, always redraw for each frame | |
curve = always_redraw( | |
lambda: ax.plot( | |
lambda x: cauchy.pdf(x), color=black_color) | |
) | |
def get_lines(samples): | |
line = ax.get_lines_to_point(ax.c2p(samples,cauchy.pdf(samples)), color=black_color)[1] | |
return line | |
samples1 = [10.9, 10.5, 3.86, 1.07, -0.02, -0.47, -1.19, 0.66, 1.35, -6.1, | |
2.72, 2.15, -0.75, -7.43, 4.56, -0.88, -2.13, 1.43, -4.51, 0.2, | |
-0.55, -0.14, 0.73, -0.38, -0.43, 0.74, 0.07, 0.19, 6.23, -0.9] | |
samples2 = [-0.49, -4.98, -0.42, 1.3, 0.15, 1.2, -0.1, 2.34, -1.16, -2.58, | |
1.29, -0.98, -2.47, 0.11, -0.67, -0.96, -74.74, 0.05, 0.1, 4.17, | |
-0.19, -0.44, -1.06, 7.06, 1.25, 0.17, 0.1, 2.99, -0.73, -2.76] | |
mean = np.round(np.average(samples1),3) | |
below = MathTex(r'\bar{x}=').next_to(ax, DOWN, buff=0.2).set_color(black_color) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
lines = list(map(get_lines, samples1)) | |
group_lines = Group(*lines) | |
ax.set_color(black_color) | |
# Start animation | |
self.add(ax.remove(ax.get_axis(1)), mu_text) | |
self.play(Create(curve)) | |
self.wait() | |
self.play(ShowIncreasingSubsets(group_lines)) | |
self.play(Write(below)) | |
self.play(Write(value)) | |
self.wait(duration = 2) | |
self.play(FadeOut(group_lines)) | |
lines = list(map(get_lines, samples2)) | |
group_lines = Group(*lines) | |
self.play(ShowIncreasingSubsets(group_lines.set_color(black_color))) | |
self.play(Unwrite(value)) | |
mean = np.round(np.average(samples2),3) | |
value = Text(str(mean)).next_to(below, RIGHT, buff=0.2).set_color(black_color) | |
self.play(Write(value.set_color(black_color))) | |
self.wait(duration = 2) | |
class Numbers(Scene): | |
''' | |
Scene to show numbers | |
''' | |
def construct(self): | |
self.camera.background_color = '#f2d803' | |
# Text to display mean | |
text1 = Text(r'0.45', font = 'SauceCode Pro Nerd Font').set_color(black_color) | |
text2 = Text(r'7.92', font = 'SauceCode Pro Nerd Font').set_color(black_color) | |
text3 = Text(r'-0.06', font = 'SauceCode Pro Nerd Font').set_color(black_color) | |
self.play(Write(text1)) | |
self.play(Unwrite(text1)) | |
self.play(Write(text2)) | |
self.play(Unwrite(text2)) | |
self.play(Write(text3)) | |
self.play(Unwrite(text3)) | |
class Equation_WLLN(Scene): | |
''' | |
Scene to show numbers | |
''' | |
def construct(self): | |
self.camera.background_color = '#f2d803' | |
# Text to display mean | |
title = Text(r'Weak Law of Large Numbers', font = 'SauceCode Pro Nerd Font', font_size=64).set_color(black_color).shift(UP) | |
wlln = MathTex(r'\lim_{n \to \infty}\text{P}(|\bar{X_n} - \mu|) < \epsilon) = 1', font_size=96).set_color(black_color).next_to(title, DOWN, buff=0.5) | |
self.add(title) | |
self.wait(5) | |
self.play(Write(wlln)) | |
self.wait(15) | |
self.play(Unwrite(wlln)) | |
self.wait(5) | |
class Count(Animation): | |
def __init__(self, number: DecimalNumber, start: float, end: float, **kwargs) -> None: | |
# Pass number as the mobject of the animation | |
super().__init__(number, **kwargs) | |
# Set start and end | |
self.start = start | |
self.end = end | |
def interpolate_mobject(self, alpha: float) -> None: | |
# Set value of DecimalNumber according to alpha | |
value = self.start + (alpha * (self.end - self.start)) | |
self.mobject.set_value(value).set_color(black_color) | |
class CountingScene(Scene): | |
def construct(self): | |
self.camera.background_color = '#f2d803' | |
# Create Decimal Number and add it to scene | |
number = DecimalNumber().set_color(black_color).scale(5).set_value(0.45) | |
# Add an updater to keep the DecimalNumber centered as its value changes | |
number.add_updater(lambda number: number.move_to(ORIGIN)) | |
self.add(number) | |
self.wait() | |
self.play(Count(number, 0.45, 7.92), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, 7.92, -0.06), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, -0.06, 1.97), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, 1.97, -3.83), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, -3.83, 0.87), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, 0.87, 0.21), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, 0.21, 0.88), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, 0.88, -3.06), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, -3.06, 0.05), run_time=.5, rate_func=linear) | |
self.wait() | |
self.play(Count(number, 0.05, -7.22), run_time=.5, rate_func=linear) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment