Skip to content

Commit 8592d57

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents 971fe31 + 711110d commit 8592d57

123 files changed

Lines changed: 5371 additions & 8 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Chi
2+
3+
> Chi distribution.
4+
5+
6+
<section class="usage">
7+
8+
## Usage
9+
10+
``` javascript
11+
var chi = require( '@stdlib/math/base/dist/chi' );
12+
```
13+
14+
#### chi
15+
16+
Chi distribution.
17+
18+
``` javascript
19+
var dist = chi;
20+
// returns {...}
21+
```
22+
23+
</section>
24+
25+
<!-- /.usage -->
26+
27+
28+
<section class="examples">
29+
30+
## Examples
31+
32+
<!-- TODO: better examples -->
33+
34+
``` javascript
35+
var getKeys = require( 'object-keys' ).shim();
36+
var chi = require( '@stdlib/math/base/dist/chi' );
37+
38+
console.log( getKeys( chi ) );
39+
```
40+
41+
</section>
42+
43+
<!-- /.examples -->
44+
45+
46+
<section class="links">
47+
48+
</section>
49+
50+
<!-- /.links -->
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Cumulative Distribution Function
2+
3+
> [Chi][chi] distribution [cumulative distribution function][cdf].
4+
5+
6+
<section class="intro">
7+
8+
The [cumulative distribution function][cdf] for a [chi][chi] random variable is
9+
10+
<!-- <equation class="equation" label="eq:chi_cdf" align="center" raw="F(x;\,k) = P\left(k/2,x^{2}/2\right)" alt="Cumulative distribution function for a chi distribution."> -->
11+
12+
<div class="equation" align="center" data-raw-text="F(x;\,k) = \leftP(k/2,x^{2}/2\right)" data-equation="eq:chi_cdf">
13+
<img src="" alt="Cumulative distribution function for a chi distribution.">
14+
<br>
15+
</div>
16+
17+
<!-- </equation> -->
18+
19+
where `k` is the degrees of freedom and `P` is the lower regularized incomplete gamma function.
20+
21+
</section>
22+
23+
<!-- /.intro -->
24+
25+
26+
<section class="usage">
27+
28+
## Usage
29+
30+
``` javascript
31+
var cdf = require( '@stdlib/math/base/dist/chi/cdf' );
32+
```
33+
34+
#### cdf( x, k )
35+
36+
Evaluates the [cumulative distribution function][cdf] (CDF) for a [chi][chi] distribution with degrees of freedom `k`.
37+
38+
``` javascript
39+
var y = cdf( 2.0, 1.0 );
40+
// returns ~0.954
41+
42+
y = cdf( 2.0, 3.0 );
43+
// returns ~0.739
44+
45+
y = cdf( 1.0, 0.5 );
46+
// returns ~0.846
47+
48+
y = cdf( -1.0, 2.0 );
49+
// returns 0.0
50+
51+
y = cdf( -Infinity, 4.0 );
52+
// returns 0.0
53+
54+
y = cdf( +Infinity, 4.0 );
55+
// returns 1.0
56+
```
57+
58+
If provided `NaN` as any argument, the function returns `NaN`.
59+
60+
``` javascript
61+
var y = cdf( NaN, 1.0 );
62+
// returns NaN
63+
64+
y = cdf( 0.0, NaN );
65+
// returns NaN
66+
```
67+
68+
If provided `k < 0`, the function returns `NaN`.
69+
70+
``` javascript
71+
var y = cdf( 2.0, -2.0 );
72+
// returns NaN
73+
```
74+
75+
If provided `k = 0`, the function evaluates the [CDF][cdf] of a [degenerate distribution][degenerate-distribution] centered at `0`.
76+
77+
``` javascript
78+
var y = cdf( 2.0, 0.0 );
79+
// returns 1.0
80+
81+
y = cdf( -2.0, 0.0 );
82+
// returns 0.0
83+
84+
y = cdf( 0.0, 0.0 );
85+
// returns 1.0
86+
```
87+
88+
#### cdf.factory( k )
89+
90+
Returns a function for evaluating the [cumulative distribution function][cdf] for a [chi][chi] distribution with degrees of freedom `k`.
91+
92+
``` javascript
93+
var mycdf = cdf.factory( 3.0 );
94+
95+
var y = mycdf( 6.0 );
96+
// returns ~1.0
97+
98+
y = mycdf( 1.5 );
99+
// returns ~0.478
100+
```
101+
102+
</section>
103+
104+
<!-- /.usage -->
105+
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
``` javascript
112+
var randu = require( '@stdlib/math/base/random/randu' );
113+
var round = require( '@stdlib/math/base/special/round' );
114+
var cdf = require( '@stdlib/math/base/dist/chi/cdf' );
115+
116+
var k;
117+
var x;
118+
var y;
119+
var i;
120+
121+
for ( i = 0; i < 20; i++ ) {
122+
x = randu() * 10.0;
123+
k = round( randu()*5.0 );
124+
y = cdf( x, k );
125+
console.log( 'x: %d, k: %d, F(x;k): %d', x.toFixed( 4 ), k.toFixed( 4 ), y.toFixed( 4 ) );
126+
}
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
134+
<section class="links">
135+
136+
[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function
137+
[chi]: https://en.wikipedia.org/wiki/Chi_distribution
138+
[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution
139+
140+
</section>
141+
142+
<!-- /.links -->
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var ceil = require( '@stdlib/math/base/special/ceil' );
7+
var randu = require( '@stdlib/math/base/random/randu' );
8+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
9+
var pkg = require( './../package.json' ).name;
10+
var cdf = require( './../lib' );
11+
12+
13+
// MAIN //
14+
15+
bench( pkg, function benchmark( b ) {
16+
var k;
17+
var x;
18+
var y;
19+
var i;
20+
21+
b.tic();
22+
for ( i = 0; i < b.iterations; i++ ) {
23+
x = randu() * 100.0;
24+
k = ceil( randu()*100.0 );
25+
y = cdf( x, k );
26+
if ( isnan( y ) ) {
27+
b.fail( 'should not return NaN' );
28+
}
29+
}
30+
b.toc();
31+
if ( isnan( y ) ) {
32+
b.fail( 'should not return NaN' );
33+
}
34+
b.pass( 'benchmark finished' );
35+
b.end();
36+
});
37+
38+
bench( pkg+':factory', function benchmark( b ) {
39+
var mycdf;
40+
var k;
41+
var x;
42+
var y;
43+
var i;
44+
45+
k = 10.0;
46+
mycdf = cdf.factory( k );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
x = randu() * 100.0;
51+
y = mycdf( x );
52+
if ( isnan( y ) ) {
53+
b.fail( 'should not return NaN' );
54+
}
55+
}
56+
b.toc();
57+
if ( isnan( y ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
{{alias}}( x, k )
3+
Evaluates the cumulative distribution function (CDF) for a chi distribution with degrees of freedom `k` at a value `x`.
4+
5+
If provided `NaN` as any argument, the function returns `NaN`.
6+
7+
If provided `k < 0`, the function returns `NaN`.
8+
9+
Parameters
10+
----------
11+
x: number
12+
Input value.
13+
14+
k: number
15+
Degrees of freedom.
16+
17+
Returns
18+
-------
19+
out: number
20+
Evaluated CDF.
21+
22+
Examples
23+
--------
24+
> var y = {{alias}}( 2.0, 3.0 )
25+
~0.739
26+
> y = {{alias}}( 1.0, 0.5 )
27+
~0.846
28+
> y = {{alias}}( -1.0, 4.0 )
29+
0.0
30+
> y = {{alias}}( NaN, 1.0 )
31+
NaN
32+
> y = {{alias}}( 0.0, NaN )
33+
NaN
34+
35+
// Negative degrees of freedom:
36+
> y = {{alias}}( 2.0, -1.0 )
37+
NaN
38+
39+
// Degenerate distribution when `k = 0`:
40+
> y = {{alias}}( 2.0, 0.0 )
41+
1.0
42+
> y = {{alias}}( -2.0, 0.0 )
43+
0.0
44+
> y = {{alias}}( 0.0, 0.0 )
45+
1.0
46+
47+
{{alias}}.factory( k )
48+
Returns a function for evaluating the cumulative distribution function (CDF)
49+
of a chi distribution with degrees of freedom `k`.
50+
51+
Parameters
52+
----------
53+
k: number
54+
Degrees of freedom.
55+
56+
Returns
57+
-------
58+
cdf: Function
59+
Cumulative distribution function (CDF).
60+
61+
Examples
62+
--------
63+
> var mycdf = {{alias}}.factory( 1.0 );
64+
> var y = mycdf( 2.0 )
65+
~0.954
66+
> y = mycdf( 1.2 )
67+
~0.77
68+
69+
See Also
70+
--------
71+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var round = require( '@stdlib/math/base/special/round' );
5+
var cdf = require( './../lib' );
6+
7+
var k;
8+
var x;
9+
var y;
10+
var i;
11+
12+
for ( i = 0; i < 20; i++ ) {
13+
x = randu() * 10.0;
14+
k = round( randu() * 10.0 );
15+
y = cdf( x, k );
16+
console.log( 'x: %d, k: %d, F(x;k): %d', x.toFixed( 4 ), k.toFixed( 4 ), y.toFixed( 4 ) );
17+
}

0 commit comments

Comments
 (0)