Skip to content

Commit 5444ad4

Browse files
Jaysukh-409kgryte
andauthored
feat: add stats/base/dists/planck/mgf
PR-URL: #4478 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 4b88dd6 commit 5444ad4

File tree

16 files changed

+1157
-0
lines changed

16 files changed

+1157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
# Moment-Generating Function
22+
23+
> Planck (discrete exponential) distribution moment-generating function (MGF).
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 [moment-generating function][mgf] for a Planck random variable is
30+
31+
<!-- <equation class="equation" label="eq:planck_mgf_function" align="center" raw="M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \frac{1 - e^{-\lambda}}{1 - e^{t - \lambda}}" alt="Moment-generating function (MGF) for the Planck distribution."> -->
32+
33+
```math
34+
M_X(t) := \mathbb{E}\!\left[e^{tX}\right] = \frac{1 - e^{-\lambda}}{1 - e^{t - \lambda}}
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 mgf = require( '@stdlib/stats/base/dists/planck/mgf' );
53+
```
54+
55+
#### mgf( t, lambda )
56+
57+
Evaluates the moment-generating function ([MGF][mgf]) of a Planck (discrete exponential) distribution with shape parameter `lambda`.
58+
59+
```javascript
60+
var y = mgf( 0.2, 0.5 );
61+
// returns ~1.5181
62+
63+
y = mgf( 0.4, 1.5 );
64+
// returns ~1.1645
65+
```
66+
67+
If provided `NaN` as any argument, the function returns `NaN`.
68+
69+
```javascript
70+
var y = mgf( NaN, 0.0 );
71+
// returns NaN
72+
73+
y = mgf( 0.0, NaN );
74+
// returns NaN
75+
```
76+
77+
If provided a shape parameter `lambda` which is nonpositive, the function returns `NaN`.
78+
79+
```javascript
80+
var y = mgf( 2.0, -1.0 );
81+
// returns NaN
82+
```
83+
84+
#### mgf.factory( lambda )
85+
86+
Returns a function for evaluating the [moment-generating function][mgf] of a Planck (discrete exponential) distribution with shape parameter `lambda`.
87+
88+
```javascript
89+
var mymgf = mgf.factory( 0.8 );
90+
var y = mymgf( -0.2 );
91+
// returns ~0.8711
92+
```
93+
94+
</section>
95+
96+
<!-- /.usage -->
97+
98+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
99+
100+
<section class="notes">
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<!-- Package usage examples. -->
107+
108+
<section class="examples">
109+
110+
## Examples
111+
112+
<!-- eslint no-undef: "error" -->
113+
114+
```javascript
115+
var uniform = require( '@stdlib/random/array/uniform' );
116+
var mgf = require( '@stdlib/stats/base/dists/planck/mgf' );
117+
118+
var lambda = uniform( 10, 0.1, 5.0 );
119+
var t = uniform( 10, 0.0, 10.0 );
120+
121+
var y;
122+
var i;
123+
for ( i = 0; i < lambda.length; i++ ) {
124+
y = mgf( t[ i ], lambda[ i ] );
125+
console.log( 't: %d, λ: %d, M_X(t;λ): %d', t[ i ].toFixed( 4 ), lambda[ i ].toFixed( 4 ), y.toFixed( 4 ) );
126+
}
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- 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. -->
134+
135+
<section class="references">
136+
137+
</section>
138+
139+
<!-- /.references -->
140+
141+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
142+
143+
<section class="related">
144+
145+
</section>
146+
147+
<!-- /.related -->
148+
149+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="links">
152+
153+
[mgf]: https://en.wikipedia.org/wiki/Moment-generating_function
154+
155+
</section>
156+
157+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 pkg = require( './../package.json' ).name;
27+
var mgf = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var lambda;
34+
var t;
35+
var y;
36+
var i;
37+
38+
lambda = uniform( 100, 0.1, 10.0 );
39+
t = uniform( 100, 0.0, 10.0 );
40+
41+
b.tic();
42+
for ( i = 0; i < b.iterations; i++ ) {
43+
y = mgf( t[ i % t.length ], lambda[ i % lambda.length ] );
44+
if ( isnan( y ) ) {
45+
b.fail( 'should not return NaN' );
46+
}
47+
}
48+
b.toc();
49+
if ( isnan( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
b.pass( 'benchmark finished' );
53+
b.end();
54+
});
55+
56+
bench( pkg+':factory', function benchmark( b ) {
57+
var mymgf;
58+
var t;
59+
var y;
60+
var i;
61+
62+
t = uniform( 100, 0.0, 10.0 );
63+
mymgf = mgf.factory( 0.3 );
64+
65+
b.tic();
66+
for ( i = 0; i < b.iterations; i++ ) {
67+
y = mymgf( t[ i % t.length ] );
68+
if ( isnan( y ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( y ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
{{alias}}( t, λ )
3+
Evaluates the moment-generating function (MGF) for a Planck distribution
4+
with shape parameter `λ` at a value `t`.
5+
6+
If provided `NaN` as any argument, the function returns `NaN`.
7+
8+
If `λ <= 0`, the function returns `NaN`.
9+
10+
Parameters
11+
----------
12+
t: number
13+
Input value.
14+
15+
λ: number
16+
Shape parameter.
17+
18+
Returns
19+
-------
20+
out: number
21+
Evaluated MGF.
22+
23+
Examples
24+
--------
25+
> var y = {{alias}}( 0.2, 0.5 )
26+
~1.5181
27+
> y = {{alias}}( 0.4, 1.5 )
28+
~1.1645
29+
> y = {{alias}}( NaN, 0.0 )
30+
NaN
31+
> y = {{alias}}( 0.0, NaN )
32+
NaN
33+
> y = {{alias}}( 2.0, -1.0 )
34+
NaN
35+
36+
37+
{{alias}}.factory( λ )
38+
Returns a function for evaluating the moment-generating function (MGF) of a
39+
Planck distribution with shape parameter `λ`.
40+
41+
Parameters
42+
----------
43+
λ: number
44+
Shape parameter.
45+
46+
Returns
47+
-------
48+
mgf: Function
49+
Moment-generating function (MGF).
50+
51+
Examples
52+
--------
53+
> var mymgf = {{alias}}.factory( 0.8 );
54+
> var y = mymgf( -0.2 )
55+
~0.8711
56+
57+
See Also
58+
--------
59+

0 commit comments

Comments
 (0)