Skip to content

Commit

Permalink
added array flatten to mean, median and mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 15, 2021
1 parent e665267 commit 8d9b8f5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
37 changes: 28 additions & 9 deletions docs/statistic-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const Stat = require('total-serialism').Statistic;

# Methods

- sort
- avarage
- center
- common
- maximum
- minimum
- change
- [sort](#sort)
- [average](#average) (mean)
- [center](#center) (median)
- [common](#common) (mode)
- [maximum](#maximum)
- [minimum](#minimum)
- [change](#change)

## sort

Expand All @@ -41,41 +41,60 @@ Measures of central tendencies (Mean, Median, Mode)

Get the average (the artihmetic mean) value from an array

**arguments**
- {Array} -> the array to take the average of
- {Bool} -> enable/disable the deep flag for n-dim arrays (default=true)

```js
Stat.average([1, 2, 3, 4, 5, 6, 7, 8, 9]);
//=> 5
// Alternative: Stat.mean()

Stat.average([2, -6, 2, 0, 10, 9, -2, 5, -8, -11, 1, -3]);
//=> -0.0833

// Alternative: Stat.mean()
```

## center

Return the center value (the median) from an array

**arguments**
- {Array} -> the array to get the median from
- {Bool} -> enable/disable the deep flag for n-dim arrays (default=true)

```js
Stat.center([1, 5, 6, 9, 13]);
//=> 6
// Alternative: Stat.median()

// Returns average of 2 middle values for even listlengths
// works with "official" statistics terminology
Stat.center([1, 7, 4, 2, 9, 5]);
//=> 4.5

// Alternative: Stat.median()
```

## common

Returns the most common value (the mode) from an array as an array

**arguments**
- {Array} -> the array to get the mode from
- {Bool} -> enable/disable the deep flag for n-dim arrays (default=true)

```js
Stat.common([8, 4, 3, 11, 9, 0, 11, 2, 10, 5, 11, 0]);
//=> [ 11 ]

Stat.common([8, [4, 3], 9, [9, 0, [2, 10], 5], 11, 0, 11]);
//=> [ 11 ]

// In the case of a multi-modal system the array contains all common values
Stat.common([8, 4, 3, 9, 9, 0, 2, 10, 5, 11, 0, 11]);
//=> [ 0, 9, 11 ]

// Alternative: Stat.mode()
```

## maximum
Expand Down
17 changes: 13 additions & 4 deletions src/statistic.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ exports.min = Util.minimum;
// The mean is a measure of central tendency
//
// @param {NumberArray} -> input array of n-numbers
// @param {Bool} -> enable/disable the deep flag for n-dim arrays (default=true)
// @return {Number} -> mean
//
function mean(a=[0]){
function mean(a=[0], d=true){
if (!Array.isArray(a)) { return a; }
if (d) { a = Util.flatten(a); }

let s = 0;
for (let i in a){
s += a[i];
s += isNaN(a[i])? 0 : a[i];
}
return s / a.length;
}
Expand All @@ -73,10 +76,13 @@ exports.average = mean;
// Ignores other datatypes then Number and Boolean
//
// @param {NumberArray} -> input array of n-numbers
// @param {Bool} -> enable/disable the deep flag for n-dim arrays (default=true)
// @return {Number} -> median
//
function median(a=[0]){
function median(a=[0], d=true){
if (!Array.isArray(a)) { return a; }
if (d) { a = Util.flatten(a); }

let arr = a.slice();
if (arr.map(x => typeof x).includes('string')) {
arr = Mod.filterType(arr, ['number', 'boolean']);
Expand All @@ -97,10 +103,13 @@ exports.center = median;
// Returns an array when multi-modal system
//
// @param {NumberArray} -> input array of n-numbers
// @param {Bool} -> enable/disable the deep flag for n-dim arrays (default=true)
// @return {Number/Array} -> the mode or modes
//
function mode(a=[0]){
function mode(a=[0], d=true){
if (!Array.isArray(a)) { return a; }
if (d) { a = Util.flatten(a); }

let arr = a.slice().sort((a,b) => { return a-b; });

let amount = 1;
Expand Down
38 changes: 29 additions & 9 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ function fullTest(){
// testGen();
// testAlgo();
// testRand();
testMod();
// testStat();
// testMod();
testStat();
// testTranslate();
// testUtil();

Expand Down Expand Up @@ -452,31 +452,51 @@ function testMod(){

function testStat(){
pagebreak("Statistic");


test("Stat.sort()");
test("Stat.sort(3.14)");
test("Stat.sort('foo')");
test("Stat.sort([-10, 8, 6, -12, -6, -7, 2, 4, 3, 11], -1)");
test("Stat.sort([10, 3.14, 'foo', 'bar', 5, -6, 'foobar'])");
test("Stat.sort([-10, 8, 6, -12, -6, -7, 2, 4, 3, 11])");


test("Stat.mean()");
test("Stat.mean(3.14)");
test("Stat.mean('foo')");
test("Stat.mean([2, -6, 2, 0, 10, 9, -2, 5, -8, -11, 1, -3])");
test("Stat.average([1, 2, 3, 4, 5, 6, 7, 8, 9])");
test("Stat.average([2, -6, 2, 0, 10, 9, -2, 5, -8, -11, 1, -3])");

test("Stat.mean([1, 2, 3, 4, 5, 6, 7, 8, 9])");
test("Stat.mean([2, -6, 2, 0, 10, 9, -2, 5, -8, -11, 1, -3])");
test("Stat.mean([2, [-6, 2, 0], [10, [9, -2], 5], -8, [-11, 1], -3])");

test("Stat.median()");
test("Stat.median(3.14)");
test("Stat.median([1, 5, 6, 9, 13])");
test("Stat.median([2, -6, 2, 0, 10, 9, -2, 5, -8, -11, -3])");
test("Stat.median([2, [-6, 2, 0], [10, [9, -2], 5], -8, [-11], -3])");
test("Stat.median([1, 5, 6, 'foo', 'bar', 13,])");

test("Stat.center([1, 5, 11])");
test("Stat.center([1, 7, 4, 2, 9, 5])");
test("Stat.center([1, 7, 4, 2, 9, 5, 'foo', 'bar', true, true])");

test("Stat.mode()");
test("Stat.common([8, 4, 3, 11, 9, 0, 11, 2, 10, 5, 11, 0])");
test("Stat.common([8, 4, 3, 9, 9, 0, 2, 10, 5, 11, 0, 11])");
test("Stat.common([8, [4, 3], 9, [9, 0, [2, 10], 5], 11, 0, 11])");
test("Stat.common([8, 4, 3, 'foo', 'foo', 0, 2, 10, 5, 11, 0, 11])");


test("Stat.maximum()");
test("Stat.maximum(10)");
test("Stat.maximum([-38, -53, -6, 33, 88, 32, -8, 73])");

test("Stat.minimum()");
test("Stat.minimum(10)");
test("Stat.minimum([-38, -53, -6, 33, 88, 32, -8, 73])");
test("Stat.minimum([-38, -53, -6, 'foo', 33, 'bar'])");


test("Stat.change()");
test("Stat.change(4)");
test("Stat.change(['foo', 'bar'])");
test("Stat.change([0, 3, 7, 0, 12, 9, 5, 7])");
}

Expand Down

0 comments on commit 8d9b8f5

Please sign in to comment.