Created
May 23, 2018 20:17
-
-
Save RobBlackwell/e44572f4665651a0fd1e5dd841f7d457 to your computer and use it in GitHub Desktop.
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
""" | |
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