|
| 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 | +# Mode |
| 22 | + |
| 23 | +> [Wald][wald-distribution] distribution [mode][mode]. |
| 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 [mode][mode] for a [Wald][wald-distribution] random variable with mean `μ` and shape parameter `λ > 0` is |
| 30 | + |
| 31 | +<!-- <equation class="equation" label="eq:wald_mode" align="center" raw="\operatorname{mode}\left( X \right) = \mu \cdot \frac{\sqrt{4\lambda^2 + 9\mu^2} - 3\mu}{2\lambda}" alt="Mode for a Wald distribution."> --> |
| 32 | + |
| 33 | +```math |
| 34 | +\mathop{\mathrm{mode}}\left( X \right) = \mu \cdot \frac{\sqrt{4\lambda^2 + 9\mu^2} - 3\mu}{2\lambda} |
| 35 | +``` |
| 36 | + |
| 37 | +<!-- <div class="equation" align="center" data-raw-text="\operatorname{mode}\left( X \right) = \mu \cdot \frac{\sqrt{4\lambda^2 + 9\mu^2} - 3\mu}{2\lambda}" data-equation="eq:wald_mode"> |
| 38 | +</div> --> |
| 39 | + |
| 40 | +<!-- </equation> --> |
| 41 | + |
| 42 | +</section> |
| 43 | + |
| 44 | +<!-- /.intro --> |
| 45 | + |
| 46 | +<!-- Package usage documentation. --> |
| 47 | + |
| 48 | +<section class="usage"> |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +```javascript |
| 53 | +var mode = require( '@stdlib/stats/base/dists/wald/mode' ); |
| 54 | +``` |
| 55 | + |
| 56 | +#### mode( mu, lambda ) |
| 57 | + |
| 58 | +Returns the [mode][mode] for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter). |
| 59 | + |
| 60 | +```javascript |
| 61 | +var y = mode( 2.0, 1.0 ); |
| 62 | +// returns ~0.325 |
| 63 | + |
| 64 | +y = mode( 5.0, 2.0 ); |
| 65 | +// returns ~0.655 |
| 66 | + |
| 67 | +y = mode( 1.0, 1.0 ); |
| 68 | +// returns ~0.303 |
| 69 | +``` |
| 70 | + |
| 71 | +If provided `NaN` as any argument, the function returns `NaN`. |
| 72 | + |
| 73 | +```javascript |
| 74 | +var y = mode( NaN, 1.0 ); |
| 75 | +// returns NaN |
| 76 | + |
| 77 | +y = mode( 1.0, NaN ); |
| 78 | +// returns NaN |
| 79 | +``` |
| 80 | + |
| 81 | +If provided `mu <= 0` or `lambda <= 0`, the function returns `NaN`. |
| 82 | + |
| 83 | +```javascript |
| 84 | +var y = mode( 0.0, 1.0 ); |
| 85 | +// returns NaN |
| 86 | + |
| 87 | +y = mode( -1.0, 1.0 ); |
| 88 | +// returns NaN |
| 89 | + |
| 90 | +y = mode( 1.0, 0.0 ); |
| 91 | +// returns NaN |
| 92 | + |
| 93 | +y = mode( 1.0, -1.0 ); |
| 94 | +// returns NaN |
| 95 | +``` |
| 96 | + |
| 97 | +</section> |
| 98 | + |
| 99 | +<!-- /.usage --> |
| 100 | + |
| 101 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 102 | + |
| 103 | +<section class="notes"> |
| 104 | + |
| 105 | +</section> |
| 106 | + |
| 107 | +<!-- /.notes --> |
| 108 | + |
| 109 | +<!-- Package usage examples. --> |
| 110 | + |
| 111 | +<section class="examples"> |
| 112 | + |
| 113 | +## Examples |
| 114 | + |
| 115 | +<!-- eslint no-undef: "error" --> |
| 116 | + |
| 117 | +```javascript |
| 118 | +var uniform = require( '@stdlib/random/array/uniform' ); |
| 119 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 120 | +var EPS = require( '@stdlib/constants/float64/eps' ); |
| 121 | +var mode = require( '@stdlib/stats/base/dists/wald/mode' ); |
| 122 | + |
| 123 | +var opts = { |
| 124 | + 'dtype': 'float64' |
| 125 | +}; |
| 126 | +var mu = uniform( 10, EPS, 10.0, opts ); |
| 127 | +var lambda = uniform( 10, EPS, 20.0, opts ); |
| 128 | + |
| 129 | +logEachMap( 'µ: %0.4f, λ: %0.4f, mode(X;µ,λ): %0.4f', mu, lambda, mode ); |
| 130 | +``` |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.examples --> |
| 135 | + |
| 136 | +<!-- C interface documentation. --> |
| 137 | + |
| 138 | +* * * |
| 139 | + |
| 140 | +<section class="c"> |
| 141 | + |
| 142 | +## C APIs |
| 143 | + |
| 144 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 145 | + |
| 146 | +<section class="intro"> |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.intro --> |
| 151 | + |
| 152 | +<!-- C usage documentation. --> |
| 153 | + |
| 154 | +<section class="usage"> |
| 155 | + |
| 156 | +### Usage |
| 157 | + |
| 158 | +```c |
| 159 | +#include "stdlib/stats/base/dists/wald/mode.h" |
| 160 | +``` |
| 161 | + |
| 162 | +#### stdlib_base_dists_wald_mode( mu, lambda ) |
| 163 | + |
| 164 | +Returns the mode for a Wald distribution with mean `mu` and shape parameter `lambda`. |
| 165 | + |
| 166 | +```c |
| 167 | +double out = stdlib_base_dists_wald_mode( 2.0, 1.0 ); |
| 168 | +// returns ~0.325 |
| 169 | +``` |
| 170 | + |
| 171 | +The function accepts the following arguments: |
| 172 | + |
| 173 | +- **mu**: `[in] double` mean. |
| 174 | +- **lambda**: `[in] double` shape parameter. |
| 175 | + |
| 176 | +```c |
| 177 | +double stdlib_base_dists_wald_mode( const double mu, const double lambda ); |
| 178 | +``` |
| 179 | +
|
| 180 | +</section> |
| 181 | +
|
| 182 | +<!-- /.usage --> |
| 183 | +
|
| 184 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 185 | +
|
| 186 | +<section class="notes"> |
| 187 | +
|
| 188 | +</section> |
| 189 | +
|
| 190 | +<!-- /.notes --> |
| 191 | +
|
| 192 | +<!-- C API usage examples. --> |
| 193 | +
|
| 194 | +<section class="examples"> |
| 195 | +
|
| 196 | +### Examples |
| 197 | +
|
| 198 | +```c |
| 199 | +#include "stdlib/stats/base/dists/wald/mode.h" |
| 200 | +#include "stdlib/constants/float64/eps.h" |
| 201 | +#include <stdlib.h> |
| 202 | +#include <stdio.h> |
| 203 | +
|
| 204 | +static double random_uniform( const double min, const double max ) { |
| 205 | + double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); |
| 206 | + return min + ( v*(max-min) ); |
| 207 | +} |
| 208 | +
|
| 209 | +int main( void ) { |
| 210 | + double lambda; |
| 211 | + double mu; |
| 212 | + double y; |
| 213 | + int i; |
| 214 | +
|
| 215 | + for ( i = 0; i < 25; i++ ) { |
| 216 | + mu = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 ); |
| 217 | + lambda = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 ); |
| 218 | + y = stdlib_base_dists_wald_mode( mu, lambda ); |
| 219 | + printf( "µ: %.4f, λ: %.4f, mode(X;µ,λ): %.4f\n", mu, lambda, y ); |
| 220 | + } |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | +</section> |
| 225 | + |
| 226 | +<!-- /.examples --> |
| 227 | + |
| 228 | +</section> |
| 229 | + |
| 230 | +<!-- /.c --> |
| 231 | + |
| 232 | +<!-- 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. --> |
| 233 | + |
| 234 | +<section class="references"> |
| 235 | + |
| 236 | +</section> |
| 237 | + |
| 238 | +<!-- /.references --> |
| 239 | + |
| 240 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 241 | + |
| 242 | +<section class="related"> |
| 243 | + |
| 244 | +</section> |
| 245 | + |
| 246 | +<!-- /.related --> |
| 247 | + |
| 248 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 249 | + |
| 250 | +<section class="links"> |
| 251 | + |
| 252 | +[wald-distribution]: https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution |
| 253 | + |
| 254 | +[mode]: https://en.wikipedia.org/wiki/Mode_%28statistics%29 |
| 255 | + |
| 256 | +</section> |
| 257 | + |
| 258 | +<!-- /.links --> |
0 commit comments