forked from srk-srinivasan/Permutation-Entropy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complexity.R
28 lines (21 loc) · 909 Bytes
/
complexity.R
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
# Function to compute complexity of a given time series
# Input (1 argument. Null argument not valid)
# op = Ordinal pattern computed using the function ordinal_pattern
# op (type=numeric vector)
# Output is complexity (type=numeric)
complexity<-function(op){
# Comp_JS = Q_o * JSdivergence * pe
# Normalizing constant Q_o
# JSdivergence = Jensen-Shannon divergence
# pe = permutation entopry
pe<-permu_entropy(op)
constant1<- (0.5+((1 - 0.5)/length(op)))*log(0.5+((1 - 0.5)/length(op)))
constant2<-((1 - 0.5)/length(op))*log((1 - 0.5)/length(op))*(length(op) - 1)
constant3<- 0.5*log(length(op))
Q_o<- -1/(constant1+constant2+constant3)
temp_op_prob<-op/sum(op)
temp_op_prob2<-(0.5*temp_op_prob)+(0.5*(1/length(op)))
JSdivergence<-(entropy::entropy(temp_op_prob2) - 0.5 * entropy::entropy(temp_op_prob) - 0.5 * log(length(op)))
Comp_JS = Q_o * JSdivergence * pe
return(Comp_JS)
}