Coder Social home page Coder Social logo

clj-statistics-fns's Introduction

clj-statistics-fns

Build Status Clojars Project

A simple Clojure library for calculating some common statistics of data sets: minimum, maximum, average, median and k-th percentile.

Usage

First, install by adding to your dependencies array:

[mnylen/clj-statistics-fns "0.1.0"]

Supported operations:

(require '[clj-statistics-fns.core :as s])

(s/average [1 2 3])
;; => 2

(s/median [1 2 3])
;; => 2

(s/kth-percentile 95 (range 0 101))
;; => 95

(s/minimum [1 2 3])
;; => 1

(s/maximum [1 2 3])
;; => 3

Collecting values

All functions in the clj-statistics-fns.core namespace accept normal Clojure collections, so you can just collect the values into a vector. However, clj-statistics-fns also comes with an memory optimized data structure more suited for collecting a lot of values falling within some finite range. If your data set has a lot of duplicate values, you should use the optimized data structure instead.

Sample use & supported operations:

(require '[clj-statistics-fns.sorted-values :as v])

;; create
(v/sorted-values 1 2 3)
;; => #object[clj_statistics_fns.sorted_values.SortedValues 0x77b707a SortedValues{1 1, 2 1, 3 1}]

(v/wrap [1 2 3])
;; => #object[clj_statistics_fns.sorted_values.SortedValues 0x56aa6084 SortedValues{1 1, 2 1, 3 1}]

;; add value
(v/add-value (v/sorted-values 1 2 3) 2)
;; => #object[clj_statistics_fns.sorted_values.SortedValues 0x407d4167 SortedValues{1 1, 2 2, 3 1}]

;; get frequency of a value
(v/frequency (v/sorted-values 3 2 1 2) 2)
;; => 2

;; count, nth
(count (v/sorted-values 1  2 3))
;; => 3

(nth (v/sorted-values 3 1 2) 0)
;; => 1

(let [values (v/sorted-values 2 1 3 1 2 3)]
  (map #(nth values %) (range (count values))))
;; => (1 1 2 2 3 3)

With these, you could implement ring middleware for collecting response times:

(require '[clj-statistics-fns.sorted-values :as v])

;; ring middleware for collecting response times:
(def ^:private response-times
  (atom (v/sorted-values)))

(defn collect-repsonse-times [handler]
  (fn [request]
    (let [start-time (System/currentTimeMillis)
          response (handler request)
          end-time (System/currentTimeMillis)
          response-time (- end-time start-time)]
      (swap! response-times v/add-value response-time)
      response))))

Then, somewhere else:

(require '[clj-statistics-fns.core :as s])

(s/median @response-times)
(s/average @response-times)
(s/kth-percentile 95 @response-times)

License

Copyright © 2018 Mikko Nylén

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.