-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathexample3TeContinuousDataKernel.clj
executable file
·66 lines (56 loc) · 2.88 KB
/
example3TeContinuousDataKernel.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
;
; Java Information Dynamics Toolkit (JIDT)
; Copyright (C) 2012, Joseph T. Lizier
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program. If not, see <http://www.gnu.org/licenses/>.
;
; = Example 3 - Transfer entropy on continuous data using kernel estimators =
; Simple transfer entropy (TE) calculation on continuous-valued data using the (box) kernel-estimator TE calculator.
; Import relevant classes:
(import infodynamics.measures.continuous.kernel.TransferEntropyCalculatorKernel)
(import java.util.Random)
(def rg (Random.))
(let
[numObservations 1000
covariance 0.4
; Generate some random normalised data.
sourceArray (double-array (take numObservations (repeatedly #(.nextGaussian rg))))
destArray (double-array
(cons 0
(map +
(map (partial * covariance) (butlast sourceArray))
(map (partial * (- covariance 1)) (double-array (take (- numObservations 1) (repeatedly #(.nextGaussian rg))))) )))
sourceArray2 (double-array (take numObservations (repeatedly #(.nextGaussian rg))))
teCalc (TransferEntropyCalculatorKernel. )
]
; Set up the calculator
(.setProperty teCalc "NORMALISE" "true")
(.initialise teCalc 1 0.5) ; Use history length 1 (Schreiber k=1), kernel width of 0.5 normalised units
(.setObservations teCalc sourceArray destArray)
; For copied source, should give something close to expected value for correlated Gaussians:
; TODO The analytic result quoted here isn't quite right, see e.g. octave demos (can't be bothered fixing here...)
(println "TE result " (.computeAverageLocalOfObservations teCalc)
" expected to be close to " (/ (Math/log (/ 1 (- 1 (* covariance covariance)))) (Math/log 2))
" for these correlated Gaussians but biased upward")
(.initialise teCalc ) ; Initialise leaving the parameters the same
(.setObservations teCalc sourceArray2 destArray)
; For random source, it should give something close to 0 bits
(println "TE result " (.computeAverageLocalOfObservations teCalc)
" expected to be close to 0 bits for these uncorrelated Gaussians but will be biased upward")
; We can get insight into the bias by examining the null distribution:
(def nullDist (.computeSignificance teCalc 100))
(println "Null distribution for unrelated source and destination "
"(i.e. the bias) has mean " (.getMeanOfDistribution nullDist)
" and standard deviation " (.getStdOfDistribution nullDist))
)