-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Feature/incrwmean #256
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
Merged
Merged
Feature/incrwmean #256
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c21c14f
Add readme
MatthewCochrane 2542759
Add examples
MatthewCochrane 5370336
Add package.json
MatthewCochrane 279f412
Add implementation
MatthewCochrane 320550e
Add benchmarks
MatthewCochrane 138402e
Add tests and add weight default of 1.0
MatthewCochrane fb07cb4
Update examples to reflect default weight parameter
MatthewCochrane c24fbc4
Add repl
MatthewCochrane 013f14b
Add typescript definition
MatthewCochrane 2d4007b
Make weight parameter required
MatthewCochrane 9181f91
Modify incremental algorithm
MatthewCochrane 8f9d990
Apply suggestions from code review
kgryte a861840
Update tests and typescript definition
MatthewCochrane 000e929
Update typescript definition
MatthewCochrane e4f236c
Add typescript accumulator tests
MatthewCochrane 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,129 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2019 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. | ||
|
|
||
| --> | ||
|
|
||
| # incrwmean | ||
|
|
||
| > Compute a [weighted arithmetic mean][weighted-arithmetic-mean] incrementally. | ||
|
|
||
| <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{\sum_{i=1}^{n} w_{i}x_{i}}{\sum_{i=1}^{n}w_{i}}" alt="Equation for the weighted arithmetic mean."> --> | ||
|
|
||
| <div class="equation" align="center" data-raw-text="\bar {x}= \frac{\sum_{i=1}^{n} w_{i}x_{i}}{\sum_{i=1}^{n}w_{i}}" data-equation="eq:weighted_arithmetic_mean"> | ||
| <img src="" alt="Equation for the weighted arithmetic mean."> | ||
| <br> | ||
| </div> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var incrwmean = require( '@stdlib/stats/incr/wmean' ); | ||
| ``` | ||
|
|
||
| #### incrwmean() | ||
|
|
||
| Returns an accumulator `function` which incrementally computes a [weighted arithmetic mean][weighted-arithmetic-mean]. | ||
|
|
||
| ```javascript | ||
| var accumulator = incrwmean(); | ||
| ``` | ||
|
|
||
| #### 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 = incrwmean(); | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **all** future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var incrwmean = require( '@stdlib/stats/incr/wmean' ); | ||
|
|
||
| var accumulator; | ||
| var v; | ||
| var w; | ||
| var i; | ||
|
|
||
| // Initialize an accumulator: | ||
| accumulator = incrwmean(); | ||
|
|
||
| // For each simulated datum, update the weighted mean... | ||
| for ( i = 0; i < 100; i++ ) { | ||
| v = randu() * 100.0; | ||
| w = randu() * 100.0; | ||
| accumulator( v, w ); | ||
| } | ||
| console.log( accumulator() ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [weighted-arithmetic-mean]: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> |
69 changes: 69 additions & 0 deletions
69
lib/node_modules/@stdlib/stats/incr/wmean/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,69 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2019 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 incrwmean = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var f; | ||
| var i; | ||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| f = incrwmean(); | ||
| 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 i; | ||
|
|
||
| acc = incrwmean(); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = acc( randu(), randu() ); | ||
| if ( v !== v ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( v !== v ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
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,37 @@ | ||
|
|
||
| {{alias}}() | ||
| Returns an accumulator function which incrementally computes a weighted | ||
| arithmetic mean. | ||
|
|
||
| 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` or a value which, when used in computations, results in | ||
| `NaN`, the accumulated value is `NaN` for all future invocations. | ||
|
|
||
| The accumulator accepts two arguments: the first is the value and the | ||
| second is the weight. | ||
|
|
||
| Returns | ||
| ------- | ||
| acc: Function | ||
| Accumulator function. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var accumulator = {{alias}}(); | ||
| > var mu = accumulator() | ||
| null | ||
| > mu = accumulator( 2.0, 1.0 ) | ||
| 2.0 | ||
| > mu = accumulator( 2.0, 0.5 ) | ||
| 2.0 | ||
| > mu = accumulator( 3.0, 1.5 ) | ||
| 2.5 | ||
| > mu = accumulator() | ||
| 2.5 | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
64 changes: 64 additions & 0 deletions
64
lib/node_modules/@stdlib/stats/incr/wmean/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,64 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2019 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: 2.0 | ||
|
|
||
| /// <reference types="@stdlib/types"/> | ||
|
|
||
| /** | ||
| * Weighted arithmetic mean accumulator function | ||
| * | ||
| * @param x value | ||
| * @param w weight | ||
| * | ||
| * @returns the incrementally computed weighted arithmetic mean. | ||
| * | ||
| */ | ||
| type incrwmean_acc = ( x: number, w: number ) => number | null; | ||
|
|
||
| /** | ||
| * Returns an accumulator function which incrementally computes a weighted arithmetic mean. | ||
| * | ||
| * @returns accumulator function | ||
| * | ||
| * @example | ||
| * var incrwmean = require( '@stdlib/stats/incr/wmean' ); | ||
| * | ||
| * var accumulator = incrwmean(); | ||
| * | ||
| * var mu = accumulator(); | ||
| * // returns null | ||
| * | ||
| * 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 | ||
| */ | ||
| declare function incrwmean(): incrwmean_acc; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = incrwmean; |
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/stats/incr/wmean/docs/types/test.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,54 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2019 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. | ||
| */ | ||
|
|
||
| import incrwmean = require( './index' ); | ||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns either a number or null... | ||
| { | ||
| incrwmean(); // $ExpectType incrwmean_acc | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided arguments... | ||
| { | ||
| incrwmean( '5' ); // $ExpectError | ||
| incrwmean( 5 ); // $ExpectError | ||
| incrwmean( true ); // $ExpectError | ||
| incrwmean( false ); // $ExpectError | ||
| incrwmean( null ); // $ExpectError | ||
| incrwmean( undefined ); // $ExpectError | ||
| incrwmean( [] ); // $ExpectError | ||
| incrwmean( {} ); // $ExpectError | ||
| incrwmean( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided arguments... | ||
| { | ||
| let acc = incrwmean(); | ||
|
|
||
| acc( '5' ); // $ExpectError | ||
| acc( 5 ); // $ExpectError | ||
| acc( true ); // $ExpectError | ||
| acc( false ); // $ExpectError | ||
| acc( null ); // $ExpectError | ||
| acc( undefined ); // $ExpectError | ||
| acc( [] ); // $ExpectError | ||
| acc( {} ); // $ExpectError | ||
| acc( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
lib/node_modules/@stdlib/stats/incr/wmean/examples/index.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,41 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2019 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'; | ||
|
|
||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var incrwmean = require( './../lib' ); | ||
|
|
||
| var accumulator; | ||
| var mu; | ||
| var x; | ||
| var w; | ||
| var i; | ||
|
|
||
| // Initialize an accumulator: | ||
| accumulator = incrwmean(); | ||
|
|
||
| // For each simulated datum, update the weighted mean... | ||
| console.log( '\nValue\tWeight\tWeighted Mean\n' ); | ||
| for ( i = 0; i < 100; i++ ) { | ||
| x = randu() * 100.0; | ||
| w = randu() * 100.0; | ||
| mu = accumulator( x ); | ||
| console.log( '%d\t%d\t%d', x.toFixed( 4 ), w.toFixed( 4 ), mu.toFixed( 4 ) ); | ||
| } | ||
| console.log( '\nFinal weighted mean: %d\n', accumulator() ); |
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.