Move d3.stats to external lib, science.stats.js!

This commit is contained in:
Jason Davies 2011-07-06 18:57:14 +01:00
Родитель bb21bbd8ce
Коммит e0a0455f7b
33 изменённых файлов: 75 добавлений и 326 удалений

Просмотреть файл

@ -6,8 +6,6 @@ all: \
d3.min.js \ d3.min.js \
d3.behavior.js \ d3.behavior.js \
d3.behavior.min.js \ d3.behavior.min.js \
d3.stats.js \
d3.stats.min.js \
d3.chart.js \ d3.chart.js \
d3.chart.min.js \ d3.chart.min.js \
d3.layout.js \ d3.layout.js \
@ -101,20 +99,6 @@ d3.behavior.js: \
src/behavior/zoom.js \ src/behavior/zoom.js \
src/end.js src/end.js
d3.stats.js: \
src/start.js \
src/stats/stats.js \
src/stats/bandwidth.js \
src/stats/kernel.js \
src/stats/kde.js \
src/stats/iqr.js \
src/stats/mean.js \
src/stats/median.js \
src/stats/mode.js \
src/stats/quantiles.js \
src/stats/variance.js \
src/end.js
d3.chart.js: \ d3.chart.js: \
src/start.js \ src/start.js \
src/chart/chart.js \ src/chart/chart.js \

Просмотреть файл

