Skip to content
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

feat: add stats/base/dists/planck/mean #3942

Merged
merged 6 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/mean/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<!--

@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.

-->

# Mean

> Planck distribution [expected value][expected-value].

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

The [expected value][expected-value] for a geometric random variable is
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved

<!-- <equation class="equation" label="eq:planck_expectation" align="center" raw="\mathbb{E}\left[ X \right] = \frac{1}{e^{\lambda} - 1}" alt="Expected value for a Planck distribution."> -->

```math
\mathbb{E}\left[ X \right] = \frac{1}{e^{\lambda} - 1}
```

<!-- </equation> -->

where `lambda` is the shape parameter.

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var mean = require( '@stdlib/stats/base/dists/planck/mean' );
```

#### mean( lambda )

Returns the [expected value][expected-value] of a planck distribution with shape parameter `lambda`.
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved

```javascript
var v = mean( 0.1 );
// returns ~9.5083

v = mean( 0.5 );
// returns ~1.5415
```

If provided a shape parameter `lambda` which is negative or `NaN`, the function returns `NaN`.

```javascript
var v = mean( NaN );
// returns NaN

v = mean( -1.5 );
// returns NaN
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<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 mean = require( '@stdlib/stats/base/dists/planck/mean' );

var lambda;
var v;
var i;

for ( i = 0; i < 10; i++ ) {
lambda = randu();
v = mean( lambda );
console.log( 'lambda: %d, E(X;lambda): %d', lambda.toFixed( 4 ), v.toFixed( 4 ) );
}
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- 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">

[expected-value]: https://en.wikipedia.org/wiki/Expected_value

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @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' );
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
var mean = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var lambda;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
lambda = ( randu()*20.0 ) + EPS;
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved
y = mean( 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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

{{alias}}( lambda )
Returns the expected value of a planck distribution with shape
parameter `lambda`.

If `lambda < 0`, the function returns `NaN`.

Parameters
----------
lambda: number
Shape parameter.

Returns
-------
out: number
Expected value.

Examples
--------
> var v = {{alias}}( 0.1 )
~9.5083
> v = {{alias}}( 0.5 )
~1.5415

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* @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.
*/

// TypeScript Version: 4.1

/**
* Returns the expected value of a planck distribution.
*
* ## Notes
*
* - If `lambda < 0`, the function returns `NaN`.
*
* @param lambda - shape parameter
* @returns expected value
*
* @example
* var v = mean( 0.1 );
* // returns ~9.5083
*
* @example
* var v = mean( 0.5 );
* // returns ~1.5415
*
* @example
* var v = mean( 1.1 );
* // returns ~0.4989
*
* @example
* var v = mean( NaN );
* // returns NaN
*/
declare function mean( lambda: number ): number;


// EXPORTS //

export = mean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @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.
*/

import mean = require( './index' );


// TESTS //

// The function returns a number...
{
mean( 0.3 ); // $ExpectType number
}

// The compiler throws an error if the function is provided a value other than a number...
{
mean( true ); // $ExpectError
mean( false ); // $ExpectError
mean( null ); // $ExpectError
mean( undefined ); // $ExpectError
mean( '5' ); // $ExpectError
mean( [] ); // $ExpectError
mean( {} ); // $ExpectError
mean( ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided insufficient arguments...
{
mean(); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @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';

var randu = require( '@stdlib/random/base/randu' );
Jaysukh-409 marked this conversation as resolved.
Show resolved Hide resolved
var mean = require( './../lib' );

var lambda;
var v;
var i;

for ( i = 0; i < 10; i++ ) {
lambda = randu();
v = mean( lambda );
console.log( 'lambda: %d, E(X;lambda): %d', lambda.toFixed( 4 ), v.toFixed( 4 ) );
}
43 changes: 43 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/mean/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @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';

/**
* Planck distribution expected value.
*
* @module @stdlib/stats/base/dists/planck/mean
*
* @example
* var mean = require( '@stdlib/stats/base/dists/planck/mean' );
*
* var v = mean( 0.1 );
* // returns ~9.5083
*
* v = mean( 0.5 );
* // returns ~1.5415
*/

// MODULES //

var mean = require( './main.js' );


// EXPORTS //

module.exports = mean;
Loading
Loading