-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add Wald distribution mode package #10205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Planeshifter
merged 14 commits into
stdlib-js:develop
from
manit2004:feat/wald-distribution-mode-209
Apr 30, 2026
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9df60d4
core implementation done
manit2004 82357c0
test added
manit2004 b364d87
benchmark added
manit2004 2ae00d8
docs added
manit2004 96bb3e7
examples added
manit2004 607dd57
readme and package added
manit2004 ae225fe
makefile added
manit2004 4b92d79
requested changes implemented
manit2004 a59b804
requested changes made
manit2004 96ef13e
chore: minor clean-up
Planeshifter 0150602
chore: minor clean-up
Planeshifter 308e19a
fix: reorder Wald mode evaluation to avoid catastrophic cancellation
Planeshifter 1832ea1
chore: minor clean-up
Planeshifter 0878594
chore: minor clean-up
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
254 changes: 254 additions & 0 deletions
254
lib/node_modules/@stdlib/stats/base/dists/wald/mode/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # Mode | ||
|
|
||
| > [Wald][wald-distribution] distribution [mode][mode]. | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The [mode][mode] for a [Wald][wald-distribution] random variable with mean `μ` and shape parameter `λ > 0` is | ||
|
|
||
| <!-- <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."> --> | ||
|
|
||
| ```math | ||
| \mathop{\mathrm{mode}}\left( X \right) = \mu \cdot \frac{\sqrt{4\lambda^2 + 9\mu^2} - 3\mu}{2\lambda} | ||
| ``` | ||
|
|
||
| <!-- <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"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@51534079fef45e990850102147e8945fb023d1d0/lib/node_modules/@stdlib/stats/base/dists/wald/mode/docs/img/equation_wald_mode.svg" alt="Mode for a Wald distribution."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| where `μ > 0` is the mean and `λ > 0` is the shape parameter. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- Package usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var mode = require( '@stdlib/stats/base/dists/wald/mode' ); | ||
| ``` | ||
|
|
||
| #### mode( mu, lambda ) | ||
|
|
||
| Returns the [mode][mode] for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter). | ||
|
|
||
| ```javascript | ||
| var y = mode( 2.0, 1.0 ); | ||
| // returns ~0.325 | ||
|
|
||
| y = mode( 5.0, 2.0 ); | ||
| // returns ~0.655 | ||
|
|
||
| y = mode( 1.0, 1.0 ); | ||
| // returns ~0.303 | ||
| ``` | ||
|
|
||
| If provided `NaN` as any argument, the function returns `NaN`. | ||
|
|
||
| ```javascript | ||
| var y = mode( NaN, 1.0 ); | ||
| // returns NaN | ||
|
|
||
| y = mode( 1.0, NaN ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| If provided `mu <= 0` or `lambda <= 0`, the function returns `NaN`. | ||
|
|
||
| ```javascript | ||
| var y = mode( 0.0, 1.0 ); | ||
| // returns NaN | ||
|
|
||
| y = mode( -1.0, 1.0 ); | ||
| // returns NaN | ||
|
|
||
| y = mode( 1.0, 0.0 ); | ||
| // returns NaN | ||
|
|
||
| y = mode( 1.0, -1.0 ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var logEachMap = require( '@stdlib/console/log-each-map' ); | ||
| var EPS = require( '@stdlib/constants/float64/eps' ); | ||
| var mode = require( '@stdlib/stats/base/dists/wald/mode' ); | ||
|
|
||
| var opts = { | ||
| 'dtype': 'float64' | ||
| }; | ||
| var mu = uniform( 10, EPS, 10.0, opts ); | ||
| var lambda = uniform( 10, EPS, 20.0, opts ); | ||
|
|
||
| logEachMap( 'µ: %0.4f, λ: %0.4f, mode(X;µ,λ): %0.4f', mu, lambda, mode ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- C interface documentation. --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="c"> | ||
|
|
||
| ## C APIs | ||
|
|
||
| <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <!-- C usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ### Usage | ||
|
|
||
| ```c | ||
| #include "stdlib/stats/base/dists/wald/mode.h" | ||
| ``` | ||
|
|
||
| #### stdlib_base_dists_wald_mode( mu, lambda ) | ||
|
|
||
| Returns the mode for a Wald distribution with mean `mu` and shape parameter `lambda`. | ||
|
|
||
| ```c | ||
| double out = stdlib_base_dists_wald_mode( 2.0, 1.0 ); | ||
| // returns ~0.325 | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **mu**: `[in] double` mean. | ||
| - **lambda**: `[in] double` shape parameter. | ||
|
|
||
| ```c | ||
| double stdlib_base_dists_wald_mode( const double mu, const double lambda ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- C API usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ### Examples | ||
|
|
||
| ```c | ||
| #include "stdlib/stats/base/dists/wald/mode.h" | ||
| #include "stdlib/constants/float64/eps.h" | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
|
|
||
| static double random_uniform( const double min, const double max ) { | ||
| double v = (double)rand() / ( (double)RAND_MAX + 1.0 ); | ||
| return min + ( v*(max-min) ); | ||
| } | ||
|
|
||
| int main( void ) { | ||
| double lambda; | ||
| double mu; | ||
| double y; | ||
| int i; | ||
|
|
||
| for ( i = 0; i < 25; i++ ) { | ||
| mu = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 ); | ||
| lambda = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 20.0 ); | ||
| y = stdlib_base_dists_wald_mode( mu, lambda ); | ||
| printf( "µ: %lf, λ: %lf, mode(X;µ,λ): %lf\n", mu, lambda, y ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
|
Comment on lines
+221
to
+227
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The C example in the README doesn't match |
||
| </section> | ||
|
|
||
| <!-- /.c --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [wald-distribution]: https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution | ||
|
|
||
| [mode]: https://en.wikipedia.org/wiki/Mode_%28statistics%29 | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
57 changes: 57 additions & 0 deletions
57
lib/node_modules/@stdlib/stats/base/dists/wald/mode/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var EPS = require( '@stdlib/constants/float64/eps' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var mode = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var lambda; | ||
| var len; | ||
| var mu; | ||
| var y; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| mu = uniform( len, EPS, 100.0 ); | ||
| lambda = uniform( len, EPS, 20.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = mode( mu[ i%len ], lambda[ i%len ] ); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
67 changes: 67 additions & 0 deletions
67
lib/node_modules/@stdlib/stats/base/dists/wald/mode/benchmark/benchmark.native.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var resolve = require( 'path' ).resolve; | ||
| var bench = require( '@stdlib/bench' ); | ||
| var uniform = require( '@stdlib/random/array/uniform' ); | ||
| var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
| var tryRequire = require( '@stdlib/utils/try-require' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var EPS = require( '@stdlib/constants/float64/eps' ); | ||
| var pkg = require( './../package.json' ).name; | ||
|
|
||
|
|
||
| // VARIABLES // | ||
|
|
||
| var mode = tryRequire( resolve( __dirname, './../lib/native.js' ) ); | ||
| var opts = { | ||
| 'skip': ( mode instanceof Error ) | ||
| }; | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( format( '%s::native', pkg ), opts, function benchmark( b ) { | ||
| var lambda; | ||
| var len; | ||
| var mu; | ||
| var y; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| mu = uniform( len, EPS, 100.0 ); | ||
| lambda = uniform( len, EPS, 20.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = mode( mu[ i%len ], lambda[ i%len ] ); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( isnan( y ) ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.