Skip to content

Commit bcf2d8f

Browse files
committed
feat: add stats/base/dists/anglit/variance
--- 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 62019bc commit bcf2d8f

30 files changed

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

0 commit comments

Comments
 (0)