Skip to content

Commit fe4e561

Browse files
committed
Add discrete uniform skewness
1 parent 3d5aad2 commit fe4e561

8 files changed

Lines changed: 420 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Skewness
2+
3+
> [Discrete uniform][discrete-uniform-distribution] distribution [skewness][skewness].
4+
5+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
6+
7+
<section class="intro">
8+
9+
The [skewness][skewness] for a [discrete uniform][discrete-uniform-distribution] random variable with minimum support `a` and maximum support `b` is
10+
11+
<!-- <equation class="equation" label="eq:discrete_uniform_skewness" align="center" raw="\operatorname{skew}\left( X \right) = 0" alt="Skewness for a discrete uniform distribution."> -->
12+
13+
<!-- </equation> -->
14+
15+
</section>
16+
17+
<!-- /.intro -->
18+
19+
<!-- Package usage documentation. -->
20+
21+
<section class="usage">
22+
23+
## Usage
24+
25+
```javascript
26+
var skewness = require( '@stdlib/math/base/dists/discrete-uniform/skewness' );
27+
```
28+
29+
#### skewness( a, b )
30+
31+
Returns the [skewness][skewness] of a [discrete uniform][discrete-uniform-distribution] distribution with parameters `a` (minimum support) and `b` (maximum support).
32+
33+
```javascript
34+
var v = skewness( 0, 1 );
35+
// returns 0.0
36+
37+
v = skewness( 4, 12 );
38+
// returns 0.0
39+
40+
v = skewness( 2, 8 );
41+
// returns 0.0
42+
```
43+
44+
If `a` or `b` is not an integer value, the function returns `NaN`.
45+
46+
```javascript
47+
var v = skewness( 0.1, 2 );
48+
// returns NaN
49+
50+
v = skewness( 0, 2.2 );
51+
// returns NaN
52+
53+
v = skewness( NaN, 2 );
54+
// returns NaN
55+
56+
v = skewness( 2, NaN );
57+
// returns NaN
58+
```
59+
60+
If provided `a > b`, the function returns `NaN`.
61+
62+
```javascript
63+
var y = skewness( 3, 2 );
64+
// returns NaN
65+
66+
y = skewness( -1, -2 );
67+
// returns NaN
68+
```
69+
70+
</section>
71+
72+
<!-- /.usage -->
73+
74+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
75+
76+
<section class="notes">
77+
78+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<!-- Package usage examples. -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
```javascript
89+
var randint = require( '@stdlib/math/base/random/discrete-uniform' );
90+
var skewness = require( '@stdlib/math/base/dists/discrete-uniform/skewness' );
91+
92+
var randa = randint.factory( 0, 10 );
93+
var randb = randint.factory();
94+
var a;
95+
var b;
96+
var v;
97+
var i;
98+
99+
for ( i = 0; i < 10; i++ ) {
100+
a = randa();
101+
b = randb( a, a+randa() );
102+
v = skewness( a, b );
103+
console.log( 'a: %d, b: %d, skew(X;a,b): %d', a.toFixed( 4 ), b.toFixed( 4 ), v.toFixed( 4 ) );
104+
}
105+
```
106+
107+
</section>
108+
109+
<!-- /.examples -->
110+
111+
<!-- 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. -->
112+
113+
<section class="references">
114+
115+
</section>
116+
117+
<!-- /.references -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
[discrete-uniform-distribution]: https://en.wikipedia.org/wiki/Discrete_uniform_distribution
124+
125+
[skewness]: https://en.wikipedia.org/wiki/Skewness
126+
127+
</section>
128+
129+
<!-- /.links -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var randu = require( '@stdlib/math/base/random/randu' );
7+
var round = require( '@stdlib/math/base/special/round' );
8+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
9+
var pkg = require( './../package.json' ).name;
10+
var skewness = require( './../lib' );
11+
12+
13+
// MAIN //
14+
15+
bench( pkg, function benchmark( b ) {
16+
var min;
17+
var max;
18+
var y;
19+
var i;
20+
21+
b.tic();
22+
for ( i = 0; i < b.iterations; i++ ) {
23+
min = round( randu()*10.0 );
24+
max = round( ( randu()*10.0 ) + min );
25+
y = skewness( min, max );
26+
if ( isnan( y ) ) {
27+
b.fail( 'should not return NaN' );
28+
}
29+
}
30+
b.toc();
31+
if ( isnan( y ) ) {
32+
b.fail( 'should not return NaN' );
33+
}
34+
b.pass( 'benchmark finished' );
35+
b.end();
36+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( a, b )
3+
Returns the skewness of a discrete uniform distribution.
4+
5+
If `a > b`, the function returns `NaN`.
6+
7+
If `a` or `b` is not an integer value, the function returns `NaN`.
8+
9+
Parameters
10+
----------
11+
a: integer
12+
Minimum support.
13+
14+
b: integer
15+
Maximum support.
16+
17+
Returns
18+
-------
19+
out: number
20+
Skewness.
21+
22+
Examples
23+
--------
24+
> var v = {{alias}}( -2, 2 )
25+
0.0
26+
> v = {{alias}}( 4, 12 )
27+
0.0
28+
> v = {{alias}}( 2, 8 )
29+
0.0
30+
31+
See Also
32+
--------
33+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var randint = require( '@stdlib/math/base/random/discrete-uniform' );
4+
var skewness = require( './../lib' );
5+
6+
var randa = randint.factory( 0, 10 );
7+
var randb = randint.factory();
8+
var a;
9+
var b;
10+
var v;
11+
var i;
12+
13+
for ( i = 0; i < 10; i++ ) {
14+
a = randa();
15+
b = randb( a, a+randa() );
16+
v = skewness( a, b );
17+
console.log( 'a: %d, b: %d, skew(X;a,b): %d', a.toFixed( 4 ), b.toFixed( 4 ), v.toFixed( 4 ) );
18+
}
19+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
/**
4+
* Discrete uniform distribution skewness.
5+
*
6+
* @module @stdlib/math/base/dists/discrete-uniform/skewness
7+
*
8+
* @example
9+
* var skewness = require( '@stdlib/math/base/dists/discrete-uniform/skewness' );
10+
*
11+
* var v = skewness( 0, 1 );
12+
* // returns 0.0
13+
*
14+
* v = skewness( 2, 10 );
15+
* // returns 0.0
16+
*
17+
* v = skewness( -10, 10 );
18+
* // returns 0.0
19+
*/
20+
21+
// MODULES //
22+
23+
var skewness = require( './skewness.js' );
24+
25+
26+
// EXPORTS //
27+
28+
module.exports = skewness;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
6+
var isInteger = require( '@stdlib/math/base/assert/is-integer' );
7+
8+
9+
// MAIN //
10+
11+
/**
12+
* Returns the skewness of a discrete uniform distribution.
13+
*
14+
* @param {integer} a - minimum support
15+
* @param {integer} b - maximum support
16+
* @returns {number} skewness
17+
*
18+
* @example
19+
* var v = skewness( 0, 1 );
20+
* // returns 0.0
21+
*
22+
* @example
23+
* var v = skewness( -4, 4 );
24+
* // returns 0.0
25+
*
26+
* @example
27+
* var v = skewness( 0, 10 );
28+
* // returns 0.0
29+
*
30+
* @example
31+
* var v = skewness( 1, -0.1 );
32+
* // returns NaN
33+
*
34+
* @example
35+
* var v = skewness( -0.1, 1 );
36+
* // returns NaN
37+
*
38+
* @example
39+
* var v = skewness( 2, NaN );
40+
* // returns NaN
41+
*
42+
* @example
43+
* var v = skewness( NaN, 2 );
44+
* // returns NaN
45+
*/
46+
function skewness( a, b ) {
47+
if (
48+
isnan( a ) ||
49+
isnan( b ) ||
50+
!isInteger( a ) ||
51+
!isInteger( b ) ||
52+
a > b
53+
) {
54+
return NaN;
55+
}
56+
return 0.0;
57+
} // end FUNCTION skewness()
58+
59+
60+
// EXPORTS //
61+
62+
module.exports = skewness;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@stdlib/math/base/dists/discrete-uniform/skewness",
3+
"version": "0.0.0",
4+
"description": "Discrete uniform distribution skewness.",
5+
"author": {
6+
"name": "The Stdlib Authors",
7+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
8+
},
9+
"contributors": [
10+
{
11+
"name": "The Stdlib Authors",
12+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
13+
}
14+
],
15+
"scripts": {},
16+
"main": "./lib",
17+
"repository": {
18+
"type": "git",
19+
"url": "git://github.com/stdlib-js/stdlib.git"
20+
},
21+
"homepage": "https://github.com/stdlib-js/stdlib",
22+
"keywords": [
23+
"stdlib",
24+
"stdmath",
25+
"statistics",
26+
"stats",
27+
"distribution",
28+
"dist",
29+
"skewness",
30+
"shape",
31+
"discrete",
32+
"probability",
33+
"prob",
34+
"uniform"
35+
],
36+
"bugs": {
37+
"url": "https://github.com/stdlib-js/stdlib/issues"
38+
},
39+
"dependencies": {},
40+
"devDependencies": {},
41+
"engines": {
42+
"node": ">=0.10.0",
43+
"npm": ">2.7.0"
44+
},
45+
"license": "Apache-2.0"
46+
}

0 commit comments

Comments
 (0)