File size: 2,778 Bytes
22e9602 | 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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import numpy as np
from scipy.stats import entropy
def forecastabilty(ts):
"""Forecastability Measure.
Args:
ts: time series
Returns:
1 - the entropy of the fourier transformation of
time series / entropy of white noise
"""
ts = (ts - ts.min())/(ts.max()-ts.min()+0.1)
# fourier_ts = np.fft.rfft(ts).real
fourier_ts = abs(np.fft.rfft(ts))
fourier_ts = (fourier_ts - fourier_ts.min()) / (
fourier_ts.max() - fourier_ts.min())
fourier_ts /= fourier_ts.sum()
entropy_ts = entropy(fourier_ts)
fore_ts = 1-entropy_ts/(np.log(len(ts)))
if np.isnan(fore_ts):
return 0
return fore_ts
def forecastabilty_moving(ts, window, jump=1):
"""Calculates the forecastability of a moving window.
Args:
ts: time series
window: length of slices
jump: skipped step when taking subslices
Returns:
a list of forecastability measures for all slices.
"""
# ts = Trend(ts).detrend()
if len(ts) <= 25:
return forecastabilty(ts)
fore_lst = np.array([
forecastabilty(ts[i - window:i])
for i in np.arange(window, len(ts), jump)
])
fore_lst = fore_lst[~np.isnan(fore_lst)] # drop nan
return fore_lst
class Trend():
"""Trend test."""
def __init__(self, ts):
self.ts = ts
self.train_length = len(ts)
self.a, self.b = self.find_trend(ts)
def find_trend(self, insample_data):
# fit a linear regression y=ax+b on the time series
x = np.arange(len(insample_data))
a, b = np.polyfit(x, insample_data, 1)
return a, b
def detrend(self):
# remove trend
return self.ts - (self.a * np.arange(0, len(self.ts), 1) + self.b)
def inverse_input(self, insample_data):
# add trend back to the input part of time series
return insample_data + (self.a * np.arange(0, len(self.ts), 1) + self.b)
def inverse_pred(self, outsample_data):
# add trend back to the predictions
return outsample_data + (
self.a * np.arange(self.train_length,
self.train_length + len(outsample_data), 1) + self.b)
def seasonality_test(original_ts, ppy):
"""Seasonality test.
Args:
original_ts: time series
ppy: periods per year/frequency
Returns:
boolean value: whether the TS is seasonal
"""
s = acf(original_ts, 1)
for i in range(2, ppy):
s = s + (acf(original_ts, i)**2)
limit = 1.645 * (np.sqrt((1 + 2 * s) / len(original_ts)))
return (abs(acf(original_ts, ppy))) > limit
def acf(ts, k):
"""Autocorrelation function.
Args:
ts: time series
k: lag
Returns:
acf value
"""
m = np.mean(ts)
s1 = 0
for i in range(k, len(ts)):
s1 = s1 + ((ts[i] - m) * (ts[i - k] - m))
s2 = 0
for i in range(0, len(ts)):
s2 = s2 + ((ts[i] - m)**2)
return float(s1 / s2) |