Skip to content

Instantly share code, notes, and snippets.

@mjul
Created October 14, 2011 14:45
Show Gist options
  • Save mjul/1287314 to your computer and use it in GitHub Desktop.
Save mjul/1287314 to your computer and use it in GitHub Desktop.
Plot the distribution of a set of measurements in a histogram and the corresponding normal distribution (Clojure Incanter)
(defn histogram-with-normal-distribution [ds]
"Produce a histgoram of the single-dimensional dataset ds and the corresponding normal distribution."
(with-data ds
(let [x-mean (mean ds)
x-sd (sd ds)
x-min (apply min ds)
x-max (apply max ds)
h (doto (histogram ds :nbins 100 :density true)
(set-stroke-color java.awt.Color/blue)
(set-title "Distribution of latencies")
(add-subtitle (format "Mean = %4.1f ms, Std.dev. = %4.2f" x-mean x-sd))
(set-x-label "latency (ms)")
(set-y-label "share of messages")
(add-function (fn [x] (pdf-normal x :mean x-mean :sd x-sd)) x-min x-max :series-label "PDF-normal")
(add-histogram (sample-normal 1000 :mean x-mean :sd x-sd) :nbins 100 :series-label "normal distribution"))]
(view h)
h)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment