Skip to content

Commit 24bf672

Browse files
committed
feat: implement betaincinv function
1 parent c487a89 commit 24bf672

File tree

12 files changed

+666
-228
lines changed

12 files changed

+666
-228
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Makefile for betaincinv module
2+
3+
# Default target
4+
all: build
5+
6+
# Build the native module
7+
build:
8+
node-gyp configure
9+
node-gyp build
10+
11+
# Run tests
12+
test:
13+
node test/test.js
14+
15+
# Run tests with coverage
16+
test-cov:
17+
nyc node test/test.js
18+
19+
# Run examples
20+
examples:
21+
node examples/index.js
22+
23+
# Run benchmarks
24+
benchmark:
25+
node benchmark/index.js
26+
27+
# Clean build artifacts
28+
clean:
29+
node-gyp clean
30+
rm -rf build/
31+
rm -rf coverage/
32+
33+
.PHONY: all build test test-cov examples benchmark clean

lib/node_modules/@stdlib/math/base/special/betaincinv/README.md

+34-92
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<!--
2-
32
@license Apache-2.0
43
5-
Copyright (c) 2018 The Stdlib Authors.
4+
Copyright (c) 2024 The Stdlib Authors.
65
76
Licensed under the Apache License, Version 2.0 (the "License");
87
you may not use this file except in compliance with the License.
@@ -15,15 +14,16 @@ distributed under the License is distributed on an "AS IS" BASIS,
1514
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1615
See the License for the specific language governing permissions and
1716
limitations under the License.
18-
1917
-->
2018

2119
# betaincinv
2220

23-
> Inverse of the [incomplete beta function][incomplete-beta-function].
21+
> Inverse incomplete beta function.
2422
2523
<section class="intro">
2624

25+
The [inverse incomplete beta function][inverse-incomplete-beta] is the inverse of the [incomplete beta function][incomplete-beta]. It is used in various statistical applications, including hypothesis testing and confidence interval calculations.
26+
2727
</section>
2828

2929
<!-- /.intro -->
@@ -36,69 +36,35 @@ limitations under the License.
3636
var betaincinv = require( '@stdlib/math/base/special/betaincinv' );
3737
```
3838

39-
#### betaincinv( p, a, b\[, upper] )
39+
#### betaincinv( p, a, b )
4040

41-
Inverts the regularized [incomplete beta function][incomplete-beta-function]. Contrary to the more commonly used definition, in this implementation the first parameter is the probability `p` and the second and third parameter are `a` and `b`. By default, the function inverts the _lower_ regularized [incomplete beta function][incomplete-beta-function]. To invert the _upper_ function instead, set the `upper` argument to `true`.
41+
Evaluates the inverse of the incomplete beta function:
4242

4343
```javascript
44-
var y = betaincinv( 0.2, 3.0, 3.0 );
44+
var y = betaincinv( 0.5, 1.0, 1.0 );
45+
// returns 0.5
46+
47+
y = betaincinv( 0.2, 3.0, 3.0 );
4548
// returns ~0.327
4649

4750
y = betaincinv( 0.4, 3.0, 3.0 );
4851
// returns ~0.446
4952

50-
y = betaincinv( 0.4, 3.0, 3.0, true );
51-
// returns ~0.554
52-
5353
y = betaincinv( 0.4, 1.0, 6.0 );
5454
// returns ~0.082
55-
56-
y = betaincinv( 0.8, 1.0, 6.0 );
57-
// returns ~0.235
5855
```
5956

60-
If provided `NaN` as any argument, the function returns `NaN`.
61-
62-
```javascript
63-
var y = betaincinv( NaN, 1.0, 1.0 );
64-
// returns NaN
65-
66-
y = betaincinv( 0.5, NaN, 1.0 );
67-
// returns NaN
68-
69-
y = betaincinv( 0.5, 1.0, NaN );
70-
// returns NaN
71-
```
72-
73-
If provided a value outside `[0,1]` for `p`, the function returns `NaN`.
74-
75-
```javascript
76-
var y = betaincinv( 1.2, 1.0, 1.0 );
77-
// returns NaN
78-
79-
y = betaincinv( -0.5, 1.0, 1.0 );
80-
// returns NaN
81-
```
82-
83-
If provided a nonpositive `a`, the function returns `NaN`.
84-
85-
```javascript
86-
var y = betaincinv( 0.5, -2.0, 2.0 );
87-
// returns NaN
88-
89-
y = betaincinv( 0.5, 0.0, 2.0 );
90-
// returns NaN
91-
```
57+
The function accepts the following parameters:
9258

93-
If provided a nonpositive `b`, the function returns `NaN`.
59+
- **p**: probability value (input value for the incomplete beta function).
60+
- **a**: first shape parameter (must be positive).
61+
- **b**: second shape parameter (must be positive).
9462

95-
```javascript
96-
var y = betaincinv( 0.5, 2.0, -2.0 );
97-
// returns NaN
63+
The function returns `NaN` if any of the following conditions are met:
9864

