-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add stats/incr/mskewness
#8948
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
sagar7162
wants to merge
8
commits into
stdlib-js:develop
Choose a base branch
from
sagar7162:movingskew
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.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3a035a5
feat: add `stats/incr/mskewness`
sagar7162 74f9187
fix: fixed tests
sagar7162 7cdbd75
fix: fixed tests
sagar7162 1c74629
fix: add suggested changes
sagar7162 f1ba8f3
Merge remote-tracking branch 'upstream/develop' into movingskew
stdlib-bot ef2ec99
refactor: update copyright years
sagar7162 c58c7a0
refactor: update copyright years in main_js
sagar7162 22a53d4
fix: fixed readme related sction
sagar7162 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
190 changes: 190 additions & 0 deletions
190
lib/node_modules/@stdlib/stats/incr/mskewness/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,190 @@ | ||
| <!-- | ||
|
|
||
| @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. | ||
|
|
||
| --> | ||
|
|
||
| # incrmskewness | ||
|
|
||
| > Compute moving [corrected sample skewness][sample-skewness] incrementally. | ||
|
|
||
| <section class="intro"> | ||
|
|
||
| The [skewness][sample-skewness] for a random variable `X` is defined as | ||
|
|
||
| <!-- <equation class="equation" label="eq:skewness" align="center" raw="\operatorname{Skewness}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr]" alt="Equation for skewness."> --> | ||
|
|
||
| ```math | ||
| \mathop{\mathrm{Skewness}}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr] | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="\operatorname{Skewness}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr]" data-equation="eq:skewness"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_skewness.svg" alt="Equation for skewness."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| For a sample of `n` values, the [sample skewness][sample-skewness] is | ||
|
|
||
| <!-- <equation class="equation" label="eq:sample_skewness" align="center" raw="b_1 = \frac{m_3}{s^3} = \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}}" alt="Equation for the sample skewness."> --> | ||
|
|
||
| ```math | ||
| b_1 = \frac{m_3}{s^3} = \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}} | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="b_1 = \frac{m_3}{s^3} = \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}}" data-equation="eq:sample_skewness"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_sample_skewness.svg" alt="Equation for the sample skewness."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| where `m_3` is the sample third central moment and `s` is the sample standard deviation. | ||
|
|
||
| An alternative definition for the [sample skewness][sample-skewness] which includes an adjustment factor (and is the implemented definition) is | ||
|
|
||
| <!-- <equation class="equation" label="eq:adjusted_sample_skewness" align="center" raw="G_1 = \frac{n^2}{(n-1)(n-2)} \frac{m_3}{s^3} = \frac{\sqrt{n(n-1)}}{n-2} \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}}" alt="Equation for the adjusted sample skewness."> --> | ||
|
|
||
| ```math | ||
| G_1 = \frac{n^2}{(n-1)(n-2)} \frac{m_3}{s^3} = \frac{\sqrt{n(n-1)}}{n-2} \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}} | ||
| ``` | ||
|
|
||
| <!-- <div class="equation" align="center" data-raw-text="G_1 = \frac{n^2}{(n-1)(n-2)} \frac{m_3}{s^3} = \frac{\sqrt{n(n-1)}}{n-2} \frac{\frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^3}{\biggl( \frac{1}{n} \sum_{i=0}^{n-1} (x_i - \bar{x})^2 \biggr)^{3/2}}" data-equation="eq:adjusted_sample_skewness"> | ||
| <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/mskewness/docs/img/equation_adjusted_sample_skewness.svg" alt="Equation for the adjusted sample skewness."> | ||
| <br> | ||
| </div> --> | ||
|
|
||
| <!-- </equation> --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.intro --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var incrmskewness = require( '@stdlib/stats/incr/mskewness' ); | ||
| ``` | ||
|
|
||
| #### incrmskewness( W ) | ||
|
|
||
| Returns an accumulator `function` which incrementally computes a moving [corrected sample skewness][sample-skewness] over a sliding window of size `W`. | ||
|
|
||
| ```javascript | ||
| var accumulator = incrmskewness( 5 ); | ||
| ``` | ||
|
|
||
| #### accumulator( \[x] ) | ||
|
|
||
| If provided an input value `x`, the accumulator function returns an updated moving [corrected sample skewness][sample-skewness]. If not provided an input value `x`, the accumulator function returns the current moving [corrected sample skewness][sample-skewness]. | ||
|
|
||
| ```javascript | ||
| var accumulator = incrmskewness( 5 ); | ||
|
|
||
| var skewness = accumulator(); | ||
| // returns null | ||
|
|
||
| skewness = accumulator( 2.0 ); | ||
| // returns null | ||
|
|
||
| skewness = accumulator( -5.0 ); | ||
| // returns null | ||
|
|
||
| skewness = accumulator( -10.0 ); | ||
| // returns ~0.492 | ||
|
|
||
| skewness = accumulator(); | ||
| // returns ~0.492 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - Input values are **not** type checked. If provided `NaN` or a value which, when used in computations, results in `NaN`, the accumulated value is `NaN` for **at least** `W-1` future invocations. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var randu = require( '@stdlib/random/base/randu' ); | ||
| var incrmskewness = require( '@stdlib/stats/incr/mskewness' ); | ||
|
|
||
| var accumulator; | ||
| var v; | ||
| var i; | ||
|
|
||
| // Initialize an accumulator: | ||
| accumulator = incrmskewness( 3 ); | ||
|
|
||
| // For each simulated datum, update the corrected sample skewness... | ||
| for ( i = 0; i < 100; i++ ) { | ||
| v = randu() * 100.0; | ||
| accumulator( v ); | ||
| } | ||
| console.log( accumulator() ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="references"> | ||
|
|
||
| ## References | ||
|
|
||
| - Joanes, D. N., and C. A. Gill. 1998. "Comparing measures of sample skewness and kurtosis." _Journal of the Royal Statistical Society: Series D (The Statistician)_ 47 (1). Blackwell Publishers Ltd: 183–89. doi:[10.1111/1467-9884.00122][@joanes:1998]. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.references --> | ||
|
|
||
| <!-- 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"> | ||
|
|
||
| [sample-skewness]: https://en.wikipedia.org/wiki/Skewness | ||
|
|
||
| [@joanes:1998]: http://onlinelibrary.wiley.com/doi/10.1111/1467-9884.00122/ | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
79 changes: 79 additions & 0 deletions
79
lib/node_modules/@stdlib/stats/incr/mskewness/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,79 @@ | ||
| /** | ||
| * @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 Float64Array = require( '@stdlib/array/float64' ); | ||
| var uniform = require( '@stdlib/random/base/uniform' ); | ||
| var format = require( '@stdlib/string/format' ); | ||
| var pkg = require( './../package.json' ).name; | ||
| var incrmskewness = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var f; | ||
| var i; | ||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| f = incrmskewness( ( i % 5 ) + 1 ); | ||
| if ( typeof f !== 'function' ) { | ||
| b.fail( 'should return a function' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( typeof f !== 'function' ) { | ||
| b.fail( 'should return a function' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); | ||
|
|
||
| bench( format( '%s::accumulator', pkg ), function benchmark( b ) { | ||
| var acc; | ||
| var len; | ||
| var v; | ||
| var i; | ||
| var x; | ||
|
|
||
| len = 100; | ||
| x = new Float64Array( len ); | ||
| for ( i = 0; i < len; i++ ) { | ||
| x[ i ] = uniform( 0.0, 1.0 ); | ||
| } | ||
|
|
||
| acc = incrmskewness( 5 ); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = acc( x[ i % len ] ); | ||
| if ( v !== v ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( v !== v ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
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.
Missing the standard note about initial window behavior. Add: "As
Wvalues are needed to fill the window buffer, the firstW-1returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all provided values." SeeincrmvarianceREADME.