Skip to content

feat: add stats/base/dists/planck/median #4346

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 6 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
139 changes: 139 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/planck/median/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<!--

@license Apache-2.0

Copyright (c) 2025 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.

-->

# Median

> Planck (discrete exponential) distribution [median][median].

<!-- 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 [median][median] for a Planck random variable is

<!-- <equation class="equation" label="eq:planck_median" align="center" raw="\operatorname{Median}\left( X \right) = \left\lceil -\frac{\ln(0.5)}{\lambda} - 1 \right\rceil" alt="Median for a Planck distribution."> -->

```math
\mathop{\mathrm{Median}}\left( X \right) = \left\lceil -\frac{\ln(0.5)}{\lambda} \right\rceil \!-1
```

<!-- </equation> -->

where `λ` is the shape parameter.

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

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

#### median( lambda )

Returns the [median][median] of a Planck distribution with shape parameter `lambda`.

```javascript
var v = median( 0.1 );
// returns 6

v = median( 1.5 );
// returns 0
```

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

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

v = median( -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 uniform = require( '@stdlib/random/array/uniform' );
var median = require( '@stdlib/stats/base/dists/planck/median' );

var lambda = uniform( 10, 0.1, 10.0 );

var v;
var i;
for ( i = 0; i < lambda.length; i++ ) {
v = median( lambda[ i ] );
console.log( 'λ: %d, Median(X;λ): %d', lambda[ i ].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">

[median]: https://en.wikipedia.org/wiki/Median

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var median = require( './../lib' );


// MAIN //

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

lambda = uniform( 100, 0.1, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = median( lambda[ i % lambda.length ] );
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,26 @@

{{alias}}( λ )
Returns the median of a Planck distribution with shape parameter `λ`.

If `λ <= 0`, the function returns `NaN`.

Parameters
----------
λ: number
Shape parameter.

Returns
-------
out: integer
Median.

Examples
--------
> var v = {{alias}}( 0.1 )
6
> v = {{alias}}( 1.5 )
0

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 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 median of a Planck distribution.
*
* ## Notes
*
* - If `lambda <= 0`, the function returns `NaN`.
*
* @param lambda - shape parameter
* @returns median
*
* @example
* var v = median( 0.1 );
* // returns 6
*
* @example
* var v = median( 1.5 );
* // returns 0
*
* @example
* var v = median( -1.1 );
* // returns NaN
*
* @example
* var v = median( NaN );
* // returns NaN
*/
declare function median( lambda: number ): number;


// EXPORTS //

export = median;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 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 median = require( './index' );


// TESTS //

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

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

// The compiler throws an error if the function is provided insufficient arguments...
{
median(); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' );
var median = require( './../lib' );

var lambda = uniform( 10, 0.1, 10.0 );

var v;
var i;
for ( i = 0; i < lambda.length; i++ ) {
v = median( lambda[ i ] );
console.log( 'λ: %d, Median(X;λ): %d', lambda[ i ].toFixed( 4 ), v.toFixed( 4 ) );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 median.
*
* @module @stdlib/stats/base/dists/planck/median
*
* @example
* var median = require( '@stdlib/stats/base/dists/planck/median' );
*
* var v = median( 0.1 );
* // returns 6
*
* v = median( 1.5 );
* // returns 0
*/

// MODULES //

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


// EXPORTS //

module.exports = median;
Loading
Loading