Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding mean and variance; Remove stride and n parameters.

  • Loading branch information...
commit 155833afcfc4b37703b9e2f0c4a975f9a0dabb47 1 parent 32c71a1
Worms David authored April 26, 2011
2  index.js
@@ -14,6 +14,8 @@ module.exports = {
14 14
 		poisson: gsl.ranPoisson
15 15
 	},
16 16
 	statistics: {
  17
+		mean: gsl.statisticsMean,
  18
+		variance: gsl.statisticsVariance,
17 19
 		sd: gsl.statisticsSd
18 20
 	}
19 21
 };
72  readme.md
Source Rendered
@@ -35,48 +35,42 @@ Using an iterator objects make sense when using seeds and when performance is a
35 35
 
36 36
 Seeds are always optional and must be provided as unsigned integers. Deviations, used by gaussian functions, are float.
37 37
 
38  
--	*Random([seed])*
  38
+-	*gsl.Random([seed])*
39 39
 	Construct a new iterator, seel below for available random methods.
40  
-
41  
-### [Sampling](http://www.gnu.org/software/gsl/manual/html_node/Sampling-from-a-random-number-generator.html)
42  
-
43  
--	*random.get([seed])*
44  
-	*Random.get()*
  40
+	
  41
+-	*gsl.random.get([seed])*
  42
