%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
def spearman_pearson(example="1", show=False):
fig, axes = plt.subplots(figsize=(5,5))
n = 50
x = np.linspace(0, 1, n)
y = x
if example=="2":
y = x**5
elif example=="3":
y = x + np.random.normal(0, 0.2, n)
x[-5:] = np.linspace(2, 3, 5)
elif example=="4":
y = x + np.random.normal(0, 1, n)
plt.scatter(x, y)
plt.xlabel("x")
plt.ylabel("y")
r = round(stats.pearsonr(x,y)[0],2)
rs = round(stats.spearmanr(x,y)[0],2)
if show:
plt.title(r"$r_{Pearson}="+str(r)+"$ $r_{Spearman}="+str(rs)+"$")
plt.grid()
$\;\;\;\;\;H_0: p=P(Y>X)=0.5$
$\;\;\;\;\;H_1: p \neq 0.5 | p > 0.5 | p < 0.5$
lp | test 1 | test 2 | różnica | znak |
---|---|---|---|---|
1 | 1.00 | 10.00 | 9.00 | $+$ |
2 | 4.00 | 6.00 | 2.00 | $+$ |
3 | 2.00 | 8.00 | 6.00 | $+$ |
4 | 3.00 | 9.00 | 6.00 | $+$ |
5 | 0.00 | 0.00 | \textbf{0.00} | |
6 | 5.00 | 9.00 | 4.00 | $+$ |
7 | 10.00 | 7.00 | -3.00 | $-$ |
8 | 9.00 | 5.00 | -4.00 | $-$ |
9 | 8.00 | 7.00 | -1.00 | $-$ |
10 | 8.00 | 4.00 | -4.00 | $-$ |
11 | 2.00 | 5.00 | 3.00 | $+$ |
12 | 3.00 | 5.00 | 2.00 | $+$ |
13 | 6.00 | 4.00 | -2.00 | $-$ |
14 | 5.00 | 7.00 | 2.00 | $+$ |
15 | 8.00 | 8.00 | \textbf{0.00} | |
16 | 1.00 | 9.00 | 8.00 | $+$ |
$\alpha = 0.05$
$H_0: p=0.5$
$H_1: p \neq 0.5$
$n=14$
$S_n=T=9$
$Z=\frac{2T-n}{\sqrt{n}}=\frac{2\cdot9-14}{\sqrt{14}}=1.069$
$\textrm{Zbiór krytyczny: }(-\infty, -1.96)\cup(1.96,\infty)$
$\;\;\;\;\;H_0: median(Y-X)=0$
$\;\;\;\;\;H_1: median(Y-X)\neq0$
Rangowanie wartości bezwzględnych różnic
Dla równych różnic średnia arytmetyczna rang
Statystyka $T=min[\sum(+),\sum(-)]$
Zbiór krytyczny: $C_{kr}=[0, T_{kr}]$
Dla dużych prób:
$\;\;\;\;\;\mu_T = \frac{n(n+1)}{4}$
$\;\;\;\;\;\sigma_T = \sqrt{\frac{n(n+1)(2n+1)}{24}}$
$\;\;\;\;\;Z = \frac{T-\mu_T}{\sigma_T}$
lp | test 1 | test 2 | różnica | moduł różnicy | ranga |
---|---|---|---|---|---|
1 | 1.00 | 10.00 | 9.00 | 9.00 | |
2 | 4.00 | 6.00 | 2.00 | 2.00 | |
3 | 2.00 | 8.00 | 6.00 | 6.00 | |
4 | 3.00 | 9.00 | 6.00 | 6.00 | |
5 | 0.00 | 0.00 | 0.00 | 0.00 | |
6 | 5.00 | 9.00 | 4.00 | 4.00 | |
7 | 10.00 | 7.00 | -3.00 | 3.00 | |
8 | 9.00 | 5.00 | -4.00 | 4.00 | |
9 | 8.00 | 7.00 | -1.00 | 1.00 | |
10 | 8.00 | 4.00 | -4.00 | 4.00 | |
11 | 2.00 | 5.00 | 3.00 | 3.00 | |
12 | 3.00 | 5.00 | 2.00 | 2.00 | |
13 | 6.00 | 4.00 | -2.00 | 2.00 | |
14 | 5.00 | 7.00 | 2.00 | 2.00 | |
15 | 8.00 | 8.00 | 0.00 | 0.00 | |
16 | 1.00 | 9.00 | 8.00 | 8.00 |
lp | test 1 | test 2 | różnica | moduł różnicy | ranga |
---|---|---|---|---|---|
1 | 1.00 | 10.00 | 9.00 | 9.00 | \textbf{14.00} |
2 | 4.00 | 6.00 | 2.00 | 2.00 | \textbf{3.50} |
3 | 2.00 | 8.00 | 6.00 | 6.00 | \textbf{11.50} |
4 | 3.00 | 9.00 | 6.00 | 6.00 | \textbf{11.50} |
5 | 0.00 | 0.00 | 0.00 | 0.00 | $-$ |
6 | 5.00 | 9.00 | 4.00 | 4.00 | \textbf{9.00} |
7 | 10.00 | 7.00 | -3.00 | 3.00 | 6.50 |
8 | 9.00 | 5.00 | -4.00 | 4.00 | 9.00 |
9 | 8.00 | 7.00 | -1.00 | 1.00 | 1.00 |
10 | 8.00 | 4.00 | -4.00 | 4.00 | 9.00 |
11 | 2.00 | 5.00 | 3.00 | 3.00 | \textbf{6.50} |
12 | 3.00 | 5.00 | 2.00 | 2.00 | \textbf{3.50} |
13 | 6.00 | 4.00 | -2.00 | 2.00 | 3.50 |
14 | 5.00 | 7.00 | 2.00 | 2.00 | \textbf{3.50} |
15 | 8.00 | 8.00 | 0.00 | 0.00 | $-$ |
16 | 1.00 | 9.00 | 8.00 | 8.00 | \textbf{13.00} |
$\alpha = 0.05$
$H_0: median(Y-X)=0$
$H_1: median(Y-X)\neq0$
$\sum(-) = 6.5 + 9 + 1 + 9 + 3.5 = 29$
$\sum(+) = 14 + 3.5 + 11.5 + 11.5 + 9 + 6.5 + 3.5 + 3.5 + 14 = 76$
$T=min[\sum(+),\sum(-)] = 29$
$\textrm{Zbiór krytyczny: }[0,26]$
$\;\;\;\;\;H_0: \rho_s=0$
$\;\;\;\;\;H_1: \rho_s\neq0$
X | -4 | 8 | 9 |
---|---|---|---|
Y | 10 | 2 | 1 |
X | -4 | 8 | 9 |
---|---|---|---|
Ranga |
Y | 10 | 2 | 1 |
---|---|---|---|
Ranga |
$\bar{x} = $
$\bar{y} = $
$r = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})}{\sqrt{\sum_{i=1}^{n}(x_i-\bar{x})^2}\sqrt{\sum_{i=1}^{n}(y_i-\bar{y})^2}}$
interact(spearman_pearson, example=["1","2","3", "4"])