Created
April 26, 2016 17:05
-
-
Save aodin/c208f067b82c48bc6a548100b3f2b65b to your computer and use it in GitHub Desktop.
Plotting the Gamma distribution sampling from it as a normalized histogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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