Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
57 changes: 57 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanvariance/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copy link
Member

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.

Copy link
Contributor Author

@Krishna-Sharma-g Krishna-Sharma-g Mar 12, 2025

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

* @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;
58 changes: 58 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/test.ts
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all of these as any casts. The entire point of these lines is to test that the compiler errors when provided various non-any input values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did it because it was showing some ecmacript error in it but i have changed it now and removed the as any from there please review

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
}
59 changes: 59 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanvariance/examples/index.js
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() );
Loading
Loading