99-
y = betaincinv( 0.5, 2.0, 0.0 );
100-
// returns NaN
101-
```
65+
- `p` is outside the interval `[0,1]`
66+
- `a` is not positive
67+
- `b` is not positive
10268

10369
</section>
10470

@@ -108,58 +74,34 @@ y = betaincinv( 0.5, 2.0, 0.0 );
10874

10975
## Examples
11076

111-
<!-- eslint no-undef: "error" -->
112-
11377
```javascript
114-
var uniform = require( '@stdlib/random/array/uniform' );
115-
var logEachMap = require( '@stdlib/console/log-each-map' );
11678
var betaincinv = require( '@stdlib/math/base/special/betaincinv' );
11779

118-
var opts = {
119-
'dtype': 'float64'
120-
};
121-
var p = uniform( 100, 0.0, 1.0, opts );
122-
var a = uniform( 100, 0.0, 10.0, opts );
123-
var b = uniform( 100, 0.0, 10.0, opts );
124-
125-
logEachMap( 'p: %0.4f, \t a: %0.4f, \t b: %0.4f, \t f(p,a,b): %0.4f', p, a, b, betaincinv );
126-
```
127-
128-
</section>
129-
130-
<!-- /.examples -->
131-
132-
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133-
134-
<section class="related">
80+
var y = betaincinv( 0.5, 1.0, 1.0 );
81+
console.log( y );
82+
// => 0.5
13583

136-
* * *
84+
y = betaincinv( 0.2, 3.0, 3.0 );
85+
console.log( y );
86+
// => ~0.327
13787

138-
## See Also
88+
y = betaincinv( 0.4, 3.0, 3.0 );
89+
console.log( y );
90+
// => ~0.446
13991

140-
- <span class="package-name">[`@stdlib/math/base/special/beta`][@stdlib/math/base/special/beta]</span><span class="delimiter">: </span><span class="description">beta function.</span>
141-
- <span class="package-name">[`@stdlib/math/base/special/betainc`][@stdlib/math/base/special/betainc]</span><span class="delimiter">: </span><span class="description">incomplete beta function.</span>
142-
- <span class="package-name">[`@stdlib/math/base/special/betaln`][@stdlib/math/base/special/betaln]</span><span class="delimiter">: </span><span class="description">natural logarithm of the beta function.</span>
92+
y = betaincinv( 0.4, 1.0, 6.0 );
93+
console.log( y );
94+
// => ~0.082
95+
```
14396

14497
</section>
14598

146-
<!-- /.related -->
147-
148-
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
99+
<!-- /.examples -->
149100

150101
<section class="links">
151102

152-
[incomplete-beta-function]: https://en.wikipedia.org/wiki/Incomplete_beta_function
153-
154-
<!-- <related-links> -->
155-
156-
[@stdlib/math/base/special/beta]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/beta
157-
158-
[@stdlib/math/base/special/betainc]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/betainc
159-
160-
[@stdlib/math/base/special/betaln]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/betaln
161-
162-
<!-- </related-links> -->
103+
[incomplete-beta]: https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function
104+
[inverse-incomplete-beta]: https://en.wikipedia.org/wiki/Beta_function#Incomplete_beta_function
163105

