Skip to content

Commit 39d2117

Browse files
committed
feat: add stats/base/ndarray/dsem
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 932f042 commit 39d2117

10 files changed

Lines changed: 887 additions & 0 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dsem
22+
23+
> Calculate the [standard error of the mean][standard-error] of a one-dimensional double-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
The [standard error of the mean][standard-error] of a finite size sample of size `n` is given by
28+
29+
<!-- <equation class="equation" label="eq:standard_error_of_the_mean" align="center" raw="\sigma_{\bar{x}} = \frac{\sigma}{\sqrt{n}}" alt="Equation for the standard error of the mean."> -->
30+
31+
```math
32+
\sigma_{\bar{x}} = \frac{\sigma}{\sqrt{n}}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\sigma_{\bar{x}} = \frac{\sigma}{\sqrt{n}}" data-equation="eq:standard_error_of_the_mean">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@227a94a6f2e49efe65e814f2da7bc9b9c2bec9b9/lib/node_modules/@stdlib/stats/strided/dsem/docs/img/equation_standard_error_of_the_mean.svg" alt="Equation for the standard error of the mean.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
where `σ` is the population [standard deviation][standard-deviation].
43+
44+
Often in the analysis of data, the true population [standard deviation][standard-deviation] is not known _a priori_ and must be estimated from a sample drawn from the population distribution. In this scenario, one must use a sample [standard deviation][standard-deviation] to compute an estimate for the [standard error of the mean][standard-error]
45+
46+
<!-- <equation class="equation" label="eq:standard_error_of_the_mean_estimate" align="center" raw="\sigma_{\bar{x}} \approx \frac{s}{\sqrt{n}}" alt="Equation for estimating the standard error of the mean."> -->
47+
48+
```math
49+
\sigma_{\bar{x}} \approx \frac{s}{\sqrt{n}}
50+
```
51+
52+
<!-- <div class="equation" align="center" data-raw-text="\sigma_{\bar{x}} \approx \frac{s}{\sqrt{n}}" data-equation="eq:standard_error_of_the_mean_estimate">
53+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@227a94a6f2e49efe65e814f2da7bc9b9c2bec9b9/lib/node_modules/@stdlib/stats/strided/dsem/docs/img/equation_standard_error_of_the_mean_estimate.svg" alt="Equation for estimating the standard error of the mean.">
54+
<br>
55+
</div> -->
56+
57+
<!-- </equation> -->
58+
59+
where `s` is the sample [standard deviation][standard-deviation].
60+
61+
</section>
62+
63+
<!-- /.intro -->
64+
65+
<section class="usage">
66+
67+
## Usage
68+
69+
```javascript
70+
var dsem = require( '@stdlib/stats/base/ndarray/dsem' );
71+
```
72+
73+
#### dsem( arrays )
74+
75+
Computes the [standard error of the mean][standard-error] of a one-dimensional double-precision floating-point ndarray.
76+
77+
```javascript
78+
var Float64Array = require( '@stdlib/array/float64' );
79+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
80+
var ndarray = require( '@stdlib/ndarray/ctor' );
81+
82+
var opts = {
83+
'dtype': 'float64'
84+
};
85+
86+
var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] );
87+
var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
88+
89+
var correction = scalar2ndarray( 1.0, opts );
90+
91+
var v = dsem( [ x, correction ] );
92+
// returns ~1.20185
93+
```
94+
95+
The function accepts the following arguments:
96+
97+
- **arrays**: array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying a degrees of freedom adjustment.
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="examples">
104+
105+
## Examples
106+
107+
```javascript
108+
var Float64Array = require( '@stdlib/array/float64' );
109+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
110+
var ndarray = require( '@stdlib/ndarray/ctor' );
111+
var dsem = require( '@stdlib/stats/base/ndarray/dsem' );
112+
113+
var opts = {
114+
'dtype': 'float64'
115+
};
116+
117+
var xbuf = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
118+
var x = new ndarray( opts.dtype, xbuf, [ 4 ], [ 2 ], 1, 'row-major' );
119+
120+
var correction = scalar2ndarray( 1.0, opts );
121+
122+
var v = dsem( [ x, correction ] );
123+
// returns 1.25
124+
```
125+
126+
</section>
127+
128+
<!-- /.examples -->
129+
130+
<!-- Section for related `stdlib` packages. -->
131+
132+
<section class="related">
133+
134+
</section>
135+
136+
<!-- /.related -->
137+
138+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
139+
140+
<section class="links">
141+
142+
[standard-error]: https://en.wikipedia.org/wiki/Standard_error
143+
144+
[standard-deviation]: https://en.wikipedia.org/wiki/Standard_deviation
145+
146+
</section>
147+
148+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var dsem = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float64'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var correction;
52+
var xbuf;
53+
var x;
54+
55+
xbuf = uniform( len, -10.0, 10.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
correction = scalar2ndarray( 1.0, options );
58+
59+
return benchmark;
60+
61+
function benchmark( b ) {
62+
var v;
63+
var i;
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
v = dsem( [ x, correction ] );
68+
if ( isnan( v ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( v ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 6; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s::len=%d', pkg, len ), f );
103+
}
104+
}
105+
106+
main();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( arrays )
3+
Computes the standard error of the mean of a one-dimensional
4+
double-precision floating-point ndarray.
5+
6+
If provided an empty one-dimensional ndarray, the function returns `NaN`.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing a one-dimensional input ndarray and a
12+
zero-dimensional ndarray specifying a degrees of freedom adjustment.
13+
14+
Returns
15+
-------
16+
out: number
17+
The standard error of the mean.
18+
19+
Examples
20+
--------
21+
// Create input ndarray:
22+
> var Float64Array = require( '@stdlib/array/float64' );
23+
> var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] );
24+
> var dt = 'float64';
25+
> var sh = [ xbuf.length ];
26+
> var st = [ 1 ];
27+
> var oo = 0;
28+
> var ord = 'row-major';
29+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord );
30+
31+
// Create correction ndarray:
32+
> var opts = { 'dtype': dt };
33+
> var correction = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, opts );
34+
35+
// Compute the standard error of the mean:
36+
> {{alias}}( [ x, correction ] )
37+
~1.20185
38+
39+
See Also
40+
--------
41+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the standard error of the mean of a one-dimensional double-precision floating-point ndarray.
27+
*
28+
* @param arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray specifying a degrees of freedom adjustment
29+
* @returns standard error of the mean
30+
*
31+
* @example
32+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
33+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
34+
* var Float64Array = require( '@stdlib/array/float64' );
35+
*
36+
* var opts = {
37+
* 'dtype': 'float64'
38+
* };
39+
*
40+
* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] );
41+
* var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
42+
* var correction = scalar2ndarray( 1.0, opts );
43+
*
44+
* var v = dsem( [ x, correction ] );
45+
* // returns ~1.20185
46+
*/
47+
declare function dsem<T extends typedndarray<number> = typedndarray<number>>( arrays: [ T, T ] ): number;
48+
49+
50+
// EXPORTS //
51+
52+
export = dsem;

0 commit comments

Comments
 (0)