@ -291,7 +291,11 @@ function d3_chart_boxWhiskers(d) {
function d3_chart_boxQuartiles(d) { function d3_chart_boxQuartiles(d) {
// TODO avoid sorting data twice? // TODO avoid sorting data twice?
return d3.stats.quantiles(d, [.25, .5, .75]); var n = d.length;
return [.25, .5, .75].map(function(q) {
q *= n;
return ~~q === q ? (d[q] + d[q + 1]) / 2 : d[Math.round(q)];
});
} }
// ranges (bad, satisfactory, good) // ranges (bad, satisfactory, good)
// measures (actual, forecast) // measures (actual, forecast)

2
d3.chart.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

1
d3.stats.min.js поставляемый
Просмотреть файл

@ -1 +0,0 @@
(function(){d3.stats={},d3.stats.bandwidth={nrd0:function(a){var b=Math.sqrt(d3.stats.variance(a));(lo=Math.min(b,d3.stats.iqr(a)/1.34))||(lo=b)||(lo=Math.abs(a[1]))||(lo=1);return.9*lo*Math.pow(a.length,-0.2)},nrd:function(a){var b=d3.stats.iqr(a)/1.34;return 1.06*Math.min(Math.sqrt(d3.stats.variance(a)),b)*Math.pow(a.length,-0.2)}},d3.stats.kernel={uniform:function(a){if(a<=1&&a>=-1)return.5;return 0},triangular:function(a){if(a<=1&&a>=-1)return 1-Math.abs(a);return 0},epanechnikov:function(a){if(a<=1&&a>=-1)return.75*(1-a*a);return 0},quartic:function(a){if(a<=1&&a>=-1){var b=1-a*a;return.9375*b*b}return 0},triweight:function(a){if(a<=1&&a>=-1){var b=1-a*a;return 35/32*b*b*b}return 0},gaussian:function(a){return 1/Math.sqrt(2*Math.PI)*Math.exp(-0.5*a*a)},cosine:function(a){if(a<=1&&a>=-1)return Math.PI/4*Math.cos(Math.PI/2*a);return 0}},d3.stats.kde=function(){function d(d,e){var f=c.call(this,b);return d.map(function(c){var d=-1,e=0,g=b.length;while(++d<g)e+=a((c-b[d])/f);return[c,e/f/g]})}var a=d3.stats.kernel.gaussian,b=[],c=d3.stats.bandwidth.nrd;d.kernel=function(b){if(!arguments.length)return a;a=b;return d},d.sample=function(a){if(!arguments.length)return b;b=a;return d},d.bandwidth=function(a){if(!arguments.length)return c;c=d3.functor(a);return d};return d},d3.stats.iqr=function(a){var b=d3.stats.quantiles(a,[.25,.75]);return b[1]-b[0]},d3.stats.mean=function(a){var b=a.length;if(b===0)return NaN;var c=0,d=-1;while(++d<b)c+=(a[d]-c)/(d+1);return c},d3.stats.median=function(a){return d3.stats.quantiles(a,[.5])[0]},d3.stats.mode=function(a){a=a.slice().sort(d3.ascending);var b,c=a.length,d=-1,e=d,f=null,g=0,h,i;while(++d<c)(i=a[d])!==f&&((h=d-e)>g&&(g=h,b=f),f=i,e=d);return b},d3.stats.quantiles=function(a,b){a=a.slice().sort(d3.ascending);var c=a.length-1;return b.map(function(b){if(b===0)return a[0];if(b===1)return a[c];var e=1+b*c,f=Math.floor(e),g=e-f,h=a[f-1];return g===0?h:h+g*(a[f]-h)})},d3.stats.variance=function(a){var b=a.length;if(b<1)return NaN;if(b===1)return 0;var c=d3.stats.mean(a),d=-1,e=0;while(++d<b){var f=a[d]-c;e+=f*f}return e/(b-1)}})()

Просмотреть файл

@ -3,7 +3,6 @@
<head> <head>
<title>Box Plot Charts</title> <title>Box Plot Charts</title>
<script type="text/javascript" src="../../d3.js"></script> <script type="text/javascript" src="../../d3.js"></script>
<script type="text/javascript" src="../../d3.stats.js"></script>
<script type="text/javascript" src="../../d3.chart.js"></script> <script type="text/javascript" src="../../d3.chart.js"></script>
<script type="text/javascript" src="../../d3.csv.js"></script> <script type="text/javascript" src="../../d3.csv.js"></script>
<link type="text/css" rel="stylesheet" href="../button.css"/> <link type="text/css" rel="stylesheet" href="../button.css"/>

Просмотреть файл

@ -5,7 +5,8 @@
<link rel="stylesheet" type="text/css" href="kde.css"/> <link rel="stylesheet" type="text/css" href="kde.css"/>
<script type="text/javascript" src="../../d3.js"></script> <script type="text/javascript" src="../../d3.js"></script>
<script type="text/javascript" src="../../d3.layout.js"></script> <script type="text/javascript" src="../../d3.layout.js"></script>
<script type="text/javascript" src="../../d3.stats.js"></script> <script type="text/javascript" src="../../lib/science/science.js"></script>
<script type="text/javascript" src="../../lib/science/science.stats.js"></script>
</head> </head>
<body> <body>
<script type="text/javascript" src="kde.js"></script> <script type="text/javascript" src="kde.js"></script>

Просмотреть файл

@ -4,10 +4,10 @@ d3.json("faithful.json", function(faithful) {
var w = 800, var w = 800,
h = 400, h = 400,
x = d3.scale.linear().domain([30, 110]).range([0, w]); x = d3.scale.linear().domain([30, 110]).range([0, w]);
bins = d3.layout.histogram().frequency(false).ticks(x.ticks(60))(data), bins = d3.layout.histogram().frequency(true).bins(x.ticks(60))(data),
max = d3.max(bins, function(d) { return d.y; }), max = d3.max(bins, function(d) { return d.y; }),
y = d3.scale.linear().domain([0, .1]).range([0, h]), y = d3.scale.linear().domain([0, .1]).range([0, h]),
kde = d3.stats.kde().sample(data); kde = science.stats.kde().sample(data);
var vis = d3.select("body") var vis = d3.select("body")
.append("svg:svg") .append("svg:svg")
@ -32,7 +32,7 @@ d3.json("faithful.json", function(faithful) {
.y(function(d) { return h - y(d[1]); }); .y(function(d) { return h - y(d[1]); });
vis.selectAll("path") vis.selectAll("path")
.data(d3.values(d3.stats.bandwidth)) .data(d3.values(science.stats.bandwidth))
.enter().append("svg:path") .enter().append("svg:path")
.attr("d", function(h) { .attr("d", function(h) {
return line(kde.bandwidth(h)(d3.range(30, 110, .1))); return line(kde.bandwidth(h)(d3.range(30, 110, .1)));

Просмотреть файл

@ -5,6 +5,8 @@
<script type="text/javascript" src="../../d3.js"></script> <script type="text/javascript" src="../../d3.js"></script>
<script type="text/javascript" src="../../d3.chart.js"></script> <script type="text/javascript" src="../../d3.chart.js"></script>
<script type="text/javascript" src="../../d3.stats.js"></script> <script type="text/javascript" src="../../d3.stats.js"></script>
<script type="text/javascript" src="../../lib/science/science.js"></script>
<script type="text/javascript" src="../../lib/science/science.stats.js"></script>
<link type="text/css" rel="stylesheet" href="../button.css"/> <link type="text/css" rel="stylesheet" href="../button.css"/>
<link type="text/css" rel="stylesheet" href="qq.css"/> <link type="text/css" rel="stylesheet" href="qq.css"/>
</head> </head>

Просмотреть файл

@ -15,8 +15,8 @@ var vis = d3.select("#chart")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")"); .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
d3.json("turkers.json", function(turkers) { d3.json("turkers.json", function(turkers) {
var tm = d3.stats.mean(turkers), var tm = science.stats.mean(turkers),
td = Math.sqrt(d3.stats.variance(turkers)), td = Math.sqrt(science.stats.variance(turkers)),
dd = [ dd = [
[0.10306430789206111, 0.0036139086950272735, 0.30498647327844536], [0.10306430789206111, 0.0036139086950272735, 0.30498647327844536],
[0.5924252668569606, 0.0462763685758622, 0.4340870312025223], [0.5924252668569606, 0.0462763685758622, 0.4340870312025223],

26
lib/science/LICENSE Normal file
Просмотреть файл

@ -0,0 +1,26 @@
Copyright (c) 2011, Jason Davies
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name Jason Davies may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL JASON DAVIES BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

5
lib/science/science.js Normal file
Просмотреть файл

@ -0,0 +1,5 @@
(function(){science = {version: "0.0.1"}; // semver
science.functor = function(v) {
return typeof v === "function" ? v : function() { return v; };
};
})()

1
lib/science/science.min.js поставляемый Normal file
Просмотреть файл

@ -0,0 +1 @@
(function(){science={version:"0.0.1"},science.functor=function(a){return typeof a=="function"?a:function(){return a}}})()

Просмотреть файл

@ -1,12 +1,12 @@
(function(){d3.stats = {}; (function(){science.stats = {};
// Bandwidth selectors for Gaussian kernels. // Bandwidth selectors for Gaussian kernels.
// Based on R's implementations in `stats.bw`. // Based on R's implementations in `stats.bw`.
d3.stats.bandwidth = { science.stats.bandwidth = {
// Silverman, B. W. (1986) Density Estimation. London: Chapman and Hall. // Silverman, B. W. (1986) Density Estimation. London: Chapman and Hall.
nrd0: function(x) { nrd0: function(x) {
var hi = Math.sqrt(d3.stats.variance(x)); var hi = Math.sqrt(science.stats.variance(x));
if (!(lo = Math.min(hi, d3.stats.iqr(x) / 1.34))) if (!(lo = Math.min(hi, science.stats.iqr(x) / 1.34)))
(lo = hi) || (lo = Math.abs(x[1])) || (lo = 1); (lo = hi) || (lo = Math.abs(x[1])) || (lo = 1);
return .9 * lo * Math.pow(x.length, -.2); return .9 * lo * Math.pow(x.length, -.2);
}, },
@ -14,13 +14,13 @@ d3.stats.bandwidth = {
// Scott, D. W. (1992) Multivariate Density Estimation: Theory, Practice, and // Scott, D. W. (1992) Multivariate Density Estimation: Theory, Practice, and
// Visualization. Wiley. // Visualization. Wiley.
nrd: function(x) { nrd: function(x) {
var h = d3.stats.iqr(x) / 1.34; var h = science.stats.iqr(x) / 1.34;
return 1.06 * Math.min(Math.sqrt(d3.stats.variance(x)), h) return 1.06 * Math.min(Math.sqrt(science.stats.variance(x)), h)
* Math.pow(x.length, -1/5); * Math.pow(x.length, -1/5);
} }
}; };
// See <http://en.wikipedia.org/wiki/Kernel_(statistics)>. // See <http://en.wikipedia.org/wiki/Kernel_(statistics)>.
d3.stats.kernel = { science.stats.kernel = {
uniform: function(u) { uniform: function(u) {
if (u <= 1 && u >= -1) return .5; if (u <= 1 && u >= -1) return .5;
return 0; return 0;
@ -56,10 +56,10 @@ d3.stats.kernel = {
} }
}; };
// http://exploringdata.net/den_trac.htm // http://exploringdata.net/den_trac.htm
d3.stats.kde = function() { science.stats.kde = function() {
var kernel = d3.stats.kernel.gaussian, var kernel = science.stats.kernel.gaussian,
sample = [], sample = [],
bandwidth = d3.stats.bandwidth.nrd; bandwidth = science.stats.bandwidth.nrd;
function kde(points, i) { function kde(points, i) {
var bw = bandwidth.call(this, sample); var bw = bandwidth.call(this, sample);
@ -88,18 +88,18 @@ d3.stats.kde = function() {
kde.bandwidth = function(x) { kde.bandwidth = function(x) {
if (!arguments.length) return bandwidth; if (!arguments.length) return bandwidth;
bandwidth = d3.functor(x); bandwidth = science.functor(x);
return kde; return kde;
}; };
return kde; return kde;
}; };
d3.stats.iqr = function(x) { science.stats.iqr = function(x) {
var quartiles = d3.stats.quantiles(x, [.25, .75]); var quartiles = science.stats.quantiles(x, [.25, .75]);
return quartiles[1] - quartiles[0]; return quartiles[1] - quartiles[0];
}; };
// Welford's algorithm. // Welford's algorithm.
d3.stats.mean = function(x) { science.stats.mean = function(x) {
var n = x.length; var n = x.length;
if (n === 0) return NaN; if (n === 0) return NaN;
var m = 0, var m = 0,
@ -107,11 +107,11 @@ d3.stats.mean = function(x) {
while (++i < n) m += (x[i] - m) / (i + 1); while (++i < n) m += (x[i] - m) / (i + 1);
return m; return m;
}; };
d3.stats.median = function(x) { science.stats.median = function(x) {
return d3.stats.quantiles(x, [.5])[0]; return science.stats.quantiles(x, [.5])[0];
}; };
d3.stats.mode = function(x) { science.stats.mode = function(x) {
x = x.slice().sort(d3.ascending); x = x.slice().sort(science.ascending);
var mode, var mode,
n = x.length, n = x.length,
i = -1, i = -1,
@ -133,8 +133,8 @@ d3.stats.mode = function(x) {
return mode; return mode;
}; };
// Uses R's quantile algorithm type=7. // Uses R's quantile algorithm type=7.
d3.stats.quantiles = function(d, quantiles) { science.stats.quantiles = function(d, quantiles) {
d = d.slice().sort(d3.ascending); d = d.slice().sort(science.ascending);
var n_1 = d.length - 1; var n_1 = d.length - 1;
return quantiles.map(function(q) { return quantiles.map(function(q) {
if (q === 0) return d[0]; if (q === 0) return d[0];
@ -150,11 +150,11 @@ d3.stats.quantiles = function(d, quantiles) {
}; };
// Unbiased estimate of a sample's variance. // Unbiased estimate of a sample's variance.
// Also known as the sample variance, where the denominator is n - 1. // Also known as the sample variance, where the denominator is n - 1.
d3.stats.variance = function(x) { science.stats.variance = function(x) {
var n = x.length; var n = x.length;
if (n < 1) return NaN; if (n < 1) return NaN;
if (n === 1) return 0; if (n === 1) return 0;
var mean = d3.stats.mean(x), var mean = science.stats.mean(x),
i = -1, i = -1,
s = 0; s = 0;
while (++i < n) { while (++i < n) {

1
lib/science/science.stats.min.js поставляемый Normal file
Просмотреть файл

@ -0,0 +1 @@
(function(){science.stats={},science.stats.bandwidth={nrd0:function(a){var b=Math.sqrt(science.stats.variance(a));(lo=Math.min(b,science.stats.iqr(a)/1.34))||(lo=b)||(lo=Math.abs(a[1]))||(lo=1);return.9*lo*Math.pow(a.length,-0.2)},nrd:function(a){var b=science.stats.iqr(a)/1.34;return 1.06*Math.min(Math.sqrt(science.stats.variance(a)),b)*Math.pow(a.length,-0.2)}},science.stats.kernel={uniform:function(a){if(a<=1&&a>=-1)return.5;return 0},triangular:function(a){if(a<=1&&a>=-1)return 1-Math.abs(a);return 0},epanechnikov:function(a){if(a<=1&&a>=-1)return.75*(1-a*a);return 0},quartic:function(a){if(a<=1&&a>=-1){var b=1-a*a;return.9375*b*b}return 0},triweight:function(a){if(a<=1&&a>=-1){var b=1-a*a;return 35/32*b*b*b}return 0},gaussian:function(a){return 1/Math.sqrt(2*Math.PI)*Math.exp(-0.5*a*a)},cosine:function(a){if(a<=1&&a>=-1)return Math.PI/4*Math.cos(Math.PI/2*a);return 0}},science.stats.kde=function(){function d(d,e){var f=c.call(this,b);return d.map(function(c){var d=-1,e=0,g=b.length;while(++d<g)e+=a((c-b[d])/f);return[c,e/f/g]})}var a=science.stats.kernel.gaussian,b=[],c=science.stats.bandwidth.nrd;d.kernel=function(b){if(!arguments.length)return a;a=b;return d},d.sample=function(a){if(!arguments.length)return b;b=a;return d},d.bandwidth=function(a){if(!arguments.length)return c;c=science.functor(a);return d};return d},science.stats.iqr=function(a){var b=science.stats.quantiles(a,[.25,.75]);return b[1]-b[0]},science.stats.mean=function(a){var b=a.length;if(b===0)return NaN;var c=0,d=-1;while(++d<b)c+=(a[d]-c)/(d+1);return c},science.stats.median=function(a){return science.stats.quantiles(a,[.5])[0]},science.stats.mode=function(a){a=a.slice().sort(science.ascending);var b,c=a.length,d=-1,e=d,f=null,g=0,h,i;while(++d<c)(i=a[d])!==f&&((h=d-e)>g&&(g=h,b=f),f=i,e=d);return b},science.stats.quantiles=function(a,b){a=a.slice().sort(science.ascending);var c=a.length-1;return b.map(function(b){if(b===0)return a[0];if(b===1)return a[c];var e=1+b*c,f=Math.floor(e),g=e-f,h=a[f-1];return g===0?h:h+g*(a[f]-h)})},science.stats.variance=function(a){var b=a.length;if(b<1)return NaN;if(b===1)return 0;var c=science.stats.mean(a),d=-1,e=0;while(++d<b){var f=a[d]-c;e+=f*f}return e/(b-1)}})()

Просмотреть файл

@ -290,5 +290,9 @@ function d3_chart_boxWhiskers(d) {
function d3_chart_boxQuartiles(d) { function d3_chart_boxQuartiles(d) {
// TODO avoid sorting data twice? // TODO avoid sorting data twice?
return d3.stats.quantiles(d, [.25, .5, .75]); var n = d.length;
return [.25, .5, .75].map(function(q) {
q *= n;
return ~~q === q ? (d[q] + d[q + 1]) / 2 : d[Math.round(q)];
});
} }

Просмотреть файл

@ -1,20 +0,0 @@
// Bandwidth selectors for Gaussian kernels.
// Based on R's implementations in `stats.bw`.
d3.stats.bandwidth = {
// Silverman, B. W. (1986) Density Estimation. London: Chapman and Hall.
nrd0: function(x) {
var hi = Math.sqrt(d3.stats.variance(x));
if (!(lo = Math.min(hi, d3.stats.iqr(x) / 1.34)))
(lo = hi) || (lo = Math.abs(x[1])) || (lo = 1);
return .9 * lo * Math.pow(x.length, -.2);
},
// Scott, D. W. (1992) Multivariate Density Estimation: Theory, Practice, and
// Visualization. Wiley.
nrd: function(x) {
var h = d3.stats.iqr(x) / 1.34;
return 1.06 * Math.min(Math.sqrt(d3.stats.variance(x)), h)
* Math.pow(x.length, -1/5);
}
};

Просмотреть файл

@ -1,4 +0,0 @@
d3.stats.iqr = function(x) {
var quartiles = d3.stats.quantiles(x, [.25, .75]);
return quartiles[1] - quartiles[0];
};

Просмотреть файл

@ -1,39 +0,0 @@
// http://exploringdata.net/den_trac.htm
d3.stats.kde = function() {
var kernel = d3.stats.kernel.gaussian,
sample = [],
bandwidth = d3.stats.bandwidth.nrd;
function kde(points, i) {
var bw = bandwidth.call(this, sample);
return points.map(function(x) {
var i = -1,
y = 0,
n = sample.length;
while (++i < n) {
y += kernel((x - sample[i]) / bw);
}
return [x, y / bw / n];
});
}
kde.kernel = function(x) {
if (!arguments.length) return kernel;
kernel = x;
return kde;
};
kde.sample = function(x) {
if (!arguments.length) return sample;
sample = x;
return kde;
};
kde.bandwidth = function(x) {
if (!arguments.length) return bandwidth;
bandwidth = d3.functor(x);
return kde;
};
return kde;
};

Просмотреть файл

@ -1,36 +0,0 @@
// See <http://en.wikipedia.org/wiki/Kernel_(statistics)>.
d3.stats.kernel = {
uniform: function(u) {
if (u <= 1 && u >= -1) return .5;
return 0;
},
triangular: function(u) {
if (u <= 1 && u >= -1) return 1 - Math.abs(u);
return 0;
},
epanechnikov: function(u) {
if (u <= 1 && u >= -1) return .75 * (1 - u * u);
return 0;
},
quartic: function(u) {
if (u <= 1 && u >= -1) {
var tmp = 1 - u * u;
return (15 / 16) * tmp * tmp;
}
return 0;
},
triweight: function(u) {
if (u <= 1 && u >= -1) {
var tmp = 1 - u * u;
return (35 / 32) * tmp * tmp * tmp;
}
return 0;
},
gaussian: function(u) {
return 1 / Math.sqrt(2 * Math.PI) * Math.exp(-.5 * u * u);
},
cosine: function(u) {
if (u <= 1 && u >= -1) return Math.PI / 4 * Math.cos(Math.PI / 2 * u);
return 0;
}
};

Просмотреть файл

@ -1,9 +0,0 @@
// Welford's algorithm.
d3.stats.mean = function(x) {
var n = x.length;
if (n === 0) return NaN;
var m = 0,
i = -1;
while (++i < n) m += (x[i] - m) / (i + 1);
return m;
};

Просмотреть файл

@ -1,3 +0,0 @@
d3.stats.median = function(x) {
return d3.stats.quantiles(x, [.5])[0];
};

Просмотреть файл

@ -1,22 +0,0 @@
d3.stats.mode = function(x) {
x = x.slice().sort(d3.ascending);
var mode,
n = x.length,
i = -1,
l = i,
last = null,
max = 0,
tmp,
v;
while (++i < n) {
if ((v = x[i]) !== last) {
if ((tmp = i - l) > max) {
max = tmp;
mode = last;
}
last = v;
l = i;
}
}
return mode;
};

Просмотреть файл

@ -1,16 +0,0 @@
// Uses R's quantile algorithm type=7.
d3.stats.quantiles = function(d, quantiles) {
d = d.slice().sort(d3.ascending);
var n_1 = d.length - 1;
return quantiles.map(function(q) {
if (q === 0) return d[0];
else if (q === 1) return d[n_1];
var index = 1 + q * n_1,
lo = Math.floor(index),
h = index - lo,
a = d[lo - 1];
return h === 0 ? a : a + h * (d[lo] - a);
});
};

Просмотреть файл

@ -1 +0,0 @@
d3.stats = {};

Просмотреть файл

@ -1,15 +0,0 @@
// Unbiased estimate of a sample's variance.
// Also known as the sample variance, where the denominator is n - 1.
d3.stats.variance = function(x) {
var n = x.length;
if (n < 1) return NaN;
if (n === 1) return 0;
var mean = d3.stats.mean(x),
i = -1,
s = 0;
while (++i < n) {
var v = x[i] - mean;
s += v * v;
}
return s / (n - 1);
};

Просмотреть файл

@ -1,13 +0,0 @@
require("./../lib/env-js/envjs/node");
require("./../d3");
require("./../d3.stats");
var data = [1, 2, 3, 4];
console.log("nrd0:");
console.log(" ", d3.stats.bandwidth.nrd0(data));
console.log("");
console.log("nrd:");
console.log(" ", d3.stats.bandwidth.nrd(data));
console.log("");

Просмотреть файл

@ -1,6 +0,0 @@
nrd0:
0.7635139420854616
nrd:
0.899249754011766

Просмотреть файл

@ -1,12 +0,0 @@
require("./../lib/env-js/envjs/node");
require("./../d3");
require("./../d3.stats");
var data = [1, 2, 3, 4, 5, 6];
for (var i = 0; i <= data.length; i++) {
var d = data.slice(0, i);
console.log("iqr [" + d + "]:");
console.log(" ", d3.stats.iqr(d));
console.log("");
}

Просмотреть файл

@ -1,21 +0,0 @@
iqr []:
NaN
iqr [1]:
0
iqr [1,2]:
0.5
iqr [1,2,3]:
1
iqr [1,2,3,4]:
1.5
iqr [1,2,3,4,5]:
2
iqr [1,2,3,4,5,6]:
2.5

Просмотреть файл

@ -1,12 +0,0 @@
require("./../lib/env-js/envjs/node");
require("./../d3");
require("./../d3.stats");
var data = [1, 2, 3, 4, 5];
for (var i = 0; i <= data.length; i++) {
var d = data.slice(0, i);
console.log("median [" + d + "]:");
console.log(" ", d3.stats.median(d));
console.log("");
}

Просмотреть файл

@ -1,18 +0,0 @@
median []:
NaN
median [1]:
1
median [1,2]:
1.5
median [1,2,3]:
2
median [1,2,3,4]:
2.5
median [1,2,3,4,5]:
3

Просмотреть файл

@ -1,12 +0,0 @@
require("./../lib/env-js/envjs/node");
require("./../d3");
require("./../d3.stats");
var data = [1, 2, 3, 4, 5];
for (var i = 0; i <= data.length; i++) {
var d = data.slice(0, i);
console.log("variance [" + d + "]:");
console.log(" ", d3.stats.variance(d));
console.log("");
}

Просмотреть файл

@ -1,18 +0,0 @@
variance []:
NaN
variance [1]:
0
variance [1,2]:
0.5
variance [1,2,3]:
1
variance [1,2,3,4]:
1.6666666666666667
variance [1,2,3,4,5]:
2.5