Skip to content

Commit 797d608

Browse files
Jaysukh-409kgryte
authored andcommitted
feat: add stats/base/dists/planck/variance
PR-URL: stdlib-js#4102 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 14b5e97 commit 797d608

File tree

12 files changed

+679
-0
lines changed

12 files changed

+679
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
> Planck 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 Planck random variable is
30+
31+
<!-- <equation class="equation" label="eq:planck_variance" align="center" raw="\operatorname{Var}\left( X \right) = \frac{e^{-\lambda}}{\left( 1 - e^{-\lambda} \right)^{2}}" alt="Variance for a Planck distribution."> -->
32+
33+
```math
34+
\mathop{\mathrm{Var}}\left( X \right) = \frac{e^{-\lambda}}{\left( 1 - e^{-\lambda} \right)^{2}}
35+
```
36+
37+
<!-- </equation> -->
38+
39+
where `λ` is the shape parameter.
40+
41+
</section>
42+
43+
<!-- /.intro -->
44+
45+
<!-- Package usage documentation. -->
46+
47+
<section class="usage">
48+
49+
## Usage
50+
51+
```javascript
52+
var variance = require( '@stdlib/stats/base/dists/planck/variance' );
53+
```
54+
55+
#### variance( lambda )
56+
57+
Returns the [variance][variance] of a Planck distribution with shape parameter `λ`.
58+
59+
```javascript
60+
var v = variance( 0.1 );
61+
// returns ~99.9167
62+
63+
v = variance( 1.5 );
64+
// returns ~0.3697
65+
```
66+
67+
If provided a shape parameter `λ` which is nonpositive, the function returns `NaN`.
68+
69+
```javascript
70+
var v = variance( NaN );
71+
// returns NaN
72+
73+
v = variance( -1.0 );
74+
// returns NaN
75+
```
76+
77+
</section>
78+
79+
<!-- /.usage -->
80+
81+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
82+
83+
<section class="notes">
84+
85+
</section>
86+
87+
<!-- /.notes -->
88+
89+
<!-- Package usage examples. -->
90+
91+
<section class="examples">
92+
93+
## Examples
94+
95+
<!-- eslint no-undef: "error" -->
96+
97+
```javascript
98+
var uniform = require( '@stdlib/random/array/uniform' );
99+
var variance = require( '@stdlib/stats/base/dists/planck/variance' );
100+
101+
var lambda = uniform( 10, 0.1, 10.0 );
102+
103+
var v;
104+
var i;
105+
for ( i = 0; i < lambda.length; i++ ) {
106+
v = variance( lambda[ i ] );
107+
console.log( 'λ: %d, Var(X;λ): %d', lambda[ i ].toFixed( 4 ), v.toFixed( 4 ) );
108+
}
109+
```
110+
111+
</section>
112+
113+
<!-- /.examples -->
114+
115+
<!-- 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. -->
116+
117+
<section class="references">
118+
119+
</section>
120+
121+
<!-- /.references -->
122+
123+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
124+
125+
<section class="related">
126+
127+
</section>
128+
129+
<!-- /.related -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
[variance]: https://en.wikipedia.org/wiki/Variance
136+
137+
</section>
138+
139+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 variance = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var lambda;
34+
var y;
35+
var i;
36+
37+
lambda = uniform( 100, 0.1, 10.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = variance( lambda[ i % lambda.length ] );
42+
if ( isnan( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnan( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( λ )
3+
Returns the variance of a Planck distribution with shape parameter `λ`.
4+
5+
If `λ <= 0`, the function returns `NaN`.
6+
7+
Parameters
8+
----------
9+
λ: number
10+
Shape parameter.
11+
12+
Returns
13+
-------
14+
out: number
15+
Variance.
16+
17+
Examples
18+
--------
19+
> var v = {{alias}}( 0.1 )
20+
~99.9167
21+
> v = {{alias}}( 1.5 )
22+
~0.3697
23+
24+
See Also
25+
--------
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
22+
* Returns the variance of a Planck distribution.
23+
*
24+
* ## Notes
25+
*
26+
* - If `lambda <= 0`, the function returns `NaN`.
27+
*
28+
* @param lambda - shape parameter
29+
* @returns variance
30+
*
31+
* @example
32+
* var v = variance( 0.1 );
33+
* // returns ~99.9167
34+
*
35+
* @example
36+
* var v = variance( 1.5 );
37+
* // returns ~0.3697
38+
*
39+
* @example
40+
* var v = variance( -1.0 );
41+
* // returns NaN
42+
*
43+
* @example
44+
* var v = variance( NaN );
45+
* // returns NaN
46+
*/
47+
declare function variance( lambda: number ): number;
48+
49+
50+
// EXPORTS //
51+
52+
export = variance;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
import variance = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
variance( 0.3 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if the function is provided a value other than a number...
30+
{
31+
variance( true ); // $ExpectError
32+
variance( false ); // $ExpectError
33+
variance( null ); // $ExpectError
34+
variance( undefined ); // $ExpectError
35+
variance( '5' ); // $ExpectError
36+
variance( [] ); // $ExpectError
37+
variance( {} ); // $ExpectError
38+
variance( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided insufficient arguments...
42+
{
43+
variance(); // $ExpectError
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
var uniform = require( '@stdlib/random/array/uniform' );
22+
var variance = require( './../lib' );
23+
24+
var lambda = uniform( 10, 0.1, 10.0 );
25+
26+
var v;
27+
var i;
28+
for ( i = 0; i < lambda.length; i++ ) {
29+
v = variance( lambda[ i ] );
30+
console.log( 'λ: %d, Var(X;λ): %d', lambda[ i ].toFixed( 4 ), v.toFixed( 4 ) );
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
/**
22+
* Planck distribution variance.
23+
*
24+
* @module @stdlib/stats/base/dists/planck/variance
25+
*
26+
* @example
27+
* var variance = require( '@stdlib/stats/base/dists/planck/variance' );
28+
*
29+
* var v = variance( 0.1 );
30+
* // returns ~99.9167
31+
*
32+
* v = variance( 1.5 );
33+
* // returns ~0.3697
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)