Skip to content

Commit c2744eb

Browse files
committed
feat: add stats/base/dists/anglit/quantile
--- 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: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 ef7e6f0 commit c2744eb

File tree

31 files changed

+2604
-0
lines changed

31 files changed

+2604
-0
lines changed
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# Quantile Function
22+
23+
> [Anglit][anglit-distribution] distribution [quantile function][quantile-function].
24+
25+
<section class="intro">
26+
27+
The [quantile function][quantile-function] for an [anglit][anglit-distribution] random variable is
28+
29+
```math
30+
Q(p;\mu,\sigma) = \mu + \sigma\left( \sin^{-1}(\sqrt{p}) - \frac{\pi}{4} \right)
31+
```
32+
33+
for `0 <= p <= 1`, where `μ` is the location parameter and `σ` is the scale parameter.
34+
35+
</section>
36+
37+
<!-- /.intro -->
38+
39+
<section class="usage">
40+
41+
## Usage
42+
43+
```javascript
44+
var quantile = require( '@stdlib/stats/base/dists/anglit/quantile' );
45+
```
46+
47+
#### quantile( p, mu, sigma )
48+
49+
Evaluates the [quantile function][quantile-function] for an [anglit][anglit-distribution] distribution with location parameter `mu` and scale parameter `sigma`.
50+
51+
```javascript
52+
var y = quantile( 0.8, 0.0, 1.0 );
53+
// returns ~0.322
54+
55+
y = quantile( 0.5, 4.0, 2.0 );
56+
// returns 4.0
57+
```
58+
59+
If provided a probability `p` outside the interval `[0,1]`, the function returns `NaN`.
60+
61+
```javascript
62+
var y = quantile( 1.9, 0.0, 1.0 );
63+
// returns NaN
64+
65+
y = quantile( -0.1, 0.0, 1.0 );
66+
// returns NaN
67+
```
68+
69+
If provided `NaN` as any argument, the function returns `NaN`.
70+
71+
```javascript
72+
var y = quantile( NaN, 0.0, 1.0 );
73+
// returns NaN
74+
75+
y = quantile( 0.2, NaN, 1.0 );
76+
// returns NaN
77+
78+
y = quantile( 0.2, 0.0, NaN );
79+
// returns NaN
80+
```
81+
82+
If provided `sigma < 0`, the function returns `NaN`.
83+
84+
```javascript
85+
var y = quantile( 0.4, 0.0, -1.0 );
86+
// returns NaN
87+
```
88+
89+
If `sigma` equals `0`, the function evaluates a degenerate distribution centered at `mu`.
90+
91+
```javascript
92+
var y = quantile( 0.9, 3.0, 0.0 );
93+
// returns 3.0
94+
```
95+
96+
#### quantile.factory( mu, sigma )
97+
98+
Returns a function for evaluating the [quantile function][quantile-function] of an [anglit][anglit-distribution] distribution with location parameter `mu` and scale parameter `sigma`.
99+
100+
```javascript
101+
var myquantile = quantile.factory( 4.0, 2.0 );
102+
103+
var y = myquantile( 0.2 );
104+
// returns ~3.356
105+
106+
y = myquantile( 0.9 );
107+
// returns ~4.927
108+
```
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<section class="examples">
115+
116+
## Examples
117+
118+
<!-- eslint no-undef: "error" -->
119+
120+
```javascript
121+
var uniform = require( '@stdlib/random/array/uniform' );
122+
var logEachMap = require( '@stdlib/console/log-each-map' );
123+
var quantile = require( '@stdlib/stats/base/dists/anglit/quantile' );
124+
125+
var opts = {
126+
'dtype': 'float64'
127+
};
128+
var p = uniform( 10, 0.0, 1.0, opts );
129+
var mu = uniform( 10, -5.0, 5.0, opts );
130+
var sigma = uniform( 10, 0.0, 5.0, opts );
131+
132+
logEachMap( 'p: %lf, mu: %lf, sigma: %lf, Q(p;mu,sigma): %lf', p, mu, sigma, quantile );
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
* * *
140+
141+
<section class="c">
142+
143+
## C APIs
144+
145+
<section class="intro">
146+
147+
</section>
148+
149+
<!-- /.intro -->
150+
151+
<section class="usage">
152+
153+
### Usage
154+
155+
```c
156+
#include "stdlib/stats/base/dists/anglit/quantile.h"
157+
```
158+
159+
#### stdlib_base_dists_anglit_quantile( p, mu, sigma )
160+
161+
Evaluates the [quantile function][quantile-function] for an [anglit][anglit-distribution] distribution with location parameter `mu` and scale parameter `sigma`.
162+
163+
```c
164+
double out = stdlib_base_dists_anglit_quantile( 0.8, 0.0, 1.0 );
165+
// returns ~0.322
166+
```
167+
168+
The function accepts the following arguments:
169+
170+
- **p**: `[in] double` probability.
171+
- **mu**: `[in] double` location parameter.
172+
- **sigma**: `[in] double` scale parameter.
173+
174+
```c
175+
double stdlib_base_dists_anglit_quantile( const double p, const double mu, const double sigma );
176+
```
177+
178+
</section>
179+
180+
<!-- /.usage -->
181+
182+
<section class="notes">
183+
184+
</section>
185+
186+
<!-- /.notes -->
187+
188+
<section class="examples">
189+
190+
### Examples
191+
192+
```c
193+
#include "stdlib/stats/base/dists/anglit/quantile.h"
194+
#include <stdlib.h>
195+
#include <stdio.h>
196+
197+
static double random_uniform( const double min, const double max ) {
198+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
199+
return min + ( v * ( max - min ) );
200+
}
201+
202+
int main( void ) {
203+
double result;
204+
double sigma;
205+
double mu;
206+
double p;
207+
int i;
208+
209+
for ( i = 0; i < 10; i++ ) {
210+
p = random_uniform( 0.0, 1.0 );
211+
mu = random_uniform( -5.0, 5.0 );
212+
sigma = random_uniform( 0.0, 5.0 );
213+
result = stdlib_base_dists_anglit_quantile( p, mu, sigma );
214+
printf( "p: %lf, mu: %lf, sigma: %lf, Q(p;mu,sigma): %lf\n", p, mu, sigma, result );
215+
}
216+
}
217+
```
218+
219+
</section>
220+
221+
<!-- /.examples -->
222+
223+
</section>
224+
225+
<!-- /.c -->
226+
227+
<section class="related">
228+
229+
</section>
230+
231+
<!-- /.related -->
232+
233+
<section class="links">
234+
235+
[quantile-function]: https://en.wikipedia.org/wiki/Quantile_function
236+
237+
[anglit-distribution]: https://en.wikipedia.org/wiki/Anglit_distribution
238+
239+
</section>
240+
241+
<!-- /.links -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var quantile = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var sigma;
35+
var opts;
36+
var idx;
37+
var mu;
38+
var p;
39+
var y;
40+
var i;
41+
42+
opts = {
43+
'dtype': 'float64'
44+
};
45+
p = uniform( 100, 0.0, 1.0, opts );
46+
mu = uniform( 100, -20.0, 20.0, opts );
47+
sigma = uniform( 100, 0.0, 20.0, opts );
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
idx = i % p.length;
52+
y = quantile( p[ idx ], mu[ idx ], sigma[ idx ] );
53+
if ( isnan( y ) ) {
54+
b.fail( 'should not return NaN' );
55+
}
56+
}
57+
b.toc();
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
64+
65+
bench( format( '%s:factory', pkg ), function benchmark( b ) {
66+
var myQuantile;
67+
var sigma;
68+
var opts;
69+
var mu;
70+
var p;
71+
var y;
72+
var i;
73+
74+
mu = 4.0;
75+
sigma = 3.0;
76+
myQuantile = quantile.factory( mu, sigma );
77+
78+
opts = {
79+
'dtype': 'float64'
80+
};
81+
p = uniform( 100, 0.0, 1.0, opts );
82+
83+
b.tic();
84+
for ( i = 0; i < b.iterations; i++ ) {
85+
y = myQuantile( p[ i % p.length ] );
86+
if ( isnan( y ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
}
90+
b.toc();
91+
if ( isnan( y ) ) {
92+
b.fail( 'should not return NaN' );
93+
}
94+
b.pass( 'benchmark finished' );
95+
b.end();
96+
});

0 commit comments

Comments
 (0)