Skip to content

Commit 0af19b2

Browse files
committed
Add more functions
1 parent 6ab5958 commit 0af19b2

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

README.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ console.log("corr = ", corr, "ci = ", ci);
3030
Functions
3131
=========
3232

33-
* min(a) - Minimum
34-
* max(a) - Maximum
35-
* mean(a) - Mean
36-
* gmean(a) - Geometric mean
37-
* hmean(a) - Harmonic mean
38-
* var(a) - Variance
39-
* std(a) - Standard deviation
40-
* skew(a) - Skewness
41-
* kurt(a) - Kurtosis
42-
* corr(x, y) - Correlation between x and y
43-
* sample(a) - Sample with replacement
44-
* boot(nboot, bootfun, data...) - Bootstrap the bootfun statistic
45-
* bootci(nboot, bootfun, data...) - Calculate bootstrap confidence intervals using the normal model
33+
* `min(a)` - [Minimum](http://en.wikipedia.org/wiki/Minimum)
34+
* `max(a)` - [Maximum](http://en.wikipedia.org/wiki/Maximum)
35+
* `mean(a)` - [Mean](http://en.wikipedia.org/wiki/Mean)
36+
* `gmean(a)` - [Geometric mean](http://en.wikipedia.org/wiki/Mean)
37+
* `hmean(a)` - [Harmonic mean](http://en.wikipedia.org/wiki/Mean)
38+
* `var(a)` - [Variance](http://en.wikipedia.org/wiki/Variance)
39+
* `std(a)` - [Standard deviation](http://en.wikipedia.org/wiki/Standard_deviation)
40+
* `skew(a)` - [Skewness](http://en.wikipedia.org/wiki/Skewness)
41+
* `kurt(a)` - [Kurtosis](http://en.wikipedia.org/wiki/Kurtosis)
42+
* `corr(x, y)` - [Correlation](http://en.wikipedia.org/wiki/Correlation) between x and y
43+
* `entropy(p)` - [Entropy](http://en.wikipedia.org/wiki/Entropy_(information_theory))
44+
* `kldiv(p, q)` - [Kullback–Leibler divergence](http://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence)
45+
* `shuffle(a)` - [Shuffle](http://en.wikipedia.org/wiki/Random_permutation) using the [Fisher–Yates shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle)
46+
* `sample(a)` - [Sample](http://en.wikipedia.org/wiki/Sampling_(statistics)) with replacement
47+
* `boot(nboot, bootfun, data...)` - [Bootstrap](http://en.wikipedia.org/wiki/Bootstrapping_(statistics)) the bootfun statistic
48+
* `bootci(nboot, bootfun, data...)` - Calculate bootstrap confidence intervals using the normal model
49+
* `randn()` - Draw random sample from the [standard normal distribution](http://en.wikipedia.org/wiki/Normal_distribution) using the [Marsaglia polar method](http://en.wikipedia.org/wiki/Marsaglia_polar_method)
4650

4751
Credits
4852
=======

examples/bootstrap.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var sk = require("../statkit.js");
2+
3+
var lsat = [576, 635, 558, 578, 666, 580, 555,
4+
661, 651, 605, 653, 575, 545, 572, 594];
5+
var gpa = [3.39, 3.30, 2.81, 3.03, 3.44, 3.07, 3.00,
6+
3.43, 3.36, 3.13, 3.12, 2.74, 2.76, 2.88, 2.96];
7+
8+
var corr = sk.corr(gpa, lsat);
9+
var ci = sk.bootci(100000, sk.corr, gpa, lsat);
10+
11+
console.log("corr = ", corr, "ci = ", ci);

statkit.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,38 @@ exports.corr = function(x, y) {
119119
return sxy / Math.sqrt(sx2 * sy2);
120120
};
121121

122+
exports.entropy = function(p) {
123+
var n = p.length;
124+
var e = 0.0;
125+
for (var i = 0; i < n; ++i) {
126+
e -= p[i] * Math.log(p[i]);
127+
}
128+
return e;
129+
}
130+
131+
exports.kldiv = function(p, q) {
132+
var n = p.length;
133+
var e = 0.0;
134+
var ce = 0.0;
135+
for (var i = 0; i < n; ++i) {
136+
e -= p[i] * Math.log(p[i]);
137+
ce -= p[i] * Math.log(q[i]);
138+
}
139+
return ce - e;
140+
}
141+
142+
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
143+
exports.shuffle = function(a) {
144+
var n = a.length;
145+
for (var i = n - 1; i > 0; i--) {
146+
var j = Math.random() * i | 0; // 0 ≤ j < i
147+
var t = array[j];
148+
array[j] = array[i];
149+
array[i] = t;
150+
}
151+
return a;
152+
}
153+
122154
exports.sample = function(a) {
123155
var n = a.length;
124156
var s = a.slice(0);
@@ -162,6 +194,19 @@ exports.bootci = function(nboot, bootfun) {
162194
return [v - 2*s, v + 2*s];
163195
};
164196

197+
// http://en.wikipedia.org/wiki/Marsaglia_polar_method
198+
// TODO: implement http://en.wikipedia.org/wiki/Ziggurat_algorithm
199+
exports.randn = function() {
200+
var u, v, s;
201+
do {
202+
u = Math.rand() * 2 - 1;
203+
v = Math.rand() * 2 - 1;
204+
s = u * u + v * v;
205+
} while (s >= 1 || s == 0);
206+
s = Math.sqrt(-2 * Math.log(s) / s);
207+
return u * s;
208+
};
209+
165210
// http://picomath.org/javascript/erf.js.html
166211
exports.erf = function(x) {
167212
// constants

test/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
print "kurtosis", np.array([st.kurtosis(a)])
1919
print "skew", np.array([st.skew(a)])
2020

21+
p = np.ones(5) / 5;
22+
print "entropy", np.array([st.entropy(p)])
23+
2124

2225
p = [0.0000001, 0.00001, 0.001, 0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65,
2326
0.75, 0.85, 0.95, 0.999, 0.99999, 0.9999999]

0 commit comments

Comments
 (0)