Skip to content

Commit d62a9e7

Browse files
feat/quantile
1 parent a5b0d83 commit d62a9e7

File tree

10 files changed

+719
-0
lines changed

10 files changed

+719
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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
22+
23+
> Compute the p-th quantile of a sorted one-dimensional ndarray.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var quantile = require( '@stdlib/stats/base/ndarray/quantile' );
37+
```
38+
39+
#### quantile( arrays , p )
40+
41+
Computes the p-th quantile of a sorted one-dimensional ndarray.
42+
43+
```javascript
44+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
45+
46+
var xbuf = [ 1.0, 2.0, 3.0, 4.0 ];
47+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
48+
49+
var v = quantile( [x], 0.5 );
50+
// returns 2.5
51+
```
52+
53+
The function has the following parameters:
54+
55+
56+
- **arrays**: array-like object containing a sorted one-dimensional input ndarray.
57+
- **p**: quantile probability in the interval 0 and 1.
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<section class="notes">
64+
65+
## Notes
66+
67+
- If provided an empty ndarray, the function returns `NaN`.
68+
69+
</section>
70+
71+
<!-- /.notes -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var linspace = require( '@stdlib/array/linspace' );
81+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
82+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
83+
var quantile = require( '@stdlib/stats/base/ndarray/quantile' );
84+
85+
// Create a linearly spaced sorted array:
86+
var xbuf = linspace( 0.0, 10.0, 11 );
87+
88+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
89+
console.log( ndarray2array( x ) );
90+
91+
var v = quantile( [ x ], 0.5 );
92+
console.log( v );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
</section>
104+
105+
<!-- /.related -->
106+
107+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="links">
110+
111+
</section>
112+
113+
<!-- /.links -->
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 bench = require( '@stdlib/bench' );
22+
var linspace = require( '@stdlib/array/linspace' );
23+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
26+
var pkg = require( './../package.json' ).name;
27+
var quantile = require( './../lib' );
28+
29+
var options = {
30+
'dtype': 'generic'
31+
};
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var xbuf;
42+
var x;
43+
44+
xbuf = linspace( 0.0, 100.0, len );
45+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
46+
47+
return benchmark;
48+
/**
49+
* Benchmark function.
50+
*
51+
* @private
52+
* @param {Object} b - benchmark instance
53+
* @returns {void}
54+
*/
55+
function benchmark( b ) {
56+
var v;
57+
var i;
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
v = quantile( [ x ], 0.5 );
62+
if ( isnan( v ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( v ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
}
73+
}
74+
75+
76+
// MAIN //
77+
78+
/**
79+
* Main execution sequence.
80+
*
81+
* @private
82+
* @returns {void}
83+
*/
84+
function main() {
85+
var len;
86+
var min;
87+
var max;
88+
var f;
89+
var i;
90+
91+
min = 1; // 10^1
92+
max = 6; // 10^6
93+
94+
for ( i = min; i <= max; i++ ) {
95+
len = pow( 10, i );
96+
f = createBenchmark( len );
97+
bench( pkg+':len='+len, f );
98+
}
99+
}
100+
101+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{{alias}}( arrays, p )
2+
Computes the p-th quantile of a sorted one-dimensional ndarray.
3+
4+
The quantile is the value below which a fraction `p` of the data falls.
5+
The input array must be sorted in ascending order.
6+
7+
If provided an empty ndarray, the function returns `NaN`.
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing a sorted one-dimensional input ndarray.
13+
p: number
14+
Quantile probability in the interval [0,1].
15+
16+
Returns
17+
-------
18+
out: number
19+
Quantile value.
20+
21+
Examples
22+
--------
23+
> var xbuf = [ 1.0, 2.0, 3.0, 4.0 ];
24+
> var dt = 'generic';
25+
> var sh = [ xbuf.length ];
26+
> var sx = [ 1 ];
27+
> var ox = 0;
28+
> var ord = 'row-major';
29+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
30+
> {{alias}}( [ x ], 0.5 )
31+
2.5
32+
33+
See Also
34+
--------
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the p-th quantile of a sorted one-dimensional ndarray.
27+
*
28+
* @param arrays - array-like object containing a sorted input ndarray
29+
* @param p - quantile probability in the interval [0,1]
30+
* @returns quantile value
31+
*
32+
* @example
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = [ 1.0, 2.0, 3.0, 4.0 ];
36+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var v = quantile( [ x ], 0.5 );
39+
* // returns ~2.5
40+
*/
41+
declare function quantile<T extends ndarray = ndarray>( arrays: [ T ], p: number ): number;
42+
43+
44+
// EXPORTS //
45+
46+
export = quantile;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import quantile = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
33+
quantile( [ x ], 0.5 ); // $ExpectType number
34+
}
35+
36+
// The compiler throws an error if the first argument is not an array of ndarrays...
37+
{
38+
quantile( '10', 0.5 ); // $ExpectError
39+
quantile( 10, 0.5 ); // $ExpectError
40+
quantile( true, 0.5 ); // $ExpectError
41+
quantile( false, 0.5 ); // $ExpectError
42+
quantile( null, 0.5 ); // $ExpectError
43+
quantile( undefined, 0.5 ); // $ExpectError
44+
quantile( [], 0.5 ); // $ExpectError
45+
quantile( {}, 0.5 ); // $ExpectError
46+
quantile( (x: number): number => x, 0.5 ); // $ExpectError
47+
}
48+
49+
// The compiler throws an error if the second argument is not a number...
50+
{
51+
const x = zeros( [ 10 ], {
52+
'dtype': 'generic'
53+
});
54+
55+
quantile( [ x ], '0.5' ); // $ExpectError
56+
quantile( [ x ], true ); // $ExpectError
57+
quantile( [ x ], false ); // $ExpectError
58+
quantile( [ x ], null ); // $ExpectError
59+
quantile( [ x ], undefined ); // $ExpectError
60+
quantile( [ x ], [] ); // $ExpectError
61+
quantile( [ x ], {} ); // $ExpectError
62+
}
63+
64+
// The compiler throws an error if the wrong number of arguments is provided...
65+
{
66+
const x = zeros( [ 10 ], {
67+
'dtype': 'generic'
68+
});
69+
70+
quantile(); // $ExpectError
71+
quantile( [ x ] ); // $ExpectError
72+
quantile( [ x ], 0.5, {} ); // $ExpectError
73+
}

0 commit comments

Comments
 (0)