Skip to content

Commit dd82731

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

File tree

28 files changed

+2089
-0
lines changed

28 files changed

+2089
-0
lines changed
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
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+
85+
```javascript
86+
var y = median( 0.0, 1.0 );
87+
// returns NaN
88+
89+
y = median( -1.0, 1.0 );
90+
// returns NaN
91+
```
92+
93+
If provided `beta <= 0`, the function returns `NaN`.
94+
95+
```javascript
96+
var y = median( 1.0, 0.0 );
97+
// returns NaN
98+
99+
y = median( 1.0, -1.0 );
100+
// returns NaN
101+
```
102+
103+
</section>
104+
105+
<!-- /.usage -->
106+
107+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="notes">
110+
111+
</section>
112+
113+
<!-- /.notes -->
114+
115+
<!-- Package usage examples. -->
116+
117+
<section class="examples">
118+
119+
## Examples
120+
121+
<!-- eslint no-undef: "error" -->
122+
123+
```javascript
124+
var uniform = require( '@stdlib/random/array/uniform' );
125+
var logEachMap = require( '@stdlib/console/log-each-map' );
126+
var median = require( '@stdlib/stats/base/dists/log-logistic/median' );
127+
128+
var opts = {
129+
'dtype': 'float64'
130+
};
131+
var alpha = uniform( 10, 0.1, 10.0, opts );
132+
var beta = uniform( 10, 0.1, 10.0, opts );
133+
134+
logEachMap( 'alpha: %0.4f, beta: %0.4f, Median(X;alpha,beta): %0.4f', alpha, beta, median );
135+
```
136+
137+
</section>
138+
139+
<!-- /.examples -->
140+
141+
<!-- C interface documentation. -->
142+
143+
* * *
144+
145+
<section class="c">
146+
147+
## C APIs
148+
149+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
150+
151+
<section class="intro">
152+
153+
</section>
154+
155+
<!-- /.intro -->
156+
157+
<!-- C usage documentation. -->
158+
159+
<section class="usage">
160+
161+
### Usage
162+
163+
```c
164+
#include "stdlib/stats/base/dists/log-logistic/median.h"
165+
```
166+
167+
#### stdlib_base_dists_log_logistic_median( alpha, beta )
168+
169+
Returns the median for a log-logistic distribution with scale `alpha` and shape `beta`.
170+
171+
```c
172+
double out = stdlib_base_dists_log_logistic_median( 1.0, 1.0 );
173+
// returns 1.0
174+
```
175+
176+
The function accepts the following arguments:
177+
178+
- **alpha**: `[in] double` scale parameter.
179+
- **beta**: `[in] double` shape parameter.
180+
181+
```c
182+
double stdlib_base_dists_log_logistic_median( const double alpha, const double beta );
183+
```
184+
185+
</section>
186+
187+
<!-- /.usage -->
188+
189+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
190+
191+
<section class="notes">
192+
193+
</section>
194+
195+
<!-- /.notes -->
196+
197+
<!-- C API usage examples. -->
198+
199+
<section class="examples">
200+
201+
### Examples
202+
203+
```c
204+
#include "stdlib/stats/base/dists/log-logistic/median.h"
205+
#include <stdlib.h>
206+
#include <stdio.h>
207+
208+
static double random_uniform( const double min, const double max ) {
209+
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
210+
return min + ( v*(max-min) );
211+
}
212+
213+
int main( void ) {
214+
double alpha;
215+
double beta;
216+
double v;
217+
int i;
218+
219+
for ( i = 0; i < 25; i++ ) {
220+
alpha = random_uniform( 0.1, 10.0 );
221+
beta = random_uniform( 0.1, 10.0 );
222+
v = stdlib_base_dists_log_logistic_median( alpha, beta );
223+
printf( "alpha: %lf, beta: %lf, Median(X;alpha,beta): %lf\n", alpha, beta, v );
224+
}
225+
}
226+
```
227+
228+
</section>
229+
230+
<!-- /.examples -->
231+
232+
</section>
233+
234+
<!-- /.c -->
235+
236+
<!-- 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. -->
237+
238+
<section class="references">
239+
240+
</section>
241+
242+
<!-- /.references -->
243+
244+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
245+
246+
<section class="related">
247+
248+
</section>
249+
250+
<!-- /.related -->
251+
252+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
253+
254+
<section class="links">
255+
256+
[log-logistic-distribution]: https://en.wikipedia.org/wiki/Log-logistic_distribution
257+
258+
[median]: https://en.wikipedia.org/wiki/Median
259+
260+
</section>
261+
262+
<!-- /.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)