I have a grid of parameters which is composed of 234 dictionaries, everyone having the same keys. Then I have a list of weights through which I want to compute a weighted average of such dictionaries' values. In other words I need to obtin just one dictionary that has the same keys of the initial 243, but as values attached to each keys a weighted average of values, using the 243 weights.
I tried to use Counter
in order to cumulate results, but it returns very small values that do not make sense to me. w[0]
is the list of 243 weights, each of them related to 243 dictionaries inside grid
from collections import Counter
def avg_grid(grid, w, labels=["V0", "omega", "kappa", "rho", "theta"]):
avgHeston = Counter({label: 0 for label in labels})
for i in range(len(grid)):
avgHeston.update(Counter({label: grid[i][label] * w[0][i] for label in labels}))
totPar = dict(avgHeston)
return totPar
Is there an easier way to implement it?