Skip to content
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

feat(stats/base/sstdevyc): add accessor protocol support #6159

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
83 changes: 83 additions & 0 deletions lib/node_modules/@stdlib/stats/base/sstdevyc/lib/accessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @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';

/**
* Computes the standard deviation of a single-precision floating-point strided array using an accessor function and a one-pass algorithm proposed by Youngs and Cramer.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {Function} accessor - accessor function for accessing array elements
* @param {Object} x - input array-like object supporting the accessor protocol
* @param {integer} stride - stride length
* @param {number} correction - degrees of freedom adjustment (e.g., 0 for population, 1 for sample)
* @returns {number} standard deviation
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* function accessor( idx ) {
* return this.values[ idx ];
* }
*
* var x = {
* 'values': new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ),
* 'get': accessor
* };
*
* var v = accessor( 5, x.get, x, 1, 1 );
* // returns ~1.4142
*/
function accessor( N, accessor, x, stride, correction ) {
if (typeof accessor !== 'function') {
throw new TypeError( 'invalid argument. Second argument must be a function.' );
}
if (!x || typeof x !== 'object') {
return NaN;
}
if (!Number.isInteger(N) || N <= 0) {
return NaN;
}
if (!Number.isFinite(correction) || correction < 0 || correction >= N) {
return NaN;
}
if (!Number.isInteger(stride)) {
return NaN;
}
if (N === 1) {
return 0.0;
}

var mean = 0.0;
var M2 = 0.0;
var ix = stride < 0 ? (1 - N) * stride : 0;

for (var i = 0; i < N; i++, ix += stride) {
var v = accessor.call( x, ix );
if (typeof v !== 'number' || isNaN(v)) {
return NaN;
}
var delta = v - mean;
mean += delta / (i + 1);
M2 += delta * (v - mean);
}

return Math.sqrt( M2 / (N - correction) );
}

module.exports = accessor;
1 change: 1 addition & 0 deletions lib/node_modules/@stdlib/stats/base/sstdevyc/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ if ( isError( tmp ) ) {

// EXPORTS //

module.exports.ndarray = require('./ndarray');
module.exports = sstdevyc;

// exports: { "ndarray": "sstdevyc.ndarray" }
103 changes: 59 additions & 44 deletions lib/node_modules/@stdlib/stats/base/sstdevyc/lib/ndarray.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,73 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2020 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.
*/
* @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 svarianceyc = require( '@stdlib/stats/base/svarianceyc' ).ndarray;
var sqrtf = require( '@stdlib/math/base/special/sqrtf' );

var accessors = require( './accessors.js' );

// MAIN //

/**
* Computes the standard deviation of a single-precision floating-point strided array using a one-pass algorithm proposed by Youngs and Cramer.
*
* ## References
*
* - Youngs, Edward A., and Elliot M. Cramer. 1971. "Some Results Relevant to Choice of Sum and Sum-of-Product Algorithms." _Technometrics_ 13 (3): 657–65. doi:[10.1080/00401706.1971.10488826](https://doi.org/10.1080/00401706.1971.10488826).
*
* @param {PositiveInteger} N - number of indexed elements
* @param {number} correction - degrees of freedom adjustment
* @param {Float32Array} x - input array
* @param {integer} strideX - stride length
* @param {NonNegativeInteger} offsetX - starting index
* @returns {number} standard deviation
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
*
* var v = sstdevyc( 4, 1, x, 2, 1 );
* // returns 2.5
*/
function sstdevyc( N, correction, x, strideX, offsetX ) {
return sqrtf( svarianceyc( N, correction, x, strideX, offsetX ) );
}
* Computes the standard deviation of a single-precision floating-point strided array using a one-pass algorithm proposed by Youngs and Cramer.
*
* @param {PositiveInteger} N - number of indexed elements
* @param {number} correction - degrees of freedom adjustment (e.g., 0 for population, 1 for sample)
* @param {Float32Array|ArrayLikeObject} x - input array-like object
* @param {integer} stride - stride length
* @param {NonNegativeInteger} offset - starting index
* @returns {number} standard deviation
*
* @example
* var Float32Array = require( '@stdlib/array/float32' );
*
* var x = new Float32Array( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] );
* var v = sstdevyc( 5, 1, x, 1, 0 );
* // returns ~3.1623
*/
function sstdevyc( N, correction, x, stride, offset ) {
// Input validation
if (!Number.isInteger(N) || N <= 0) {
return NaN;
}
if (!Number.isFinite(correction) || correction < 0 || correction >= N) {
return NaN;
}
if (!Number.isInteger(stride)) {
return NaN;
}
if (!Number.isInteger(offset) || offset < 0) {
return NaN;
}
if (!x || typeof x !== 'object') {
return NaN;
}

// Check for accessor protocol (only `get` is required for reading)
if (typeof x.get === 'function') {
return accessors( N, x.get, x, stride, correction );
}

// EXPORTS //
// Fallback to variance-based computation for non-accessor arrays
return sqrtf( svarianceyc( N, correction, x, stride, offset ) );
}

module.exports = sstdevyc;
// EXPORTS //
module.exports = sstdevyc;
Loading