normalize(clojure.contrib.probabilities.finite-distributions)

Convert a weight map (e.g. a map of counter values) to a distribution by multiplying with a normalization factor. If the map has a key :total, its value is assumed to be the sum over all the other values and it is used for normalization. Otherwise, the sum is calculated explicitly. The :total key is removed from the resulting distribution.

; clojure/contrib/probabilities/finite_distributions.clj:72
(defn normalize
  [weights]
  (let [total (:total weights)
	w (dissoc weights :total)
	s (/ 1 (if (nil? total) (reduce + (vals w)) total))]
    (scale-by w s)))

Copyright (c) Rich Hickey. All rights reserved.

The use and distribution terms for this software are covered by the Eclipse Public License 1.0, which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.