Skip to content

Commit 6325573

Browse files
feat: add stats/base/dists/burr-type3/logcdf
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: passed - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent ef90f52 commit 6325573

File tree

18 files changed

+1576
-0
lines changed

18 files changed

+1576
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Logarithm of Cumulative Distribution Function
22+
23+
> [Burr (type III)][burr-distribution] distribution logarithm of [cumulative distribution function][cdf].
24+
25+
<section class="intro">
26+
27+
The [cumulative distribution function][cdf] for a [burr][burr-distribution] random variable is
28+
29+
<!-- <equation class="equation" label="eq:burr-type3_logcdf" align="center"
30+
raw="F(x; c, d) = log(1 + x^{-c})*{-d}"
31+
alt="Logarithm of cumulative distribution function for a Burr (Type III) distribution."> -->
32+
33+
```math
34+
F(x; alpha, beta) = log(1 + x^{-alpha})*{-beta}
35+
```
36+
37+
<!-- </equation> -->
38+
39+
where `alpha > 0` is the first shape parameter and `beta > 0` is the second shape parameter.
40+
41+
</section>
42+
43+
<!-- /.intro -->
44+
45+
<section class="usage">
46+
47+
## Usage
48+
49+
```javascript
50+
var logcdf = require( '@stdlib/stats/base/dists/burr-type3/logcdf' );
51+
```
52+
53+
#### logcdf( x, alpha, beta )
54+
55+
Evaluates the natural logarithm of the [cumulative distribution function][cdf] (CDF) for a [burr][burr-distribution] distribution with parameters `alpha` (first shape parameter) and `beta` (second shape parameter).
56+
57+
```javascript
58+
var y = logcdf( 0.1, 1.0, 1.0 );
59+
// returns ~-2.398
60+
61+
y = logcdf( 0.2, 2.0, 2.0 );
62+
// returns ~-6.516
63+
64+
y = logcdf( 0.3, 3.0, 3.0 );
65+
// returns ~-10.916
66+
67+
y = logcdf( 0.4, 4.0, 4.0 );
68+
// returns ~-14.76
69+
70+
y = logcdf( 0.5, 1.0, 1.0 );
71+
// returns ~-1.099
72+
73+
y = logcdf( 0.5, 2.0, 4.0 );
74+
// returns ~-6.438
75+
76+
y = logcdf( 0.8, 0.5, 0.5 );
77+
// returns ~-0.375
78+
79+
y = logcdf( -Infinity, 4.0, 2.0 );
80+
// returns NaN
81+
```
82+
83+
If provided `NaN` as any argument, the function returns `NaN`.
84+
85+
```javascript
86+
var y = logcdf( NaN, 1.0, 1.0 );
87+
// returns NaN
88+
89+
y = logcdf( 0.0, NaN, 1.0 );
90+
// returns NaN
91+
92+
y = logcdf( 0.0, 1.0, NaN );
93+
// returns NaN
94+
```
95+
96+
If provided `alpha <= 0`, the function returns `NaN`.
97+
98+
```javascript
99+
var y = logcdf( 2.0, -1.0, 0.5 );
100+
// returns NaN
101+
102+
y = logcdf( 2.0, 0.0, 0.5 );
103+
// returns NaN
104+
```
105+
106+
If provided `beta <= 0`, the function returns `NaN`.
107+
108+
```javascript
109+
var y = logcdf( 2.0, 0.5, -1.0 );
110+
// returns NaN
111+
112+
y = logcdf( 2.0, 0.5, 0.0 );
113+
// returns NaN
114+
```
115+
116+
#### logcdf.factory( alpha, beta )
117+
118+
Returns a function for evaluating the natural logarithm of the [cumulative distribution function][cdf] for a [burr][burr-distribution] distribution with parameters `alpha` (first shape parameter) and `beta` (second shape parameter).
119+
120+
```javascript
121+
var mylogcdf = logcdf.factory( 0.5, 0.5 );
122+
123+
var y = mylogcdf( 0.8 );
124+
// returns ~-0.375
125+
126+
y = mylogcdf( 0.3 );
127+
// returns ~-0.519
128+
```
129+
130+
</section>
131+
132+
<!-- /.usage -->
133+
134+
<section class="notes">
135+
136+
## Notes
137+
138+
- In virtually all cases, using the `logpdf` or `logcdf` functions is preferable to manually computing the logarithm of the `pdf` or `cdf`, respectively, since the latter is prone to overflow and underflow.
139+
140+
</section>
141+
142+
<!-- /.notes -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var logcdf = require( '@stdlib/stats/base/dists/burr-type3/logcdf' );
152+
var EPS = require( '@stdlib/constants/float64/eps' );
153+
var linspace = require( '@stdlib/array/linspace' );
154+
155+
var alpha = linspace( EPS, 10.0, 10 );
156+
var beta = linspace( EPS, 10.0, 10 );
157+
var x = linspace( EPS, 1.0, 10 );
158+
var y;
159+
var i;
160+
161+
for ( i = 0; i < 10; i++ ) {
162+
y = logcdf( x[i], alpha[i], beta[i] );
163+
console.log( 'x: %d, α: %d, β: %d, ln(F(x;α,β)): %d', x[i].toFixed( 4 ), alpha[i].toFixed( 4 ), beta[i].toFixed( 4 ), y.toFixed( 4 ) );
164+
}
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
172+
173+
<section class="related">
174+
175+
</section>
176+
177+
<!-- /.related -->
178+
179+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
180+
181+
<section class="links">
182+
183+
[burr-distribution]: https://en.wikipedia.org/wiki/Burr_distribution
184+
185+
[cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function
186+
187+
</section>
188+
189+
<!-- /.links -->
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var EPS = require( '@stdlib/constants/float64/eps' );
27+
var pkg = require( './../package.json' ).name;
28+
var logcdf = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var alpha;
35+
var beta;
36+
var len;
37+
var x;
38+
var y;
39+
var i;
40+
41+
len = 100;
42+
x = uniform( len, EPS, 10.0 );
43+
alpha = uniform( len, EPS, 100.0 );
44+
beta = uniform( len, EPS, 100.0 );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
y = logcdf( x[ i % len ], alpha[ i % len ], beta[ i % len ] );
49+
if ( isnan( y ) ) {
50+
b.fail( 'should not return NaN' );
51+
}
52+
}
53+
b.toc();
54+
if ( isnan( y ) ) {
55+
b.fail( 'should not return NaN' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( pkg+':factory', function benchmark( b ) {
62+
var mylogcdf;
63+
var len;
64+
var x;
65+
var y;
66+
var i;
67+
68+
x = uniform( len, EPS, 10.0 );
69+
mylogcdf = logcdf.factory( 5.0, 6.0 );
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
y = mylogcdf( x[ i % len ] );
74+
if ( isnan( y ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnan( y ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/usr/bin/env python
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2018 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
"""Benchmark scipy.stats.burr.logcdf."""
20+
21+
from __future__ import print_function
22+
import timeit
23+
24+
NAME = "burr:logcdf"
25+
REPEATS = 3
26+
ITERATIONS = 1000
27+
28+
29+
def print_version():
30+
"""Print the TAP version."""
31+
print("TAP version 13")
32+
33+
34+
def print_summary(total, passing):
35+
"""Print the benchmark summary.
36+
37+
# Arguments
38+
39+
* `total`: total number of tests
40+
* `passing`: number of passing tests
41+
42+
"""
43+
print("#")
44+
print("1.." + str(total)) # TAP plan
45+
print("# total " + str(total))
46+
print("# pass " + str(passing))
47+
print("#")
48+
print("# ok")
49+
50+
51+
def print_results(elapsed):
52+
"""Print benchmark results.
53+
54+
# Arguments
55+
56+
* `elapsed`: elapsed time (in seconds)
57+
58+
# Examples
59+
60+
``` python
61+
python> print_results(0.131009101868)
62+
```
63+
"""
64+
rate = ITERATIONS / elapsed
65+
66+
print(" ---")
67+
print(" iterations: " + str(ITERATIONS))
68+
print(" elapsed: " + str(elapsed))
69+
print(" rate: " + str(rate))
70+
print(" ...")
71+
72+
73+
def benchmark():
74+
"""Run the benchmark and print benchmark results."""
75+
setup = "from scipy.stats import burr; from random import random;"
76+
stmt = "y = burr.logcdf(random(), 100.56789, 55.54321)"
77+
78+
t = timeit.Timer(stmt, setup=setup)
79+
80+
print_version()
81+
82+
for i in range(REPEATS):
83+
print("# python::" + NAME)
84+
elapsed = t.timeit(number=ITERATIONS)
85+
print_results(elapsed)
86+
print("ok " + str(i+1) + " benchmark finished")
87+
88+
print_summary(REPEATS, REPEATS)
89+
90+
91+
def main():
92+
"""Run the benchmark."""
93+
benchmark()
94+
95+
96+
if __name__ == "__main__":
97+
main()

0 commit comments

Comments
 (0)