Skip to content
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

Statistic Module added #243

Closed
wants to merge 15 commits into from
151 changes: 151 additions & 0 deletions docs/underscore.collections.statistics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
### statistics

> Statisctics Functions. <a href="docs/underscore.collections.statistics.js.html" class="btn btn-primary btn-xs">View Annotated Source</a>

#### mean

Signature: `_.mean(... arrays:Array ...)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really arrays, plural?


The `_.mean` function finds out the average value from the array of numbers.

```javascript

_.mean([]);
//=> 0

_.mean([0, 1, 2, 3, 4]);
//=> 2

_.mean(null)
//=> 0

```

#### median

Signature: `_.median(... arrays:Array ...)`

The `_.median` function finds out the middle value from the array of numbers.

Calulation of median is done using the following method.

If the array has odd numbers then median is the middle element.

If the array has even numbers then average of middle two numbers is the median value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd/even number of numbers. For brevity and less confusion, though, I suggest "length".

Suggested change
If the array has odd numbers then median is the middle element.
If the array has even numbers then average of middle two numbers is the median value.
If the array has odd length then median is the middle element.
If the array has even length then average of middle two numbers is the median value.

```javascript

_.median([]);
//=> undefined

_.median([1, 2, 3]);
//=> 2

_.median([1, 2, 3, 4])
// => 2.5

```

#### sum

Signature: `_.sum(... arrays:Array ...)`

The `_.sum` function calculates the sum of the given arrays.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume it sums a single array.

Suggested change
The `_.sum` function calculates the sum of the given arrays.
The `_.sum` function calculates the sum of the given array.


```javascript

_.sum([]);
//=> 0

_.sum([0, 1, 2, 3, 4]);
//=> 10
```

#### variance

Signature: `_.variance(... arrays:Array ...)`

The `_.variance` function return the variance of the numeric elements of the collection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps add a link here to the Wikipedia entry that explains the variance. Likewise for the other statistical concepts that follow.


```javascript

_.variance([]);
//=> 0

_.variance([0, 1, 2, 3, 4]);
//=> 1.25
```

#### standardDeviation

Signature: `_.standardDeviation(... arrays:Array ...)`

The `_.standardDeviation` function return the standard deviation of the numeric elements of the collection.

```javascript

_.standardDeviation([]);
//=> 0

_.standardDeviation([1, 2, 3, 4]);
//=> 1.118
```

#### standardError

Signature: `_.standardError(... arrays:Array ...)`

The `_.standardError` function return the standard error of the numeric elements of the collection.

```javascript

_.standardError([]);
//=> 0

_.standardError([1, 2, 3, 4]);
//=> 0.64
```

#### mode

Signature: `_.mode(... arrays:Array ...)`

The `_.mode` function return the element (or element-based computation) that appears most frequently in the collection.

```javascript

_.mode([]);
//=> undefined

_.mode([1, 1, 3, 4]);
//=> 1
```

#### statRange

Signature: `_.statRange(... arrays:Array ...)`

The `_.statRange` function return the difference of the max and min value of the elements in the array.

```javascript

_.statRange([]);
//=> -Infinity

_.statRange([1, 1, 3, 4]);
//=> 3
```

#### percentile

Signature: `_.percentile(... arrays:Array ...,percentileval:number)`

The `_.percentile` function return the percentile value of the numeric elements from the collection like 50th,75th,99th etc.

```javascript

_.percentile([], 10);
//=> 0

_.percentile([1, 1, 3, 4], 50);
//=> 2
```
2 changes: 1 addition & 1 deletion modules/variance.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function variance(collection, iteratee, context) {
if (typeof iteratee == 'number' && collection != null && typeof collection[0] != 'object') {
iteratee = null;
}
iteratee = cb(iteratee, context);
iteratee = iteratee(iteratee, context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be _.iteratee in order to work.

Suggested change
iteratee = iteratee(iteratee, context);
iteratee = _.iteratee(iteratee, context);


var computed = [];
var avg = mean(collection, function(value, key, collection) {
Expand Down