Skip to content

Commit a49f5d7

Browse files
committed
feat: add stats/base/dists/log-logistic/median
1 parent 2db5404 commit a49f5d7

File tree

28 files changed

+2087
-0
lines changed

28 files changed

+2087
-0
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
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+
# Median
22+
23+
> [Log-logistic][log-logistic-distribution] distribution [median][median].
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
The [median][median] for a [log-logistic][log-logistic-distribution] random variable with scale `α > 0` and shape `β > 0` is
30+
31+
<!-- <equation class="equation" label="eq:log-logistic_median" align="center" raw="\operatorname{Median}\left( X \right) = \alpha" alt="Median for a log-logistic distribution."> -->
32+
33+
```math
34+
\mathop{\mathrm{Median}}\left( X \right) = \alpha
35+
```
36+
37+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{Median}\left( X \right) = \alpha" data-equation="eq:log-logistic_median">
38+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/log-logistic/median/docs/img/equation_log_logistic_median.svg" alt="Median for a log-logistic distribution.">
39+
<br>
40+
</div> -->
41+
42+
<!-- </equation> -->
43+
44+
</section>
45+
46+
<!-- /.intro -->
47+
48+
<!-- Package usage documentation. -->
49+
50+
<section class="usage">
51+
52+
## Usage
53+
54+
```javascript
55+
var median = require( '@stdlib/stats/base/dists/log-logistic/median' );
56+
```
57+
58+
#### median( alpha, beta )
59+
60+
Returns the [median][median] for a [log-logistic][log-logistic-distribution] distribution with scale parameter `alpha` and shape parameter `beta`.
61+
62+
```javascript
63+
var y = median( 2.0, 1.0 );
64+
// returns 2.0
65+
66+
y = median( 4.0, 3.0 );
67+
// returns 4.0
68+
69+
y = median( 1.0, 5.0 );
70+
// returns 1.0
71+
```
72+
73+
If provided `NaN` as any argument, the function returns `NaN`.
74+
75+
```javascript
76+
var y = median( NaN, 1.0 );
77+
// returns NaN
78+
79+
y = median( 1.0, NaN );
80+
// returns NaN
81+
```
82+
83+
If provided `alpha <= 0`, the function returns `NaN`.
84+
```javascript
85+
var y = median( 0.0, 1.0 );
86+
// returns NaN
87+
88+
y = median( -1.0, 1.0 );
89+
// returns NaN
90+
```
91+
92+
If provided `beta <= 0`, the function returns `NaN`.
93+
```javascript
94+
var y = median( 1.0, 0.0 );
95+
// returns NaN
96+
97+
y = median( 1.0, -1.0 );
98+
// returns NaN
99+
```
100+
101+
</section>
102+
103+
<!-- /.usage -->
104+
105+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
106+
107+
<section class="notes">
108+
109+
</section>
110+
111+
<!-- /.notes -->
112+
113+
<!-- Package usage examples. -->
114+
115+
<section class="examples">
116+
117+
## Examples
118+
119+
<!-- eslint no-undef: "error" -->
120+
121+
```javascript
122+
var uniform = require( '@stdlib/random/array/uniform' );
123+
var logEachMap = require( '@stdlib/console/log-each-map' );
124+
var median = require( '@stdlib/stats/base/dists/log-logistic/median' );
125+
126+
var opts = {
127+
'dtype': 'float64'
128+
};
129+
var alpha = uniform( 10, 0.1, 10.0, opts );
130+
var beta = uniform( 10, 0.1, 10.0, opts );
131+
132+
logEachMap( 'alpha: %0.4f, beta: %0.4f, Median(X;alpha,beta): %0.4f', alpha, beta, median );
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- C interface documentation. -->
140+
141+
* * *
142+
143+
<section class="c">
144+
145+
## C APIs
146+
147+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
148+
149+
<section class="intro">
150+
151+
</section>
152+
153+
<!-- /.intro -->
154+
155+
<!-- C usage documentation. -->
156+
157+
<section class="usage">
158+
159+
### Usage
160+
161+
```c
162+
#include "stdlib/stats/base/dists/log-logistic/median.h"
163+
```
164+
165+
#### stdlib_base_dists_log_logistic_median( alpha, beta )
166+
167+
Returns the median for a log-logistic distribution with scale `alpha` and shape `beta`.
168+
169+
```c
170+
double out = stdlib_base_dists_log_logistic_median( 1.0, 1.0 );
171+
// returns 1.0
172+
```
173+
174+
The function accepts the following arguments:
175+
176+
- **alpha**: `[in] double` scale parameter.
177+
- **beta**: `[in] double` shape parameter.
178+
179+
```c
180+
double stdlib_base_dists_log_logistic_median( const double alpha, const double beta );
181+
```
182+
183+
</section>
184+
185+
<!-- /.usage -->
186+
187+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
188+
189+
<section class="notes">
190+
191+
</section>
192+
193+
<!-- /.notes -->
194+
195+
<!-- C API usage examples. -->
196+
197+
<section class="examples">
198+
199+
### Examples
200+
201+
```c
202+
#include "stdlib/stats/base/dists/log-logistic/median.h"
203+
#include <stdlib.h>
204+
#include <stdio.h>
205+
206+
static double random_uniform( const double min, const double max ) {
207+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
208+
return min + ( v*(max-min) );
209+
}
210+
211+
int main( void ) {
212+
double alpha;
213+
double beta;
214+
double v;
215+
int i;
216+
217+
for ( i = 0; i < 25; i++ ) {
218+
alpha = random_uniform( 0.1, 10.0 );
219+
beta = random_uniform( 0.1, 10.0 );
220+
v = stdlib_base_dists_log_logistic_median( alpha, beta );
221+
printf( "alpha: %lf, beta: %lf, Median(X;alpha,beta): %lf\n", alpha, beta, v );
222+
}
223+
}
224+
```
225+
226+
</section>
227+
228+
<!-- /.examples -->
229+
230+
</section>
231+
232+
<!-- /.c -->
233+
234+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
235+
236+
<section class="references">
237+
238+
</section>
239+
240+
<!-- /.references -->
241+
242+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
243+
244+
<section class="related">
245+
246+
</section>
247+
248+
<!-- /.related -->
249+
250+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
251+
252+
<section class="links">
253+
254+
[log-logistic-distribution]: https://en.wikipedia.org/wiki/Log-logistic_distribution
255+
256+
[median]: https://en.wikipedia.org/wiki/Median
257+
258+
</section>
259+
260+
<!-- /.links -->
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 EPS = require( '@stdlib/constants/float64/eps' );
27+
var pkg = require( './../package.json' ).name;
28+
var median = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var alpha;
35+
var beta;
36+
var opts;
37+
var y;
38+
var i;
39+
40+
opts = {
41+
'dtype': 'float64'
42+
};
43+
alpha = uniform( 100, EPS, 10.0, opts );
44+
beta = uniform( 100, EPS, 10.0, opts );
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
y = median( alpha[ i % alpha.length ], beta[ i % beta.length ] );
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+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var tryRequire = require( '@stdlib/utils/try-require' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var EPS = require( '@stdlib/constants/float64/eps' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var median = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( median instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
44+
var alpha;
45+
var beta;
46+
var opts;
47+
var y;
48+
var i;
49+
50+
opts = {
51+
'dtype': 'float64'
52+
};
53+
alpha = uniform( 100, EPS, 10.0, opts );
54+
beta = uniform( 100, EPS, 10.0, opts );
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
y = median( alpha[ i % alpha.length ], beta[ i % beta.length ] );
58+
if ( isnan( y ) ) {
59+
b.fail( 'should not return NaN' );
60+
}
61+
}
62+
b.toc();
63+
if ( isnan( y ) ) {
64+
b.fail( 'should not return NaN' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});

0 commit comments

Comments
 (0)