Skip to content

Instantly share code, notes, and snippets.

@robert-spurrier
Created February 17, 2016 21:15
Show Gist options
  • Select an option

  • Save robert-spurrier/bdba86f53db27350d983 to your computer and use it in GitHub Desktop.

Select an option

Save robert-spurrier/bdba86f53db27350d983 to your computer and use it in GitHub Desktop.
Planck Script (ClojureScript) To Generate Common Confusion Matrix Derivations
#!/usr/local/bin/planck
(ns confusion_matrix_derivations.core
(:require [planck.core :refer [*command-line-args*]]
[cljs.reader :refer [read-string]]
[cljs.pprint :refer [pprint]]))
(defn sensitivity
"Sensitivity, or True Positive Rate"
[tp fln]
(/ tp (+ tp fln)))
(defn specificity
"Specificity, or True Negative Rate"
[tn fp]
(/ tn (+ tn fp)))
(defn ppv
"Positive Predictive Value, or Precision"
[tp fp]
(/ tp (+ tp fp)))
(defn npv
"Negative Predictive Value"
[tn fln]
(/ tn (+ tn fln)))
(defn fpr
"Fall-out, or False Positive Rate"
[tn fp]
(- 1 (specificity tn fp)))
(defn fnr
"FNR, False Negative Rate"
[tp fln]
(- 1 (sensitivity tp fln)))
(defn fdr
"FDR, False Discovery Rate"
[tp fp]
(- 1 (ppv tp fp)))
(defn accuracy
"Accuracy"
[tp tn fp fln]
(/ (+ tp tn)
(+ tp tn fp fln)))
(defn f1
"F1 Score"
[tp fp fln]
(/ (* 2 tp)
(+ (* 2 tp) fp fln)))
(defn mcc
"Matthews Correlation Coefficient"
[tp tn fp fln]
(let [numerator (- (* tp tn)
(* fp fln))
denominator (.sqrt js/Math (* (+ tp fp)
(+ tp fln)
(+ tn fp)
(+ tn fln)))]
(/ numerator denominator)))
(defn -main [tp tn fp fln]
(let [tp (read-string tp)
tn (read-string tn)
fp (read-string fp)
fln (read-string fln)]
{:sensitivity (sensitivity tp fln)
:specificity (specificity tn fp)
:positive-predictive-value (ppv tp fp)
:negative-predictive-value (npv tn fln)
:false-positive-rate (fpr tn fp)
:false-negative-rate (fnr tp fln)
:false-discovery-rate (fdr tp fp)
:accuracy (accuracy tp tn fp fln)
:f1 (f1 tp fp fln)
:mcc (mcc tp tn fp fln)}))
(pprint (apply -main *command-line-args*))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment