├── BTCUSDT3600.csv ├── LICENSE ├── README.md └── perm_entropy.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 neurotrader888 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PermutationEntropy 2 | A simple python implementation of permutation entropy. 3 | 4 | Includes hourly bitcoin data for testing. 5 | 6 | 7 | -------------------------------------------------------------------------------- /perm_entropy.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | import math 4 | import matplotlib.pyplot as plt 5 | 6 | def ordinal_patterns(arr: np.array, d: int) -> np.array: 7 | assert(d >= 2) 8 | fac = math.factorial(d); 9 | d1 = d - 1 10 | mults = [] 11 | for i in range(1, d): 12 | mult = fac / math.factorial(i + 1) 13 | mults.append(mult) 14 | 15 | # Create array to put ordinal pattern in 16 | ordinals = np.empty(len(arr)) 17 | ordinals[:] = np.nan 18 | 19 | for i in range(d1, len(arr)): 20 | dat = arr[i - d1: i+1] 21 | pattern_ordinal = 0 22 | for l in range(1, d): 23 | count = 0 24 | for r in range(l): 25 | if dat[d1 - l] >= dat[d1 - r]: 26 | count += 1 27 | 28 | pattern_ordinal += count * mults[l - 1] 29 | ordinals[i] = int(pattern_ordinal) 30 | 31 | return ordinals 32 | 33 | def permutation_entropy(arr: np.array, d:int, mult: int) -> np.array: 34 | fac = math.factorial(d) 35 | lookback = fac * mult 36 | 37 | ent = np.empty(len(arr)) 38 | ent[:] = np.nan 39 | ordinals = ordinal_patterns(arr, d) 40 | 41 | for i in range(lookback + d - 1, len(arr)): 42 | window = ordinals[i - lookback + 1 :i+1] 43 | 44 | # Create distribution 45 | freqs = pd.Series(window).value_counts().to_dict() 46 | for j in range(fac): 47 | if j in freqs: 48 | freqs[j] = freqs[j] / lookback 49 | 50 | # Calculate entropy 51 | perm_entropy = 0.0 52 | for k, v in freqs.items(): 53 | perm_entropy += v * math.log2(v) 54 | 55 | # Normalize to 0-1 56 | perm_entropy = -1. * (1. / math.log2(fac)) * perm_entropy 57 | ent[i] = perm_entropy 58 | 59 | return ent 60 | 61 | if __name__ == '__main__': 62 | data = pd.read_csv('BTCUSDT3600.csv') 63 | data['date'] = data['date'].astype('datetime64[s]') 64 | data = data.set_index('date') 65 | 66 | 67 | plt.style.use('dark_background') 68 | data['perm_entropy'] = permutation_entropy(data['close'].to_numpy(), 3, 28) 69 | data['vol_perm_entropy'] = permutation_entropy(data['volume'].to_numpy(), 3, 28) 70 | 71 | fig, ax1 = plt.subplots() 72 | np.log(data['close']).plot(ax=ax1, color='green', label='Close') 73 | ax2 = ax1.twinx() 74 | data['perm_entropy'].plot(ax=ax2, color='white', alpha=0.75, label='Perm Entropy (Close)') 75 | data['vol_perm_entropy'].plot(ax=ax2, color='yellow', alpha=0.75, label='Perm Entropy (Volume)') 76 | ax1.legend() 77 | plt.legend() 78 | plt.show() 79 | 80 | 81 | --------------------------------------------------------------------------------