Skip to content

Commit

Permalink
Gauss 0.2.8
Browse files Browse the repository at this point in the history
- Collection.unique, return a set of unique values
97b8db8

- Collection.split, binomial cluster function
242384e

- Fix object extension and refactoring
fredrick#14

- Power/general mean
8628696

- Sample statistics and increased test coverage
  • Loading branch information
Fredrick Galoso committed Apr 22, 2013
1 parent 8628696 commit 81fec67
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 49 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
The awesome people that have contributed to Gauss.

#Author
Fredrick Galoso - Stackd
Fredrick Galoso

#Contributors
Philip Tellis - Yahoo!
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Gauss: JavaScript statistics, analytics, and set library - Node.js and web browser ready
Copyright (c) 2012 Fredrick Galoso https://stackd.com/
Copyright (c) 2013 Fredrick Galoso http://fredrickgaloso.me/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
121 changes: 81 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ both on Node.js and within the web browser.
## License
MIT/X11 - See [LICENSE][2]

[2]: http://github.com/stackd/gauss/blob/master/LICENSE
[2]: http://github.com/wayoutmind/gauss/blob/master/LICENSE

## Getting started

Expand All @@ -39,7 +39,7 @@ To invoke the tests:

### Using Gauss within a web browser

Gauss requires support for ECMAScript 5 `Object.defineProperties`. Compatibility is listed [here](http://kangax.github.com/es5-compat-table/). Download and include [gauss.min.js](https://raw.github.com/stackd/gauss/master/gauss.min.js):
Gauss requires support for ECMAScript 5 `Object.defineProperties`. Compatibility is listed [here](http://kangax.github.com/es5-compat-table/). Download and include [gauss.min.js](https://raw.github.com/wayoutmind/gauss/master/gauss.min.js):

``` html
<script src="gauss.min.js" type="text/javascript"></script>
Expand All @@ -52,44 +52,9 @@ Gauss requires support for ECMAScript 5 `Object.defineProperties`. Compatibility
</script>
```

### Using the REPL console

To experiment with Gauss or to quickly start a Node.js command-line environment for number crunching, Gauss ships with a lightweight REPL (Read–eval–print loop). Start the REPL with `npm start` within the source directory, or `gauss` if installed globally (via `npm install -g gauss`).

For example, using the `help()` function and analyzing a data file from the Gauss REPL:

``` javascript
$ gauss
gauss> help()
Gauss 0.2.7
/* https://github.com/stackd/gauss#api */
Functions: print, inspect, cwd, clear, install, uninstall, help
Usage:
var set = new Vector(1, 2, 3);
var times = new gauss.TimeSeries();
{ version: '0.2.7',
Collection: [Function],
Vector: [Function],
TimeSeries: [Function] }
gauss> var fs = require('fs');
gauss> var data = fs.readFileSync('data.txt').toString();
gauss> data = data.split('\n');
[ '8',
'6',
'7',
'5',
'3',
'0',
'9' ]
gauss> data = data.map(function(line) { return parseInt(line) });
gauss> var set = new Vector(data);
gauss> set.mean()
5.428571428571429
```

## API

Gauss has methods for univariate (Vector) and time series (TimeSeries) analysis. We're constantly working on adding more functions, adding multivariate statistics, and we encourage additions to the library. Accuracy is a primary concern. If Gauss is returning incorrect results, [please submit an issue](https://github.com/stackd/gauss/issues) and/or [submit a patch](https://github.com/stackd/gauss#fork_box)!
Gauss has methods for univariate (Vector) and time series (TimeSeries) analysis. We're constantly working on adding more functions, adding multivariate statistics, and we encourage additions to the library. Accuracy is a primary concern. If Gauss is returning incorrect results, [please submit an issue](https://github.com/wayoutmind/gauss/issues) and/or [submit a patch](https://github.com/wayoutmind/gauss#fork_box)!

### Instantiation

Expand All @@ -106,7 +71,7 @@ numbers[0] = 2;
set[1] = 7;
```

*Note: To prevent unintended scope/prototype pollution, Gauss versions after 0.2.3 have [removed support for monkey patching](https://github.com/stackd/gauss/issues/6) the native Array data type.
*Note: To prevent unintended scope/prototype pollution, Gauss versions after 0.2.3 have [removed support for monkey patching](https://github.com/wayoutmind/gauss/issues/6) the native Array data type.
Use the .toArray() method of any Gauss object to a convert to a vanilla Array. Gauss adds a toVector() convenience method to the Array prototype to facilitate converting to Vectors.*

### Callbacks and method chaining
Expand Down Expand Up @@ -206,7 +171,7 @@ people.findOne({ lastname: 'Smith' });

.split(predicate[, callback])

Returns a Collection split by a condition.
Returns a Collection split by a condition (binomial cluster).

``` javascript
Collection(1, 2, 3, 4).split(function(e) { return e % 2 === 0 });
Expand Down Expand Up @@ -261,6 +226,17 @@ var numbers = new Collection(1, 2, 3).append([1, 2, 3]);
> [1, 2, 3, 1, 2, 3]
```

#### Collection.unique

.unique(callback)

Return a Collection with unique values.

``` javascript
var numbers = new Collection(1, 2, 3, 3, 4, 4).unique();
> [1, 2, 3, 4]
```

### Vector

#### Vector.min
Expand Down Expand Up @@ -343,6 +319,21 @@ Returns the harmonic mean.

Returns the quadratic mean (RMS, root mean square).

#### Vector.pmean

.pmean(p, callback)

Returns the power/generalized mean given an order or power *p*.

```javascript
// p = -1, harmonic mean
set.pmean(-1);
// p = 1, arithmetic mean
set.pmean(1);
// p = 2, quadratic mean
set.pmean(2);
```

#### Vector.median

.median(callback)
Expand Down Expand Up @@ -559,6 +550,21 @@ Returns a copy of the data set.

Returns another instance of the Vector object and data.

### Sample

By default, `Vector` calculates values against the population `n`. However, sample statistics functions on `n - 1` are available by using the `sample` modifier for the following functions:

``` javascript
Vector().sample
{ mean: [Function],
gmean: [Function],
hmean: [Function],
qmean: [Function],
pmean: [Function],
variance: [Function],
stdev: [Function] }
```

### Math

`Vector` supports applying all the [Math](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math#Methods) object methods to an entire Vector set of numbers.
Expand Down Expand Up @@ -592,3 +598,38 @@ Returns a Vector of the times.
.values(callback)

Returns a Vector of the time series values.

### Using the REPL console

To experiment with Gauss or to quickly start a Node.js command-line environment for number crunching, Gauss ships with a lightweight REPL (Read–eval–print loop). Start the REPL with `npm start` within the source directory, or `gauss` if installed globally (via `npm install -g gauss`).

For example, using the `help()` function and analyzing a data file from the Gauss REPL:

``` javascript
$ gauss
gauss> help()
Gauss 0.2.8
/* https://github.com/wayoutmind/gauss#api */
Functions: print, inspect, cwd, clear, install, uninstall, help
Usage:
var set = new Vector(1, 2, 3);
var times = new gauss.TimeSeries();
{ version: '0.2.8',
Collection: [Function],
Vector: [Function],
TimeSeries: [Function] }
gauss> var fs = require('fs');
gauss> var data = fs.readFileSync('data.txt').toString();
gauss> data = data.split('\n');
[ '8',
'6',
'7',
'5',
'3',
'0',
'9' ]
gauss> data = data.map(function(line) { return parseInt(line) });
gauss> var set = new Vector(data);
gauss> set.mean()
5.428571428571429
```
4 changes: 2 additions & 2 deletions bin/gauss
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
_/_/
Gauss: JavaScript statistics, analytics, and set library - Node.js and web browser ready
Copyright (c) 2012 Fredrick Galoso https://stackd.com/
Copyright (c) 2013 Fredrick Galoso http://fredrickgaloso.me/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -64,7 +64,7 @@ session.context.uninstall = function(name) {

session.context.help = function(command) {
var banner = 'Gauss '+ gauss.version +'\n \
/* https://github.com/stackd/gauss#api */ \n \
/* https://github.com/wayoutmind/gauss#api */ \n \
Functions: print, inspect, cwd, clear, install, uninstall, help\n \
Usage:\n \
var set = new Vector(1, 2, 3);\n \
Expand Down
2 changes: 1 addition & 1 deletion gauss.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/gauss.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Gauss
* https://github.com/stackd/gauss
* Copyright(c) 2012 Fredrick Galoso
* https://github.com/wayoutmind/gauss
* Copyright(c) 2013 Fredrick Galoso
* LICENSE: MIT/X11
*/

Expand Down
18 changes: 18 additions & 0 deletions lib/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,17 @@
else
return qmean;
},
pmean: function(p, callback) {
var pmean = 0.0;
for (var i = 0; i < vector.length;) {
pmean += Math.pow(vector[i++], p);
}
pmean = Math.pow(pmean / (vector.length - 1), 1/p);
if (callback)
return callback(pmean);
else
return pmean;
},
variance: function(callback) {
var mean = 0.0,
variance = 0.0;
Expand All @@ -425,6 +436,13 @@
return callback(variance);
else
return variance;
},
stdev: function(callback) {
var stdev = Math.sqrt(vector.sample.variance());
if (callback)
return callback(stdev);
else
return stdev;
}
},
writable: true,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"author": "Fredrick Galoso <fgaloso@stackd.com>",
"author": "Fredrick Galoso <fredrickgaloso@gmail.com>",
"name": "gauss",
"description": "JavaScript statistics, analytics, and set library",
"version": "0.2.8",
"keywords": [ "statistics", "analytics", "timeseries", "math", "gauss", "set" ],
"repository": {
"type": "git",
"url": "git://github.com/stackd/gauss.git"
"url": "git://github.com/wayoutmind/gauss.git"
},
"main": "index",
"engines": {
Expand Down
12 changes: 12 additions & 0 deletions test/vector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,23 @@ vows.describe('Vector').addBatch({
assert.equal(topic, 61.88748843255635);
}
},
'(Power Mean)': {
topic: set.sample.pmean(1),
'55.04081632653061': function(topic) {
assert.equal(topic, set.sample.mean());
}
},
'(Variance)': {
topic: set.sample.variance(),
'861.1595918367346': function(topic) {
assert.equal(topic, 861.1595918367346);
}
},
'(Standard Deviation)': {
topic: set.sample.stdev(),
'29.34552081386075': function(topic) {
assert.equal(topic, 29.34552081386075);
}
}
},
// Math methods
Expand Down

0 comments on commit 81fec67

Please sign in to comment.