julia で t 分布と正規分布の確率密度函数 PDF を描いてみよう

julia で t 分布と正規分布の確率密度函数 probability density function、PDF を描いてみましょう。

確率密度函数は、Distributions パッケージの中にあるので、Distributitons を

Pkg.add("Distributitons")
で、インストールしましょう。

正規分布も t 分布も Univariable Distributions の中の Continuous Distributions というところで定義されています。

正規分布は、Normal() 、自由度 n の t 分布は、TDist(n) で良いようです。

using Distributions
using PyCall
using PyPlot
@pyimport seaborn as sns

x=collect(-4:0.1:4)

fig, ax = subplots()

for i in [3,5,10,20]
  y = pdf.(TDist(i), x)
   plot(x, y, alpha=0.6, label="n = $i")
end

y = pdf.(Normal(), x)
plot(x, y, alpha=0.6, label="Normal Distribution")

ax[:legend]() 

julia 0.6 と、PyCall.pyversion v"2.7.13" だと、これで、下記のような図ができます。

n が増えると、正規分布に近付くの図です。

x は、[-4:0.1:4] では動かずに、collect()の中に入れると動きます。

x=linspace(-4:4,801) でも動きます。

fig, ax = subplots() という1行を入れると、label を貯めて、ax[:legend]() で1ヶ所に描画してくれます。

ax[:plot](...) という描き方では、julia 0.6 では上手く動きませんでした。plot(...) と単純な方が良いようです。

ちなみに、seaborn は趣味なので、不要ですね。

たくさん重ねて描きたい時には、pyplot は便利ですね。

B! LINE