裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

散布図とピアソンの積率相関係数の GIF アニメーション

2021年06月20日 | ブログラミング

散布図を見ただけで,ピアソンの積率相関係数のだいたいの値をいえるようになると,経験値が上がるかもしれない...

作成する 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)

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« Julia でアニメーション GIF... | トップ | 二項分布が正規近似される様... »
最新の画像もっと見る

コメントを投稿

ブログラミング」カテゴリの最新記事