裏 RjpWiki

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

標準正規分布の確率密度関数と分布関数を GIF アニメーションで表示する

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

収穫物:前の記事で,2つのグラフをまとめて gif ファイルにする方法がわからないとか,愚痴っていたけど,簡単だった。自己解決。

plt1 = foo1();
plt1 = bar1();
plt2 = baz2()
plt2 = boo()
plot(plt1, plt2, layout=(n, m))

とすればよいだけだった。

ということで,標準正規分布の確率密度関数 f(z) と 分布関数 F(z) の対応図をアニメ化した。

追記:p, 1-p の表示が間違ってました

途中の図

using Plots, PlotThemes, Rmath

function normaldistribution(; fps=7)
    round3(a) = round(a, digits=3)
    z = -3.5:0.1:4.5
    theme(:lime)
    pyplot(grid=false, label="", size=(500, 600))
    y1 = dnorm.(z)
    y2 = pnorm.(z)
    anim = @animate for i = 1:length(z)
        z[i] > 3 && break
        plt1 = plot(z, y1)
        y = dnorm.(z)
        plt1 = plot!(vcat(z[1:i], z[i]), vcat(y1[1:i], y1[1]),
            seriestype=:shape, color=:red, alpha=0.8,
            tick_direction=:out, xlims=(-3.5, 3.5), ylims=(0, 0.4),
            xlabel="\$z\$", ylabel="\$f\$: Probability density function")
        plt1 = annotate!(-3, 0.3, text(" p=$(round3(pnorm(z[i])))", :left, 10, :white))
        plt1 = annotate!(3, 0.3, text("1-p=$(round3(pnorm(z[i], false)))", :right, 10, :white))

        plt1 = annotate!(z[i], 0.05, text(" z=$(z[i])", :left, 10, :white))
        plt2 = plot(z, y2)
        plot2 = plot!([z[i], z[i], z[1]], [0, y2[i], y2[i]], arrow=true)
        plt2 = scatter!([z[i]], [y2[i]],
            tick_direction=:out, xlims=(-3.5, 3.5), ylims=(0, 1.05),
            xlabel="\$z\$", ylabel="\$F\$: Distribution function")
        plt2 = annotate!(-3, y2[i] < 0.14 ? 0.14 : y2[i]+0.05,
            text("F($(z[i]))=$(round3(y2[i]))", :left, :bottom, 10, :white))
        plt2 = annotate!(z[i], 0.12, text(" z=$(z[i])", :left, 10, :white))
        plot(plt1, plt2, layout=(2, 1))
        z[i] == 1.6 && savefig("mid.png")
    end
    gif(anim, "normaldistribution.gif", fps=fps)
end

normaldistribution(;fps=1)

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

コメントを投稿

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