概要
統計量について解説し、pandas、numpy を使った計算方法について紹介します。
キーワード
- 標本 (sample)、ランダム標本 (random sample)
- 母集団 (population)
- 標本抽出 (sampling)
- 統計量 (statistic)
母集団
これから知りたいと思う集団全体を母集団 (population) という。 母集団から分析のために選んだ要素を標本 (sample)、標本を選び出すことを標本抽出 (sampling) という。
標本
確率変数 が互いに独立で同一な確率分布に従う (independent and identically distributed / i.i.d.) とき、確率変数 はその確率分布を持つ母集団 (population) からの大きさ のランダム標本 (random sample of size n) または単に標本 (sample) という。
を母集団確率変数としたとき、
と表す。
統計量
を母集団確率変数 からの大きさ の標本としたとき、 の関数 を統計量 (statistic) という。
- 統計量もまた確率変数である。
- 標本 の観測値を としたとき、 は統計量の値である。
- 統計量の分布を標本分布 (sampling distribution) という。
順序統計量
を母集団確率変数 からの大きさ の標本としたとき、これを小さい順に並べたものを
つまり、
を順序統計量 (order statistics) という。
統計量の一覧
- 標本和 (sample sum)
- 標本平均 (sample mean)
- 標本平均絶対偏差 (sample mean absolute deviation / MAD)
- (不偏)標本分散 (sample variance)
- (不偏)標本標準偏差 (sample standard deviation)
- (不偏)標本標準誤差 (sample standard error of mean / SEM)
- 次の標本積率 (r-th sample moment)
- 周りの 次の標本積率 (r-th sample moment about )
- (不偏)標本歪度 (sample skew)
- (不偏)標本尖度 (sample kurtosis)
正規分散の尖度を0とする定義
- 標本モード (sample mode)
- (不偏)標本共分散 (sample covariance)
- (不偏)標本相関係数 (sample correlation coefficient)
- 標準メディアン (sample median)
- 標本範囲 (sample range)
- 標本中点 (sample midpoint)
pandas、numpy で計算する方法
pandas、numpy で計算する方法について紹介します。
In [1]:
import numpy as np
import pandas as pd
from scipy import stats as stats
np.random.seed(0)
x = np.random.randint(0, 10, 100)
s = pd.Series(x)
n = len(x)
## 標本和
print(x.sum())
print(s.sum())
## 標本平均
print(x.mean())
print(s.mean())
# 標本平均絶対偏差
print(np.abs(x - x.mean()).mean())
print(s.mad())
# (不偏)標本分散
print(x.var(ddof=1))
print(s.var())
# (不偏)標本標準偏差
print(x.std(ddof=1))
print(s.std())
# (不偏)標本標準誤差
print(x.std(ddof=1) / np.sqrt(n))
print(s.sem())
# r 次の標本積率
r = 4
print((x ** 4).mean())
print((s ** 4).mean())
# \bar{X} 周りの r 次の標本積率
r = 4
print(((x - x.mean()) ** 4).mean())
print(((s - s.mean()) ** 4).mean())
# 標本歪度
c1 = n / ((n - 1) * (n - 2))
print(c1 * (((x - x.mean()) / x.std(ddof=1)) ** 3).sum())
print(s.skew())
# 標本尖度
c1 = n * (n + 1) / ((n - 1) * (n - 2) * (n - 3))
c2 = 3 * (n - 1) ** 2 / ((n - 2) * (n - 3))
print(c1 * (((x - x.mean()) / x.std(ddof=1)) ** 4).sum() - c2)
print(s.kurt())
# 標本モード
print(stats.mode(x))
print(s.mode())
# 標本共分散
x2 = np.random.randint(0, 10, 100)
s2 = pd.Series(x2)
# 標本共分散行列
print(np.cov([x, x2], rowvar=1, ddof=1))
print([[s.cov(s), s.cov(s2)], [s2.cov(s), s2.cov(s2)]])
# 標本相関係数
print(np.corrcoef([x, x2], rowvar=1)) # 計算には不偏標本共分散を使用
print([[s.corr(s), s.corr(s2)], [s2.corr(s), s2.corr(s2)]])
# 標本メディアン
print(np.median(x))
print(s.median())
# 標本範囲
print(x.max() - x.min())
print(s.max() - s.min())
# 標本中点
print((x.max() + x.min()) / 2)
print((s.max() + s.min()) / 2)
コメント