generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support weighted average aggregation function (#3052)
* support wavg aggregate function * fixing function handlers for wavg * fixing pct java standard test * updating data for wavg function * reusing processing of aggregation function * fixing compilation of empty list * Improving wavg functionality with documentation and tests * fixing pct test for java binding adapter
- Loading branch information
Showing
11 changed files
with
287 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
19 changes: 19 additions & 0 deletions
19
...re-code-compiled-core/src/main/resources/core/pure/corefunctions/tests/math/testWavg.pure
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
// Copyright 2022 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
function <<test.Test>> meta::pure::functions::math::tests::wavg::testWavg():Boolean[1] | ||
{ | ||
assertEq(292.5, [200,300,250,850,100]->wavg([0.25,0.35,0.15,0.1,0.15])); | ||
} |
This file contains 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
111 changes: 111 additions & 0 deletions
111
...ctions-standard-pure/src/main/resources/core_functions_standard/math/aggregator/wavg.pure
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import meta::pure::test::pct::*; | ||
|
||
function | ||
<<PCT.function>> | ||
{ | ||
doc.doc='Performs weighted average on a given data field with its respective weight field', | ||
PCT.grammarDoc='wavg is used by wavg(Number[*], Number[*])' | ||
} | ||
meta::pure::functions::math::wavg(numbers:Number[*], weights:Number[*]):Float[1] | ||
{ | ||
if($numbers->isEmpty() || $weights->isEmpty(), | ||
|fail('Error: Mean of an empty set.');0.0;, | ||
| if ($numbers->size() != $weights->size(), | ||
|fail('Error: The data and weight column must have the same number of values');0.0;, | ||
|let weightedQuantitySum = $numbers->zip($weights)->map(p | $p.first * $p.second)->sum(); | ||
let weightsSum = $weights->sum(); | ||
assert($weightsSum != 0.0,|'Weighted Average can\'t be performed as sum of weight column values equal 0'); | ||
$weightedQuantitySum/$weightsSum;)); | ||
} | ||
|
||
function meta::pure::functions::math::wavg(wavgRows: meta::pure::functions::math::wavgUtility::WavgRowMapper[*]):Float[1] | ||
{ | ||
meta::pure::functions::math::wavg($wavgRows.quantity, $wavgRows.weight); | ||
} | ||
|
||
Class meta::pure::functions::math::wavgUtility::WavgRowMapper | ||
{ | ||
quantity: Number[0..1]; | ||
weight: Number[0..1]; | ||
} | ||
|
||
function meta::pure::functions::math::wavgUtility::wavgRowMapper(quantity:Number[0..1], weight:Number[0..1]):meta::pure::functions::math::wavgUtility::WavgRowMapper[1] | ||
{ | ||
^meta::pure::functions::math::wavgUtility::WavgRowMapper( | ||
quantity= $quantity, | ||
weight= $weight | ||
) | ||
} | ||
|
||
function <<PCT.test>> meta::pure::functions::math::tests::wavg::testSimpleGroupByWavg<T|m>(f:Function<{Function<{->T[m]}>[1]->T[m]}>[1]):Boolean[1] | ||
{ | ||
let expr = { | ||
|#TDS | ||
id, grp, name, quantity, weight | ||
1, 2, A, 200, 0.5 | ||
2, 1, B, 100, 0.45 | ||
3, 3, C, 250, 0.25 | ||
4, 4, D, 700, 1 | ||
5, 2, E, 100, 0.5 | ||
6, 1, F, 500, 0.15 | ||
7, 3, G, 400, 0.75 | ||
8, 1, H, 150, 0.4 | ||
9, 5, I, 350, 1 | ||
#->groupBy(~grp, ~wavgCol : x | meta::pure::functions::math::wavgUtility::wavgRowMapper($x.quantity, $x.weight) : y | $y->wavg()) | ||
}; | ||
|
||
let res = $f->eval($expr); | ||
|
||
assertEquals( '#TDS\n'+ | ||
' grp,wavgCol\n'+ | ||
' 1,180.0\n'+ | ||
' 2,150.0\n'+ | ||
' 3,362.5\n'+ | ||
' 4,700.0\n'+ | ||
' 5,350.0\n'+ | ||
'#', $res->sort(~grp->ascending())->toString()); | ||
} | ||
|
||
function <<PCT.test>> meta::pure::functions::math::tests::wavg::testSimpleGroupByMultipleWavg<T|m>(f:Function<{Function<{->T[m]}>[1]->T[m]}>[1]):Boolean[1] | ||
{ | ||
let expr = { | ||
|#TDS | ||
id, grp, name, quantity, weight, weight1 | ||
1, 2, A, 200, 0.5, 0.75 | ||
2, 1, B, 100, 0.45, 0.35 | ||
3, 3, C, 250, 0.25, 0.50 | ||
4, 4, D, 700, 1, 1 | ||
5, 2, E, 100, 0.5, 0.25 | ||
6, 1, F, 500, 0.15, 0.25 | ||
7, 3, G, 400, 0.75, 0.50 | ||
8, 1, H, 150, 0.4, 0.4 | ||
9, 5, I, 350, 1, 1 | ||
#->groupBy(~grp, ~[wavgCol1 : x | meta::pure::functions::math::wavgUtility::wavgRowMapper($x.quantity, $x.weight) : y | $y->wavg(), | ||
wavgCol2 : x | meta::pure::functions::math::wavgUtility::wavgRowMapper($x.quantity, $x.weight1) : y | $y->wavg()]) | ||
}; | ||
|
||
let res = $f->eval($expr); | ||
|
||
assertEquals( '#TDS\n'+ | ||
' grp,wavgCol1,wavgCol2\n'+ | ||
' 1,180.0,220.0\n'+ | ||
' 2,150.0,175.0\n'+ | ||
' 3,362.5,325.0\n'+ | ||
' 4,700.0,700.0\n'+ | ||
' 5,350.0,350.0\n'+ | ||
'#', $res->sort(~grp->ascending())->toString()); | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.