Skip to content

Commit

Permalink
Fix moving function linear weighted avg (elastic#118516)
Browse files Browse the repository at this point in the history
Fix moving function linear weighted avg

# Conflicts:
#	modules/aggregations/build.gradle
  • Loading branch information
Quentin18 authored and iverase committed Dec 16, 2024
1 parent 93ffc84 commit cbd8453
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
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
2 changes: 2 additions & 0 deletions modules/aggregations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ tasks.named("yamlRestTestV7CompatTransform").configure { task ->

// Something has changed with response codes
task.skipTest("search.aggregation/20_terms/IP test", "Hybrid t-digest produces different results.")
// Maths changed
task.skipTest("aggregations/moving_fn/linearWeightedAvg", "math was wrong in previous versions")

task.addAllowedWarningRegex("\\[types removal\\].*")
}
Expand Down
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 @@ -40,6 +40,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 @@ -56,6 +58,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

0 comments on commit cbd8453

Please sign in to comment.