Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Line # Hits Time Per Hit % Time Line Contents
- ==============================================================
- 162 @profile
- 163 def weighted_permutation_entropy(time_series, order=3, normalize=False):
- 164 """Calculate the Weighted Permutation Entropy.
- 165 Weighted permutation entropy is based on the regular permutation entropy,
- 166 but puts additional weight on those windows that show a high variability
- 167 in the initial time series.
- 168 Parameters
- 169 ----------
- 170 time_series : list or np.array
- 171 Time series
- 172 order : int
- 173 Order of permutation entropy
- 174 normalize : bool
- 175 If True, divide by log2(factorial(m)) to normalize the entropy
- 176 between 0 and 1. Otherwise, return the permutation entropy in bit.
- 177 Returns
- 178 -------
- 179 pe : float
- 180 Weighted Permutation Entropy
- 181 References
- 182 ----------
- 183 .. [1] Bilal Fadlallah et al. Weighted-permutation entropy: A complexity
- 184 measure for time series incorporating amplitude information
- 185 https://link.aps.org/accepted/10.1103/PhysRevE.87.022911
- 186 """
- 187 4474 9620.4 2.2 0.1 x = np.array(time_series)
- 188 4474 24392.2 5.5 0.3 hashmult = np.power(order, np.arange(order))
- 189 # Embed x and sort the order of permutations
- 190
- 191 4474 44677.1 10.0 0.5 embedded = _embed(x, order=order)
- 192 4474 186231.3 41.6 2.0 sorted_idx = embedded.argsort(kind='quicksort')
- 193 4474 591196.2 132.1 6.3 weights = np.var(util_rolling_window(x, order), 1)
- 194 4474 132936.5 29.7 1.4 hashval = (np.multiply(sorted_idx, hashmult)).sum(1)
- 195 4474 2772.1 0.6 0.0 mapping = {}
- 196 31318 229677.3 7.3 2.5 for i in np.unique(hashval):
- 197 26844 257770.9 9.6 2.8 mapping[i] = np.where(hashval == i)[0]
- 198 4474 6726.3 1.5 0.1 weighted_counts = dict.fromkeys(mapping)
- 199 31313 19757.1 0.6 0.2 for k, v in mapping.items():
- 200 26840 11780.6 0.4 0.1 weighted_count = 0
- 201 7112504 3190639.2 0.4 34.2 for i in v:
- 202 7085665 4504354.6 0.6 48.3 weighted_count += weights[i]
- 203 26839 15134.9 0.6 0.2 weighted_counts[k] = weighted_count
- 204 # Associate unique integer to each permutations
- 205 # Return the counts
- 206 # Use np.true_divide for Python 2 compatibility
- 207 4473 20884.7 4.7 0.2 weighted_counts_array = np.array(list(weighted_counts.values()))
- 208 4473 39333.5 8.8 0.4 p = np.true_divide(weighted_counts_array, weighted_counts_array.sum())
- 209 4473 26361.7 5.9 0.3 pe = -np.multiply(p, np.log2(p)).sum()
- 210 4473 2543.8 0.6 0.0 if normalize:
- 211 4473 14847.6 3.3 0.2 pe /= np.log2(math.factorial(order))
- 212 4473 2191.3 0.5 0.0 return pe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement