Skip to content

Instantly share code, notes, and snippets.

@aodin
Created April 26, 2016 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aodin/c208f067b82c48bc6a548100b3f2b65b to your computer and use it in GitHub Desktop.
Save aodin/c208f067b82c48bc6a548100b3f2b65b to your computer and use it in GitHub Desktop.
Plotting the Gamma distribution sampling from it as a normalized histogram
# Plotting the Gamma distribution and sample as a normalize histogram
using Distributions
using Gadfly
pdf(d::Gamma, x) = (x ^ (d.α - 1) * e ^ (-x / d.α)) / (d.θ ^ d.α * gamma(d.α))
n = 500
g = Gamma(2., 2.)
r = rand(g, n)
bins, counts = hist(r, 100)
# normalize on area of the histogram
counts_norm = counts / sum(counts * step(bins))
plot(
layer(x=bins, y=[pdf(g, l + step(bins) / 2.) for l in bins], Geom.line, Theme(default_color=colorant"black")),
layer(x=bins[1:end-1], y=counts_norm, Geom.bar),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment