%matplotlib inline
from ipywidgets import *
import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg')
import numpy as np
import scipy.stats as stats
plt.rcParams['axes.grid'] = True
def dice():
plt.stem(range(1,7), [1/6]*6, use_line_collection=True)
plt.xlabel("Wynik")
plt.ylabel("Prawdopodobieństwo")
def dice_dist():
y = [1/6*i for i in range(7)]
plt.hlines(y, range(0, 7), range(1, 8), color='#1f77b4')
plt.plot(range(1, 7), y[1:], 'o')
plt.plot(range(1, 7), y[:-1], 'o', fillstyle='none', color='#1f77b4')
plt.xlabel("Wynik")
plt.ylabel("Dystrybuanta")
plt.xlim(0,7)
def normal(density=True):
x = np.linspace(-3, 3, 100)
fun = stats.norm.pdf if density else stats.norm.cdf
plt.plot(x, fun(x, 0, 1))
plt.xlabel("x")
ylab = "f(x)" if density else "F(x)"
plt.ylabel(ylab)
def binary(p=0.5):
plt.stem([0,1], [1-p, p], use_line_collection=True)
plt.xlabel("x")
plt.ylabel("P(X=x)")
def binom(n=10, p=0.5):
plt.stem(range(n+1), stats.binom.pmf(range(n+1), n, p), use_line_collection=True)
plt.xlabel("x")
plt.ylabel("P(X=x)")
def uniform(a=0, b=1):
x_lim = 4
p = 1/(b-a)
y = [0, p, 0]
plt.hlines(y, [-x_lim, a, b], [a, b, x_lim], color='#1f77b4')
plt.plot([a, b], [p, p], 'o')
plt.plot([a, b], [0, 0], 'o', fillstyle='none', color='#1f77b4')
plt.xlabel("x")
plt.ylabel("f(x)")
plt.xlim(-x_lim, x_lim)
def normal_param(mu=0, sigma=1):
x_min = -10
x_max = 10
x = np.linspace(x_min, x_max, 100)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.xlabel("x")
plt.ylabel("f(x)")
plt.xlim(x_min,x_max)
plt.ylim(0,0.45)
def normal_s(mu=0, sigma=1, a=0, b=1):
x_min = -10
x_max = 10
x_s = np.linspace(-3, 3, 100)
x = np.linspace(x_min, x_max, 100)
plt.plot(x_s, stats.norm.pdf(x_s, 0, 1))
plt.plot(x, stats.norm.pdf(x, mu+a, sigma/b))
plt.xlabel("x")
plt.ylabel("f(x)")
plt.xlim(x_min,x_max)
plt.ylim(0,0.45)
def fill(lower, upper, mu, sigma):
x = np.linspace(lower, upper, 100)
y = stats.norm.pdf(x,mu,sigma)
plt.fill_between(x, y, color='#0b559f', alpha=0.5)
def normal_shade(lower=0, upper=0):
mu = 0
sigma = 1
x = np.linspace(-3.5*sigma, 3.5*sigma, 100)
plt.plot(x, stats.norm.pdf(x, mu, sigma))
plt.xlabel("x")
plt.ylabel("f(x)")
fill(lower, upper, mu, sigma)
plt.xlim(-3.25,3.25)
def monte_carlo(k=1):
n = 10**k
circle = plt.Circle((0, 0), 1, alpha=0.5)
fig, ax = plt.subplots(figsize=(5,5))
ax.add_artist(circle)
points = np.random.uniform(size=(2,n))
plt.scatter(*points, s=5, c='r')
pi = np.mean(np.linalg.norm(points, axis=0)<1)*4
plt.title(r"$n=$"+str(n)+r" $\pi=$"+str(pi))
plt.xlim(0,1)
plt.ylim(0,1)
$\displaystyle p_i = P (X = x_i)$
dice()
$\displaystyle F(x_0) = \sum_{x_i \leq x_0} P(X=x_i)$
dice_dist()
$f(x)$
normal()
$F(x_0) = \int\limits_{-\infty}^{x_0}f(x)dx$
normal(density=False)
interact(binary, p=(0,1,0.1))
interact(binom, n=(5,10,1), p=(0,1,0.1))
interact(uniform, a=(-3,3,1), b=(-3,3,1))
interact(normal_param, mu=(-10,10,1), sigma=(1,5,1))
mu = np.random.randint(-5, 5)
sigma = np.random.randint(1,5)
print("m =", mu, "sd =", sigma)
interact(normal_s, mu=fixed(mu), sigma=fixed(sigma), a = (-5,5,1), b=(1,5,1))
interact(normal_shade, lower=(-3.5,3.5,0.1), upper=(-3.5,3.5,0.1))
$X \sim N(0, 1)$
$X \sim N(10, 5)$
$IQ \sim N(100, 15)$
Niech $X_1, X_2, ..., X_n$ będzie ciągiem zmiennych losowych:
Wtedy: $$\forall u \in \mathbf{R} \lim\limits_{n\rightarrow\infty} P(U_n < u) = \Phi(u)$$
Dla sum: $$S_n = \sum\limits_{i=1}^{n}X_i$$
$$Z_n = \frac{S_n - n\cdot\mu}{\sigma\cdot\sqrt{n}}$$interact(monte_carlo, k=(1,4,1))