[SERVER-32790] Add Weighted Average method to aggregation framework Created: 19/Jan/18  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: 3.6.0
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Yair Lenga Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 1
Labels: expression
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Query Optimization
Participants:

 Description   

Add '$weightedAverage' operator to the aggregation framework.

Currently, MongoDB has the $avg operator. It simplify the calculation of simple averages. The request is to create built in operator for calculating weighted average - where the weight of each item is specified by a different field.

Weighted average is used extensively in financial, statistical and scientific analysis of large data set. While it is technically possible to implement it based on existing operators (sum(...)), the implementation is error-prone, and correct handling of missing/non-numeric values make is difficult.

Proposed operator: $weightedAvg($weight, $value)

Should calculation

RESULT = (w1*v1 + w2*v2 + w3*v3 ... + w(n)*v(n)) / (w1 + w2 + w3 + ... + W(n))

Calculation should ignore any record where w(I), or v(I) is not numeric. this is important, as the current replacement $sum($multiply($v1, $w1)) / sum($w1) does not handle missing values correctly, and require complex filtering.

If possible, it will be nice to allow for multiple values to be calculated with the same pass.
$weightedAvg($weight, $v1, $v2, $v3) will return the weighted average of the specific value list, weighted by the specific weight, similar to the ability to perform sum/average on multiple values.


Generated at Thu Feb 08 04:31:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.