How to get sum over time horizont and different names from dictionaries
Using numpy, you can craft an array, compute the absolute difference (numpy.diff
+abs
), and sum
:
h1={'a':1, 'b':3, 'c':10}
h2={'a':7, 'b':15, 'c':2}
h3={'a':9, 'b':15, 'c':35}
import numpy as np
dicts = [h1, h2, h3]
a = np.array([list(d.values()) for d in dicts])
abs(np.diff(a, axis=0)).sum()
output: 61
With beta:
beta = 1
abs(np.diff(a, axis=0)*beta).sum()
Probably better to use pandas
than numpy
for this.
Pretty sure it would just be
beta = 1
pd.DataFrame([h1, h2, h3]).diff().dropna().abs().mul(beta).sum().sum()
Out: 61.0