Rolling Shannon Entropy of returns.
Measures the uncertainty/disorder in the distribution of returns.
High entropy indicates random/noise regime.
Low entropy indicates predictable/trend regime.
Source code in src/quantmaster/features/entropy.py
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 | def shannon_entropy(
data: pd.DataFrame | pd.Series,
*,
window: int = 60,
bins: int = 10,
price_col: str = "close",
log_returns: bool = True,
) -> pd.Series:
"""
Rolling Shannon Entropy of returns.
Measures the uncertainty/disorder in the distribution of returns.
High entropy indicates random/noise regime.
Low entropy indicates predictable/trend regime.
"""
window = validate_positive_int(window, name="window")
bins = validate_positive_int(bins, name="bins")
price = get_price_series(data, price_col=price_col).astype(float)
price = price.where(price > 0)
if log_returns:
x = np.log(price).diff()
else:
x = price.pct_change()
# Pre-allocate output
out = pd.Series(np.nan, index=price.index, dtype=float)
out.name = f"shannon_entropy_{window}_{bins}"
if len(x) < window:
return out
x_arr = x.to_numpy(dtype=float)
windows = np.lib.stride_tricks.sliding_window_view(x_arr, window_shape=window)
entropies = np.full(windows.shape[0], np.nan, dtype=float)
for i in range(windows.shape[0]):
w = windows[i]
w = w[np.isfinite(w)]
if w.size < 2:
continue
hist, _ = np.histogram(w, bins=bins, density=True)
probs = hist / np.sum(hist)
probs = probs[probs > 0]
if probs.size > 0:
entropies[i] = -np.sum(probs * np.log2(probs))
out.iloc[window - 1 :] = entropies
return out
|