Skip to content

Commit 84e44ba

Browse files
committed
Merge branch 'develop' of https://github.com/stdlib-js/stdlib into develop
2 parents 8017d21 + 7c482ff commit 84e44ba

20 files changed

Lines changed: 965 additions & 23 deletions

File tree

lib/node_modules/@stdlib/math/base/dist/chi/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ setReadOnly( chi, 'entropy', require( '@stdlib/math/base/dist/chi/entropy' ) );
5454
*/
5555
setReadOnly( chi, 'kurtosis', require( '@stdlib/math/base/dist/chi/kurtosis' ) );
5656

57+
/**
58+
* @name logpdf
59+
* @memberof chi
60+
* @readonly
61+
* @type {Function}
62+
* @see {@link module:@stdlib/math/base/dist/chi/logpdf}
63+
*/
64+
setReadOnly( chi, 'logpdf', require( '@stdlib/math/base/dist/chi/logpdf' ) );
65+
5766
/**
5867
* @name mean
5968
* @memberof chi
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Logarithm of Probability Density Function
2+
3+
> Evaluate the natural logarithm of the [probability density function][pdf] (PDF) for a [chi][chi-distribution] distribution .
4+
5+
<section class="intro">
6+
7+
The [probability density function][pdf] (PDF) for a [chi][chi-distribution] random variable is
8+
9+
<!-- <equation class="equation" label="eq:chi_pdf" align="center" raw="f(x;\,k) = \frac{2^{{1-k/2}}x^{{k-1}}e^{{-x^{2}/2}}}{\Gamma(k/2)}" alt="Probability density function (PDF) for a chi distribution."> -->
10+
11+
<div class="equation" align="center" data-raw-text="f(x;\,k) = \frac{2^{{1-k/2}}x^{{k-1}}e^{{-x^{2}/2}}}{\Gamma(k/2)}" data-equation="eq:chi_pdf">
12+
<img src="https://cdn.rawgit.com/stdlib-js/stdlib/bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/dist/chi/pdf/docs/img/equation_chi_pdf.svg" alt="Probability density function (PDF) for a chi distribution.">
13+
<br>
14+
</div>
15+
16+
<!-- </equation> -->
17+
18+
where `k` is the degrees of freedom and `Γ` denotes the [gamma][gamma-function] function.
19+
20+
</section>
21+
22+
<!-- /.intro -->
23+
24+
<section class="usage">
25+
26+
## Usage
27+
28+
```javascript
29+
var logpdf = require( '@stdlib/math/base/dist/chi/logpdf' );
30+
```
31+
32+
#### logpdf( x, k )
33+
34+
Evaluates the natural logarithm of the [probability density function][pdf] (PDF) for a [chi][chi-distribution] distribution with degrees of freedom `k`.
35+
36+
```javascript
37+
var y = logpdf( 0.1, 1.0 );
38+
// returns ~-0.231
39+
40+
y = logpdf( 0.5, 2.0 );
41+
// returns ~-0.818
42+
43+
y = logpdf( -1.0, 4.0 );
44+
// returns -Infinity
45+
```
46+
47+
If provided `NaN` as any argument, the function returns `NaN`.
48+
49+
```javascript
50+
var y = logpdf( NaN, 1.0 );
51+
// returns NaN
52+
53+
y = logpdf( 0.0, NaN );
54+
// returns NaN
55+
```
56+
57+
If provided `k < 0`, the function returns `NaN`.
58+
59+
```javascript
60+
var y = logpdf( 2.0, -2.0 );
61+
// returns NaN
62+
```
63+
64+
If provided `k = 0`, the function evaluates the natural logarithm of the [PDF][pdf] for a [degenerate distribution][degenerate-distribution] centered at `0`.
65+
66+
```javascript
67+
var y = logpdf( 2.0, 0.0 );
68+
// returns -Infinity
69+
70+
y = logpdf( 0.0, 0.0 );
71+
// returns Infinity
72+
```
73+
74+
#### logpdf.factory( k )
75+
76+
Returns a `function` for evaluating the natural logarithm of the [PDF][pdf] for a [chi][chi-distribution] distribution with degrees of freedom `k`.
77+
78+
```javascript
79+
var mylogPDF = logpdf.factory( 6.0 );
80+
81+
var y = mylogPDF( 3.0 );
82+
// returns ~-1.088
83+
84+
y = mylogPDF( 1.0 );
85+
// returns ~-2.578
86+
```
87+
88+
</section>
89+
90+
<!-- /.usage -->
91+
92+
<section class="examples">
93+
94+
## Examples
95+
96+
```javascript
97+
var randu = require( '@stdlib/math/base/random/randu' );
98+
var logpdf = require( '@stdlib/math/base/dist/chi/logpdf' );
99+
100+
var k;
101+
var x;
102+
var y;
103+
var i;
104+
105+
for ( i = 0; i < 20; i++ ) {
106+
x = randu() * 10.0;
107+
k = randu() * 10.0;
108+
y = logpdf( x, k );
109+
console.log( 'x: %d, k: %d, ln(f(x;k)): %d', x.toFixed( 4 ), k.toFixed( 4 ), y.toFixed( 4 ) );
110+
}
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<section class="links">
118+
119+
[chi-distribution]: https://en.wikipedia.org/wiki/Chi_distribution
120+
121+
[degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution
122+
123+
[gamma-function]: https://en.wikipedia.org/wiki/Gamma_function
124+
125+
[pdf]: https://en.wikipedia.org/wiki/Probability_density_function
126+
127+
</section>
128+
129+
<!-- /.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 logpdf = 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 = logpdf( 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 mylogpdf;
40+
var k;
41+
var x;
42+
var y;
43+
var i;
44+
45+
k = 10.0;
46+
mylogpdf = logpdf.factory( k );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
x = randu() * 100.0;
51+
y = mylogpdf( 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: 70 additions & 0 deletions
Loading
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
{{alias}}( x, k )
3+
Evaluates the natural logarithm of the probability density function (PDF)
4+
for a chi distribution with degrees of freedom `k` at a value `x`.
5+
6+
If provided `NaN` as any argument, the function returns `NaN`.
7+
8+
If provided `k < 0`, the function returns `NaN`.
9+
10+
Parameters
11+
----------
12+
x: number
13+
Input value.
14+
15+
k: number
16+
Degrees of freedom.
17+
18+
Returns
19+
-------
20+
out: number
21+
Evaluated logPDF.
22+
23+
Examples
24+
--------
25+
> var y = {{alias}}( 0.3, 4.0 )
26+
~-4.35
27+
> y = {{alias}}( 0.7, 0.7 )
28+
~-0.622
29+
> y = {{alias}}( -1.0, 0.5 )
30+
-Infinity
31+
> y = {{alias}}( 0.0, NaN )
32+
NaN
33+
> y = {{alias}}( NaN, 2.0 )
34+
NaN
35+
36+
// Negative degrees of freedom:
37+
> y = {{alias}}( 2.0, -1.0 )
38+
NaN
39+
40+
// Degenerate distribution when `k = 0`:
41+
> y = {{alias}}( 2.0, 0.0, 2.0 )
42+
-Infinity
43+
> y = {{alias}}( 0.0, 0.0, 2.0 )
44+
Infinity
45+
46+
47+
{{alias}}.factory( k )
48+
Returns a function for evaluating the natural logarithm of the probability
49+
density function (PDF) 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+
logpdf: Function
59+
Logarithm of probability density function (PDF).
60+
61+
Examples
62+
--------
63+
> var mylogPDF = {{alias}}.factory( 6.0 );
64+
> var y = mylogPDF( 3.0 )
65+
~-1.088
66+
67+
See Also
68+
--------
69+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var logpdf = require( './../lib' );
5+
6+
var k;
7+
var x;
8+
var y;
9+
var i;
10+
11+
for ( i = 0; i < 20; i++ ) {
12+
x = randu() * 10.0;
13+
k = randu() * 10.0;
14+
y = logpdf( x, k );
15+
console.log( 'x: %d, k: %d, ln(f(x;k)): %d', x.toFixed( 4 ), k.toFixed( 4 ), y.toFixed( 4 ) );
16+
}

0 commit comments

Comments
 (0)