Advertisement
olivercromwell

Untitled

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