Created
January 29, 2015 14:08
-
-
Save shane5ul/76dbb153f3199d108147 to your computer and use it in GitHub Desktop.
Given the logarithm of the weights, L, and observation, R, this computes the weighted average
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
def logWeightedAverage(L, R): | |
n = len(L) | |
Lscale = 0. | |
sumNumerator = 0. | |
sumDenominator = 0. | |
for j in range(n): | |
if j == 0 : | |
wt = 1.0 | |
Lscale = L[j] | |
sumNumerator = R[j] | |
sumDenominator = 1.0 | |
else: | |
wt = np.exp(L[j] - Lscale) | |
if wt <= 1.0: | |
sumNumerator = sumNumerator + wt * R[j] | |
sumDenominator = sumDenominator + wt | |
else: # Lscale needs to be updated to L[j] | |
sumNumerator = sumNumerator/wt + R[j] | |
sumDenominator = sumDenominator/wt + 1.0 | |
Lscale = L[j] | |
weightAverage = sumNumerator/sumDenominator | |
return weightAverage | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment