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