-
-
Notifications
You must be signed in to change notification settings - Fork 838
feat: add stats/incr/nanwmean
#6441
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
Open
SergeantQuickscoper
wants to merge
9
commits into
stdlib-js:develop
Choose a base branch
from
SergeantQuickscoper:feature/nanwmean
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
155daca
feat (stats/incr/nanwmean): adds nanwmean package to the stats/incr/*…
SergeantQuickscoper 4974c82
fixup! feat: feat (stats/incr/nanwmean): adds nanwmean package to the…
SergeantQuickscoper d8df986
Merge branch 'stdlib-js:develop' into feature/nanwmean
SergeantQuickscoper 028dc53
fixup! feat (stats/incr/nanwmean): fixes lint errors
SergeantQuickscoper cb7d379
fixup! feat (stats/incr/nanwmean): fixes remark lint errors
SergeantQuickscoper d7b1d7b
Merge branch 'stdlib-js:develop' into feature/nanwmean
SergeantQuickscoper a2378d0
fix: resolve lint errors
stdlib-bot 896bbf3
fixup! feat (stats/incr/nanwmean): fix final license header lint
SergeantQuickscoper c27d2c8
Merge branch 'feature/nanwmean' of github.com:SergeantQuickscoper/std…
SergeantQuickscoper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2025 The Stdlib Authors. | ||
|
||
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. | ||
|
||
--> | ||
|
||
# incrnanwmean | ||
|
||
> Compute a [weighted arithmetic mean][weighted-arithmetic-mean] incrementally, ignoring `NaN` values. | ||
|
||
<section class="intro"> | ||
|
||
The [weighted arithmetic mean][weighted-arithmetic-mean] is defined as | ||
|
||
<!-- <equation class="equation" label="eq:weighted_arithmetic_mean" align="center" raw="\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}}" alt="Equation for the weighted arithmetic mean."> --> | ||
|
||
```math | ||
\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}} | ||
``` | ||
|
||
<!-- <div class="equation" align="center" data-raw-text="\bar{x} = \frac{\displaystyle\sum_{i=0}^{n-1} w_{i} x_{i}}{\displaystyle\sum_{i=0}^{n-1} w_{i}}" data-equation="eq:weighted_arithmetic_mean"> | ||
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@adbea9806383f70c982e3191475c874efba1296b/lib/node_modules/@stdlib/stats/incr/wmean/docs/img/equation_weighted_arithmetic_mean.svg" alt="Equation for the weighted arithmetic mean."> | ||
<br> | ||
</div> --> | ||
|
||
<!-- </equation> --> | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var incrnanwmean = require( '@stdlib/stats/incr/nanwmean' ); | ||
``` | ||
|
||
#### incrnanwmean() | ||
|
||
Returns an accumulator `function` which incrementally computes a [weighted arithmetic mean][weighted-arithmetic-mean], ignoring NaN values. | ||
|
||
```javascript | ||
var accumulator = incrnanwmean(); | ||
``` | ||
|
||
#### accumulator( \[x, w] ) | ||
|
||
If provided an input value `x` and a weight `w`, the accumulator function returns an updated weighted mean. If not provided any input values, the accumulator function returns the current mean. | ||
|
||
```javascript | ||
var accumulator = incrnanwmean(); | ||
|
||
var mu = accumulator( 2.0, 1.0 ); | ||
// returns 2.0 | ||
|
||
mu = accumulator( 2.0, 0.5 ); | ||
// returns 2.0 | ||
|
||
mu = accumulator( 3.0, 1.5 ); | ||
// returns 2.5 | ||
|
||
mu = accumulator(); | ||
// returns 2.5 | ||
|
||
mu = accumulator( NaN, 2 ); | ||
// returns 2.5 | ||
|
||
mu = accumulator( 2, NaN ); | ||
// returns 2.5 | ||
|
||
mu = accumulator( NaN, NaN ); | ||
// returns 2.5 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="notes"> | ||
|
||
</section> | ||
|
||
<!-- /.notes --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var incrnanwmean = require( '@stdlib/stats/incr/nanwmean' ); | ||
|
||
var accumulator; | ||
var mean; | ||
var v; | ||
var w; | ||
var i; | ||
|
||
accumulator = incrnanwmean(); | ||
|
||
for ( i = 0; i < 100; i++ ) { | ||
if ( randu() < 0.2 ) { | ||
v = NaN; | ||
w = NaN; | ||
} else { | ||
v = randu() * 100.0; | ||
w = randu() * 10.0; | ||
} | ||
accumulator( v, w ); | ||
} | ||
console.log( accumulator() ); | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[weighted-arithmetic-mean]: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
66 changes: 66 additions & 0 deletions
66
lib/node_modules/@stdlib/stats/incr/nanwmean/benchmark/benchmark.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var pkg = require( './../package.json' ).name; | ||
var incrnanwmean = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var f; | ||
var i; | ||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
f = incrnanwmean(); | ||
if ( typeof f !== 'function' ) { | ||
b.fail( 'should return a function' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( typeof f !== 'function' ) { | ||
b.fail( 'should return a function' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+'::accumulator', function benchmark( b ) { | ||
var acc; | ||
var v; | ||
var w; | ||
var i; | ||
|
||
acc = incrnanwmean(); | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
v = ( randu() < 0.2 ) ? NaN : randu() * 100.0; | ||
w = ( randu() < 0.2 ) ? NaN : randu() * 10.0; | ||
acc( v, w ); | ||
} | ||
b.toc(); | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
63 changes: 63 additions & 0 deletions
63
...ules/@stdlib/stats/incr/nanwmean/docs/img/equation_weighted_arithmetic_mean.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/stats/incr/nanwmean/docs/repl.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{{alias}}() | ||
Returns an accumulator function which incrementally computes a weighted | ||
arithmetic mean, ignoring `NaN` values. | ||
|
||
If provided arguments, the accumulator function returns an updated weighted | ||
mean. If not provided arguments, the accumulator function returns the | ||
current weighted mean. | ||
|
||
If provided `NaN` for either a value or a weight, the accumulated value | ||
remains unchanged and does not include the `NaN` values in computations. | ||
|
||
The accumulator function accepts two arguments: | ||
|
||
- x: value. | ||
- w: weight. | ||
|
||
Returns | ||
------- | ||
acc: Function | ||
Accumulator function. | ||
|
||
Examples | ||
-------- | ||
> var accumulator = {{alias}}(); | ||
> var mu = accumulator() | ||
null | ||
> mu = accumulator( 2.0, 1.0 ) | ||
2.0 | ||
> mu = accumulator( NaN, 1.0 ) | ||
2.0 | ||
> mu = accumulator( 3.0, 2.0 ) | ||
2.6666666666666665 | ||
> mu = accumulator( 4.0, NaN ) | ||
2.6666666666666665 | ||
> mu = accumulator( 5.0, 3.0 ) | ||
3.833333333333333 | ||
> mu = accumulator() | ||
3.833333333333333 | ||
|
||
See Also | ||
-------- |
63 changes: 63 additions & 0 deletions
63
lib/node_modules/@stdlib/stats/incr/nanwmean/docs/types/index.d.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2025 The Stdlib Authors. | ||
* | ||
* 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. | ||
*/ | ||
|
||
// TypeScript Version: 4.1 | ||
|
||
/// <reference types="@stdlib/types"/> | ||
|
||
/** | ||
* If provided values, returns an updated weighted mean; otherwise, returns the current weighted mean. | ||
* | ||
* @param x - value | ||
* @param w - weight | ||
* @returns weighted mean | ||
*/ | ||
type accumulator = ( x?: number, w?: number ) => number | null; | ||
|
||
/** | ||
* Returns an accumulator function which incrementally computes a weighted mean, ignoring `NaN` values. | ||
* | ||
* @returns accumulator function | ||
* | ||
* @example | ||
* var accumulator = incrnanwmean(); | ||
* | ||
* var v = accumulator(); | ||
* // returns null | ||
* | ||
* v = accumulator( 2.0, 1.0 ); | ||
* // returns 2.0 | ||
* | ||
* v = accumulator( 3.0, 2.0 ); | ||
* // returns 2.666... | ||
* | ||
* v = accumulator( NaN, 1.0 ); | ||
* // returns 2.666... | ||
* | ||
* v = accumulator( 4.0, 3.0 ); | ||
* // returns 3.166... | ||
* | ||
* v = accumulator(); | ||
* // returns 3.166... | ||
*/ | ||
declare function incrnanwmean(): accumulator; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = incrnanwmean; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.