Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix moving function linear weighted avg #118516

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/118516.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 118435
summary: Fix moving function linear weighted avg
area: Aggregations
type: bug
issues:
- 113751
1 change: 1 addition & 0 deletions modules/aggregations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ tasks.named("yamlRestCompatTestTransform").configure({ task ->
task.skipTest("aggregations/date_agg_per_day_of_week/Date aggregartion per day of week", "week-date behaviour has changed")
task.skipTest("aggregations/time_series/Configure with no synthetic source", "temporary until backport")
task.skipTest("aggregations/percentiles_hdr_metric/Negative values test", "returned exception has changed")
task.skipTest("aggregations/moving_fn/linearWeightedAvg", "math was wrong in previous versions")
})
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ linearWeightedAvg:
- skip:
features: close_to

- requires:
test_runner_features: [capabilities]

- requires:
capabilities:
- method: POST
path: /_search
parameters: [method, path, parameters, capabilities]
capabilities: [moving_fn_right_math]
reason: "math not fixed yet"

- do:
search:
index: no_gaps
Expand All @@ -275,11 +286,11 @@ linearWeightedAvg:
- match: { hits.total.value: 6 }
- length: { [email protected]: 6 }
- is_false: [email protected]
- close_to: { [email protected]: { value: 0.500, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.250, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.000, error: 0.0005 } }
- close_to: { [email protected]: { value: 2.250, error: 0.0005 } }
- close_to: { [email protected]: { value: 3.500, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.000, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.667, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.333, error: 0.0005 } }
- close_to: { [email protected]: { value: 3.000, error: 0.0005 } }
- close_to: { [email protected]: { value: 4.667, error: 0.0005 } }

- do:
search:
Expand All @@ -301,11 +312,11 @@ linearWeightedAvg:
- match: { hits.total.value: 6 }
- length: { [email protected]: 6 }
- is_false: [email protected]
- close_to: { [email protected]: { value: 0.500, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.250, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.143, error: 0.0005 } }
- close_to: { [email protected]: { value: 2.286, error: 0.0005 } }
- close_to: { [email protected]: { value: 3.429, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.000, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.667, error: 0.0005 } }
- close_to: { [email protected]: { value: 1.333, error: 0.0005 } }
- close_to: { [email protected]: { value: 2.667, error: 0.0005 } }
- close_to: { [email protected]: { value: 4.000, error: 0.0005 } }

---
ewma:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private SearchCapabilities() {}
private static final String RANK_VECTORS_SCRIPT_ACCESS = "rank_vectors_script_access";
/** Initial support for rank-vectors maxSim functions access. */
private static final String RANK_VECTORS_SCRIPT_MAX_SIM = "rank_vectors_script_max_sim_with_bugfix";
/** Fixed the math in {@code moving_fn}'s {@code linearWeightedAvg}. */
private static final String MOVING_FN_RIGHT_MATH = "moving_fn_right_math";

private static final String RANDOM_SAMPLER_WITH_SCORED_SUBAGGS = "random_sampler_with_scored_subaggs";
private static final String OPTIMIZED_SCALAR_QUANTIZATION_BBQ = "optimized_scalar_quantization_bbq";
Expand All @@ -59,6 +61,7 @@ private SearchCapabilities() {}
capabilities.add(RANDOM_SAMPLER_WITH_SCORED_SUBAGGS);
capabilities.add(OPTIMIZED_SCALAR_QUANTIZATION_BBQ);
capabilities.add(KNN_QUANTIZED_VECTOR_RESCORE);
capabilities.add(MOVING_FN_RIGHT_MATH);
if (RankVectorsFieldMapper.FEATURE_FLAG.isEnabled()) {
capabilities.add(RANK_VECTORS_FIELD_MAPPER);
capabilities.add(RANK_VECTORS_SCRIPT_ACCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static double stdDev(double[] values, double avg) {
*/
public static double linearWeightedAvg(double[] values) {
double avg = 0;
long totalWeight = 1;
long totalWeight = 0;
long current = 1;

for (double v : values) {
Expand All @@ -110,7 +110,7 @@ public static double linearWeightedAvg(double[] values) {
current += 1;
}
}
return totalWeight == 1 ? Double.NaN : avg / totalWeight;
return totalWeight == 0 ? Double.NaN : avg / totalWeight;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public void testLinearMovAvg() {
}

double avg = 0;
long totalWeight = 1;
long totalWeight = 0;
long current = 1;

for (double value : window) {
Expand Down