Permalink
Browse files

Adding mean and variance; Remove stride and n parameters.

  • Loading branch information...
1 parent 32c71a1 commit 155833afcfc4b37703b9e2f0c4a975f9a0dabb47 @wdavidw committed Apr 25, 2011
View
@@ -14,6 +14,8 @@ module.exports = {
poisson: gsl.ranPoisson
},
statistics: {
+ mean: gsl.statisticsMean,
+ variance: gsl.statisticsVariance,
sd: gsl.statisticsSd
}
};
View
@@ -35,48 +35,42 @@ Using an iterator objects make sense when using seeds and when performance is a
Seeds are always optional and must be provided as unsigned integers. Deviations, used by gaussian functions, are float.
-- *Random([seed])*
+- *gsl.Random([seed])*
Construct a new iterator, seel below for available random methods.
-
-### [Sampling](http://www.gnu.org/software/gsl/manual/html_node/Sampling-from-a-random-number-generator.html)
-
-- *random.get([seed])*
- *Random.get()*
+
+- *gsl.random.get([seed])*
+ *gsl.Random.get()*
Returns a random integer. The minimum and maximum values depend on the algorithm used, but all integers in the range [min,max] are equally likely. The values of min and max can determined using the auxiliary functions `random.min()` and `random.max()`.
-- *random.min()*
- *Random.min()*
- Returns the smallest value that `random.get()` can return
+- *gsl.random.min()*
+ *gsl.Random.min()*
+ Returns the smallest value that `random.get()` can return.
-- *random.max()*
- *Random.max()*
- Returns the largest value that `random.get()` can return
+- *gsl.random.max()*
+ *gsl.Random.max()*
+ Returns the largest value that `random.get()` can return.
-- *random.uniform([seed])*
- *Random.uniform([])*
+- *gsl.random.uniform([seed])*
+ *gsl.Random.uniform([])*
Returns a double precision floating point number uniformly distributed in the range [0,1). The range includes 0.0 but excludes 1.0.
-
-### [The Gaussian Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html)
-
-- *random.gaussian([seed], deviation)*
- *Random.gaussian([deviation])*
+
+- *gsl.random.gaussian([seed], deviation)*
+ *gsl.Random.gaussian([deviation])*
Returns a Gaussian random float with mean zero given a standart deviation as a float.
-- *random.gaussianZiggurat([seed], deviation)*
- *Random.gaussianZiggurat(deviation)*
+- *gsl.random.gaussianZiggurat([seed], deviation)*
+ *gsl.Random.gaussianZiggurat(deviation)*
Same as `random.gaussian` but using the alternative Marsaglia-Tsang ziggurat method.
-- *random.gaussianRatioMethod([seed], deviation)*
- *Random.gaussianRatioMethod(deviation)*
+- *gsl.random.gaussianRatioMethod([seed], deviation)*
+ *gsl.Random.gaussianRatioMethod(deviation)*
Same as `random.gaussian` but using the alternative Kinderman-Monahan-Leva ratio method.
-
-### [The Poisson Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Poisson-Distribution.html)
-
-- *random.poisson([seed], mean)*
- *Random.poisson(mean)*
+
+- *gsl.random.poisson([seed], mean)*
+ *gsl.Random.poisson(mean)*
Returns a random integer from the Poisson distribution given a provided mean as a float.
-Exemple
+### Exemple
var gsl = require('gsl'),
seed = 50,
@@ -89,6 +83,26 @@ Exemple
console.log( iterator.gaussian(deviation) );
console.log( iterator.gaussian(deviation) );
+### Resources
+
+* [Sampling](http://www.gnu.org/software/gsl/manual/html_node/Sampling-from-a-random-number-generator.html)
+* [The Gaussian Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html)
+* [The Poisson Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Poisson-Distribution.html)
+
+Statistics API
+--------------
+
+Data are expected to be arrays of float numbers. Means are float numbers.
+
+- *gsl.statistics.mean(data)*
+ Returns the arithmetic mean of data.
+
+- *gsl.statistics.variance(data, [mean])*
+ Returns the estimated, or sample, variance of data.
+
+- *gsl.statistics.variance(data, [mean])*
+ Returns the standard deviation defined as the square root of the variance defined above.
+
Running the tests
-----------------
@@ -19,4 +19,4 @@ var data = [ -213, -564, -35, -15, 141, 115, -420, -360, 203, -338, -431,
-190, 172, -424, -385, 198, -218, -536, 96 ];
-console.log('-> '+gsl.statistics.sd(data,1,200));
+console.log('-> ', gsl.statistics.sd(data));
View
@@ -22,8 +22,8 @@ extern "C" {
{
gsl_rng_env_setup ();
r_global = gsl_rng_alloc (gsl_rng_default);
- gaussian_init(target);
random_init(target);
+ statistics_init(target);
}
NODE_MODULE(gsl, init);
}
View
@@ -13,23 +13,79 @@
using namespace node;
using namespace v8;
+// Note about stride http://awhan.wordpress.com/2009/06/17/gsl-vector-stride/
+
+Handle<Value> statisticsMean(const Arguments& args){
+ if(args.Length() != 1){
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ Local<Array> data_arg = Local<Array>::Cast(args[0]);
+ double datas [data_arg->Length()];
+ for(int i=0; i<data_arg->Length(); i++){
+ datas[i] = data_arg->Get(i)->NumberValue();
+ }
+ double val = gsl_stats_mean(datas, 1, data_arg->Length());
+ return Number::New(val);
+}
+
+Handle<Value> statisticsVariance(const Arguments& args){
+ Local<Array> data_arg;
+ bool useMean = false;
+ double mean;
+ if(args.Length() == 2){
+ data_arg = Local<Array>::Cast(args[0]);
+ mean = args[1]->NumberValue();
+ useMean = true;
+ }else if(args.Length() == 1){
+ data_arg = Local<Array>::Cast(args[0]);
+ }else{
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ double datas [data_arg->Length()];
+ for(int i=0; i<data_arg->Length(); i++){
+ datas[i] = data_arg->Get(i)->NumberValue();
+ }
+ double val;
+ if(useMean){
+ val = gsl_stats_variance_m(datas, 1, data_arg->Length(), mean);
+ }else{
+ val = gsl_stats_variance(datas, 1, data_arg->Length());
+ }
+ return Number::New(val);
+}
Handle<Value> statisticsSd(const Arguments& args){
- Local<Array> data_o = Local<Array>::Cast(args[0]);
- double datas [data_o->Length()];
- for(int i=0; i<data_o->Length(); i++){
- datas[i] = data_o->Get(i)->NumberValue();
+ Local<Array> data_arg;
+ bool useMean = false;
+ double mean;
+ if(args.Length() == 2){
+ data_arg = Local<Array>::Cast(args[0]);
+ mean = args[1]->NumberValue();
+ useMean = true;
+ }else if(args.Length() == 1){
+ data_arg = Local<Array>::Cast(args[0]);
+ }else{
+ return ThrowException(Exception::TypeError(String::New("Invalid argument")));
+ }
+ double datas [data_arg->Length()];
+ for(int i=0; i<data_arg->Length(); i++){
+ datas[i] = data_arg->Get(i)->NumberValue();
+ }
+ double val;
+ if(useMean){
+ val = gsl_stats_sd_m(datas, 1, data_arg->Length(), mean);
+ }else{
+ val = gsl_stats_sd(datas, 1, data_arg->Length());
}
- const size_t stride = args[1]->Uint32Value() ;
- const size_t n = args[2]->Uint32Value() ;
- double val = gsl_stats_sd (datas, stride, n);
return Number::New(val);
}
extern "C" {
- static void gaussian_init(Handle<Object> target)
+ static void statistics_init(Handle<Object> target)
{
HandleScope scope;
+ target->Set(String::New("statisticsMean"),FunctionTemplate::New(statisticsMean)->GetFunction());
+ target->Set(String::New("statisticsVariance"),FunctionTemplate::New(statisticsVariance)->GetFunction());
target->Set(String::New("statisticsSd"),FunctionTemplate::New(statisticsSd)->GetFunction());
}
}
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,18 @@
+
+var assert = require('assert'),
+ gsl = require('gsl');
+
+module.exports = {
+ 'Test mean': function(){
+ assert.eql(20, gsl.statistics.mean([10,30,20]));
+ },
+ 'Test variance': function(){
+ assert.eql(100, gsl.statistics.variance([10,30,20]));
+ // With mean
+ assert.eql(250, gsl.statistics.variance([10,30,20],30));
+ },
+ 'Test sd': function(){
+ assert.eql(10, gsl.statistics.sd([10,30,20]));
+ assert.eql(16, Math.round(gsl.statistics.sd([10,30,20],30)));
+ }
+};

0 comments on commit 155833a

Please sign in to comment.