In [1]:
%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
In [2]:
def target(ax):
    for r in [1, 0.75, 0.5, 0.25]:
        circle = plt.Circle((0, 0), r, alpha=0.5, zorder=0)
        ax.add_artist(circle)
    ax.axhline(y=0, color='y', linestyle='--', zorder=1)
    ax.axvline(x=0, color='y', linestyle='--', zorder=1)
    ax.set_xlim(-1,1)
    ax.set_ylim(-1,1)
    ax.axis('off')
    
def double_target():
    fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,5))
    target(ax[0])
    target(ax[1])
    return ax

def add_points(mu, sigma, n, ax):
    points = np.random.normal(mu, sigma, (2, n))
    ax.scatter(*points, c='r', zorder=2, marker='x')

def estimate(mu_left=0, mu_right=0, sigma_left=0.25, sigma_right=0.25, n_left=100, n_right=100):
    axes = double_target()
    for mu, sigma, n, ax in zip([mu_left, mu_right], [sigma_left, sigma_right], [n_left, n_right], axes):
        add_points(mu, sigma, n, ax) 
        
def conf_int(n=10, alpha=0.05):
    plt.subplots(figsize=(10,5))
    mu = 0
    sigma = 1
    ints_num = 100
    points = np.random.normal(mu, sigma, (ints_num, n))
    xs = np.mean(points, axis=1)
    z = stats.norm.ppf(1-alpha/2, mu, sigma)
    left = xs - z*sigma/np.sqrt(n)
    right = xs + z*sigma/np.sqrt(n)
    y = np.linspace(0, 100, ints_num)
    in_count = np.mean([1 if l <= mu <= r else 0 for l,r in zip(left, right)])
    colors = ['#1f77b4' if l <= mu <= r else '#ff7f50' for l,r in zip(left, right)]
    plt.vlines(y, left, right, color=colors)
    plt.plot(y, xs, '.')
    plt.axhline(y=mu, color='gray', linestyle='--')
    plt.ylim([-1.5, 1.5])
    plt.title(in_count)

Estymatory

Pojęcia

  • Próba prosta $\boldsymbol{X} = (X_1, X_2, ..., X_n)$ niezależne i o tym samym rozkładzie
  • Statystyka funkcja próby losowej prostej: $T(\boldsymbol{X})=T(X_1, X_2, ..., X_n)$
  • Estymator statystyka $\hat{\theta}(\boldsymbol{X})$ będąca oszacowaniem nieznanego parametru populacji $\theta$

Własności estymatorów

  • nieobciążoność
  • efektywność
  • zgodność
  • ...

Estymator nieobciążony

Estymator $\hat{\theta}$ parametru $\theta$ jest nieobciążony jeżeli: $$E[\hat{\theta}] = \theta$$

Przykład:

$X_1, X_2, ..., X_n$ - niezależne, o takim samym rozkładzie:

$E[X_i]=\mu$

$D^2[X_i]=\sigma^2$

$E[\bar{X}] = $

$E[X_1] = $

In [3]:
interact(estimate, mu_left=fixed(0), mu_right=(-0.4, 0.4, 0.05), sigma_left=fixed(0.25), sigma_right=fixed(0.25), n_left=fixed(100), n_right=fixed(100))
Out[3]:
<function __main__.estimate(mu_left=0, mu_right=0, sigma_left=0.25, sigma_right=0.25, n_left=100, n_right=100)>

Obciążenie estymatora wariancji $\frac{1}{n}\sum_{i=1}^{n}(X_i-\bar{X})^2$

$E[S^2] = E[\frac{1}{n}\sum_{i=1}^n(X_i-\bar{X})^2]=E[\frac{1}{n}\sum_{i=1}^n(X_i-\mu+\mu-\bar{X})^2]$

$= E[\frac{1}{n}\sum_{i=1}^n((X_i-\mu)-(\bar{X}-\mu))^2]$

$= E[\frac{1}{n}\sum_{i=1}^n((X_i-\mu)^2-2(\bar{X}-\mu)(X_i-\mu)+(\bar{X}-\mu)^2)]$

$= E[\frac{1}{n}\sum_{i=1}^nX_i-\mu)^2-2(\bar{X}-\mu)\frac{1}{n}\sum_{i=1}^n(X_i-\mu)+\frac{1}{n}\sum_{i=1}^n(\bar{X}-\mu)^2]$

$= E[\frac{1}{n}\sum_{i=1}^n(X_i-\mu)^2-2(\bar{X}-\mu)(\frac{1}{n}\sum_{i=1}^nX_i-\frac{1}{n}\sum_{i=1}^n\mu)+\frac{1}{n}n(\bar{X}-\mu)^2]$

$= E[\frac{1}{n}\sum_{i=1}^n(X_i-\mu)^2-2(\bar{X}-\mu)(\bar{X}-\mu)+(\bar{X}-\mu)^2]$