+	*gsl.Random.get()*
45 43
 	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()`.
46 44
 	
47  
--	*random.min()*
48  
-	*Random.min()*
49  
-	Returns the smallest value that `random.get()` can return
  45
+-	*gsl.random.min()*
  46
+	*gsl.Random.min()*
  47
+	Returns the smallest value that `random.get()` can return.
50 48
 	
51  
--	*random.max()*
52  
-	*Random.max()*
53  
-	Returns the largest value that `random.get()` can return
  49
+-	*gsl.random.max()*
  50
+	*gsl.Random.max()*
  51
+	Returns the largest value that `random.get()` can return.
54 52
 	
55  
--	*random.uniform([seed])*
56  
-	*Random.uniform([])*
  53
+-	*gsl.random.uniform([seed])*
  54
+	*gsl.Random.uniform([])*
57 55
 	Returns a double precision floating point number uniformly distributed in the range [0,1). The range includes 0.0 but excludes 1.0.
58  
-
59  
-### [The Gaussian Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html)
60  
-
61  
--	*random.gaussian([seed], deviation)*
62  
-	*Random.gaussian([deviation])*
  56
+	
  57
+-	*gsl.random.gaussian([seed], deviation)* 
  58
+	*gsl.Random.gaussian([deviation])*
63 59
 	Returns a Gaussian random float with mean zero given a standart deviation as a float.
64 60
 	
65  
--	*random.gaussianZiggurat([seed], deviation)*
66  
-	*Random.gaussianZiggurat(deviation)*
  61
+-	*gsl.random.gaussianZiggurat([seed], deviation)* 
  62
+	*gsl.Random.gaussianZiggurat(deviation)*
67 63
 	Same as `random.gaussian` but using the alternative Marsaglia-Tsang ziggurat method.
68 64
 	
69  
--	*random.gaussianRatioMethod([seed], deviation)*
70  
-	*Random.gaussianRatioMethod(deviation)*
  65
+-	*gsl.random.gaussianRatioMethod([seed], deviation)* 
  66
+	*gsl.Random.gaussianRatioMethod(deviation)*
71 67
 	Same as `random.gaussian` but using the alternative Kinderman-Monahan-Leva ratio method.
72  
-
73  
-### [The Poisson Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Poisson-Distribution.html)
74  
-
75  
--	*random.poisson([seed], mean)*
76  
-	*Random.poisson(mean)*
  68
+	
  69
+-	*gsl.random.poisson([seed], mean)* 
  70
+	*gsl.Random.poisson(mean)*
77 71
 	Returns a random integer from the Poisson distribution given a provided mean as a float.
78 72
 
79  
-Exemple
  73
+### Exemple
80 74
 
81 75
 	var gsl = require('gsl'),
82 76
 		seed = 50,
@@ -89,6 +83,26 @@ Exemple
89 83
 	console.log( iterator.gaussian(deviation) );
90 84
 	console.log( iterator.gaussian(deviation) );
91 85
 
  86
+### Resources
  87
+
  88
+*	[Sampling](http://www.gnu.org/software/gsl/manual/html_node/Sampling-from-a-random-number-generator.html)
  89
+*	[The Gaussian Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html)
  90
+*	[The Poisson Distribution](http://www.gnu.org/software/gsl/manual/html_node/The-Poisson-Distribution.html)
  91
+
  92
+Statistics API
  93
+--------------
  94
+
  95
+Data are expected to be arrays of float numbers. Means are float numbers.
  96
+
  97
+-	*gsl.statistics.mean(data)*
  98
+	Returns the arithmetic mean of data.
  99
+	
  100
+-	*gsl.statistics.variance(data, [mean])*
  101
+	Returns the estimated, or sample, variance of data.
  102
+	
  103
+-	*gsl.statistics.variance(data, [mean])*
  104
+	Returns the standard deviation defined as the square root of the variance defined above.
  105
+
92 106
 Running the tests
93 107
 -----------------
94 108
 
2  samples/statistics_sd.js
@@ -19,4 +19,4 @@ var data = [ -213, -564,  -35,  -15,  141,  115, -420, -360,  203, -338, -431,
19 19
             -190,  172, -424, -385,  198, -218, -536,   96 ];
20 20
 
21 21
 
22  
-console.log('-> '+gsl.statistics.sd(data,1,200));
  22
+console.log('-> ', gsl.statistics.sd(data));
2  src/gsl.cc
@@ -22,8 +22,8 @@ extern "C" {
22 22
 	{
23 23
 		gsl_rng_env_setup ();
24 24
 		r_global = gsl_rng_alloc (gsl_rng_default);
25  
-		gaussian_init(target);
26 25
 		random_init(target);
  26
+		statistics_init(target);
27 27
 	}
28 28
 	NODE_MODULE(gsl, init);
29 29
 }
72  src/statistics.cc
@@ -13,23 +13,79 @@
13 13
 
14 14
 using namespace node;
15 15
 using namespace v8;
  16
+// Note about stride http://awhan.wordpress.com/2009/06/17/gsl-vector-stride/
  17
+
  18
+Handle<Value> statisticsMean(const Arguments& args){
  19
+	if(args.Length() != 1){
  20
+		return ThrowException(Exception::TypeError(String::New("Invalid argument")));
  21
+	}
  22
+    Local<Array> data_arg = Local<Array>::Cast(args[0]);
  23
+    double datas [data_arg->Length()];
  24
+    for(int i=0; i<data_arg->Length(); i++){
  25
+    	datas[i] = data_arg->Get(i)->NumberValue();
  26
+    }
  27
+    double val = gsl_stats_mean(datas, 1, data_arg->Length());
  28
+    return Number::New(val);
  29
+}
  30
+
  31
+Handle<Value> statisticsVariance(const Arguments& args){
  32
+	Local<Array> data_arg;
  33
+	bool useMean = false;
  34
+	double mean;
  35
+	if(args.Length() == 2){
  36
+		data_arg = Local<Array>::Cast(args[0]);
  37
+		mean = args[1]->NumberValue();
  38
+		useMean = true;
  39
+	}else if(args.Length() == 1){
  40
+		data_arg = Local<Array>::Cast(args[0]);
  41
+	}else{
  42
+		return ThrowException(Exception::TypeError(String::New("Invalid argument")));
  43
+	}
  44
+    double datas [data_arg->Length()];
  45
+    for(int i=0; i<data_arg->Length(); i++){
  46
+    	datas[i] = data_arg->Get(i)->NumberValue();
  47
+    }
  48
+    double val;
  49
+    if(useMean){
  50
+    	val = gsl_stats_variance_m(datas, 1, data_arg->Length(), mean);
  51
+    }else{
  52
+        val = gsl_stats_variance(datas, 1, data_arg->Length());
  53
+    }
  54
+    return Number::New(val);
  55
+}
16 56
 
17 57
 Handle<Value> statisticsSd(const Arguments& args){
18  
-    Local<Array> data_o = Local<Array>::Cast(args[0]);
19  
-    double datas [data_o->Length()];
20  
-    for(int i=0; i<data_o->Length(); i++){
21  
-    	datas[i] = data_o->Get(i)->NumberValue();
  58
+	Local<Array> data_arg;
  59
+	bool useMean = false;
  60
+	double mean;
  61
+	if(args.Length() == 2){
  62
+		data_arg = Local<Array>::Cast(args[0]);
  63
+		mean = args[1]->NumberValue();
  64
+		useMean = true;
  65
+	}else if(args.Length() == 1){
  66
+		data_arg = Local<Array>::Cast(args[0]);
  67
+	}else{
  68
+		return ThrowException(Exception::TypeError(String::New("Invalid argument")));
  69
+	}
  70
+    double datas [data_arg->Length()];
  71
+    for(int i=0; i<data_arg->Length(); i++){
  72
+    	datas[i] = data_arg->Get(i)->NumberValue();
  73
+    }
  74
+    double val;
  75
+    if(useMean){
  76
+    	val = gsl_stats_sd_m(datas, 1, data_arg->Length(), mean);
  77
+    }else{
  78
+        val = gsl_stats_sd(datas, 1, data_arg->Length());
22 79
     }
23  
-    const size_t stride = args[1]->Uint32Value() ;
24  
-    const size_t n = args[2]->Uint32Value() ;
25  
-    double val = gsl_stats_sd (datas, stride, n);
26 80
     return Number::New(val);
27 81
 }
28 82
 
29 83
 extern "C" {
30  
-	static void gaussian_init(Handle<Object> target)
  84
+	static void statistics_init(Handle<Object> target)
31 85
 	{
32 86
 		HandleScope scope;
  87
+		target->Set(String::New("statisticsMean"),FunctionTemplate::New(statisticsMean)->GetFunction());
  88
+		target->Set(String::New("statisticsVariance"),FunctionTemplate::New(statisticsVariance)->GetFunction());
33 89
 		target->Set(String::New("statisticsSd"),FunctionTemplate::New(statisticsSd)->GetFunction());
34 90
 	}
35 91
 }
0  test/gaussian.js → test/random_gaussian.js
File renamed without changes
0  test/poisson.js → test/random_poisson.js
File renamed without changes
18  test/statistics.js
... ...
@@ -0,0 +1,18 @@
  1
+
  2
+var assert = require('assert'),
  3
+	gsl = require('gsl');
  4
+
  5
+module.exports = {
  6
+	'Test mean': function(){
  7
+		assert.eql(20, gsl.statistics.mean([10,30,20]));
  8
+	},
  9
+	'Test variance': function(){
  10
+		assert.eql(100, gsl.statistics.variance([10,30,20]));
  11
+		// With mean
  12
+		assert.eql(250, gsl.statistics.variance([10,30,20],30));
  13
+	},
  14
+	'Test sd': function(){
  15
+		assert.eql(10, gsl.statistics.sd([10,30,20]));
  16
+		assert.eql(16, Math.round(gsl.statistics.sd([10,30,20],30)));
  17
+	}
  18
+};

0 notes on commit 155833a

Please sign in to comment.
Something went wrong with that request. Please try again.