-
-
Notifications
You must be signed in to change notification settings - Fork 975
feat: add stats/incr/nanvariance function
#5755
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
base: develop
Are you sure you want to change the base?
Changes from 9 commits
1bfafba
1fa04a8
9ff829e
8fedca1
cddf2b9
2842260
64b11ca
a3dd815
117fd8b
c2e1cf1
7f96a1a
57b9e93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /** | ||
| * @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 incrnanvariance = require( './../lib' ); | ||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var f; | ||
| var i; | ||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| f = incrnanvariance(); | ||
| 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 = incrnanvariance(); | ||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = acc( 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(); | ||
| }); | ||
|
|
||
| bench( pkg+'::accumulator,known_mean', function benchmark( b ) { | ||
| var acc; | ||
| var v; | ||
| var i; | ||
| acc = incrnanvariance( 3.14 ); | ||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = acc( 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(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| {{alias}}( [mean] ) | ||
| Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values. | ||
|
|
||
| If provided a value, the accumulator function returns an updated unbiased sample variance. If not provided a value, the accumulator function returns the current unbiased sample variance. | ||
|
|
||
| If provided `NaN`, the `NaN` value is ignored and the current accumulated variance is returned. | ||
|
|
||
|
|
||
| Parameters | ||
| ---------- | ||
| mean: number (optional) | ||
| Known mean. | ||
|
|
||
|
|
||
| Returns | ||
| ------- | ||
| acc: Function | ||
| Accumulator function. | ||
|
|
||
|
|
||
| Examples | ||
| -------- | ||
| > var accumulator = {{alias}}(); | ||
| > var s2 = accumulator() | ||
| null | ||
| > s2 = accumulator( 2.0 ) | ||
| 0.0 | ||
| > s2 = accumulator( NaN ) | ||
| 0.0 | ||
| > s2 = accumulator( -5.0 ) | ||
| 24.5 | ||
| > s2 = accumulator() | ||
| 24.5 | ||
|
|
||
|
|
||
| See Also | ||
| -------- | ||
| @stdlib/stats/incr/variance | ||
| @stdlib/stats/incr/nanstdev | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * @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 a value, returns an updated unbiased sample variance; otherwise, returns the current unbiased sample variance. | ||
| * | ||
| * ## Notes | ||
| * | ||
| * - If provided `NaN`, the `NaN` value is ignored and the current accumulated variance is returned. | ||
| * | ||
| * @param x - value | ||
| * @returns unbiased sample variance | ||
| */ | ||
| type accumulator = ( x?: number ) => number | null; | ||
|
|
||
| /** | ||
| * Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values. | ||
| * | ||
| * @param mu - known mean | ||
| * @returns accumulator function | ||
| * | ||
| * @example | ||
| * var accumulator = incrnanvariance(); | ||
| * | ||
| * var s2 = accumulator(); | ||
| * // returns null | ||
| * | ||
| * s2 = accumulator( 2.0 ); | ||
| * // returns 0.0 | ||
| * | ||
| * s2 = accumulator( NaN ); | ||
| * // returns 0.0 | ||
| * | ||
| * s2 = accumulator( -5.0 ); | ||
| * // returns 24.5 | ||
| * | ||
| * s2 = accumulator(); | ||
| * // returns 24.5 | ||
| */ | ||
| declare function incrnanvariance( mu?: number ): accumulator; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = incrnanvariance; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * @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. | ||
| */ | ||
|
|
||
| import incrnanvariance from './index' ; | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns an accumulator function... | ||
| { | ||
| incrnanvariance(); // $ExpectType accumulator | ||
| incrnanvariance( 0.0 ); // $ExpectType accumulator | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided invalid arguments... | ||
| { | ||
| incrnanvariance( '5' as any ); // $ExpectError | ||
|
||
| incrnanvariance( true as any ); // $ExpectError | ||
| incrnanvariance( false as any ); // $ExpectError | ||
| incrnanvariance( null as any ); // $ExpectError | ||
| incrnanvariance( [] as any ); // $ExpectError | ||
| incrnanvariance( {} as any ); // $ExpectError | ||
| incrnanvariance( ((x: number): number => x) as any ); // $ExpectError | ||
| } | ||
|
|
||
| // The function returns an accumulator function which returns an accumulated result... | ||
| { | ||
| const acc = incrnanvariance(); | ||
| acc(); // $ExpectType number | null | ||
| acc( 3.14 ); // $ExpectType number | null | ||
| } | ||
|
|
||
| // The compiler throws an error if the returned accumulator function is provided invalid arguments... | ||
| { | ||
| const acc = incrnanvariance(); | ||
| acc( '5' as any ); // $ExpectError | ||
| acc( true as any ); // $ExpectError | ||
| acc( false as any ); // $ExpectError | ||
| acc( null as any ); // $ExpectError | ||
| acc( [] as any ); // $ExpectError | ||
| acc( {} as any ); // $ExpectError | ||
| acc( ((x: number): number => x) as any ); // $ExpectError | ||
| } | ||
Krishna-Sharma-g marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * @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'; | ||
|
|
||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var incrnanvariance = require( './../lib' ); | ||
|
|
||
| var accumulator; | ||
| var s2; | ||
| var v; | ||
| var i; | ||
|
|
||
| // Initialize an accumulator: | ||
| accumulator = incrnanvariance(); | ||
|
|
||
| // For each simulated datum, update the unbiased sample variance... | ||
| console.log( '\nValue\tVariance\n' ); | ||
| for ( i = 0; i < 100; i++ ) { | ||
| if ( randu() < 0.2 ) { | ||
| v = NaN; | ||
| } else { | ||
| v = randu() * 100.0; | ||
| } | ||
| s2 = accumulator( v ); | ||
| console.log( '%d\t%d', v, s2 ); | ||
| } | ||
| console.log( '\nFinal variance: %d\n', accumulator() ); | ||
|
|
||
| // Initialize an accumulator with a known mean: | ||
| accumulator = incrnanvariance( 50.0 ); | ||
|
|
||
| // For each simulated datum, update the unbiased sample variance... | ||
| console.log( '\nValue\tVariance\n' ); | ||
| for ( i = 0; i < 100; i++ ) { | ||
| if ( randu() < 0.2 ) { | ||
| v = NaN; | ||
| } else { | ||
| v = randu() * 100.0; | ||
| } | ||
| s2 = accumulator( v ); | ||
| console.log( '%d\t%d', v, s2 ); | ||
| } | ||
| console.log( '\nFinal variance: %d\n', accumulator() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire file is incorrect. Please follow our repl text style guide and avoid using LLMs to generate files.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was showing warning in the linting years so i added that licence code in that but now i have resolved it please check is that exactly you wanted