Created
September 7, 2012 21:30
-
-
Save danhammer/3669839 to your computer and use it in GitHub Desktop.
entropy measures
This file contains hidden or 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
(ns forma.trends.entropy | |
(:use [forma.matrix.utils] | |
[clojure.math.numeric-tower :only (sqrt floor abs expt)] | |
[clojure-csv.core]) | |
(:require [forma.utils :as utils] | |
[incanter.core :as i] | |
[incanter.stats :as s] | |
[incanter.charts :as c] | |
[forma.trends.filter :as filter])) | |
(defn- ordinal-idx | |
"Returns a sequence of indices that rank the values of the supplied | |
time series in ascending order. If there are equal values, the | |
lexicographic ordering kicks in and the order at which the values | |
appear is used to order the indices. | |
Example: | |
(take 5 ndvi) => (6217 8599 7074 8437 8471) | |
(ordinal-idx (take 5 ndvi)) => (0 2 3 4 1)" | |
[sub-ts] | |
(let [indexed-vector (map-indexed vector sub-ts)] | |
(map first | |
(sort-by second indexed-vector)))) | |
(defn- permutation-count | |
"Returns a map of the ordinal sequences of length `D` and their | |
count; note that the offset is fixed at 1" | |
[D ts] | |
(let [subs (partition D 1 ts)] | |
(frequencies (map ordinal-idx subs)))) | |
(defn- log-fn | |
[x] | |
(* x (i/log2 x))) | |
(defn- to-freq | |
"Returns the normalized frequency of the supplied column" | |
[coll] | |
(let [total (reduce + coll)] | |
(map #(/ % total) coll))) | |
(defn permutation-entropy | |
"Normalixed permutation entropy based on the Shannon entropy | |
distribution" | |
[D ts] | |
(let [pi-seq (to-freq (vals (permutation-count D ts))) | |
scale-by (* -1 (/ 1 (i/log2 (i/factorial D))))] | |
(* scale-by | |
(reduce + (map log-fn pi-seq))))) | |
(defn kl-entropy | |
"Returns the normalized Kullback-Leibler entropy (KLE) information | |
measure, which quantifies the distance between the orginal pattern | |
probability distribution of `ts` and the uniform distribution" | |
[D ts] | |
(- 1 (permutation-entropy D ts))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment