Skip to content

feat: add stats/base/dists/bradford/variance #5326

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 10 commits into from
Feb 22, 2025
138 changes: 138 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/bradford/variance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<!--

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

-->

# Mean

> [Bradford][bradford-distribution] distribution [variance][variance].

<!-- 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 [variance][variance] for a [Bradford][bradford-distribution] random variable is

<!-- <equation class="equation" label="eq:bradford_variance" align="center" raw="\mathop{\mathrm{Var}}\left( X \right) = \frac{(c+2) \ln(1+c) - 2c}{2c (\ln(1+c))^2}" alt="Variance for a Bradford distribution."> -->

```math
\mathop{\mathrm{Var}}\left( X \right) = \frac{(c+2) \ln(1+c) - 2c}{2c (\ln(1+c))^2}
```

<!-- <div class="equation" align="center" data-raw-text="\mathop{\mathrm{Var}}\left( X \right) = \frac{(c+2) \ln(1+c) - 2c}{2c (\ln(1+c))^2}" data-equation="eq:bradford_variance">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/stats/base/dists/bradford/variance/docs/img/equation_bradford_variance.svg" alt="Variance for a Bradford distribution.">
<br>
</div> -->

<!-- </equation> -->

where `c` is the shape parameter.

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var variance = require( '@stdlib/stats/base/dists/bradford/variance' );
```

#### variance( c )

Returns the [variance][variance] of a [Bradford][bradford-distribution] distribution with shape parameter `c`.

```javascript
var v = variance( 0.1 );
// returns ~0.083

v = variance( 10.0 );
// returns ~0.076
```

If provided a shape parameter `c <= 0`, the function returns `NaN`.

```javascript
var v = variance( 0.0 );
// returns NaN

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

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

var v;
var i;
for ( i = 0; i < c.length; i++ ) {
v = variance( c[ i ] );
console.log( 'c: %d, Var(X;c): %d', c[ i ].toFixed( 4 ), v.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">

[bradford-distribution]: https://en.wikipedia.org/wiki/Bradford%27s_law

[variance]: https://en.wikipedia.org/wiki/Variance

</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 variance = require( './../lib' );


// MAIN //

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

c = uniform( 100, 0.1, 10.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = variance( c[ i % c.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();
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

{{alias}}( c )
Returns the variance of a Bradford distribution with shape
parameter `c`.

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

Parameters
----------
c: number
Shape parameter.

Returns
-------
out: number
Variance.

Examples
--------
> var v = {{alias}}( 0.1 )
~0.083
> v = {{alias}}( 0.5 )
~0.083
> v = {{alias}}( 10.0 )
~0.076

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @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 variance of a Bradford distribution.
*
* ## Notes
*
* - If `c <= 0`, the function returns `NaN`.
*
* @param c - shape parameter
* @returns variance
*
* @example
* var v = variance( 0.1 );
* // returns ~0.083
*
* @example
* var v = variance( 0.5 );
* // returns ~0.083
*
* @example
* var v = variance( 10.0 );
* // returns ~0.076
*
* @example
* var v = variance( 0.0 );
* // returns NaN
*
* @example
* var v = variance( -1.0 );
* // returns NaN
*
* @example
* var v = variance( NaN );
* // returns NaN
*/
declare function variance( c: number ): number;


// EXPORTS //

export = variance;
Loading