$= E[\frac{1}{n}\sum_{i=1}^n(X_i-\mu)^2-2(\bar{X}-\mu)^2+(\bar{X}-\mu)^2]$

$= E[\frac{1}{n}\sum_{i=1}^n(X_i-\mu)^2-(\bar{X}-\mu)^2]$

$= E[\frac{1}{n}\sum_{i=1}^n(X_i-\mu)^2]-E[(\bar{X}-\mu)^2]$

$= \frac{1}{n}\sum_{i=1}^n \underbrace{E[(X_i-\mu)^2]}_{\sigma^2}-\underbrace{E[(\bar{X}-\mu)^2]}_{\frac{\sigma^2}{n}}$

$= \frac{n\sigma^2}{n} - \frac{\sigma^2}{n}$

$= \frac{n\sigma^2-\sigma^2}{n} = \frac{(n-1)\sigma^2}{n}$

Estymator efektywny

Estymator nieobciążony $\hat{\theta}_1$ jest efektywniejszy od nieobciążonego estymatora $\hat{\theta}_2$, jeżeli zachodzi:

$$ \forall \theta \;\;\; D^2[\hat{\theta}_1] \leq D^2[\hat{\theta}_2] $$

Estymator nieobciążony parametru $\theta$ nazywamy efektywnym (najefektywniejszym), gdy jest efektywniejszy od wszystkich estymatorów nieobciążonych tego parametru.

In [4]:
interact(estimate, mu_left=fixed(0), mu_right=fixed(0), sigma_left=fixed(0.1), sigma_right=(0.1, 1, 0.05), n_left=fixed(100), n_right=fixed(100))
Out[4]:
<function __main__.estimate(mu_left=0, mu_right=0, sigma_left=0.25, sigma_right=0.25, n_left=100, n_right=100)>

Estymator zgodny

Estymator $\hat{\theta}$ parametru $\theta$ jest zgodny jeżeli:

$$\forall \boldsymbol{X},\theta, \epsilon > 0 \;\;\; \lim\limits_{n\rightarrow\infty} P(|\hat{\theta}_n(\boldsymbol{X})-\theta| < \epsilon) = 1$$
In [5]:
interact(estimate, mu_left=fixed(0), mu_right=fixed(0), sigma_left=fixed(0.1), sigma_right=fixed(0.1), n_left=fixed(100), n_right=(5, 500, 10))
Out[5]:
<function __main__.estimate(mu_left=0, mu_right=0, sigma_left=0.25, sigma_right=0.25, n_left=100, n_right=100)>

Estymatory przedziałowe

  • $\hat{\theta}$: estymator punktowy
  • $\hat{\theta}_L = \hat{\theta} - \Delta$
    $\hat{\theta}_P = \hat{\theta} + \Delta$
  • $1 - \alpha$: poziom ufności
  • P($\hat{\theta}_L \leq \theta \leq \hat{\theta}_P$) = $1 - \alpha$

Estymatory przedziałowe: rozkład normalny ze znaną wariancją

Przedział ufności dla średniej:

$1 - \alpha = P(\bar{X}-\Delta \leq \mu \leq \bar{X}+\Delta)$

$= P(-\Delta \leq \bar{X} - \mu \leq \Delta)$

$= P(-\frac{\Delta}{\sigma}\sqrt{n} \leq \frac{\bar{X} - \mu}{\sigma/\sqrt{n}} \leq \frac{\Delta}{\sigma}\sqrt{n})$

$=\Phi(\frac{\Delta}{\sigma}\sqrt{n})-\Phi(-\frac{\Delta}{\sigma}\sqrt{n})$

$=2\Phi(\frac{\Delta}{\sigma}\sqrt{n}) - 1$

$2\Phi(\frac{\Delta}{\sigma}\sqrt{n}) - 1=1-\alpha$

$2\Phi(\frac{\Delta}{\sigma}\sqrt{n}) = 2-\alpha$

$\Phi(\frac{\Delta}{\sigma}\sqrt{n})=1-\frac{\alpha}{2}$

$\frac{\Delta}{\sigma}\sqrt{n} = \underbrace{\Phi^{-1}(1-\frac{\alpha}{2})}_{z_{\alpha}}$

$\Delta = z_\alpha\frac{\sigma}{\sqrt{n}}$

$$\textrm{estymator przedziałowy:}\;\; (\bar{X}-z_\alpha\frac{\sigma}{\sqrt{n}}, \bar{X}+z_\alpha\frac{\sigma}{\sqrt{n}})$$
In [6]:
interact(conf_int, n=(10, 1000, 100), alpha=(0.01, 0.5, 0.05))
Out[6]:
<function __main__.conf_int(n=10, alpha=0.05)>

Przykład

  • $X\sim N(\mu, \sigma)$
  • $\sigma^2=81$
  • $n = 36$
  • $\bar{x}=175$
  • $1-\alpha=0,95$