164106
</section>
165107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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+
var randu = require( '@stdlib/random/base/randu' );
22+
var betaincinv = require( './../lib' );
23+
24+
var p;
25+
var a;
26+
var b;
27+
var i;
28+
29+
console.log( 'Running benchmarks...\n' );
30+
31+
// Benchmark 1: Basic usage
32+
console.log( 'Benchmark 1: Basic usage' );
33+
console.time( 'betaincinv' );
34+
for ( i = 0; i < 1e6; i++ ) {
35+
p = randu();
36+
a = randu() * 10.0 + 1.0;
37+
b = randu() * 10.0 + 1.0;
38+
betaincinv( p, a, b );
39+
}
40+
console.timeEnd( 'betaincinv' );
41+
42+
// Benchmark 2: Edge cases
43+
console.log( '\nBenchmark 2: Edge cases' );
44+
console.time( 'betaincinv (edge cases)' );
45+
for ( i = 0; i < 1e6; i++ ) {
46+
p = (i % 2 === 0) ? 0.0 : 1.0;
47+
a = 1.0;
48+
b = 1.0;
49+
betaincinv( p, a, b );
50+
}
51+
console.timeEnd( 'betaincinv (edge cases)' );
52+
53+
// Benchmark 3: Invalid inputs
54+
console.log( '\nBenchmark 3: Invalid inputs' );
55+
console.time( 'betaincinv (invalid inputs)' );
56+
for ( i = 0; i < 1e6; i++ ) {
57+
p = (i % 2 === 0) ? -0.5 : 1.5;
58+
a = 1.0;
59+
b = 1.0;
60+
betaincinv( p, a, b );
61+
}
62+
console.timeEnd( 'betaincinv (invalid inputs)' );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "betaincinv",
5+
"sources": [
6+
"src/betaincinv.c"
7+
],
8+
"include_dirs": [
9+
"<!@(node -p \"require('node-addon-api').include\")"
10+
],
11+
"dependencies": [
12+
"<!(node -p \"require('node-addon-api').gyp\")"
13+
],
14+
"cflags!": [
15+
"-fno-exceptions"
16+
],
17+
"cflags_cc!": [
18+
"-fno-exceptions"
19+
],
20+
"xcode_settings": {
21+
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
22+
"CLANG_CXX_LIBRARY": "libc++",
23+
"MACOSX_DEPLOYMENT_TARGET": "10.7"
24+
},
25+
"msvs_settings": {
26+
"VCCLCompilerTool": {
27+
"ExceptionHandling": 1
28+
}
29+
},
30+
"defines": [
31+
"NAPI_DISABLE_CPP_EXCEPTIONS"
32+
],
33+
"conditions": [
34+
['OS=="win"', {
35+
"defines": [
36+
"NOMINMAX"
37+
]
38+
}]
39+
]
40+
}
41+
]
42+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -18,15 +18,23 @@
1818

1919
'use strict';
2020

21-
var uniform = require( '@stdlib/random/array/uniform' );
22-
var logEachMap = require( '@stdlib/console/log-each-map' );
2321
var betaincinv = require( './../lib' );
2422

25-
var opts = {
26-
'dtype': 'float64'
27-
};
28-
var p = uniform( 100, 0.0, 1.0, opts );
29-
var a = uniform( 100, 0.0, 10.0, opts );
30-
var b = uniform( 100, 0.0, 10.0, opts );
23+
// Example 1: Basic usage
24+
console.log( '\nExample 1: Basic usage' );
25+
console.log( 'betaincinv(0.5, 1.0, 1.0) =', betaincinv( 0.5, 1.0, 1.0 ) );
26+
console.log( 'betaincinv(0.2, 3.0, 3.0) =', betaincinv( 0.2, 3.0, 3.0 ) );
27+
console.log( 'betaincinv(0.4, 3.0, 3.0) =', betaincinv( 0.4, 3.0, 3.0 ) );
28+
console.log( 'betaincinv(0.4, 1.0, 6.0) =', betaincinv( 0.4, 1.0, 6.0 ) );
3129

32-
logEachMap( 'p: %0.4f, \t a: %0.4f, \t b: %0.4f, \t f(p,a,b): %0.4f', p, a, b, betaincinv );
30+
// Example 2: Edge cases
31+
console.log( '\nExample 2: Edge cases' );
32+
console.log( 'betaincinv(0.0, 1.0, 1.0) =', betaincinv( 0.0, 1.0, 1.0 ) );
33+
console.log( 'betaincinv(1.0, 1.0, 1.0) =', betaincinv( 1.0, 1.0, 1.0 ) );
34+
35+
// Example 3: Invalid inputs
36+
console.log( '\nExample 3: Invalid inputs' );
37+
console.log( 'betaincinv(-0.5, 1.0, 1.0) =', betaincinv( -0.5, 1.0, 1.0 ) );
38+
console.log( 'betaincinv(1.5, 1.0, 1.0) =', betaincinv( 1.5, 1.0, 1.0 ) );
39+
console.log( 'betaincinv(0.5, 0.0, 1.0) =', betaincinv( 0.5, 0.0, 1.0 ) );
40+
console.log( 'betaincinv(0.5, 1.0, -1.0) =', betaincinv( 0.5, 1.0, -1.0 ) );

0 commit comments

Comments
 (0)