散布図を見ただけで,ピアソンの積率相関係数のだいたいの値をいえるようになると,経験値が上がるかもしれない...
作成する Julia プログラムは以下の通り。
using LinearAlgebra, StatsBase, Statistics
using Plots, StatsPlots, PlotThemes
using Random
function pearsoncorrelationcoefficient(n)
ρ = vcat([0:0.1:1, 0.9:-0.1:-1, -0.9:0.1:-0.1]...);
x = randn(n, 2);
t = fit(ZScoreTransform, x, dims = 1);
x = StatsBase.transform(t, x);
r = cor(x);
solver = inv(r);
vect, valu, junk = svd(r);
coeff = solver * (sqrt.(valu) .* vect')';
theme(:gruvbox_light)
pyplot(grid=false, xlims=(-3.9, 3.9), ylims=(-3.9, 3.9),
color=:blue, markerstrokewidth=0, alpha=0.4,
aspect_ratio=1, label="", size=(400, 400))
Random.seed!(12345);
r = ones(2, 2)
anim = @animate for ri in ρ
r[1,2] = r[2,1] = abs(ri) == 1 ? sign(ri)*(1-eps()) : ri
z = x * coeff * cholesky(r).U
covellipse([0,0], r; n_std=1.96, color=:red, alpha=0.1)
scatter!(z[:, 1], z[:, 2], showaxis=false, ticks=false,
grid=false, title="Pearson's correlation coefficient: r")
annotate!(0, -3.5, text("r = $ri", 10))
end
gif(anim, "pearsoncorrelationcoefficient.gif", fps=2)
end
d = pearsoncorrelationcoefficient(1500)