Skip to content

Commit 736cc37

Browse files
committed
feat: added package @stdlib/stats/incr/nanskewness
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent befe02c commit 736cc37

File tree

13 files changed

+1208
-0
lines changed

13 files changed

+1208
-0
lines changed
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# incrnanskewness
22+
23+
> Compute a [corrected sample skewness][sample-skewness] incrementally.
24+
25+
<section class="intro">
26+
27+
The [skewness][sample-skewness] for a random variable `X` is defined as
28+
29+
<!-- <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."> -->
30+
31+
```math
32+
\mathop{\mathrm{Skewness}}[X] = \mathrm{E}\biggl[ \biggl( \frac{X - \mu}{\sigma} \biggr)^3 \biggr]
33+
```
34+
35+
<!-- <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">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/skewness/docs/img/equation_skewness.svg" alt="Equation for skewness.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
For a sample of `n` values, the [sample skewness][sample-skewness] is
43+
44+
<!-- <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."> -->
45+
46+
```math
47+
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}}
48+
```
49+
50+
<!-- <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">
51+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/skewness/docs/img/equation_sample_skewness.svg" alt="Equation for the sample skewness.">
52+
<br>
53+
</div> -->
54+
55+
<!-- </equation> -->
56+
57+
where `m_3` is the sample third central moment and `s` is the sample standard deviation.
58+
59+
An alternative definition for the [sample skewness][sample-skewness] which includes an adjustment factor (and is the implemented definition) is
60+
61+
<!-- <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."> -->
62+
63+
```math
64+
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}}
65+
```
66+
67+
<!-- <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">
68+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@49d8cabda84033d55d7b8069f19ee3dd8b8d1496/lib/node_modules/@stdlib/stats/incr/skewness/docs/img/equation_adjusted_sample_skewness.svg" alt="Equation for the adjusted sample skewness.">
69+
<br>
70+
</div> -->
71+
72+
<!-- </equation> -->
73+
74+
</section>
75+
76+
<!-- /.intro -->
77+
78+
<section class="usage">
79+
80+
## Usage
81+
82+
```javascript
83+
var incrnanskewness = require( '@stdlib/stats/incr/skewness' );
84+
```
85+
86+
#### incrnanskewness()
87+
88+
Returns an accumulator `function` which incrementally computes a [corrected sample skewness][sample-skewness].
89+
90+
```javascript
91+
var accumulator = incrnanskewness();
92+
```
93+
94+
#### accumulator( \[x] )
95+
96+
If provided an input value `x`, the accumulator function returns an updated [corrected sample skewness][sample-skewness]. If not provided an input value `x`, the accumulator function returns the current [corrected sample skewness][sample-skewness].
97+
98+
<!-- eslint-disable stdlib/doctest -->
99+
100+
```javascript
101+
var accumulator = incrnanskewness();
102+
103+
var skewness = accumulator();
104+
// returns null
105+
106+
skewness = accumulator(2.0);
107+
// returns null
108+
109+
skewness = accumulator(-5.0);
110+
// returns null
111+
112+
skewness = accumulator(-10.0);
113+
// returns ~0.492
114+
115+
skewness = accumulator( NaN );
116+
// returns ~0.492
117+
118+
skewness = accumulator();
119+
// returns ~0.492
120+
```
121+
122+
<!-- run-disable -->
123+
124+
</section>
125+
126+
<!-- /.usage -->
127+
128+
<section class="notes">
129+
130+
## Notes
131+
132+
- Input values are **not** type checked. If non-numeric inputs are possible, you are advised to type check and handle accordingly **before** passing the value to the accumulator function.
133+
134+
- For long running accumulations or accumulations of large numbers, care should be taken to prevent overflow.
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<section class="examples">
141+
142+
## Examples
143+
144+
<!-- eslint no-undef: "error" -->
145+
146+
<!-- eslint-disable stdlib/no-redeclare -->
147+
148+
```javascript
149+
var randu = require( '@stdlib/random/base/randu' );
150+
var incrnanskewness = require( '@stdlib/stats/incr/lib' );
151+
152+
var accumulator;
153+
var skewness;
154+
var v;
155+
var i;
156+
157+
// Initialize an accumulator:
158+
accumulator = incrnanskewness();
159+
160+
// For each simulated datum, update the skewness...
161+
console.log('\nValue\tSkewness\n');
162+
for (i = 0; i < 100; i++) {
163+
if (randu() < 0.2) {
164+
v = NaN;
165+
} else {
166+
v = randu() * 100.0;
167+
}
168+
skewness = accumulator(v);
169+
console.log( '%d\t%s', v.toFixed(4), (skewness === null) ? 'NaN' : skewness.toFixed(4));
170+
}
171+
console.log('\nFinal skewness: %s\n', accumulator());
172+
```
173+
174+
<!-- run-disable -->
175+
176+
</section>
177+
178+
<!-- /.examples -->
179+
180+
* * *
181+
182+
<section class="references">
183+
184+
## References
185+
186+
- Joanes, D. N., and C. A. Gill. 1998. "Comparing measures of sample skewness and kurtosis."
187+
- 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].
188+
189+
</section>
190+
191+
<!-- /.references -->
192+
193+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
194+
195+
<section class="related">
196+
197+
* * *
198+
199+
## See Also
200+
201+
- <span class="package-name">[`@stdlib/stats/incr/skewness`][@stdlib/stats/incr/skewness]</span><span class="delimiter">: </span><span class="description">compute a corrected sample skewness incrementally.</span>
202+
- <span class="package-name">[`@stdlib/stats/incr/kurtosis`][@stdlib/stats/incr/kurtosis]</span><span class="delimiter">: </span><span class="description">compute a corrected sample excess kurtosis incrementally.</span>
203+
- <span class="package-name">[`@stdlib/stats/incr/mean`][@stdlib/stats/incr/mean]</span><span class="delimiter">: </span><span class="description">compute an arithmetic mean incrementally.</span>
204+
- <span class="package-name">[`@stdlib/stats/incr/stdev`][@stdlib/stats/incr/stdev]</span><span class="delimiter">: </span><span class="description">compute a corrected sample standard deviation incrementally.</span>
205+
- <span class="package-name">[`@stdlib/stats/incr/summary`][@stdlib/stats/incr/summary]</span><span class="delimiter">: </span><span class="description">compute a statistical summary incrementally.</span>
206+
- <span class="package-name">[`@stdlib/stats/incr/variance`][@stdlib/stats/incr/variance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample variance incrementally.</span>
207+
208+
</section>
209+
210+
<!-- /.related -->
211+
212+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
213+
214+
<section class="links">
215+
216+
[sample-skewness]: https://en.wikipedia.org/wiki/Skewness
217+
[@joanes:1998]: http://onlinelibrary.wiley.com/doi/10.1111/1467-9884.00122/
218+
219+
<!-- <related-links> -->
220+
221+
[@stdlib/stats/incr/skewness]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/skewness
222+
[@stdlib/stats/incr/kurtosis]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/kurtosis
223+
[@stdlib/stats/incr/mean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mean
224+
[@stdlib/stats/incr/stdev]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/stdev
225+
[@stdlib/stats/incr/summary]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/summary
226+
[@stdlib/stats/incr/variance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/variance
227+
228+
<!-- </related-links> -->
229+
230+
</section>
231+
232+
<!-- /.links -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanskewness = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var f;
33+
var i;
34+
b.tic();
35+
for ( i = 0; i < b.iterations; i++ ) {
36+
f = incrnanskewness();
37+
if ( typeof f !== 'function' ) {
38+
b.fail( 'should return a function' );
39+
}
40+
}
41+
b.toc();
42+
if ( typeof f !== 'function' ) {
43+
b.fail( 'should return a function' );
44+
}
45+
b.pass( 'benchmark finished' );
46+
b.end();
47+
});
48+
49+
bench( pkg+'::accumulator', function benchmark( b ) {
50+
var acc;
51+
var v;
52+
var i;
53+
54+
acc = incrnanskewness();
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = acc( randu() );
59+
if ( v !== v ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( v !== v ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});

0 commit comments

Comments
 (0)