karajan9 / statisticalrethinking

Working through Statistical Rethinking by Richard McElreath
MIT License
11 stars 1 forks source link

re 4.15 (https://github.com/karajan9/statisticalrethinking/blob/master/scripts/snippet_04_07.jl) #2

Open ym-han opened 4 years ago

ym-han commented 4 years ago

Might be worth noting that given how we're dealing with normals (linear transform of normals is also normal), another way to get the prior predictive distribution would be:

function sample_h(μ, σ, n=10_000)
  sample_μ = rand(μ, n)
  sample_σ = rand(σ, n)

  ppd = sample_μ + sample_σ.*randn(n)
end

The crucial line is the ppd line; I included the rest because I was worried about making typos if I changed it to your notation/nomenclature.

karajan9 commented 4 years ago

I'm not quite sure this is correct, shouldn't sample_μ also be drawn from a normal distribution, Normal(178, 100)?

While I think this would work here I'm not sure what I think about this in general since it won't generalize to other distributions while drawing from a Distribution should.

ym-han commented 4 years ago

Yes, I forgot to add

μ = Normal(178, 20) # for 4.14
σ = Uniform(0, 50)
μ_big_sd = Normal(178, 100) # for 4.15
ppd_new_μ = sample_h(μ_big_sd, σ)

I'll try to check if this works later today

ym-han commented 4 years ago

Also, just to clarify: the key proposition I'm relying on with the sampling is this.

Let Y = μ+σZ, where Z∼N(0,1). Then Y∼N(μ,σ^2).