-
-
Notifications
You must be signed in to change notification settings - Fork 826
feat: add stats/base/dists/planck/pmf
#3896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1de1097
feat: add stats/base/dists/planck
Jaysukh-409 c8e3626
test: fix the invalid tests
Jaysukh-409 c81fe33
test: add test for large and small shape parameter
Jaysukh-409 b854d88
fix: change implementation to clear all test
Jaysukh-409 e6fce33
fix: lint errors
Jaysukh-409 1e9e606
fix: change type of lambda
Jaysukh-409 b05f01f
docs: update the condition description
Jaysukh-409 f01e62b
fix: apply code review suggestion
Jaysukh-409 f162cbb
Merge remote-tracking branch 'upstream/develop' into planck
stdlib-bot 44ceba0
Merge remote-tracking branch 'upstream/develop' into planck
stdlib-bot 5bbe766
fix: apply code review suggestion
Jaysukh-409 c6fd26b
docs: update the function argument name
Jaysukh-409 1d8dd6f
chore: minor clean up
Jaysukh-409 704afdd
chore: clean-up
Jaysukh-409 3ed9705
chore: update docs
Jaysukh-409 47f2614
chore: clean-up and remove namespace files
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
lib/node_modules/@stdlib/stats/base/dists/planck/lib/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/* | ||
* When adding modules to the namespace, ensure that they are added in alphabetical order according to module name. | ||
*/ | ||
|
||
// MODULES // | ||
|
||
var setReadOnly = require( '@stdlib/utils/define-read-only-property' ); | ||
|
||
|
||
// MAIN // | ||
|
||
/** | ||
* Top-level namespace. | ||
* | ||
* @namespace planck | ||
*/ | ||
var planck = {}; | ||
|
||
/** | ||
* @name pmf | ||
* @memberof planck | ||
* @readonly | ||
* @type {Function} | ||
* @see {@link module:@stdlib/stats/base/dists/planck/pmf} | ||
*/ | ||
setReadOnly( planck, 'pmf', require( '@stdlib/stats/base/dists/planck/pmf' ) ); | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = planck; |
144 changes: 144 additions & 0 deletions
144
lib/node_modules/@stdlib/stats/base/dists/planck/pmf/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2024 The Stdlib Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
|
||
--> | ||
|
||
# Probability Mass Function | ||
|
||
> Planck distribution [probability mass function][pmf] (PMF). | ||
|
||
<section class="intro"> | ||
|
||
The [probability mass function][pmf] (PMF) for a planck random variable is defined as | ||
|
||
<!-- <equation class="equation" label="eq:planck_pmf" align="center" raw="\Pr(X = x, \lambda) = \begin{cases}(1 - e^{-\lambda})e^{-\lambda x} & \text{for } x = 0, 1, 2, \ldots \\ 0 & \text{otherwise} \end{cases}" alt="Probability mass function (PMF) for a Planck distribution."> --> | ||
|
||
```math | ||
\Pr(X = x, \lambda) = \begin{cases}(1 - e^{-\lambda})e^{-\lambda x} & \text{for } x = 0, 1, 2, \ldots \\ 0 & \text{otherwise} \end{cases} | ||
``` | ||
|
||
<!-- </equation> --> | ||
|
||
where `lambda > 0` is the shape parameter. The random variable `X` denotes the count of events in a quantized system. | ||
|
||
</section> | ||
|
||
<!-- /.intro --> | ||
|
||
<section class="usage"> | ||
|
||
## Usage | ||
|
||
```javascript | ||
var pmf = require( '@stdlib/stats/base/dists/planck/pmf' ); | ||
``` | ||
|
||
#### pmf( x, p ) | ||
|
||
Evaluates the [probability mass function][pmf] (PMF) of a planck distribution with shape parameter `lambda`. | ||
|
||
```javascript | ||
var y = pmf( 4.0, 0.3 ); | ||
// returns ~0.0781 | ||
|
||
y = pmf( 2.0, 1.7 ); | ||
// returns ~0.0273 | ||
|
||
y = pmf( -1.0, 2.5 ); | ||
// returns 0.0 | ||
``` | ||
|
||
If provided `NaN` as any argument, the function returns `NaN`. | ||
|
||
```javascript | ||
var y = pmf( NaN, 0.0 ); | ||
// returns NaN | ||
|
||
y = pmf( 0.0, NaN ); | ||
// returns NaN | ||
``` | ||
|
||
If provided a shape parameter `lambda` which is negative number, the function returns `NaN`. | ||
|
||
```javascript | ||
var y = pmf( 2.0, -1.0 ); | ||
// returns NaN | ||
``` | ||
|
||
#### pmf.factory( p ) | ||
|
||
Returns a function for evaluating the [probability mass function][pmf] (PMF) of a planck distribution with shape parameter `lambda`. | ||
|
||
```javascript | ||
var mypmf = pmf.factory( 0.5 ); | ||
var y = mypmf( 3.0 ); | ||
// returns ~0.0878 | ||
|
||
y = mypmf( 1.0 ); | ||
// returns ~0.2387 | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.usage --> | ||
|
||
<section class="examples"> | ||
|
||
## Examples | ||
|
||
<!-- eslint no-undef: "error" --> | ||
|
||
```javascript | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
Jaysukh-409 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var round = require( '@stdlib/math/base/special/round' ); | ||
Jaysukh-409 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var pmf = require( '@stdlib/stats/base/dists/planck/pmf' ); | ||
|
||
var lambda; | ||
var x; | ||
var y; | ||
var i; | ||
|
||
for ( i = 0; i < 10; i++ ) { | ||
x = round( randu() * 5.0 ); | ||
lambda = randu(); | ||
y = pmf( x, lambda ); | ||
console.log( 'x: %d, lambda: %d, P( X = x; lambda ): %d', x, lambda.toFixed( 4 ), y.toFixed( 4 ) ); | ||
} | ||
``` | ||
|
||
</section> | ||
|
||
<!-- /.examples --> | ||
|
||
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
||
<section class="related"> | ||
|
||
</section> | ||
|
||
<!-- /.related --> | ||
|
||
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
||
<section class="links"> | ||
|
||
[pmf]: https://en.wikipedia.org/wiki/Probability_mass_function | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
81 changes: 81 additions & 0 deletions
81
lib/node_modules/@stdlib/stats/base/dists/planck/pmf/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 The Stdlib Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// MODULES // | ||
|
||
var bench = require( '@stdlib/bench' ); | ||
var randu = require( '@stdlib/random/base/randu' ); | ||
var round = require( '@stdlib/math/base/special/round' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var EPS = require( '@stdlib/constants/float64/eps' ); | ||
var pkg = require( './../package.json' ).name; | ||
var pmf = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var lambda; | ||
var x; | ||
var y; | ||
var i; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
x = round( randu()*40.0 ); | ||
Jaysukh-409 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
lambda = ( randu()*1.0 ) + EPS; | ||
y = pmf( x, lambda ); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); | ||
|
||
bench( pkg+':factory', function benchmark( b ) { | ||
var lambda; | ||
var mypmf; | ||
var x; | ||
var y; | ||
var i; | ||
|
||
lambda = 0.3; | ||
mypmf = pmf.factory( lambda ); | ||
Jaysukh-409 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
x = randu() * 40.0; | ||
Jaysukh-409 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
y = mypmf( x ); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( isnan( y ) ) { | ||
b.fail( 'should not return NaN' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
63 changes: 63 additions & 0 deletions
63
lib/node_modules/@stdlib/stats/base/dists/planck/pmf/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
{{alias}}( x, lambda ) | ||
Evaluates the probability mass function (PMF) for a planck distribution | ||
with shape parameter `lambda` at a value `x`. | ||
|
||
If provided `NaN` as any argument, the function returns `NaN`. | ||
|
||
If `lambda < 0`, the function returns `NaN`. | ||
|
||
Parameters | ||
---------- | ||
x: number | ||
Input value. | ||
|
||
lambda: number | ||
Shape parameter. | ||
|
||
Returns | ||
------- | ||
out: number | ||
Evaluated PMF. | ||
|
||
Examples | ||
-------- | ||
> var y = {{alias}}( 4.0, 0.3 ) | ||
~0.0781 | ||
> y = {{alias}}( 2.0, 1.7 ) | ||
~0.0273 | ||
> y = {{alias}}( -1.0, 0.5 ) | ||
0.0 | ||
> y = {{alias}}( 0.0, NaN ) | ||
NaN | ||
> y = {{alias}}( NaN, 0.5 ) | ||
NaN | ||
// Invalid shape parameter: | ||
> y = {{alias}}( 2.0, -1.0 ) | ||
NaN | ||
|
||
|
||
{{alias}}.factory( lambda ) | ||
Returns a function for evaluating the probability mass function (PMF) of a | ||
planck distribution with shape parameter `lambda`. | ||
|
||
Parameters | ||
---------- | ||
lambda: number | ||
Shape parameter. | ||
|
||
Returns | ||
------- | ||
pmf: Function | ||
Probability mass function (PMF). | ||
|
||
Examples | ||
-------- | ||
> var mypmf = {{alias}}.factory( 0.5 ); | ||
> var y = mypmf( 3.0 ) | ||
~0.0878 | ||
> y = mypmf( 1.0 ) | ||
~0.2387 | ||
|
||
See Also | ||
-------- |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.