Weighted average using numpy.average

南楼画角 提交于 2019-12-06 03:32:18

问题


I have an array:

In [37]: bias_2e13 # our array
Out[37]: 
[1.7277990734072355,
 1.9718263893212737,
 2.469657573252167,
 2.869022991373125,
 3.314720313010104,
 4.232269039271717]

The error on each value in the array is:

In [38]: bias_error_2e13 # the error on each value
Out[38]: 
array([ 0.13271387,  0.06842465,  0.06937965,  0.23886647,  0.30458249,
        0.57906816])

Now I divide the error on each value by 2:

In [39]: error_half # error divided by 2
Out[39]: 
array([ 0.06635694,  0.03421232,  0.03468982,  0.11943323,  0.15229124,
        0.28953408])

Now I calculate the average of the array using numpy.average, but using the errors as weights.

First I am using the full error on the values, then I am using half the error, i.e. the error divided by 2.

In [40]: test = np.average(bias_2e13,weights=bias_error_2e13)

In [41]: test_2 = np.average(bias_2e13,weights=error_half)

How do both the averages give me the same result when one array has errors which are half of that of the other?

In [42]: test
Out[42]: 3.3604746813456936

In [43]: test_2
Out[43]: 3.3604746813456936

回答1:


Because all of the errors have the same relative weight. Supplying a weight parameter does not change the actual values you are averaging, it just indicates the weight with which each value value contributes to the average. In other words, after multiplying each value passed by its corresponding weight, np.average divides by the sum of the weights provided.

>>> import numpy as np
>>> np.average([1, 2, 3], weights=[0.2, 0.2, 0.2])
2.0
>>> np.average([1, 2, 3])
2.0

Effectively, the average formula for an n-dimensional array-like container is

                               

where each weight is assumed to be equal to 1 when not provided to numpy.average.




回答2:


From scipy.org about numpy average: "An array of weights associated with the values in a. Each value in a contributes to the average according to its associated weight." That means that the errors contribute relative to the average! So a multiplication of the errors with the same factor doesn't change anything! Try multiplying for example only the first error with 0.5 and you'll get a different result.



来源:https://stackoverflow.com/questions/38241174/weighted-average-using-numpy-average

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!