Skip to content

Commit b1762eb

Browse files
committed
feat: add implementation of stats/base/dists/gilbrat/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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 5ad3041 commit b1762eb

26 files changed

Lines changed: 1883 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)