Skip to content

Commit 9c85ee6

Browse files
committed
feat: add stats/base/dists/anglit/stdev
--- 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: passed - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 3609aed commit 9c85ee6

26 files changed

Lines changed: 2011 additions & 0 deletions

File tree

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# Standard Deviation
22+
23+
> [Anglit][anglit-distribution] distribution [standard deviation][stdev].
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
The [standard deviation][stdev] for an [anglit][anglit-distribution] random variable with location parameter `mu` and scale parameter `sigma` is
30+
31+
<!-- <equation class="equation" label="eq:anglit_stdev" align="center" raw="\operatorname{SD}(X) = \sigma \sqrt{\left({\frac{\pi^{2}}{16}}-\frac{1}{2}\right)}" alt="Standard deviation for an anglit distribution."> -->
32+
33+
```math
34+
\operatorname{SD}(X) = \sigma \sqrt{\left({\frac{\pi^{2}}{16}}-\frac{1}{2}\right)}
35+
```
36+
37+
<!-- </equation> -->
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<!-- Package usage documentation. -->
44+
45+
<section class="usage">
46+
47+
## Usage
48+
49+
```javascript
50+
var stdev = require( '@stdlib/stats/base/dists/anglit/stdev' );
51+
```
52+
53+
#### stdev( mu, sigma )
54+
55+
Returns the [standard deviation][stdev] for an [anglit][anglit-distribution] distribution with location parameter `mu` and scale parameter `sigma`.
56+
57+
```javascript
58+
var y = stdev( 0.0, 1.0 );
59+
// returns ~0.342
60+
61+
y = stdev( 2.0, 4.0 );
62+
// returns ~1.367
63+
```
64+
65+
If provided `NaN` as any argument, the function returns `NaN`.
66+
67+
```javascript
68+
var y = stdev( NaN, 1.0 );
69+
// returns NaN
70+
71+
y = stdev( 0.0, NaN );
72+
// returns NaN
73+
```
74+
75+
If provided `sigma <= 0`, the function returns `NaN`.
76+
77+
```javascript
78+
var y = stdev( 0.0, 0.0 );
79+
// returns NaN
80+
81+
y = stdev( 0.0, -1.0 );
82+
// returns NaN
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="notes">
92+
93+
</section>
94+
95+
<!-- /.notes -->
96+
97+
<!-- Package usage examples. -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var uniform = require( '@stdlib/random/array/uniform' );
107+
var logEachMap = require( '@stdlib/console/log-each-map' );
108+
var stdev = require( '@stdlib/stats/base/dists/anglit/stdev' );
109+
110+
var opts = {
111+
'dtype': 'float64'
112+
};
113+
var mu = uniform( 10, -50.0, 50.0, opts );
114+
var sigma = uniform( 10, 0.1, 20.0, opts );
115+
116+
logEachMap( 'µ: %0.4f, σ: %0.4f, SD(X;µ,σ): %0.4f', mu, sigma, stdev );
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- C interface documentation. -->
124+
125+
* * *
126+
127+
<section class="c">
128+
129+
## C APIs
130+
131+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
132+
133+
<section class="intro">
134+
135+
</section>
136+
137+
<!-- /.intro -->
138+
139+
<!-- C usage documentation. -->
140+
141+
<section class="usage">
142+
143+
### Usage
144+
145+
```c
146+
#include "stdlib/stats/base/dists/anglit/stdev.h"
147+
```
148+
149+
#### stdlib_base_dists_anglit_stdev( mu, sigma )
150+
151+
Returns the [standard deviation][stdev] for an [anglit][anglit-distribution] distribution with location parameter `mu` and scale parameter `sigma`.
152+
153+
```c
154+
double out = stdlib_base_dists_anglit_stdev( 0.0, 1.0 );
155+
// returns ~0.342
156+
```
157+
158+
The function accepts the following arguments:
159+
160+
- **mu**: `[in] double` location parameter.
161+
- **sigma**: `[in] double` scale parameter.
162+
163+
```c
164+
double stdlib_base_dists_anglit_stdev( const double mu, const double sigma );
165+
```
166+
167+
</section>
168+
169+
<!-- /.usage -->
170+
171+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
172+
173+
<section class="notes">
174+
175+
</section>
176+
177+
<!-- /.notes -->
178+
179+
<!-- C API usage examples. -->
180+
181+
<section class="examples">
182+
183+
### Examples
184+
185+
```c
186+
#include "stdlib/stats/base/dists/anglit/stdev.h"
187+
#include <stdlib.h>
188+
#include <stdio.h>
189+
190+
static double random_uniform( const double min, const double max ) {
191+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
192+
return min + ( v * (max - min) );
193+
}
194+
195+
int main( void ) {
196+
double sigma;
197+
double mu;
198+
double y;
199+
int i;
200+
201+
for ( i = 0; i < 10; i++ ) {
202+
mu = random_uniform( -50.0, 50.0 );
203+
sigma = random_uniform( 0.1, 20.0 );
204+
y = stdlib_base_dists_anglit_stdev( mu, sigma );
205+
printf( "µ: %lf, σ: %lf, SD(X;µ,σ): %lf\n", mu, sigma, y );
206+
}
207+
}
208+
```
209+
210+
</section>
211+
212+
<!-- /.examples -->
213+
214+
</section>
215+
216+
<!-- /.c -->
217+
218+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
219+
220+
221+
<section class="related">
222+
223+
</section>
224+
225+
<!-- /.related -->
226+
227+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
228+
229+
<section class="links">
230+
231+
[anglit-distribution]: https://en.wikipedia.org/wiki/Anglit_distribution
232+
233+
[stdev]: https://en.wikipedia.org/wiki/Standard_deviation
234+
235+
</section>
236+
237+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 pkg = require( './../package.json' ).name;
27+
var stdev = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var sigma;
34+
var opts;
35+
var mu;
36+
var y;
37+
var i;
38+
39+
opts = {
40+
'dtype': 'float64'
41+
};
42+
mu = uniform( 100, -50.0, 50.0, opts );
43+
sigma = uniform( 100, 0.1, 20.0, opts );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
y = stdev( mu[ i % mu.length ], sigma[ i % sigma.length ] );
48+
if ( isnan( y ) ) {
49+
b.fail( 'should not return NaN' );
50+
}
51+
}
52+
b.toc();
53+
if ( isnan( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
31+
32+
// VARIABLES //
33+
34+
var stdev = tryRequire( resolve( __dirname, './../lib/native.js' ) );
35+
var opts = {
36+
'skip': ( stdev instanceof Error )
37+
};
38+
39+
40+
// MAIN //
41+
42+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
43+
var sigma;
44+
var opts;
45+
var mu;
46+
var y;
47+
var i;
48+
49+
opts = {
50+
'dtype': 'float64'
51+
};
52+
mu = uniform( 100, -50.0, 50.0, opts );
53+
sigma = uniform( 100, 0.1, 20.0, opts );
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
y = stdev( mu[ i % mu.length ], sigma[ i % sigma.length ] );
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( y ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});

0 commit comments

Comments
 (0)