非負の整数からなる r×c 行列に値を持つ確率変数を分割表 (contingency table) と呼ぶ. 以下では, r×c の分割表を r×c の行列 A=[aij] で表す. 以下において i は 1,…,r を走り, j は 1,…,c を走るものとする.
このノートでは分割表の確率分布として以下の4種類を考える. そして, サンプルを生成する分布としては, 独立性の条件を満たすものを考える.
非負の整数成分の r×c の分割表 A=[aij] に何も制限を課さない場合.
Λ=[λij] は正の実数を成分とする r×c 行列であるとし,
λ=∑i,jλij,pij=λijλ,P=[pij]とおく. このとき分割表 A=[aij] が生じる確率 p(A|Λ) を
p(A|Λ)=∏i,je−λijλaijijaij!と定めることができる. このようにして定まる分割表の確率分布を rc 個のPoisson分布の直積と呼ぶ.
この rc 個のPoisson分布の直積において, 各 aij の期待値は λij になり, aij 達の総和の期待値は λ になる.
パラメーター Λ=[λij] もしくは P=[pij] が独立性の条件を満たしているとは, pij 達が,
pij=piqj,pi,qj≧0,∑ipi=∑jqj=1と表わされることだと定める. この条件は
μi=λpi,νj=λqjと定めると,
λij=μiνjλ,μi,νj≧0,∑iμi=∑jνj=λと書き直される. このとき,
p(A|Λ)=∏i,je−μiνj/λ(μiνj/λ)aijaij!.rc 個のPoisson分布の直積におけるパラメーター全体の空間の次元は rc であり, その中で独立性を満たすパラメーター達のなす部分空間の次元は λ の分の 1 と μi 達の分の r−1 と νj 達の分の c−1 の総和である r+c−1 次元になり, パラメーター全体の空間との次元の差は rc−r−c+1=(r−1)(c−1) になる. この (r−1)(c−1) がχ²検定におけるχ²分布の自由度になる.
分割表 A=[aij] に総和 ∑i,jaij=n が一定であるという制限を課す場合.
pij は非負の実数であるとし, それらの総和は 1 になると仮定し, P=[pij] とおく.
非負の整数成分の r×c 行列を r×c の分割表と呼ぶのであった. 成分の総和が n に固定された r×c の分割表 A=[aij], (∑i,jaij=n) が生じる確率を
p(A|n,P)=n!∏i,jpaijijaij!と定めることによって, 成分の総和が n に固定された分割表全体に確率分布を定めることができる. これを rc 項分布と呼ぶことにする.
この rc 項分布における aij の期待値 λij は
λij=npijになる. これを用いると, rc 項分布における確率は
p(A|n,P)=n!nn∏i,jλaijijaij!と書き直される.
この場合のパラメーター P=[pij] に関する独立性の条件は
pij=piqj,pi,qj≧0,∑ipi=∑jqj=1もしくは
λij=μiνjλ,μi,νj≧0,∑iμi=∑jνj=nと書ける.
rc 項分布におけるパラメーター全体の空間の次元は ∑i,jpij=1 という制限によって rc より1小さい rc−1 になり, 独立性を満たすパラメーター達のなす部分空間の次元は pi 達の分の r−1 と qi 達の分の c−1 の和の r+c−2 になり, 全体の次元との差は (r−1)(c−1) になる. この (r−1)(c−1) がχ²検定におけるχ²分布の自由度になる.
分割表 A=[aij] に行の和 ∑jaij=μj がすべて一定であるという制限を課す場合.
n, μi は非負の整数であるとし, ∑imui=n と仮定し,
μ=(μ1,…,μr)とおく. qij は非負の実数であるとし, ∑jqij=1 であると仮定し,
Q=[qij]とおく.
各行の総和が μi になるという条件
∑jaij=μiという条件を満たす分割表 A=[aij] が生じる確率を
p(A|μ,Q)=∏i(μi!∏jqaijijaij!)と定めることによって, 各行の総和が μi になるという制限付きの分割表全体に確率分布を定義できる. これを r 個の c 項分布の直積と呼ぶことにする.
この rc 項分布にいて, 各 aij の期待値は λij は
λij=μiqijになる. これを用いると, r 個の c 項分布の直積における確率は
p(A|μ,Q)=∏iμi!μμii⋅∏i,jλaijijaij!と書き直される.
この場合の独立性の条件は
νj=∑iλij,qj=νjnとおくと
q1j=⋯=qrj=qjもしくは
λ1jμ1=⋯=λrjμr=νjnと書ける.
r 個の c 項分布におけるパラメーター全体の空間の次元は r(c−1) になり, 独立性を満たすパラメーター達のなす部分空間の次元は qi 達の分の c−1 になり, 全体の次元との差は (r−1)(c−1) になる. この (r−1)(c−1) がχ²検定におけるχ²分布の自由度になる.
分割表 A=[aij] に行の和 ∑jaij=μj と列の和 ∑iaij=νj がすべて一定であるという制限を課す場合.
n, μi, νj は正の整数で
∑iμi=∑jνj=nを満たしていると仮定し,
μ=(μ1,…,μr),ν=(ν1,…,νc)とおく. λij は正の実数であるとし,
∑jλij=μi,∑iλij=νiを満たしていると仮定し,
Λ=[λij]とおく.
すべての行とすべての列の総和が
∑jaij=μi,∑iaij=νi,と固定された分割表 A=[aij] が生じる確率を
p(A|μ,ν,Λ)=1Z(Λ)∏i,jλaijijaij!,Z(Λ)=∑A∏i,jλaijijaij!と定めることができる. ここで Z(Λ) の定義和における A=[aij] は条件(1)を満たす分割表全体を走る. この確率分布を周辺度数がすべて固定されている場合の分割表の確率分布と呼ぶことにする.
ϕkl=λklλk+1,l+1λk+1,lλk,l+1,skl=k∑i=1l∑j=1aijとおくと,
aij=sij+si−1,j−1−si−1,j−si,j−1なので, 上の確率は次のようにも書ける:
p(A|μ,ν,Λ)=1˜Z(Λ)∏k,lϕsklkl∏i,jaij!,˜Z(Λ)=∑A∏k,lϕsklkl∏i,jaij!.ここで, i,j,k,l はそれぞれ i=1,…,r, j=1,…,c, k=1,…,r−1, l=1,…,c−1 を走り, A は条件(1)を満たす分割表全体を走る.
このとき, パラメーター Λ=[λij] の独立性は
ϕkl=1(k=1,…,r−1,l=1,…,c−1)という (r−1)(c−1) 個の連立条件で書ける. パラメーター Λ=[λij] が独立性を満たしているとき, λij は
λij=μiνjnに一意的に決まってしまい, 上の確率は次の形になる:
p(A|μ,ν,Λ)=∏iμi!⋅∏jνj!n!∏i,jaij!.すなわち, パラメーター Λ=[λij] が独立性を満たしているとき,
˜Z(Λ)=n!∏iμi!⋅∏jνj!になる. 確率(2)は次のように書き直される:
p(A|μ,ν,Λ)=c∏j=1(νja1j,…,arj)(nμ1,…,μr).ここで, 多項係数を次のように書いた:
(nm1,…,mr)=n!m1!⋯mr!,m1+⋯+mr=n.この多項係数は n 個のものを m1 個, …, mr 個に分割する方法の個数を表している. 確率(3)の分子分母は以下のような意味を持っている.
このことから, (3)で定義される確率分布がどのようなものであるかがわかる.
周辺度数がすべて固定されている分割表の確率分布のパラメーター全体の空間の次元は (r−1)(c−1) になり, 独立性の条件を満たすパラメーター達のなす部分空間の次元は 0 になり, 全体の次元との差は (r−1)(c−1) になる.
注意: r=c=2 の場合の周辺度数がすべて固定されている 2×2 の分割表の確率分布は Fisher's noncentral hypergeometric distribution と呼ばれており, その独立性を満たす場合は hypergeometric distribution と呼ばれている. □
前節の記号をそのまま引き継ぎ,
λij=npij,aij−λij=√nxijとなっていると仮定する. このとき, 前節の式(2)の中の階乗にStirlingの近似公式を適用すると, n→∞ において,
p(A|μ,ν,Λ)=∏i(μμiie−μi√2πμi)⋅∏j(ννjje−νj√2πνj)!nne−n√2πn∏i,j(aaijije−aij√2πaij)(1+o(1))=∏i(μμii√2πμi)⋅∏j(ννjj√2πνj)!nn√2πn∏i,j(aaijij√2πaij)(1+o(1))=√∏iμi⋅∏jνj(2π)(r−1)(c−1)∏i,jaijexp(−∑i,jaijlognaijμiνj)(1+o(1))=√∏iμi⋅∏jνj(2π)(r−1)(c−1)∏i,jaijexp(−∑i,jaijlogaijλij)(1+o(1))=√(n2π)(r−1)(c−1)exp(−∑i,jaijlogaijλij)(1+o(1))1つ目の等号でStirlingの公式を用い, 2つ目の等号で ∑iμi=∑jνj=∑i,jaij=n を使い, 3つ目の等号では指数部分に
μi=∑jaij,νi=∑iaij,n=∑i,jaijを使い, 4つ目の等号では λij=μiνj/n を使い, 5つ目の等号では aij=λij(1+o(1)) と λij=μiνj/n を使った.
このとき, λ を大きくすると, 上で得た近似式の指数函数の中身の −2 倍は次のように近似される:
2∑i,jaijlogaijλij=2∑i,jλij(1+aij−λijλij)log(1+aij−λijλij)=2∑i,j(aij−λij+(aij−λij)22λij)+O(1√n)=∑i,j(aij−λij)2λij+O(1√n).1つ目の等号で aij=λij(1+(aij−λij)/λij) を用い, 2つ目の等号で log(1+x)=x−x2/2+O(x3) を用い, 3つ目の等号で ∑i,jaij=∑i,jλij=n を用いた.
さらに, λij=npij, aij−λij=√nxij より,
2∑i,jaijlogaijλij=∑i,j(aij−λij)2λij+O(1√n)=∑i,jx2ijpij+O(1√n)でかつ daij=√ndxij より,
p(A|μ,ν,Λ)r−1∏i=1c−1∏j=1daij≈1√(2π)(r−1)(c−1)exp(−12∑i,jx2ijpij)r−1∏i=1c−1∏j=1dxij.以上の計算結果から, 周辺度数がすべて固定されている分割表の独立性を満たす確率分布は, n が大きなときに, 台が (r−1)(c−1) 次元の多変量正規分布で近似され, 統計量
X2=∑i,j(aij−λij)2λij=∑i,jx2ijpijが漸近的に自由度 (r−1)(c−1) のχ²分布に従うことがわかる. 一般に台が N 次元の
const.exp(−12∑i,jaijxixj)×(delta function with N-dim. support)の形の平均が 0 の多変量正規分布において, ∑i,jaijxixj の部分に対応する統計量は自由度 N のχ²分布を満たすことを使った.
さらに, 上の X2 で近似される統計量
G=2∑i,jaijlogaijλijも漸近的に自由度 (r−1)(c−1) のχ²分布に従うこともわかる.
後で周辺度数がすべて固定されていない場合にも同様の結果が得られることを説明する.
r×c の分割表 A=[aij] に対して, 以下の量をPearsonのχ²統計量と呼ぶ:
X2=∑i,j(Oij−Eij)2Eij.ここで,
Oij=aij,Eij=MiNjn,n=∑i,jaij,Mi=∑jaij,Nj=∑iaij.次のように G 統計量を定義しておく:
G=2∑i,jOijlogOijEij.G と X2 は, Taylor展開
(1+x)log(1+x)=(1+x)(x−x22+O(x3))=x+x22+O(x3)から得られる次の公式によって, 互いに相手を近似するという関係になっている:
2alogaλ=2(a−λ)+(a−λ)2λ+O((a−λ)3λ2)∑i,jOij=∑i,jEij より, a−λ=Oij−Eij の和が消えることに注意せよ.
safediv(x, y) = iszero(x) ? x : x/y
safemult(x, y) = iszero(x) ? x : x*y
function chisq(A)
r, c = size(A)
n = sum(A)
M = vec(sum(A, dims=2))
N = vec(sum(A, dims=1))
sum(safediv((A[i,j] - M[i]*N[j]/n)^2, M[i]*N[j]/n) for i in 1:r, j in 1:c)
end
function gstat(A)
r, c = size(A)
n = sum(A)
M = vec(sum(A, dims=2))
N = vec(sum(A, dims=1))
2sum(safemult(A[i,j], log(A[i,j]) - log(M[i]*N[j]/n)) for i in 1:r, j in 1:c)
end
A = [
1 2 3
2 4 6
]
@show chisq(A)
@show gstat(A)
B = [
1 8 1
2 1 6
]
@show chisq(B)
@show gstat(B);
chisq(A) = 0.0 gstat(A) = 0.0 chisq(B) = 9.322398589065257 gstat(B) = 10.447245765410694
2×2 の分割表
A=[abcd]のPearsonのχ²統計量は
X2=(ad−bc)2(a+b+c+d)(a+b)(a+c)(b+d)(c+d)と表わされる.
using SymPy
@vars a b c d
A = [
a b
c d
]
chisq(A).factor()
定理: 分割表について前節で定義した4つの確率分布のどれにおいても, そのパラメーターが独立性を満たしているならば, Pearsonのχ²統計量 X2 と G 統計量 G はともに, λ もしくは n を大きくするとき漸近的に, 自由度 (r−1)(c−1) のχ²分布に従う. □
前節において, 周辺度数がすべて固定されている分割表の独立性を満たす確率分布の場合にはこれが成立することをすでに示した.
他の3つの場合のこの定理はWilksの定理から導かれる. 以下ではこの定理の成立を数値的に確認してみよう.
using Distributions
using Plots
# Plots.jlのデフォルトの設定を表示
#@show Plots.reset_defaults()
# legendの半透明化
@show default(:bglegend, plot_color(default(:bg), 0.5))
@show default(:fglegend, plot_color(ifelse(isdark(plot_color(default(:bg))), :white, :black), 0.6));
using Base64
displayfile(mime, file; tag="img") = open(file) do f
base64 = base64encode(f)
display("text/html", """<$(tag) src="data:$(mime);base64,$(base64)"/>""")
end
pyplotclf() = if backend() == Plots.PyPlotBackend(); PyPlot.clf(); end
pyplot(fmt=:svg)
default(:bglegend, plot_color(default(:bg), 0.5)) = RGBA{Float64}(1.0,1.0,1.0,0.5) default(:fglegend, plot_color(ifelse(isdark(plot_color(default(:bg))), :white, :black), 0.6)) = RGBA{Float64}(0.0,0.0,0.0,0.6)
Plots.PyPlotBackend()
ecdf(x; Y=rand(10)) = mean(Y .≤ x)
eccdf(x; Y=rand(10)) = mean(Y .≥ x)
n = 2^6
dist = Gamma(10, 0.5)
Y = rand(dist, n)
x = range(mean(dist)-5std(dist), mean(dist)+5std(dist), length=400)
plot(size=(400, 270), legend=:outertop)
plot!(x, eccdf.(x; Y=Y); label="eccdf of $dist, n = $n")
plot!(x, ccdf.(dist, x); label="ccdf of $dist", ls=:dash)
equantile(p; Y=randn(100)) = quantile(Y, p)
ecquantile(p; Y=randn(100)) = quantile(Y, 1-p)
n = 2^6
dist = Gamma(10, 0.5)
Y = rand(dist, n)
p = range(0, 1, length=200)
plot(size=(400, 270), legend=:outertop)
plot!(p, ecquantile.(p; Y=Y); label="ecquantile of $dist, n = $n")
plot!(p, cquantile.(dist, p); label="cquantile of $dist", ls=:dash)
param_indep(p, q) = p .* q'
p = [0.2, 0.3, 0.5]
q = [0.1, 0.2, 0.3, 0.4]
P = param_indep(p, q)
@show p
@show q
@show sum(P)
P
p = [0.2, 0.3, 0.5] q = [0.1, 0.2, 0.3, 0.4] sum(P) = 1.0000000000000002
3×4 Array{Float64,2}: 0.02 0.04 0.06 0.08 0.03 0.06 0.09 0.12 0.05 0.1 0.15 0.2
df_chisq(r, c) = (r-1)*(c-1)
df_chisq(P) = prod(size(P) .- 1)
@show size(P)
@show size(P) .- 1
@show df_chisq(P);
size(P) = (3, 4) size(P) .- 1 = (2, 3) df_chisq(P) = 6
function plot_sim(title, PearsonChisq, G_Statistics, df, binstep)
chisq_dist = Chisq(df)
f(x) = pdf(chisq_dist, x)
xmax = 4df + 2
x = range(0, xmax, length=200)
bin = range(0, xmax, step=binstep)
P1 = plot(xlabel="x")
plot!(title=title, titlefontsize=9, legendfontsize=8, guidefontsize=8)
histogram!(PearsonChisq; bin=bin, norm=true, alpha=0.3, label="Pearson's χ²-statistics")
plot!(x, f.(x); label="pdf of Chisq(df=$(df))")
plot!(tickfontsize=7)
P2 = plot(xlabel="x")
plot!(title=title, titlefontsize=9, legendfontsize=8, guidefontsize=8)
histogram!(G_Statistics; bin=bin, norm=true, alpha=0.3, label="G-statistics")
plot!(x, f.(x); label="pdf of Chisq(df=$(df))")
plot!(tickfontsize=7)
P3 = plot(guidefontsize=8)
plot!(xlabel="ccdf of Chisq(df=$(df))", ylabel="eccdf of Pearson's χ²-statistics")
xx = ccdf.(chisq_dist, x)
yy = eccdf.(x; Y=PearsonChisq)
plot!(xx, yy; label="")
plot!([0,1], [0,1]; label="", color=:black, ls=:dot, alpha=0.5)
plot!(xtick=0:0.1:1, ytick=0:0.1:1, tickfontsize=7, xrotation=90)
P4 = plot(guidefontsize=8)
plot!(xlabel="ccdf of Chisq(df=$(df))", ylabel="eccdf of G-statistics")
xx = ccdf.(chisq_dist, x)
yy = eccdf.(x; Y=G_Statistics)
plot!(xx, yy; label="")
plot!([0,1], [0,1]; label="", color=:black, ls=:dot, alpha=0.7)
plot!(xtick=0:0.1:1, ytick=0:0.1:1, tickfontsize=7, xrotation=90)
α_max = 0.055
x0 = range(cquantile(chisq_dist, α_max), 2xmax, length=200)
P5 = plot(guidefontsize=8)
plot!(xlabel="ccdf of Chisq(df=$(df))", ylabel="eccdf of Pearson's χ²-statistics")
xx = ccdf.(chisq_dist, x0)
yy = eccdf.(x0; Y=PearsonChisq)
plot!(xx, yy; label="")
plot!([0, α_max], [0, α_max]; label="", color=:black, ls=:dot, alpha=0.5)
plot!(xtick=0:0.005:1, ytick=0:0.005:1, tickfontsize=7, xrotation=90)
P6 = plot(guidefontsize=8)
plot!(xlabel="ccdf of Chisq(df=$(df))", ylabel="eccdf of G-statistics")
xx = ccdf.(chisq_dist, x0)
yy = eccdf.(x0; Y=G_Statistics)
plot!(xx, yy; label="")
plot!([0, α_max], [0, α_max]; label="", color=:black, ls=:dot, alpha=0.5)
plot!(xtick=0:0.005:1, ytick=0:0.005:1, tickfontsize=7, xrotation=90)
plot(P1, P3, P5, P2, P4, P6;
size=(800, 500), layout=grid(2, 3; widths=[3.2/8, 2.4/8, 2.4/8])
)
end
plot_sim (generic function with 1 method)
prod_Poisson(Λ) = product_distribution(Poisson.(vec(Λ)))
function sim_Poisson(; λ=100, P=param_indep([0.2, 0.3, 0.5], [0.1, 0.2, 0.3, 0.4]), L=10^5)
dist = prod_Poisson(λ*P)
PearsonChisq = Array{Float64,1}(undef, L)
G_Statistics = Array{Float64,1}(undef, L)
for l in 1:L
A = reshape(rand(dist), size(P))
PearsonChisq[l] = chisq(A)
G_Statistics[l] = gstat(A)
end
PearsonChisq, G_Statistics
end
function plot_sim_Poisson(; λ=100, P=param_indep([0.2, 0.3, 0.5], [0.1, 0.2, 0.3, 0.4]),
L=10^5, binstep=0.5
)
@show expectation = λ*P
@show total = sum(expectation)
@show r, c = size(expectation)
@show df = df_chisq(expectation)
@time PearsonChisq, G_Statistics = sim_Poisson(λ=λ, P=P, L=L)
title = "$(r)×$(c) Poisson distributions (λ = $(λ))"
sleep(0.1)
plot_sim(title, PearsonChisq, G_Statistics, df, binstep)
end
plot_sim_Poisson (generic function with 1 method)
P = param_indep([0.2, 0.3, 0.5], [0.1, 0.2, 0.3, 0.4])
3×4 Array{Float64,2}: 0.02 0.04 0.06 0.08 0.03 0.06 0.09 0.12 0.05 0.1 0.15 0.2
plot_sim_Poisson(λ=50, P=P)
expectation = λ * P = [1.0000000000000002 2.0000000000000004 3.0 4.000000000000001; 1.5 3.0 4.5 6.0; 2.5 5.0 7.5 10.0] total = sum(expectation) = 50.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 1.290666 seconds (3.10 M allocations: 149.537 MiB, 3.67% gc time)
plot_sim_Poisson(λ=100, P=P)
expectation = λ * P = [2.0000000000000004 4.000000000000001 6.0 8.000000000000002; 3.0 6.0 9.0 12.0; 5.0 10.0 15.0 20.0] total = sum(expectation) = 100.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 1.099794 seconds (3.10 M allocations: 149.537 MiB, 3.80% gc time)
plot_sim_Poisson(λ=200, P=P)
expectation = λ * P = [4.000000000000001 8.000000000000002 12.0 16.000000000000004; 6.0 12.0 18.0 24.0; 10.0 20.0 30.0 40.0] total = sum(expectation) = 200.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 1.265358 seconds (3.10 M allocations: 149.537 MiB, 4.13% gc time)
小さな λ での誤差は G 統計量よりも, Pearsonのχ²統計量の方が小さい.
function sim_Multinomial(; n=100, P=param_indep([0.2, 0.3, 0.5], [0.1, 0.2, 0.3, 0.4]), L=10^5)
dist = Multinomial(n, vec(P))
PearsonChisq = Array{Float64,1}(undef, L)
G_Statistics = Array{Float64,1}(undef, L)
for l in 1:L
A = reshape(rand(dist), size(P))
PearsonChisq[l] = chisq(A)
G_Statistics[l] = gstat(A)
end
PearsonChisq, G_Statistics
end
function plot_sim_Multinomial(; n=100, P=param_indep([0.2, 0.3, 0.5], [0.1, 0.2, 0.3, 0.4]),
L=10^5, binstep=0.5
)
@show expectation = n*P
@show total = sum(expectation)
@show r, c = size(expectation)
@show df = df_chisq(expectation)
@time PearsonChisq, G_Statistics = sim_Multinomial(n=n, P=P, L=L)
title = "$(r)×$(c)-nomial distribution (n = $(n))"
sleep(0.1)
plot_sim(title, PearsonChisq, G_Statistics, df, binstep)
end
plot_sim_Multinomial (generic function with 1 method)
plot_sim_Multinomial(n=50, P=P)
expectation = n * P = [1.0000000000000002 2.0000000000000004 3.0 4.000000000000001; 1.5 3.0 4.5 6.0; 2.5 5.0 7.5 10.0] total = sum(expectation) = 50.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 0.562773 seconds (3.10 M allocations: 149.536 MiB, 8.50% gc time)
plot_sim_Multinomial(n=100, P=P)
expectation = n * P = [2.0000000000000004 4.000000000000001 6.0 8.000000000000002; 3.0 6.0 9.0 12.0; 5.0 10.0 15.0 20.0] total = sum(expectation) = 100.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 0.682386 seconds (3.10 M allocations: 149.536 MiB, 7.32% gc time)
plot_sim_Multinomial(n=200, P=P)
expectation = n * P = [4.000000000000001 8.000000000000002 12.0 16.000000000000004; 6.0 12.0 18.0 24.0; 10.0 20.0 30.0 40.0] total = sum(expectation) = 200.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 0.649971 seconds (3.10 M allocations: 149.536 MiB, 8.89% gc time)
小さな n での誤差は G 統計量よりも, Pearsonのχ²統計量の方が小さい.
function rand_prod_Multinomial(M, q)
r, c = length(M), length(q)
A = Array{Int, 2}(undef, r, c)
for i in 1:r
A[i,:] = rand(Multinomial(M[i], q))
end
A
end
rand_prod_Multinomial (generic function with 1 method)
M = [20, 30, 50]
q = [0.1, 0.2, 0.3, 0.4]
rand_prod_Multinomial(M, q)
3×4 Array{Int64,2}: 2 3 4 11 3 5 8 14 2 7 17 24
function sim_prod_Multinomial(; M=[20, 30, 50], q=[0.1, 0.2, 0.3, 0.4], L=10^5)
PearsonChisq = Array{Float64,1}(undef, L)
G_Statistics = Array{Float64,1}(undef, L)
for l in 1:L
A = rand_prod_Multinomial(M, q)
PearsonChisq[l] = chisq(A)
G_Statistics[l] = gstat(A)
end
PearsonChisq, G_Statistics
end
function plot_sim_prod_Multinomial(; M=[20, 30, 50], q=[0.1, 0.2, 0.3, 0.4],
L=10^5, binstep=0.5
)
n = sum(M)
@show expectation = M*q'
@show total = sum(expectation)
@show r, c = size(expectation)
@show df = df_chisq(expectation)
@time PearsonChisq, G_Statistics = sim_prod_Multinomial(M=M, q=q, L=L)
if c == 2
title = "$(r) binomial distributions (n = $n)"
elseif c == 3
title = "$(r) trinomial distributions (n = $n)"
elseif c == 4
title = "$(r) quadranomial distributions (n = $n)"
else
title = "$(r) $(c)-nomial distributions (n = $n)"
end
sleep(0.1)
plot_sim(title, PearsonChisq, G_Statistics, df, binstep)
end
plot_sim_prod_Multinomial (generic function with 1 method)
plot_sim_prod_Multinomial(M=div.(M,2), q=q)
expectation = M * q' = [1.0 2.0 3.0 4.0; 1.5 3.0 4.5 6.0; 2.5 5.0 7.5 10.0] total = sum(expectation) = 50.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 0.746881 seconds (3.20 M allocations: 172.424 MiB, 6.70% gc time)
plot_sim_prod_Multinomial(M=M, q=q)
expectation = M * q' = [2.0 4.0 6.0 8.0; 3.0 6.0 9.0 12.0; 5.0 10.0 15.0 20.0] total = sum(expectation) = 100.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 0.706654 seconds (3.20 M allocations: 172.424 MiB, 6.82% gc time)
plot_sim_prod_Multinomial(M=2M, q=q)
expectation = M * q' = [4.0 8.0 12.0 16.0; 6.0 12.0 18.0 24.0; 10.0 20.0 30.0 40.0] total = sum(expectation) = 200.0 (r, c) = size(expectation) = (3, 4) df = df_chisq(expectation) = 6 0.570665 seconds (3.20 M allocations: 172.424 MiB, 7.11% gc time)
以上のように, 周辺度数をすべて固定するという不自然な前提を採用しなくても, Pearsonのχ²統計量と G 統計量は漸近的に自由度 (r−1)(c−1) のχ²分布に従っていることを数値的に確認できる.
そして, 小さな n での誤差は G 統計量よりも, Pearsonのχ²統計量の方が小さいことも確認できる. Pearsonのχ²統計量は優れた統計量である.
45度線に近いほど誤差が小さい. グラフが45度線よりも上にある部分はP値が余計に小さくなって有意差が出易くなることを意味している. 以上の例において, G 統計量は有意差が出易くなる方向で誤差が大きくなっている.
aoki-takemura-ohp.pdf の p.48 の例.
A = [
2 1 1 0 0
8 3 3 0 0
0 2 1 1 1
0 0 0 1 1
0 0 0 0 1
]
r, c = size(A)
n = sum(A)
M = vec(sum(A, dims=2))
p = M/n
N = vec(sum(A, dims=1))
q = N/n
P = param_indep(p, q)
df = df_chisq(P)
@show chi_squared = chisq(A)
@show p_value = ccdf(Chisq(df), chi_squared)
n*P
chi_squared = chisq(A) = 25.337619047619047 p_value = ccdf(Chisq(df), chi_squared) = 0.06409042450667916
5×5 Array{Float64,2}: 1.53846 0.923077 0.769231 0.307692 0.461538 5.38462 3.23077 2.69231 1.07692 1.61538 1.92308 1.15385 0.961538 0.384615 0.576923 0.769231 0.461538 0.384615 0.153846 0.230769 0.384615 0.230769 0.192308 0.0769231 0.115385
plot_sim_Poisson(λ=n, P=P; binstep=1)
expectation = λ * P = [1.5384615384615385 0.9230769230769231 0.7692307692307693 0.3076923076923077 0.46153846153846156; 5.384615384615385 3.230769230769231 2.6923076923076925 1.076923076923077 1.6153846153846154; 1.9230769230769231 1.153846153846154 0.9615384615384616 0.38461538461538464 0.576923076923077; 0.7692307692307693 0.46153846153846156 0.38461538461538464 0.15384615384615385 0.23076923076923078; 0.38461538461538464 0.23076923076923078 0.19230769230769232 0.07692307692307693 0.11538461538461539] total = sum(expectation) = 26.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 1.932493 seconds (3.10 M allocations: 166.322 MiB, 2.53% gc time)
plot_sim_Multinomial(n=n, P=P; binstep=1)
expectation = n * P = [1.5384615384615385 0.9230769230769231 0.7692307692307693 0.3076923076923077 0.46153846153846156; 5.384615384615385 3.230769230769231 2.6923076923076925 1.076923076923077 1.6153846153846154; 1.9230769230769231 1.153846153846154 0.9615384615384616 0.38461538461538464 0.576923076923077; 0.7692307692307693 0.46153846153846156 0.38461538461538464 0.15384615384615385 0.23076923076923078; 0.38461538461538464 0.23076923076923078 0.19230769230769232 0.07692307692307693 0.11538461538461539] total = sum(expectation) = 26.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 0.913220 seconds (3.10 M allocations: 166.321 MiB, 5.20% gc time)
plot_sim_prod_Multinomial(M=M, q=q; binstep=1)
expectation = M * q' = [1.5384615384615385 0.9230769230769231 0.7692307692307693 0.3076923076923077 0.46153846153846156; 5.384615384615385 3.230769230769231 2.6923076923076925 1.076923076923077 1.6153846153846154; 1.9230769230769231 1.153846153846154 0.9615384615384616 0.38461538461538464 0.576923076923077; 0.7692307692307693 0.46153846153846156 0.38461538461538464 0.15384615384615385 0.23076923076923078; 0.38461538461538464 0.23076923076923078 0.19230769230769232 0.07692307692307693 0.11538461538461539] total = sum(expectation) = 26.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 0.801838 seconds (3.40 M allocations: 218.201 MiB, 5.74% gc time)
plot_sim_prod_Multinomial(M=N, q=p; binstep=1)
expectation = M * q' = [1.5384615384615385 5.384615384615384 1.9230769230769231 0.7692307692307693 0.38461538461538464; 0.9230769230769231 3.230769230769231 1.153846153846154 0.46153846153846156 0.23076923076923078; 0.7692307692307693 2.692307692307692 0.9615384615384616 0.38461538461538464 0.19230769230769232; 0.3076923076923077 1.0769230769230769 0.38461538461538464 0.15384615384615385 0.07692307692307693; 0.46153846153846156 1.6153846153846154 0.576923076923077 0.23076923076923078 0.11538461538461539] total = sum(expectation) = 26.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 0.790298 seconds (3.40 M allocations: 218.201 MiB, 11.79% gc time)
plot_sim_Poisson(λ=2n, P=P; binstep=1)
expectation = λ * P = [3.076923076923077 1.8461538461538463 1.5384615384615385 0.6153846153846154 0.9230769230769231; 10.76923076923077 6.461538461538462 5.384615384615385 2.153846153846154 3.230769230769231; 3.8461538461538463 2.307692307692308 1.9230769230769231 0.7692307692307693 1.153846153846154; 1.5384615384615385 0.9230769230769231 0.7692307692307693 0.3076923076923077 0.46153846153846156; 0.7692307692307693 0.46153846153846156 0.38461538461538464 0.15384615384615385 0.23076923076923078] total = sum(expectation) = 52.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 2.063475 seconds (3.10 M allocations: 166.322 MiB, 2.47% gc time)
plot_sim_Multinomial(n=2n, P=P; binstep=1)
expectation = n * P = [3.076923076923077 1.8461538461538463 1.5384615384615385 0.6153846153846154 0.9230769230769231; 10.76923076923077 6.461538461538462 5.384615384615385 2.153846153846154 3.230769230769231; 3.8461538461538463 2.307692307692308 1.9230769230769231 0.7692307692307693 1.153846153846154; 1.5384615384615385 0.9230769230769231 0.7692307692307693 0.3076923076923077 0.46153846153846156; 0.7692307692307693 0.46153846153846156 0.38461538461538464 0.15384615384615385 0.23076923076923078] total = sum(expectation) = 52.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 0.787292 seconds (3.10 M allocations: 166.321 MiB, 5.53% gc time)
plot_sim_prod_Multinomial(M=2M, q=q; binstep=1)
expectation = M * q' = [3.076923076923077 1.8461538461538463 1.5384615384615385 0.6153846153846154 0.9230769230769231; 10.76923076923077 6.461538461538462 5.384615384615385 2.153846153846154 3.230769230769231; 3.8461538461538463 2.307692307692308 1.9230769230769231 0.7692307692307693 1.153846153846154; 1.5384615384615385 0.9230769230769231 0.7692307692307693 0.3076923076923077 0.46153846153846156; 0.7692307692307693 0.46153846153846156 0.38461538461538464 0.15384615384615385 0.23076923076923078] total = sum(expectation) = 52.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 0.861111 seconds (3.40 M allocations: 218.201 MiB, 9.77% gc time)
plot_sim_prod_Multinomial(M=2N, q=p; binstep=1)
expectation = M * q' = [3.076923076923077 10.769230769230768 3.8461538461538463 1.5384615384615385 0.7692307692307693; 1.8461538461538463 6.461538461538462 2.307692307692308 0.9230769230769231 0.46153846153846156; 1.5384615384615385 5.384615384615384 1.9230769230769231 0.7692307692307693 0.38461538461538464; 0.6153846153846154 2.1538461538461537 0.7692307692307693 0.3076923076923077 0.15384615384615385; 0.9230769230769231 3.230769230769231 1.153846153846154 0.46153846153846156 0.23076923076923078] total = sum(expectation) = 52.0 (r, c) = size(expectation) = (5, 5) df = df_chisq(expectation) = 16 0.840041 seconds (3.40 M allocations: 218.201 MiB, 10.17% gc time)
より詳細な比較については以下を参照せよ:
@show M = [10, 15]
@show q = [0.1, 0.9]
P = param_indep(M/sum(M), q)
M = [10, 15] = [10, 15] q = [0.1, 0.9] = [0.1, 0.9]
2×2 Array{Float64,2}: 0.04 0.36 0.06 0.54
E = round.(Int, 2M*q')
display("text/html", raw"$\text{期待値} = " * sympy.latex(Sym.(E)) * raw"$")
plot_sim_Poisson(λ=50, P=P, binstep=0.2)
expectation = λ * P = [2.0000000000000004 18.000000000000004; 3.0 27.0] total = sum(expectation) = 50.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.563940 seconds (3.10 M allocations: 137.330 MiB, 6.30% gc time)
plot_sim_Multinomial(n=50, P=P, binstep=0.2)
expectation = n * P = [2.0000000000000004 18.000000000000004; 3.0 27.0] total = sum(expectation) = 50.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.366426 seconds (3.10 M allocations: 137.329 MiB, 12.20% gc time)
plot_sim_prod_Multinomial(M=2M, q=q, binstep=0.2)
expectation = M * q' = [2.0 18.0; 3.0 27.0] total = sum(expectation) = 50.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.353167 seconds (3.10 M allocations: 146.485 MiB, 11.12% gc time)
plot_sim_prod_Multinomial(M=round.(Int, sum(2M)*q), q=M/sum(M), binstep=0.2)
expectation = M * q' = [2.0 3.0; 18.0 27.0] total = sum(expectation) = 50.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.365518 seconds (3.10 M allocations: 146.485 MiB, 11.57% gc time)
E = round.(Int, 4M*q')
display("text/html", raw"$\text{期待値} = " * sympy.latex(Sym.(E)) * raw"$")
plot_sim_Poisson(λ=100, P=P, binstep=0.2)
expectation = λ * P = [4.000000000000001 36.00000000000001; 6.0 54.0] total = sum(expectation) = 100.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.684127 seconds (3.10 M allocations: 137.330 MiB, 13.31% gc time)
plot_sim_Multinomial(n=100, P=P, binstep=0.2)
expectation = n * P = [4.000000000000001 36.00000000000001; 6.0 54.0] total = sum(expectation) = 100.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.352802 seconds (3.10 M allocations: 137.329 MiB, 11.88% gc time)
plot_sim_prod_Multinomial(M=4M, q=q, binstep=0.2)
expectation = M * q' = [4.0 36.0; 6.0 54.0] total = sum(expectation) = 100.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.357124 seconds (3.10 M allocations: 146.485 MiB, 10.65% gc time)
plot_sim_prod_Multinomial(M=round.(Int, sum(4M)*q), q=M/sum(M), binstep=0.2)
expectation = M * q' = [4.0 6.0; 36.0 54.0] total = sum(expectation) = 100.0 (r, c) = size(expectation) = (2, 2) df = df_chisq(expectation) = 1 0.404579 seconds (3.10 M allocations: 146.485 MiB, 9.92% gc time)
rc 個のPoisson分布の直積:
p(A|Λ)=∏i,je−λijλaijijaij!,logp(A|Λ)=∑i,j(aijlogλij−λij−logaij!).パラメーター: Λ=[λij], λij は非負の実数.
独立性を満たすパラメーター: 正の実数 λ と非負の実数 μi, νj たちで ∑iμi=∑jνj=λ を満たすもの. 上の λij との関係は
λij=μiνjλ.このとき
λ=∑i,jλij,μi=∑jλij,νi=∑iλij.対数尤度
L=logp(A|Λ)=∑i,j(aijlogλij−λij−logaij!)を最大にするパラメーター λij=ˆλij を求めよう.
∂L∂λij=aijλij−1より,
ˆλij=aij.独立性 λij=μiνj/λ, λ=∑i,jλij を満たすパラメーターに制限した場合の対数尤度を最大にするパラメーター λ=˜λ, μi=˜μi, νj=˜νj, ˜λij=˜μi˜νj/˜λ をLagrangeの未定乗数法で求めよう. そのために
M=∑i,j(aij(logμi+logνj−logλ)−μiνjλ)−α(∑iμi−λ)−β(∑jνj−λ)とおくと,
∂M∂α=−∑iμi+λ,∂M∂β=−∑jνj+λ,∂M∂λ=1−∑i,jaijλ+α+β,∂M∂μi=∑jaijμi−1−α,∂M∂νj=∑iaijνj−1−βなので,
˜λ=∑i,jaij,˜μi=∑jaij,˜νj=∑jaij,˜λij=˜μi˜νj˜λ.この場合には α=β=0 となる.
rc 個のPoisson分布の直積モデルの場合の対数尤度比は ˆΛ=[ˆλij]=[aij]=A, ˜Λ=[˜λij] とおくとき, 以下のようになる:
G=2(logp(A|ˆΛ)−logp(A|˜Λ))=2∑i,jaijlogaij˜λij.ここで ∑i,jaij=∑i,j˜λij を使った.
分割表 A=[aij] が独立性を満たすパラメーターに対する rc 個のPoisson分布の直積に従う確率変数であるとき, Wilksの定理より(もしくは直接的な計算によって), G およびそれを近似する
X2=∑i,j(aij−˜λij)2˜λijはサンプルサイズを大きくするとき漸近的に自由度 (r−1)(c−1) のχ²分布に従う.
分割表の制限: ∑i,jaij=n が一定.
rc 項分布:
p(A|n,P)=n!nn∏i,jλaijijaij!,logp(A|n,P)=∑i,j(aijlogλij−logaij!)+logn!−nlogn.パラメーター: 非負の実数 λij で ∑i,jλij=n を満たすもの. ただし P=[pij] と λij の関係は λij=npij.
独立性を満たすパラメーター: 非負の実数 μi, νj 達で ∑iμi=∑jνj=n を満たすもの. ただし λij との関係は
λij=μiνjn.対数尤度を最大にする総和が n のパラメーター λij=ˆλij を求めるためにLagrangeの未定乗数法を使う. そのために
L=∑i,jaijlogλij−α(∑i,jλij−n)とおくと,
∂L∂α=−∑i,jλij+n,∂L∂λij=aijλij−αなので,
ˆλij=aij.独立性 λij=μiνj/n, ∑i,jλij=∑i,jaij=n を満たすパラメーターに制限した場合の対数尤度を最大にするもの μi=˜μi, νj=˜νj, ˜λij=˜μi˜νj/n をLagrangeの未定乗数法で求めよう. そのために
M=∑i,jaij(logμi+logνj)−α(∑iμi−n)−β(∑iνi−n)とおくと,
∂M∂α=−∑iμi+n,∂M∂β=−∑jνi+n,∂M∂μi=∑jaijμi−α,∂M∂νj=∑iaijνj−β.なので,
˜μi=∑jaij,˜νj=∑iaij,˜λij=˜μi˜νjn.この場合には α=β=1 になる.
rc 項分布モデルの場合の対数尤度比は ˆΛ=[ˆλij]=[aij]=A, ˜Λ=[˜λij], ˆP=ˆΛ/n, ˜P=˜Λ/n とおくとき, 以下のようになる:
G=2(logp(A|n,ˆP)−logp(A|n,˜P))=2∑i,jaijlogaij˜λij.分割表 A=[aij] が独立性を満たすパラメーターに対する rc 項分布に従う確率変数であるとき, Wilksの定理より(もしくは直接的な計算によって), G およびそれを近似する
X2=∑i,j(aij−˜λij)2˜λijはサンプルサイズを大きくするとき漸近的に自由度 (r−1)(c−1) のχ²分布に従う.
分割表の制限: ∑jaij=μi がすべて一定. n=∑iμi, μ=(μ1,…,μr) とおく.
r 個の c 項分布の直積:
p(A|μ,Q)=∏iμi!μμii⋅∏i,jλaijijaij!,logp(A|μ,Q)=∑i,j(aijlogλij−logaij!)+∑i(logμi!−μilogμi).パラメーター: 非負の実数 λij 達で ∑jλij=μi を満たすもの. ただし, Q=[qij] と λij の関係は λij=μiqij.
独立性を満たすパラメーター: 非負の実数 νj 達で ∑jνj=n を満たすもの. ただし λij との関係は
λij=μiνjn.∑jλij=μi がすべて一定という条件を満たすパラメーター λij で対数尤度を最大にするもの λij=˜λij を求めるためにLagrangeの未定乗数法を使おう. そのために
L=∑i,jaijlogλij−∑iαi(∑jλij−μi)とおくと,
∂L∂αi=−∑jλij+μi,∂L∂λij=aijλij−αiなので
˜λ=aij.この場合には ∑jaij=μi より αi=1 となる.
独立性 λij=μiνj/n, ∑jλij=∑jaij=μi を満たすパラメーターに制限した場合の対数尤度を最大にするもの νj=˜νj, ˜λij=μi˜νj/n をLagrangeの未定乗数法で求めよう. そのために
M=∑i,jaijlogνj−α(∑jνj−n)とおくと
∂L∂α=−∑jνj+n,∂L∂νj=∑iaijνj−αなので,
˜νj=∑iaij,˜λij=μi˜νjn.r 個の c 項分布の直積モデルの場合の対数尤度比は ˆΛ=[ˆλij]=[aij]=A, ˜Λ=[˜λij], ˆQ=[ˆqij], ˆqij=ˆλij/μi, ˜Q=[˜qij], ˜qij=˜λij/μi=˜νj/n とおくとき, 以下のようになる:
G=2(logp(A|μ,ˆQ)−logp(A|μ,˜Q))=2∑i,jaijlogaij˜λij.分割表 A=[aij] が独立性を満たすパラメーターに対する r 個の c 項分布の直積に従う確率変数であるとき, Wilksの定理より(もしくは直接的な計算によって), この G およびそれを近似する
X2=∑i,j(aij−˜λij)2˜λijはサンプルサイズを大きくするとき漸近的に自由度 (r−1)(c−1) のχ²分布に従う.
分割表の制限: ∑jaij=μi と ∑iaij=νj がすべて一定. n=∑iμi=∑jνj, μ=(μ1,…,μr), ν=(ν1,…,νc) とおく.
周辺度数がすべて固定されている場合の分割表の確率分布:
p(A|μ,ν,Λ)=1Z(Λ)∏i,jλaijijaij!,Z(Λ)=∑A∏i,jλaijijaij!.パラメーター: 正の実数 λij で ∑jλij=μi, ∑iλij=νj を満たすもの.
独立性を満たすパラメーター: この場合には独立性を満たすパラメーターは μi, νj から次のように一意に決まってしまう:
λij=μiνjn.このとき, 分割表の確率分布は次の形になる:
p(A|μ,ν,Λ)=p(A|μ,ν)=∏iμi!⋅∏jνj!n!∏i,jaij!=c∏j=1(νja1j,…,arj)(nμ1,…,μr).分割表 A=[aij] がこの独立性を満たすパラメーター λij=μiνj/n に対する周辺度数 μi,νj 達がすべて固定されている場合の分割表の確率分布に従う確率変数であるとき,
G = 2\sum_{i,j}a_{ij}\log\frac{a_{ij}}{\lambda_{ij}}, \quad X^2 = \sum_{i,j}\frac{(a_{ij}-\lambda_{ij})^2}{\lambda_{ij}}がサンプルサイズを大きくするとき漸近的に自由度 (r-1)(c-1) のχ²分布に従うはすでに上の方で示してあった.