1

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?

AleB
  • 153
  • 1
  • 3
  • 10
  • 1
    Can you give an example of what you expect with just 2 dictionaries and 2 weights? – jpp Jan 26 '18 at 14:57

1 Answers1

1

You might want to use a defaultdict instead:

from collections import defaultdict

def avg_grid(grid, wgts, labels=["V0", "rho"]):

    avgHeston = defaultdict(int)

    for g,w in zip(grid, wgts):
        for label in labels:
            avgHeston[label] += g[label] * w

    return avgHeston

weights = [4,3]
grd = [{'V0':4,'rho':3}, {'V0':1,'rho':2}]

print(avg_grid(grd, weights))

Output:

defaultdict(<class 'int'>, {'V0': 19, 'rho': 18})

Notes:

I have changed w so that you need to pass in a straight list. You might want to call the function like this: avg_grid(grids, w[0])

Also this doesn't produce an average as such. you may want to do a divide by len(grid) at some point.

Also for g,w in zip(grid, wgts): is a more pythonic iteration

quamrana
  • 37,849
  • 12
  • 53
  • 71