Skip to content

Commit 82070ea

Browse files
committed
feat: Added Rice Distribution Base Package
1 parent 39a6773 commit 82070ea

File tree

7 files changed

+150
-0
lines changed

7 files changed

+150
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Rice Distribution
2+
3+
This package implements the Rice distribution for the `@stdlib` library.
4+
5+
## Overview
6+
7+
The Rice distribution is a continuous probability distribution. This package provides the following primary functions:
8+
9+
- **PDF (Probability Density Function)**
10+
- **CDF (Cumulative Distribution Function)**
11+
12+
## Installation
13+
14+
```bash
15+
npm install @stdlib/stats-base-dists-rice
16+
```
17+
18+
## Usage
19+
20+
### PDF
21+
22+
The PDF function evaluates the probability density function for given `x`, `nu`, and `sigma`.
23+
24+
#### Syntax
25+
26+
```javascript
27+
var pdf = require('@stdlib/stats/base/dists/rice/pdf');
28+
29+
pdf(x, nu, sigma);
30+
```
31+
32+
#### Example
33+
34+
```javascript
35+
var pdf = require('@stdlib/stats/base/dists/rice/pdf');
36+
37+
var y = pdf(2, 1, 1);
38+
console.log(y); // returns the PDF value at x=2 for nu=1 and sigma=1
39+
```
40+
41+
### CDF
42+
43+
The CDF function evaluates the cumulative distribution function for given `x`, `nu`, and `sigma`.
44+
45+
#### Syntax
46+
47+
```javascript
48+
var cdf = require('@stdlib/stats/base/dists/rice/cdf');
49+
50+
cdf(x, nu, sigma);
51+
```
52+
53+
#### Example
54+
55+
```javascript
56+
var cdf = require('@stdlib/stats/base/dists/rice/cdf');
57+
58+
var y = cdf(2, 1, 1);
59+
console.log(y); // returns the CDF value at x=2 for nu=1 and sigma=1
60+
```
61+
62+
## Testing
63+
64+
To run the tests, use the following commands:
65+
66+
```bash
67+
npm install
68+
npm test
69+
```
70+
71+
72+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/**
4+
* Evaluates the cumulative distribution function (CDF) for a Rice distribution.
5+
*
6+
* @param {number} x - input value
7+
* @param {number} nu - noncentrality parameter
8+
* @param {number} sigma - scale parameter
9+
* @returns {number} evaluated CDF
10+
*/
11+
function cdf(x, nu, sigma) {
12+
if (sigma <= 0) {
13+
throw new Error('Scale parameter `sigma` must be positive');
14+
}
15+
const z = x / sigma;
16+
const bessel = require('@stdlib/math/base/special/besseli');
17+
const normal = require('@stdlib/stats/base/dists/normal/cdf');
18+
return normal.cdf(z, nu / sigma, 1) - Math.exp(-z * z / 2 - nu * nu / (2 * sigma * sigma)) * bessel(z * nu / sigma, 1);
19+
}
20+
21+
module.exports = cdf;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var pdf = require('./pdf');
4+
var cdf = require('./cdf');
5+
6+
module.exports = {
7+
pdf: pdf,
8+
cdf: cdf
9+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
/**
4+
* Evaluates the probability density function (PDF) for a Rice distribution.
5+
*
6+
* @param {number} x - input value
7+
* @param {number} nu - noncentrality parameter
8+
* @param {number} sigma - scale parameter
9+
* @returns {number} evaluated PDF
10+
*/
11+
function pdf(x, nu, sigma) {
12+
if (sigma <= 0) {
13+
throw new Error('Scale parameter `sigma` must be positive');
14+
}
15+
const factor = x / (sigma * sigma);
16+
const expFactor = Math.exp(-((x*x + nu*nu) / (2 * sigma * sigma)));
17+
const bessel = require('@stdlib/math/base/special/bessel0');
18+
return factor * expFactor * bessel(x * nu / (sigma * sigma));
19+
}
20+
21+
module.exports = pdf;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var tape = require('tape');
4+
var cdf = require('./../lib/cdf');
5+
6+
tape('CDF of Rice distribution', function (t) {
7+
t.equal(cdf(2, 1, 1), /* expected value */);
8+
t.end();
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var tape = require('tape');
4+
var rice = require('./../lib');
5+
6+
tape('main export is an object', function (t) {
7+
t.equal(typeof rice, 'object', 'main export is an object');
8+
t.end();
9+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var tape = require('tape');
4+
var pdf = require('./../lib/pdf');
5+
6+
tape('PDF of Rice distribution', function (t) {
7+
t.equal(pdf(2, 1, 1), /* expected value */);
8+
t.end();
9+
});

0 commit comments

Comments
 (0)