Last active
May 10, 2019 08:27
-
-
Save vikramsoni2/e33fae8995d56c64dae54e7128d29063 to your computer and use it in GitHub Desktop.
ewm shifted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy import optimize | |
def calc_shifted_ewm(series, alpha, adjust=True): | |
return series.shift().ewm(alpha=alpha, adjust=adjust).mean() | |
def find_best_signal(series, adjust=False, eps=10e-5): | |
def f(alpha): | |
shifted_ewm = calc_shifted_ewm(series=series, alpha=min(max(alpha, 0), 1), adjust=adjust) | |
corr = np.mean(np.power(series - shifted_ewm, 2)) | |
return corr | |
res = optimize.differential_evolution(func=f, bounds=[(0 + eps, 1 - eps)]) | |
return calc_shifted_ewm(series=series, alpha=res['x'][0], adjust=adjust) | |
############################################# | |
roll = df.groupby(['entity', 'i_n_dow']).apply(lambda g: find_best_signal(g['target'])) | |
df['i_n_optimized_ewm_by_entity_dow'] = roll.sort_index(level=['entity', 'time_stamp']).values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment