-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add stats/base/dists/wald/cdf
#9709
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
Open
Neerajpathak07
wants to merge
8
commits into
stdlib-js:develop
Choose a base branch
from
Neerajpathak07:wald/cdf
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,839
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d24de8
feat: add initial setup
Neerajpathak07 ddc6da2
chore: updating tests
Neerajpathak07 1448183
chore: update package.json
Neerajpathak07 86d2872
chore: updating return values
Neerajpathak07 92c7952
chore: clean up
Neerajpathak07 480cfb0
chore: updating return value
Neerajpathak07 13b639d
chore: apply suggested changes
Neerajpathak07 8c7437d
chore: clean up
Neerajpathak07 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
279 changes: 279 additions & 0 deletions
279
lib/node_modules/@stdlib/stats/base/dists/wald/cdf/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,279 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # Cumulative Distribution Function | ||
|
|
||
| > [Wald][wald-distribution] distribution [cumulative distribution function][cdf]. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The [cumulative distribution function][cdf] for a [Wald][wald-distribution] random variable is | ||
|
|
||
| <!-- <equation class="equation" label="eq:wald_cdf" align="center" raw="F(x;\mu,\lambda)=\Phi(\sqrt{\lambda/x}((x/\mu)-1))+\exp(2\lambda/\mu)\Phi(-\sqrt{\lambda/x}((x/\mu)+1))" alt="Cumulative distribution function (CDF) for a Wald distribution."> --> | ||
|
|
||
| ```math | ||
| F(x;\mu,\lambda) = \Phi\!\left(\sqrt{\frac{\lambda}{x}}\left(\frac{x}{\mu}-1\right)\right) + \exp\!\left(\frac{2\lambda}{\mu}\right)\,\Phi\!\left(-\sqrt{\frac{\lambda}{x}}\left(\frac{x}{\mu}+1\right)\right) | ||
| ``` | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| where `µ > 0` is the mean and `λ > 0` is the shape parameter. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var cdf = require( '@stdlib/stats/base/dists/wald/cdf' ); | ||
| ``` | ||
|
|
||
| #### cdf( x, mu, lambda ) | ||
|
|
||
| Evaluates the [cumulative distribution function][cdf] (CDF) for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter). | ||
|
|
||
| ```javascript | ||
| var y = cdf( 2.0, 2.0, 1.0 ); | ||
| // returns ~0.714 | ||
|
|
||
| y = cdf( 1.0, 2.0, 1.0 ); | ||
| // returns ~0.490 | ||
|
|
||
| y = cdf( 4.0, 2.0, 1.0 ); | ||
| // returns ~0.873 | ||
| ``` | ||
|
|
||
| If provided `NaN` as any argument, the function returns `NaN`. | ||
|
|
||
| ```javascript | ||
| var y = cdf( NaN, 2.0, 1.0 ); | ||
| // returns NaN | ||
|
|
||
| y = cdf( 2.0, NaN, 1.0 ); | ||
| // returns NaN | ||
|
|
||
| y = cdf( 2.0, 2.0, NaN ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| If provided `mu <= 0` or `lambda < 0`, the function returns `NaN`. | ||
|
|
||
| ```javascript | ||
| var y = cdf( 2.0, 0.0, -3.0 ); | ||
| // returns NaN | ||
|
|
||
| y = cdf( 2.0, -1.0, -2.0 ); | ||
| // returns NaN | ||
|
|
||
| y = cdf( 2.0, -2.0, -1.0 ); | ||
| // returns NaN | ||
| ``` | ||
|
|
||
| If provided `x <= 0`, the function returns `0`. | ||
|
|
||
| ```javascript | ||
| var y = cdf( 0.0, 2.0, 1.0 ); | ||
| // returns 0.0 | ||
|
|
||
| y = cdf( -1.0, 2.0, 1.0 ); | ||
| // returns 0.0 | ||
| ``` | ||
|
|
||
| If `lambda = 0`, the function evaluates the [CDF][cdf] of a [degenerate distribution][degenerate-distribution] centered at `mu`. | ||
|
|
||
| ```javascript | ||
| var y = cdf( 2.0, 8.0, 0.0 ); | ||
| // returns 0.0 | ||
|
|
||
| y = cdf( 8.0, 8.0, 0.0 ); | ||
| // returns 1.0 | ||
|
|
||
| y = cdf( 10.0, 8.0, 0.0 ); | ||
| // returns 1.0 | ||
| ``` | ||
|
|
||
| #### cdf.factory( mu, lambda ) | ||
|
|
||
| Returns a function for evaluating the [cumulative distribution function][cdf] of a wald distribution with parameters `mu` and `lambda`. | ||
|
|
||
| ```javascript | ||
| var mycdf = cdf.factory( 1.0, 1.0 ); | ||
|
|
||
| var y = mycdf( 2.0 ); | ||
| // returns ~0.885 | ||
|
|
||
| y = mycdf( 8.0 ); | ||
| // returns ~0.999 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <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 cdf = require( '@stdlib/stats/base/dists/wald/cdf' ); | ||
|
|
||
| var opts = { | ||
| 'dtype': 'float64' | ||
| }; | ||
| var x = uniform( 10, EPS, 10.0, opts ); | ||
| var mu = uniform( 10, EPS, 10.0, opts ); | ||
| var lambda = uniform( 10, EPS, 20.0, opts ); | ||
|
|
||
| logEachMap( 'x: %0.4f, µ: %0.4f, λ: %0.4f, f(x;µ,λ): %0.4f', x, mu, lambda, cdf ); | ||
| ``` | ||
|
|
||
| </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/cdf.h" | ||
| ``` | ||
|
|
||
| #### stdlib_base_dists_wald_cdf( x, mu, lambda ) | ||
|
|
||
| Evaluates the [cumulative distribution function][cdf] (CDF) for a [Wald][wald-distribution] distribution with parameters `mu` (mean) and `lambda` (shape parameter). | ||
|
|
||
| ```c | ||
| double out = stdlib_base_dists_wald_cdf( 2.0, 1.0, 1.0 ); | ||
| // returns ~0.885 | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **x**: `[in] double` input value. | ||
| - **mu**: `[in] double` mean. | ||
| - **lambda**: `[in] double` shape parameter. | ||
|
|
||
| ```c | ||
| double stdlib_base_dists_wald_cdf( const double x, 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/cdf.h" | ||
| #include "stdlib/constants/float64/eps.h" | ||
| #include <stdlib.h> | ||
| #include <stdio.h> | ||
| #include <time.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 x; | ||
| double y; | ||
| int i; | ||
|
|
||
| for ( i = 0; i < 10; i++ ) { | ||
| x = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 ); | ||
| mu = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 ); | ||
| lambda = random_uniform( STDLIB_CONSTANT_FLOAT64_EPS, 10.0 ); | ||
| y = stdlib_base_dists_wald_cdf( x, mu, lambda ); | ||
| printf( "x: %lf, µ: %lf, λ: %lf, F(x;µ,λ): %lf\n", x, mu, lambda, y ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| </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"> | ||
|
|
||
| [cdf]: https://en.wikipedia.org/wiki/Cumulative_distribution_function | ||
|
|
||
| [wald-distribution]: https://en.wikipedia.org/wiki/Inverse_Gaussian_distribution | ||
|
|
||
| [degenerate-distribution]: https://en.wikipedia.org/wiki/Degenerate_distribution | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
84 changes: 84 additions & 0 deletions
84
lib/node_modules/@stdlib/stats/base/dists/wald/cdf/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,84 @@ | ||
| /** | ||
| * @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 format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var cdf = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var lambda; | ||
| var len; | ||
| var mu; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| len = 100; | ||
| x = uniform( len, EPS, 100.0 ); | ||
| mu = uniform( len, EPS, 50.0 ); | ||
| lambda = uniform( len, EPS, 20.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = cdf( x[ i % len ], 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(); | ||
| }); | ||
|
|
||
| bench( format( '%s:factory', pkg ), function benchmark( b ) { | ||
| var mycdf; | ||
| var x; | ||
| var y; | ||
| var i; | ||
|
|
||
| mycdf = cdf.factory( 1.0, 1.5 ); | ||
| x = uniform( 100, EPS, 10.0 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| y = mycdf( x[ i % x.length ] ); | ||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throws an error if
0.998is rounded of to1.000so used this instead!!