-
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Rice Distribution Base Package
- Loading branch information
1 parent
39a6773
commit 82070ea
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
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 | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
9 changes: 9 additions & 0 deletions
9
lib/node_modules/@stdlib/stats/base/dists/rice/test/test.cdf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
9 changes: 9 additions & 0 deletions
9
lib/node_modules/@stdlib/stats/base/dists/rice/test/test.pdf.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |