Skip to content

feat: add support for skew normal distribution PDF #2110

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

Closed
wants to merge 5 commits into from
Closed
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
168 changes: 168 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/skew-normal/pdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@

<!--

@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 Density Function

> [Skew normal][skew-normal-distribution] distribution probability density function (PDF).

<section class="intro">

The [probability density function][pdf] (PDF) for a [skew normal][skew-normal-distribution] random variable is

```math
f(x;\mu,\sigma,\alpha)=\frac{2}{\sigma}\phi \left({\frac {x-\mu }{\sigma }}\right)\Phi \left(\alpha \left({\frac {x-\mu }{\sigma }}\right)\right)
```

where,

> `ϕ` is the standard normal probability distribution function,
> `Φ` is the standard normal cumulative distribution function,
> `μ` is the mean,
> `σ` is the standard deviation,
> `α` is the skewness.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var pdf = require( '@stdlib/stats/base/dists/skew-normal/pdf' );
```

#### pdf( x, mu, sigma, alpha )

Evaluates the [probability density function][pdf] (PDF) for a [skew normal][skew-normal-distribution] distribution with parameters `mu` (mean), `sigma` (standard deviation), and `alpha` (skewness).

```javascript
var y = pdf( 2.0, 0.0, 1.0, 2.0 );
// returns ~0.108

y = pdf( -1.0, 4.0, 2.0, 0.0 );
// returns ~0.009
```

If provided `NaN` as any argument, the function returns `NaN`.

```javascript
var y = pdf( NaN, 0.0, 1.0, 0.0 );
// returns NaN

y = pdf( 0.0, NaN, 1.0, 0.0 );
// returns NaN

y = pdf( 0.0, 0.0, NaN, 0.0 );
// returns NaN

y = pdf( 0.0, 0.0, 0.0, NaN );
// returns NaN
```

If provided `sigma < 0`, the function returns `NaN`.

```javascript
var y = pdf( 2.0, 0.0, -1.0, 0.0 );
// returns NaN
```

If provided `sigma = 0`, the function evaluates the [PDF][pdf] of a [degenerate distribution][degenerate-distribution] centered at `mu`.

```javascript
var y = pdf( 2.0, 8.0, 0.0, 0.0 );
// returns 0.0

y = pdf( 8.0, 8.0, 0.0, 0.0 );
// returns Infinity
```

#### pdf.factory( mu, sigma, alpha )

Partially apply `mu`, `sigma`, and `alpha` to create a reusable `function` for evaluating the PDF.

```javascript
var mypdf = pdf.factory( 10.0, 2.0, -1.0 );

var y = mypdf( 10.0 );
// returns ~0.199

y = mypdf( 5.0 );
// returns ~0.017
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var pdf = require( '@stdlib/stats/base/dists/skew-normal/pdf' );

var alpha;
var sigma;
var mu;
var x;
var y;
var i;

for ( i = 0; i < 10; i++ ) {
x = randu() * 10.0;
mu = (randu() * 10.0) - 5.0;
sigma = randu() * 20.0;
alpha = (randu() * 40.0) - 20.0;
y = pdf( x, mu, sigma, alpha );
console.log( 'x: %d, µ: %d, σ: %d, α: %d, f(x;µ,σ,α): %d', x, mu, sigma, alpha, y );
}
```

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

[pdf]: https://en.wikipedia.org/wiki/Probability_density_function

[skew-normal-distribution]: https://en.wikipedia.org/wiki/Skew_normal_distribution

[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @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 isnan = require( '@stdlib/math/base/assert/is-nan' );
var EPS = require( '@stdlib/constants/float64/eps' );
var pkg = require( './../package.json' ).name;
var pdf = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var alpha;
var sigma;
var mu;
var x;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*200.0 ) - 100;
mu = ( randu()*100.0 ) - 50.0;
sigma = ( randu()*20.0 ) + EPS;
alpha = ( randu()*40.0 ) - 20.0;
y = pdf( x, mu, sigma, alpha );
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 mypdf;
var alpha;
var sigma;
var mu;
var x;
var y;
var i;

mu = 0.0;
sigma = 1.5;
alpha = 0.5;
mypdf = pdf.factory( mu, sigma, alpha);

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*6.0 ) - 3.0;
y = mypdf( 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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Package: sn-benchmarks
Title: Benchmarks
Version: 0.0.0
Authors@R: person("stdlib", "js", role = c("aut","cre"))
Description: Benchmarks.
Depends: R (>=3.4.0)
Imports:
microbenchmark
sn
LazyData: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/env Rscript
#
# @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.

# Set the precision to 16 digits:
options( digits = 16L );

#' Run benchmarks.
#'
#' @examples
#' main();
main <- function() {
# Define benchmark parameters:
name <- 'dist-skew-normal-pdf';
iterations <- 1000000L;
repeats <- 3L;

#' Print the TAP version.
#'
#' @examples
#' print_version();
print_version <- function() {
cat( 'TAP version 13\n' );
}

#' Print the TAP summary.
#'
#' @param total Total number of tests.
#' @param passing Total number of passing tests.
#'
#' @examples
#' print_summary( 3, 3 );
print_summary <- function( total, passing ) {
cat( '#\n' );
cat( paste0( '1..', total, '\n' ) ); # TAP plan
cat( paste0( '# total ', total, '\n' ) );
cat( paste0( '# pass ', passing, '\n' ) );
cat( '#\n' );
cat( '# ok\n' );
}

#' Print benchmark results.
#'
#' @param iterations Number of iterations.
#' @param elapsed Elapsed time in seconds.
#'
#' @examples
#' print_results( 10000L, 0.131009101868 );
print_results <- function( iterations, elapsed ) {
rate <- iterations / elapsed;
cat( ' ---\n' );
cat( paste0( ' iterations: ', iterations, '\n' ) );
cat( paste0( ' elapsed: ', elapsed, '\n' ) );
cat( paste0( ' rate: ', rate, '\n' ) );
cat( ' ...\n' );
}

#' Run a benchmark.
#'
#' ## Notes
#'
#' * We compute and return a total "elapsed" time, rather than the minimum
#' evaluation time, to match benchmark results in other languages (e.g.,
#' Python).
#'
#'
#' @param iterations Number of Iterations.
#' @return Elapsed time in seconds.
#'
#' @examples
#' elapsed <- benchmark( 10000L );
benchmark <- function( iterations ) {
# Run the benchmarks:
results <- microbenchmark::microbenchmark(sn::dsn(
runif( 1.0, -100.0, 100.0 ),
runif( 1.0, -50.0, 50.0 ),
runif( 1.0, .Machine$double.eps, 20.0 ), # nolint
runif( 1.0, -20.0, 20.0 )
), times = iterations );

# Sum all the raw timing results to get a total "elapsed" time:
elapsed <- sum( results$time ); # nolint

# Convert the elapsed time from nanoseconds to seconds:
elapsed <- elapsed / 1.0e9;

return( elapsed );
}

print_version();
for ( i in 1L:repeats ) {
cat( paste0( '# r::', name, '\n' ) );
elapsed <- benchmark( iterations );
print_results( iterations, elapsed );
cat( paste0( 'ok ', i, ' benchmark finished', '\n' ) );
}
print_summary( repeats, repeats );
}

main();
Loading
Loading