Skip to content

Instantly share code, notes, and snippets.

@RobBlackwell
Created May 23, 2018 20:17
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 RobBlackwell/e44572f4665651a0fd1e5dd841f7d457 to your computer and use it in GitHub Desktop.
Save RobBlackwell/e44572f4665651a0fd1e5dd841f7d457 to your computer and use it in GitHub Desktop.
"""
freedman_diaconis(x)
Estimates the required bin width for the distribution x.
Freedman, D. and Diaconis, P., 1981. On the histogram as a density
estimator: L 2 theory. Zeitschrift für Wahrscheinlichkeitstheorie und
verwandte Gebiete, 57(4), pp.453-476.
"""
function freedman_diaconis(x)
2 * iqr(x) / length(x)^(1/3)
end
## colors not working well
function compare_histograms(dists; labels = nothing, colors= cgrad(:inferno), nbins = nothing)
if labels == nothing
labels = ["histogram $i" for i in 1:length(dists)]
end
p= plot()
mmin = floor(minimum([minimum(x) for x in dists]))
mmax = ceil(maximum([maximum(x) for x in dists])) + 1
if nbins == nothing
h = minimum([freedman_diaconis(x) for x in dists])
nbins = (mmax - mmin) / h
else
h = (mmax - mmin) / nbins
end
println("step $h")
println("nbins $nbins")
for i in 1:length(dists)
a = fit(Histogram, dists[i], mmin:h:mmax, closed=:left)
plot!(a.edges[1][1:end-1], a.weights, color=colors[i],label=labels[i])
end
return p
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment