Skip to content

Commit

Permalink
feat: Added Rice Distribution Base Package
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMaverick2 committed Aug 1, 2024
1 parent 39a6773 commit 82070ea
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 0 deletions.
72 changes: 72 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/rice/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Rice Distribution

This package implements the Rice distribution for the `@stdlib` library.

## Overview

The Rice distribution is a continuous probability distribution. This package provides the following primary functions:

- **PDF (Probability Density Function)**
- **CDF (Cumulative Distribution Function)**

## Installation

```bash
npm install @stdlib/stats-base-dists-rice
```

## Usage

### PDF

The PDF function evaluates the probability density function for given `x`, `nu`, and `sigma`.

#### Syntax

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

pdf(x, nu, sigma);
```

#### Example

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

var y = pdf(2, 1, 1);
console.log(y); // returns the PDF value at x=2 for nu=1 and sigma=1
```

### CDF

The CDF function evaluates the cumulative distribution function for given `x`, `nu`, and `sigma`.

#### Syntax

```javascript
var cdf = require('@stdlib/stats/base/dists/rice/cdf');

cdf(x, nu, sigma);
```

#### Example

```javascript
var cdf = require('@stdlib/stats/base/dists/rice/cdf');

var y = cdf(2, 1, 1);
console.log(y); // returns the CDF value at x=2 for nu=1 and sigma=1
```

## Testing

To run the tests, use the following commands:

```bash
npm install
npm test
```



21 changes: 21 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/rice/lib/cdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

/**
* Evaluates the cumulative distribution function (CDF) for a Rice distribution.
*
* @param {number} x - input value
* @param {number} nu - noncentrality parameter
* @param {number} sigma - scale parameter
* @returns {number} evaluated CDF
*/
function cdf(x, nu, sigma) {
if (sigma <= 0) {
throw new Error('Scale parameter `sigma` must be positive');
}
const z = x / sigma;
const bessel = require('@stdlib/math/base/special/besseli');
const normal = require('@stdlib/stats/base/dists/normal/cdf');
return normal.cdf(z, nu / sigma, 1) - Math.exp(-z * z / 2 - nu * nu / (2 * sigma * sigma)) * bessel(z * nu / sigma, 1);
}

module.exports = cdf;
9 changes: 9 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/rice/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var pdf = require('./pdf');
var cdf = require('./cdf');

module.exports = {
pdf: pdf,
cdf: cdf
};
21 changes: 21 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/rice/lib/pdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

/**
* Evaluates the probability density function (PDF) for a Rice distribution.
*
* @param {number} x - input value
* @param {number} nu - noncentrality parameter
* @param {number} sigma - scale parameter
* @returns {number} evaluated PDF
*/
function pdf(x, nu, sigma) {
if (sigma <= 0) {
throw new Error('Scale parameter `sigma` must be positive');
}
const factor = x / (sigma * sigma);
const expFactor = Math.exp(-((x*x + nu*nu) / (2 * sigma * sigma)));
const bessel = require('@stdlib/math/base/special/bessel0');
return factor * expFactor * bessel(x * nu / (sigma * sigma));
}

module.exports = pdf;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var tape = require('tape');
var cdf = require('./../lib/cdf');

tape('CDF of Rice distribution', function (t) {
t.equal(cdf(2, 1, 1), /* expected value */);
t.end();
});
9 changes: 9 additions & 0 deletions lib/node_modules/@stdlib/stats/base/dists/rice/test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var tape = require('tape');
var rice = require('./../lib');

tape('main export is an object', function (t) {
t.equal(typeof rice, 'object', 'main export is an object');
t.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var tape = require('tape');
var pdf = require('./../lib/pdf');

tape('PDF of Rice distribution', function (t) {
t.equal(pdf(2, 1, 1), /* expected value */);
t.end();
});

0 comments on commit 82070ea

Please sign in to comment.