You've already forked wakapi-readme-stats
Bar graph added.
This commit is contained in:
27
node_modules/vega-statistics/LICENSE
generated
vendored
Normal file
27
node_modules/vega-statistics/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
Copyright (c) 2015-2018, University of Washington Interactive Data Lab
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
2. 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.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors
|
||||
may 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||
302
node_modules/vega-statistics/README.md
generated
vendored
Normal file
302
node_modules/vega-statistics/README.md
generated
vendored
Normal file
@@ -0,0 +1,302 @@
|
||||
# vega-statistics
|
||||
|
||||
Statistical routines and probability distributions.
|
||||
|
||||
## API Reference
|
||||
|
||||
- [Random Number Generation](#random-number-generation)
|
||||
- [Distribution Methods](#distribution-methods)
|
||||
- [Distribution Objects](#distribution-objects)
|
||||
- [Regression](#regression)
|
||||
- [Statistics](#statistics)
|
||||
|
||||
### Random Number Generation
|
||||
|
||||
<a name="random" href="#random">#</a>
|
||||
vega.<b>random</b>()
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/random.js "Source")
|
||||
|
||||
Returns a uniform pseudo-random number in the domain [0, 1). By default this is simply a call to JavaScript's built-in `Math.random` function. All Vega routines that require random numbers should use this function.
|
||||
|
||||
<a name="setRandom" href="#setRandom">#</a>
|
||||
vega.<b>setRandom</b>(<i>randfunc</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/random.js "Source")
|
||||
|
||||
Sets the random number generator to the provided function _randfunc_. Subsequent calls to <a href="#random">random</a> will invoke the new function to generate random numbers. Setting a custom generator can be helpful if one wishes to use an alternative source of randomness or replace the default generator with a deterministic function for testing purposes.
|
||||
|
||||
<a name="randomLCG" href="#randomLCG">#</a>
|
||||
vega.<b>randomLCG</b>(<i>seed</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lcg.js "Source")
|
||||
|
||||
Returns a new random number generator with the given random _seed_. The returned function takes zero arguments and generates random values in the domain [0, 1) using a [linear congruential generator (LCG)](https://en.wikipedia.org/wiki/Linear_congruential_generator). This method is helpful in conjunction with [setRandom](#setRandom) to provide seeded random numbers for stable outputs and testing.
|
||||
|
||||
### Distribution Methods
|
||||
|
||||
Methods for sampling and calculating values for probability distributions.
|
||||
|
||||
<a name="sampleNormal" href="#sampleNormal">#</a>
|
||||
vega.<b>sampleNormal</b>([<i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source")
|
||||
|
||||
Returns a sample from a univariate [normal (Gaussian) probability distribution](https://en.wikipedia.org/wiki/Normal_distribution) with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`.
|
||||
|
||||
<a name="cumulativeNormal" href="#cumulativeNormal">#</a>
|
||||
vega.<b>cumulativeNormal</b>(value[, <i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source")
|
||||
|
||||
Returns the value of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) at the given input domain *value* for a normal distribution with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`.
|
||||
|
||||
<a name="densityNormal" href="#densityNormal">#</a>
|
||||
vega.<b>densityNormal</b>(value[, <i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source")
|
||||
|
||||
Returns the value of the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) at the given input domain *value*, for a normal distribution with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`.
|
||||
|
||||
<a name="quantileNormal" href="#quantileNormal">#</a>
|
||||
vega.<b>quantileNormal</b>(probability[, <i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source")
|
||||
|
||||
Returns the quantile value (the inverse of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)) for the given input *probability*, for a normal distribution with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`.
|
||||
|
||||
<a name="sampleLogNormal" href="#sampleLogNormal">#</a>
|
||||
vega.<b>sampleLogNormal</b>([<i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source")
|
||||
|
||||
Returns a sample from a univariate [log-normal probability distribution](https://en.wikipedia.org/wiki/Log-normal_distribution) with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`.
|
||||
|
||||
<a name="cumulativeLogNormal" href="#cumulativeNormal">#</a>
|
||||
vega.<b>cumulativeLogNormal</b>(value[, <i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source")
|
||||
|
||||
Returns the value of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) at the given input domain *value* for a log-normal distribution with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`.
|
||||
|
||||
<a name="densityLogNormal" href="#densityLogNormal">#</a>
|
||||
vega.<b>densityLogNormal</b>(value[, <i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source")
|
||||
|
||||
Returns the value of the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) at the given input domain *value*, for a log-normal distribution with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`.
|
||||
|
||||
<a name="quantileLogNormal" href="#quantileLogNormal">#</a>
|
||||
vega.<b>quantileLogNormal</b>(probability[, <i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source")
|
||||
|
||||
Returns the quantile value (the inverse of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)) for the given input *probability*, for a log-normal distribution with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`.
|
||||
|
||||
<a name="sampleUniform" href="#sampleUniform">#</a>
|
||||
vega.<b>sampleUniform</b>([<i>min</i>, <i>max</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source")
|
||||
|
||||
Returns a sample from a univariate [continuous uniform probability distribution](https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)) over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value.
|
||||
|
||||
<a name="cumulativeUniform" href="#cumulativeUniform">#</a>
|
||||
vega.<b>cumulativeUniform</b>(value[, <i>min</i>, <i>max</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source")
|
||||
|
||||
Returns the value of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) at the given input domain *value* for a uniform distribution over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value.
|
||||
|
||||
<a name="densityUniform" href="#densityUniform">#</a>
|
||||
vega.<b>densityUniform</b>(value[, <i>min</i>, <i>max</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source")
|
||||
|
||||
Returns the value of the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) at the given input domain *value*, for a uniform distribution over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value.
|
||||
|
||||
<a name="quantileUniform" href="#quantileUniform">#</a>
|
||||
vega.<b>quantileUniform</b>(probability[, <i>min</i>, <i>max</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source")
|
||||
|
||||
Returns the quantile value (the inverse of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)) for the given input *probability*, for a uniform distribution over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value.
|
||||
|
||||
### Distribution Objects
|
||||
|
||||
Objects representing probability distributions, with methods for sampling and calculating values. Each method takes a set of distributional parameters and returns a distribution object representing a random variable.
|
||||
|
||||
Distribution objects expose the following methods:
|
||||
|
||||
- dist.<b>sample</b>(): Samples a random value drawn from this distribution.
|
||||
- dist.<b>pdf</b>(<i>value</i>): Calculates the value of the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) at the given input domain *value*.
|
||||
- dist.<b>cdf</b>(<i>value</i>): Calculates the value of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) at the given input domain *value*.
|
||||
- dist.<b>icdf</b>(<i>probability</i>): Calculates the inverse of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) for the given input *probability*.
|
||||
|
||||
<a name="randomNormal" href="#randomNormal">#</a>
|
||||
vega.<b>randomNormal</b>([<i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source")
|
||||
|
||||
Creates a distribution object representing a [normal (Gaussian) probability distribution](https://en.wikipedia.org/wiki/Normal_distribution) with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`.
|
||||
|
||||
Once created, *mean* and *stdev* values can be accessed or modified using the `mean` and `stdev` getter/setter methods.
|
||||
|
||||
<a name="randomLogNormal" href="#randomLogNormal">#</a>
|
||||
vega.<b>randomLogNormal</b>([<i>mean</i>, <i>stdev</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source")
|
||||
|
||||
Creates a distribution object representing a [log-normal probability distribution](https://en.wikipedia.org/wiki/Log-normal_distribution) with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`.
|
||||
|
||||
Once created, *mean* and *stdev* values can be accessed or modified using the `mean` and `stdev` getter/setter methods.
|
||||
|
||||
<a name="randomUniform" href="#randomUniform">#</a>
|
||||
vega.<b>randomUniform</b>([<i>min</i>, <i>max</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source")
|
||||
|
||||
Creates a distribution object representing a [continuous uniform probability distribution](https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)) over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value.
|
||||
|
||||
Once created, *min* and *max* values can be accessed or modified using the `min` and `max` getter/setter methods.
|
||||
|
||||
<a name="randomInteger" href="#randomInteger">#</a>
|
||||
vega.<b>randomInteger</b>([<i>min</i>,] <i>max</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/integer.js "Source")
|
||||
|
||||
Creates a distribution object representing a [discrete uniform probability distribution](https://en.wikipedia.org/wiki/Discrete_uniform_distribution) over the integer domain [*min*, *max*). If only one argument is provided, it is interpreted as the *max* value. If unspecified, *min* defaults to `0`.
|
||||
|
||||
Once created, *min* and *max* values can be accessed or modified using the `min` and `max` getter/setter methods.
|
||||
|
||||
<a name="randomMixture" href="#randomMixture">#</a>
|
||||
vega.<b>randomMixture</b>(<i>distributions</i>[, <i>weights</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/mixture.js "Source")
|
||||
|
||||
Creates a distribution object representing a (weighted) mixture of probability distributions. The *distributions* argument should be an array of distribution objects. The optional *weights* array provides proportional numerical weights for each distribution. If provided, the values in the *weights* array will be normalized to ensure that weights sum to 1. Any unspecified weight values default to `1` (prior to normalization). Mixture distributions do **not** support the `icdf` method: calling `icdf` will result in an error.
|
||||
|
||||
Once created, the *distributions* and *weights* arrays can be accessed or modified using the `distributions` and `weights` getter/setter methods.
|
||||
|
||||
<a name="randomKDE" href="#randomKDE">#</a>
|
||||
vega.<b>randomKDE</b>(<i>values</i>[, <i>bandwidth</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/kde.js "Source")
|
||||
|
||||
Creates a distribution object representing a [kernel density estimate](https://en.wikipedia.org/wiki/Kernel_density_estimation) for an array of numerical *values*. This method uses a Gaussian kernel to estimate a smoothed, continuous probability distribution. The optional *bandwidth* parameter determines the width of the Gaussian kernel. If the *bandwidth* is either `0` or unspecified, a default bandwidth value will be automatically estimated based on the input data. KDE distributions do **not** support the `icdf` method: calling `icdf` will result in an error.
|
||||
|
||||
Once created, *data* and *bandwidth* values can be accessed or modified using the `data` and `bandwidth` getter/setter methods.
|
||||
|
||||
### Regression
|
||||
|
||||
Two-dimensional regression methods to predict one variable given another.
|
||||
|
||||
<a name="regressionLinear" href="#regressionLinear">#</a>
|
||||
vega.<b>regressionLinear</b>(<i>data</i>, <i>x</i>, <i>y</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/regression/linear.js "Source")
|
||||
|
||||
Fit a linear regression model with functional form _y = a + b * x_ for the input *data* array and corresponding *x* and *y* accessor functions. Returns an object for the fit model parameters with the following properties:
|
||||
|
||||
- _coef_: An array of fitted coefficients of the form _[a, b]_.
|
||||
- _predict_: A function that returns a regression prediction for an input _x_ value.
|
||||
- _rSquared_: The R<sup>2</sup> [coefficient of determination](https://en.wikipedia.org/wiki/Coefficient_of_determination), indicating the amount of total variance of _y_ accounted for by the model.
|
||||
|
||||
<a name="regressionLog" href="#regressionLog">#</a>
|
||||
vega.<b>regressionLog</b>(<i>data</i>, <i>x</i>, <i>y</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/regression/log.js "Source")
|
||||
|
||||
Fit a logarithmic regression model with functional form _y = a + b * log(x)_ for the input input *data* array and corresponding *x* and *y* accessor functions.
|
||||
|
||||
Returns an object for the fit model parameters with the following properties:
|
||||
|
||||
- _coef_: An array of fitted coefficients of the form _[a, b]_.
|
||||
- _predict_: A function that returns a regression prediction for an input _x_ value.
|
||||
- _rSquared_: The R<sup>2</sup> [coefficient of determination](https://en.wikipedia.org/wiki/Coefficient_of_determination), indicating the amount of total variance of _y_ accounted for by the model.
|
||||
|
||||
<a name="regressionExp" href="#regressionExp">#</a>
|
||||
vega.<b>regressionExp</b>(<i>data</i>, <i>x</i>, <i>y</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/regression/exp.js "Source")
|
||||
|
||||
Fit an exponential regression model with functional form _y = a + e<sup>b * x</sup>_ for the input *data* array and corresponding *x* and *y* accessor functions. Returns an object for the fit model parameters with the following properties:
|
||||
|
||||
- _coef_: An array of fitted coefficients of the form _[a, b]_.
|
||||
- _predict_: A function that returns a regression prediction for an input _x_ value.
|
||||
- _rSquared_: The R<sup>2</sup> [coefficient of determination](https://en.wikipedia.org/wiki/Coefficient_of_determination), indicating the amount of total variance of _y_ accounted for by the model.
|
||||
|
||||
<a name="regressionPow" href="#regressionPow">#</a>
|
||||
vega.<b>regressionPow</b>(<i>data</i>, <i>x</i>, <i>y</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/regression/pow.js "Source")
|
||||
|
||||
Fit a power law regression model with functional form _y = a * x<sup>b</sup>_ for the input *data* array and corresponding *x* and *y* accessor functions. Returns an object for the fit model parameters with the following properties:
|
||||
|
||||
- _coef_: An array of fitted coefficients of the form _[a, b]_.
|
||||
- _predict_: A function that returns a regression prediction for an input _x_ value.
|
||||
- _rSquared_: The R<sup>2</sup> [coefficient of determination](https://en.wikipedia.org/wiki/Coefficient_of_determination), indicating the amount of total variance of _y_ accounted for by the model.
|
||||
|
||||
<a name="regressionQuad" href="#regressionQuad">#</a>
|
||||
vega.<b>regressionLinear</b>(<i>data</i>, <i>x</i>, <i>y</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/regression/quad.js "Source")
|
||||
|
||||
Fit a quadratic regression model with functional form _y = a + b * x + c * x<sup>2</sup>_ for the input *data* array and corresponding *x* and *y* accessor functions. Returns an object for the fit model parameters with the following properties:
|
||||
|
||||
- _coef_: An array of fitted coefficients of the form _[a, b, c]_,
|
||||
- _predict_: A function that returns a regression prediction for an input _x_ value.
|
||||
- _rSquared_: The R<sup>2</sup> [coefficient of determination](https://en.wikipedia.org/wiki/Coefficient_of_determination), indicating the amount of total variance of _y_ accounted for by the model.
|
||||
|
||||
<a name="regressionPoly" href="#regressionPoly">#</a>
|
||||
vega.<b>regressionPoly</b>(<i>data</i>, <i>x</i>, <i>y</i>, <i>order</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/regression/poly.js "Source")
|
||||
|
||||
Fit a polynomial regression model of specified _order_ with functional form _y = a + b * x + ... + k * x<sup>order</sup>_ for the input *data* array and corresponding *x* and *y* accessor functions. Returns an object for the fit model parameters with the following properties:
|
||||
|
||||
- _coef_: An _(order + 1)_-length array of polynomial coefficients of the form _[a, b, c, d, ...]_.
|
||||
- _predict_: A function that returns a regression prediction for an input _x_ value.
|
||||
- _rSquared_: The R<sup>2</sup> [coefficient of determination](https://en.wikipedia.org/wiki/Coefficient_of_determination), indicating the amount of total variance of _y_ accounted for by the model.
|
||||
|
||||
<a name="regressionLoess" href="#regressionLoess">#</a>
|
||||
vega.<b>regressionLoess</b>(<i>data</i>, <i>x</i>, <i>y</i>, <i>bandwidth</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/regression/loess.js "Source")
|
||||
|
||||
Fit a smoothed, non-parametric trend line the input *data* array and corresponding *x* and *y* accessor functions using _loess_ (locally-estimated scatterplot smoothing). Loess performs a sequence of local weighted regressions over a sliding window of nearest-neighbor points. The _bandwidth_ argument determines the size of the sliding window, expressed as a [0, 1] fraction of the total number of data points included.
|
||||
|
||||
<a name="sampleCurve" href="#sampleCurve">#</a>
|
||||
vega.<b>sampleCurve</b>(<i>f</i>, <i>extent</i>[, <i>minSteps</i>, <i>maxSteps</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/sampleCurve.js "Source")
|
||||
|
||||
Generate sample points from an interpolation function _f_ for the provided domain _extent_ and return an array of _[x, y]_ points. Performs adaptive subdivision to dynamically sample more points in regions of higher curvature. Subdivision stops when the difference in angles between the current samples and a proposed subdivision falls below one-quarter of a degree. The optional _minSteps_ argument (default 25), determines the minimal number of initial, uniformly-spaced sample points to draw. The optional _maxSteps_ argument (default 200), indicates the maximum resolution at which adaptive sampling will stop, defined relative to a uniform grid of size _maxSteps_. If _minSteps_ and _maxSteps_ are identical, no adaptive sampling will be performed and only the initial, uniformly-spaced samples will be returned.
|
||||
|
||||
### Statistics
|
||||
|
||||
Statistical methods for bandwidth estimation, bin calculation, bootstrapped confidence intervals, and quartile boundaries.
|
||||
|
||||
<a name="bandwidthNRD" href="#bandwidthNRD">#</a>
|
||||
vega.<b>bandwidthNRD</b>(<i>array</i>[, <i>accessor</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/bandwidth.js "Source")
|
||||
|
||||
Given an *array* of numeric values, estimates a bandwidth value for use in Gaussian kernel density estimation, assuming a normal reference distribution. The underlying formula (from Scott 1992) is 1.06 times the minimum of the standard deviation and the interquartile range divided by 1.34 times the sample size to the negative one-fifth power, along with special case handling in case of zero values for the interquartile range or deviation. An optional *accessor* function can be used to first extract numerical values from an array of input objects, and is equivalent to first calling `array.map(accessor)`.
|
||||
|
||||
<a name="bin" href="#bin">#</a>
|
||||
vega.<b>bin</b>(<i>options</i>)
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/bin.js "Source")
|
||||
|
||||
Determine a quantitative binning scheme, for example to create a histogram. Based on the options provided given, this method will search over a space of possible bins, aligning step sizes with a given number base and applying constraints such as the maximum number of allowable bins. Given a set of options (see below), returns an object describing the binning scheme, in terms of `start`, `stop`, and `step` properties.
|
||||
|
||||
The supported options properties are:
|
||||
|
||||
- _extent_: (required) A two-element (`[min, max]`) array indicating the range over which the bin values are defined.
|
||||
- _base_: The number base to use for automatic bin determination (default base `10`).
|
||||
- _maxbins_: The maximum number of allowable bins (default `20`).
|
||||
- _span_: The value span over which to generate bin boundaries. The default is `extent[1] - extent[0]`. This parameter allows automatic step size determination over custom spans (for example, a zoomed-in region) while retaining the overall _extent_.
|
||||
- _step_: An exact step size to use between bins. If provided, the _maxbins_, _span_, and _steps_ options will be ignored.
|
||||
- _steps_: An array of allowable step sizes to choose from. If provided, the _maxbins_ option will be ignored.
|
||||
- _minstep_: A minimum allowable step size (particularly useful for integer values, default `0`).
|
||||
- _divide_: An array of scale factors indicating allowable subdivisions. The default value is `[5, 2]`, which indicates that the method may consider dividing bin sizes by 5 and/or 2. For example, for an initial step size of 10, the method can check if bin sizes of 2 (= 10/5), 5 (= 10/2), or 1 (= 10/(5*2)) might also satisfy the given constraints.
|
||||
- _nice_: Boolean indicating if the start and stop values should be nicely-rounded relative to the step size (default `true`).
|
||||
|
||||
```js
|
||||
vega.bin({extent:[0, 1], maxbins:10}); // {start:0, stop:1, step:0.1}
|
||||
vega.bin({extent:[0, 1], maxbins:5}); // {start:0, stop:10, step:2}
|
||||
vega.bin({extent:[5, 10], maxbins:5}); // {start:5, stop:10, step:1}
|
||||
```
|
||||
|
||||
<a name="bootstrapCI" href="#bootstrapCI">#</a>
|
||||
vega.<b>bootstrapCI</b>(<i>array</i>, <i>samples</i>, <i>alpha</i>[, <i>accessor</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/bootstrapCI.js "Source")
|
||||
|
||||
Calculates a [bootstrapped](https://en.wikipedia.org/wiki/Bootstrapping_(statistics)) [confidence interval](https://en.wikipedia.org/wiki/Confidence_interval) for an input *array* of values, based on a given number of *samples* iterations and a target *alpha* value. For example, an *alpha* value of `0.05` corresponds to a 95% confidence interval An optional *accessor* function can be used to first extract numerical values from an array of input objects, and is equivalent to first calling `array.map(accessor)`. This method ignores null, undefined, and NaN values.
|
||||
|
||||
<a name="dotbin" href="#dotbin">#</a>
|
||||
vega.<b>dotbin</b>(<i>sortedArray</i>, <i>step</i>[, <i>smooth</i>, <i>accessor</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/dotbin.js "Source")
|
||||
|
||||
Calculates [dot plot](https://en.wikipedia.org/wiki/Dot_plot_%28statistics%29) bin locations for an input *sortedArray* of numerical values, and returns an array of bin locations with indices matching the input *sortedArray*. This method implements the ["dot density" algorithm of Wilkinson, 1999](https://www.cs.uic.edu/~wilkinson/Publications/dotplots.pdf). The *step* parameter determines the bin width: points within *step* values of an anchor point will be assigned the same bin location. The optional *smooth* parameter is a boolean value indicating if the bin locations should additionally be smoothed to reduce variance. An optional *accessor* function can be used to first extract numerical values from an array of input objects, and is equivalent to first calling `array.map(accessor)`. Any null, undefined, or NaN values should be removed prior to calling this method.
|
||||
|
||||
<a name="quantiles" href="#quartiles">#</a>
|
||||
vega.<b>quantiles</b>(<i>array</i>, <i>p</i>[, <i>accessor</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/quartiles.js "Source")
|
||||
|
||||
Given an *array* of numeric values and array *p* of probability thresholds in the range [0, 1], returns an array of p-[quantiles](https://en.wikipedia.org/wiki/Quantile). The return value is a array the same length as the input *p*. An optional *accessor* function can be used to first extract numerical values from an array of input objects, and is equivalent to first calling `array.map(accessor)`. This method ignores null, undefined and NaN values.
|
||||
|
||||
<a name="quartiles" href="#quartiles">#</a>
|
||||
vega.<b>quartiles</b>(<i>array</i>[, <i>accessor</i>])
|
||||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/quartiles.js "Source")
|
||||
|
||||
Given an *array* of numeric values, returns an array of [quartile](https://en.wikipedia.org/wiki/Quartile) boundaries. The return value is a 3-element array consisting of the first, second (median), and third quartile boundaries. An optional *accessor* function can be used to first extract numerical values from an array of input objects, and is equivalent to first calling `array.map(accessor)`. This method ignores null, undefined and NaN values.
|
||||
1215
node_modules/vega-statistics/build/vega-statistics.js
generated
vendored
Normal file
1215
node_modules/vega-statistics/build/vega-statistics.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1
node_modules/vega-statistics/build/vega-statistics.min.js
generated
vendored
Normal file
1
node_modules/vega-statistics/build/vega-statistics.min.js
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
40
node_modules/vega-statistics/index.js
generated
vendored
Normal file
40
node_modules/vega-statistics/index.js
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
export {default as bandwidthNRD} from './src/bandwidth';
|
||||
export {default as bin} from './src/bin';
|
||||
export {default as bootstrapCI} from './src/bootstrapCI';
|
||||
export {default as dotbin} from './src/dotbin';
|
||||
export {default as quantiles} from './src/quantiles';
|
||||
export {default as quartiles} from './src/quartiles';
|
||||
export {random, setRandom} from './src/random';
|
||||
export {default as randomLCG} from './src/lcg';
|
||||
export {default as randomInteger} from './src/integer';
|
||||
export {default as randomKDE} from './src/kde';
|
||||
export {
|
||||
default as randomLogNormal,
|
||||
sampleLogNormal,
|
||||
densityLogNormal,
|
||||
cumulativeLogNormal,
|
||||
quantileLogNormal
|
||||
} from './src/lognormal';
|
||||
export {default as randomMixture} from './src/mixture';
|
||||
export {
|
||||
default as randomNormal,
|
||||
sampleNormal,
|
||||
densityNormal,
|
||||
cumulativeNormal,
|
||||
quantileNormal
|
||||
} from './src/normal';
|
||||
export {
|
||||
default as randomUniform,
|
||||
sampleUniform,
|
||||
densityUniform,
|
||||
cumulativeUniform,
|
||||
quantileUniform
|
||||
} from './src/uniform';
|
||||
export {default as regressionLinear} from './src/regression/linear';
|
||||
export {default as regressionLog} from './src/regression/log';
|
||||
export {default as regressionExp} from './src/regression/exp';
|
||||
export {default as regressionPow} from './src/regression/pow';
|
||||
export {default as regressionQuad} from './src/regression/quad';
|
||||
export {default as regressionPoly} from './src/regression/poly';
|
||||
export {default as regressionLoess} from './src/regression/loess';
|
||||
export {default as sampleCurve} from './src/sampleCurve';
|
||||
70
node_modules/vega-statistics/package.json
generated
vendored
Normal file
70
node_modules/vega-statistics/package.json
generated
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"_from": "vega-statistics@~1.7.6",
|
||||
"_id": "vega-statistics@1.7.6",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-w6z5t4p1zYNSvsg3lln4TZakxXlH/tM0w5WAP1EXLYrCYRw0F/SvxqLQ+WqEZVnI/WGQDq2v5xMAn0WvHJ/kUg==",
|
||||
"_location": "/vega-statistics",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "vega-statistics@~1.7.6",
|
||||
"name": "vega-statistics",
|
||||
"escapedName": "vega-statistics",
|
||||
"rawSpec": "~1.7.6",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "~1.7.6"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/vega",
|
||||
"/vega-functions",
|
||||
"/vega-geo",
|
||||
"/vega-regression",
|
||||
"/vega-transforms",
|
||||
"/vega-wordcloud"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/vega-statistics/-/vega-statistics-1.7.6.tgz",
|
||||
"_shasum": "6f920c83adbec9a25087ca60c41af272838f85bd",
|
||||
"_spec": "vega-statistics@~1.7.6",
|
||||
"_where": "/home/prabhatdev/Documents/opensource/gitHubStats/waka-readme-stats/node_modules/vega",
|
||||
"author": {
|
||||
"name": "Jeffrey Heer",
|
||||
"url": "http://idl.cs.washington.edu"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/vega/vega/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"d3-array": "^2.4.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Statistical routines and probability distributions.",
|
||||
"gitHead": "62565bbe084a422c4a0cbc6e19c6f7c45a3e5137",
|
||||
"homepage": "https://github.com/vega/vega#readme",
|
||||
"keywords": [
|
||||
"vega",
|
||||
"statistics",
|
||||
"probability",
|
||||
"distribution"
|
||||
],
|
||||
"license": "BSD-3-Clause",
|
||||
"main": "build/vega-statistics.js",
|
||||
"module": "index",
|
||||
"name": "vega-statistics",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/vega/vega.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "yarn rollup",
|
||||
"postbuild": "terser build/vega-statistics.js -c -m -o build/vega-statistics.min.js",
|
||||
"postpublish": "git push && git push --tags",
|
||||
"prebuild": "rimraf build && mkdir build",
|
||||
"prepublishOnly": "yarn test && yarn build",
|
||||
"pretest": "yarn prebuild && yarn rollup",
|
||||
"rollup": "rollup -g d3-array:d3 -f umd -n vega -o build/vega-statistics.js -- index.js",
|
||||
"test": "tape 'test/**/*-test.js'"
|
||||
},
|
||||
"version": "1.7.6"
|
||||
}
|
||||
15
node_modules/vega-statistics/src/bandwidth.js
generated
vendored
Normal file
15
node_modules/vega-statistics/src/bandwidth.js
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
import quartiles from './quartiles';
|
||||
import {deviation} from 'd3-array';
|
||||
|
||||
// Scott, D. W. (1992) Multivariate Density Estimation:
|
||||
// Theory, Practice, and Visualization. Wiley.
|
||||
export default function(array, f) {
|
||||
var n = array.length,
|
||||
v = deviation(array, f),
|
||||
q = quartiles(array, f),
|
||||
h = (q[2] - q[0]) / 1.34;
|
||||
|
||||
v = Math.min(v, h) || v || Math.abs(q[0]) || 1;
|
||||
|
||||
return 1.06 * v * Math.pow(n, -0.2);
|
||||
}
|
||||
54
node_modules/vega-statistics/src/bin.js
generated
vendored
Normal file
54
node_modules/vega-statistics/src/bin.js
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
export default function(_) {
|
||||
// determine range
|
||||
var maxb = _.maxbins || 20,
|
||||
base = _.base || 10,
|
||||
logb = Math.log(base),
|
||||
div = _.divide || [5, 2],
|
||||
min = _.extent[0],
|
||||
max = _.extent[1],
|
||||
span = _.span || (max - min) || Math.abs(min) || 1,
|
||||
step, level, minstep, precision, v, i, n, eps;
|
||||
|
||||
if (_.step) {
|
||||
// if step size is explicitly given, use that
|
||||
step = _.step;
|
||||
} else if (_.steps) {
|
||||
// if provided, limit choice to acceptable step sizes
|
||||
v = span / maxb;
|
||||
for (i=0, n=_.steps.length; i < n && _.steps[i] < v; ++i);
|
||||
step = _.steps[Math.max(0, i-1)];
|
||||
} else {
|
||||
// else use span to determine step size
|
||||
level = Math.ceil(Math.log(maxb) / logb);
|
||||
minstep = _.minstep || 0;
|
||||
step = Math.max(
|
||||
minstep,
|
||||
Math.pow(base, Math.round(Math.log(span) / logb) - level)
|
||||
);
|
||||
|
||||
// increase step size if too many bins
|
||||
while (Math.ceil(span/step) > maxb) { step *= base; }
|
||||
|
||||
// decrease step size if allowed
|
||||
for (i=0, n=div.length; i<n; ++i) {
|
||||
v = step / div[i];
|
||||
if (v >= minstep && span / v <= maxb) step = v;
|
||||
}
|
||||
}
|
||||
|
||||
// update precision, min and max
|
||||
v = Math.log(step);
|
||||
precision = v >= 0 ? 0 : ~~(-v / logb) + 1;
|
||||
eps = Math.pow(base, -precision - 1);
|
||||
if (_.nice || _.nice === undefined) {
|
||||
v = Math.floor(min / step + eps) * step;
|
||||
min = min < v ? v - step : v;
|
||||
max = Math.ceil(max / step) * step;
|
||||
}
|
||||
|
||||
return {
|
||||
start: min,
|
||||
stop: max === min ? min + step : max,
|
||||
step: step
|
||||
};
|
||||
}
|
||||
26
node_modules/vega-statistics/src/bootstrapCI.js
generated
vendored
Normal file
26
node_modules/vega-statistics/src/bootstrapCI.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
import numbers from './numbers';
|
||||
import {random} from './random';
|
||||
import {ascending, quantile} from 'd3-array';
|
||||
|
||||
export default function(array, samples, alpha, f) {
|
||||
if (!array.length) return [undefined, undefined];
|
||||
|
||||
var values = Float64Array.from(numbers(array, f)),
|
||||
n = values.length,
|
||||
m = samples,
|
||||
a, i, j, mu;
|
||||
|
||||
for (j=0, mu=Array(m); j<m; ++j) {
|
||||
for (a=0, i=0; i<n; ++i) {
|
||||
a += values[~~(random() * n)];
|
||||
}
|
||||
mu[j] = a / n;
|
||||
}
|
||||
|
||||
mu.sort(ascending);
|
||||
|
||||
return [
|
||||
quantile(mu, alpha/2),
|
||||
quantile(mu, 1-(alpha/2))
|
||||
];
|
||||
}
|
||||
2
node_modules/vega-statistics/src/constants.js
generated
vendored
Normal file
2
node_modules/vega-statistics/src/constants.js
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export const SQRT2PI = Math.sqrt(2 * Math.PI);
|
||||
export const SQRT2 = Math.SQRT2;
|
||||
63
node_modules/vega-statistics/src/dotbin.js
generated
vendored
Normal file
63
node_modules/vega-statistics/src/dotbin.js
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
// Dot density binning for dot plot construction.
|
||||
// Based on Leland Wilkinson, Dot Plots, The American Statistician, 1999.
|
||||
// https://www.cs.uic.edu/~wilkinson/Publications/dotplots.pdf
|
||||
export default function(array, step, smooth, f) {
|
||||
f = f || (_ => _);
|
||||
|
||||
let i = 0, j = 1,
|
||||
n = array.length,
|
||||
v = new Float64Array(n),
|
||||
a = f(array[0]),
|
||||
b = a,
|
||||
w = a + step,
|
||||
x;
|
||||
|
||||
for (; j<n; ++j) {
|
||||
x = f(array[j]);
|
||||
if (x >= w) {
|
||||
b = (a + b) / 2;
|
||||
for (; i<j; ++i) v[i] = b;
|
||||
w = x + step;
|
||||
a = x;
|
||||
}
|
||||
b = x;
|
||||
}
|
||||
|
||||
b = (a + b) / 2;
|
||||
for (; i<j; ++i) v[i] = b;
|
||||
|
||||
return smooth ? smoothing(v, step + step / 4) : v;
|
||||
}
|
||||
|
||||
// perform smoothing to reduce variance
|
||||
// swap points between "adjacent" stacks
|
||||
// Wilkinson defines adjacent as within step/4 units
|
||||
function smoothing(v, thresh) {
|
||||
let n = v.length,
|
||||
a = 0,
|
||||
b = 1,
|
||||
c, d;
|
||||
|
||||
// get left stack
|
||||
while (v[a] === v[b]) ++b;
|
||||
|
||||
while (b < n) {
|
||||
// get right stack
|
||||
c = b + 1;
|
||||
while (v[b] === v[c]) ++c;
|
||||
|
||||
// are stacks adjacent?
|
||||
// if so, compare sizes and swap as needed
|
||||
if (v[b] - v[b-1] < thresh) {
|
||||
d = b + ((a + c - b - b) >> 1);
|
||||
while (d < b) v[d++] = v[b];
|
||||
while (d > b) v[d--] = v[a];
|
||||
}
|
||||
|
||||
// update left stack indices
|
||||
a = b;
|
||||
b = c;
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
50
node_modules/vega-statistics/src/integer.js
generated
vendored
Normal file
50
node_modules/vega-statistics/src/integer.js
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
import {random} from './random';
|
||||
|
||||
export default function(min, max) {
|
||||
if (max == null) {
|
||||
max = min;
|
||||
min = 0;
|
||||
}
|
||||
|
||||
var dist = {},
|
||||
a, b, d;
|
||||
|
||||
dist.min = function(_) {
|
||||
if (arguments.length) {
|
||||
a = _ || 0;
|
||||
d = b - a;
|
||||
return dist;
|
||||
} else {
|
||||
return a;
|
||||
}
|
||||
};
|
||||
|
||||
dist.max = function(_) {
|
||||
if (arguments.length) {
|
||||
b = _ || 0;
|
||||
d = b - a;
|
||||
return dist;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
};
|
||||
|
||||
dist.sample = function() {
|
||||
return a + Math.floor(d * random());
|
||||
};
|
||||
|
||||
dist.pdf = function(x) {
|
||||
return (x === Math.floor(x) && x >= a && x < b) ? 1 / d : 0;
|
||||
};
|
||||
|
||||
dist.cdf = function(x) {
|
||||
var v = Math.floor(x);
|
||||
return v < a ? 0 : v >= b ? 1 : (v - a + 1) / d;
|
||||
};
|
||||
|
||||
dist.icdf = function(p) {
|
||||
return (p >= 0 && p <= 1) ? a - 1 + Math.floor(p * d) : NaN;
|
||||
};
|
||||
|
||||
return dist.min(min).max(max);
|
||||
}
|
||||
51
node_modules/vega-statistics/src/kde.js
generated
vendored
Normal file
51
node_modules/vega-statistics/src/kde.js
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
import estimateBandwidth from './bandwidth';
|
||||
import gaussian from './normal';
|
||||
import {random} from './random';
|
||||
|
||||
// TODO: support for additional kernels?
|
||||
export default function(support, bandwidth) {
|
||||
var kernel = gaussian(),
|
||||
dist = {},
|
||||
n = 0;
|
||||
|
||||
dist.data = function(_) {
|
||||
if (arguments.length) {
|
||||
support = _;
|
||||
n = _ ? _.length : 0;
|
||||
return dist.bandwidth(bandwidth);
|
||||
} else {
|
||||
return support;
|
||||
}
|
||||
};
|
||||
|
||||
dist.bandwidth = function(_) {
|
||||
if (!arguments.length) return bandwidth;
|
||||
bandwidth = _;
|
||||
if (!bandwidth && support) bandwidth = estimateBandwidth(support);
|
||||
return dist;
|
||||
};
|
||||
|
||||
dist.sample = function() {
|
||||
return support[~~(random() * n)] + bandwidth * kernel.sample();
|
||||
};
|
||||
|
||||
dist.pdf = function(x) {
|
||||
for (var y=0, i=0; i<n; ++i) {
|
||||
y += kernel.pdf((x - support[i]) / bandwidth);
|
||||
}
|
||||
return y / bandwidth / n;
|
||||
};
|
||||
|
||||
dist.cdf = function(x) {
|
||||
for (var y=0, i=0; i<n; ++i) {
|
||||
y += kernel.cdf((x - support[i]) / bandwidth);
|
||||
}
|
||||
return y / n;
|
||||
};
|
||||
|
||||
dist.icdf = function() {
|
||||
throw Error('KDE icdf not supported.');
|
||||
};
|
||||
|
||||
return dist.data(support);
|
||||
}
|
||||
8
node_modules/vega-statistics/src/lcg.js
generated
vendored
Normal file
8
node_modules/vega-statistics/src/lcg.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
export default function(seed) {
|
||||
// Random numbers using a Linear Congruential Generator with seed value
|
||||
// Uses glibc values from https://en.wikipedia.org/wiki/Linear_congruential_generator
|
||||
return function() {
|
||||
seed = (1103515245 * seed + 12345) % 2147483647;
|
||||
return seed / 2147483647;
|
||||
};
|
||||
}
|
||||
53
node_modules/vega-statistics/src/lognormal.js
generated
vendored
Normal file
53
node_modules/vega-statistics/src/lognormal.js
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
import {cumulativeNormal, quantileNormal, sampleNormal} from './normal';
|
||||
import {SQRT2PI} from './constants';
|
||||
|
||||
export function sampleLogNormal(mean, stdev) {
|
||||
mean = mean || 0;
|
||||
stdev = stdev == null ? 1 : stdev;
|
||||
return Math.exp(mean + sampleNormal() * stdev);
|
||||
}
|
||||
|
||||
export function densityLogNormal(value, mean, stdev) {
|
||||
if (value <= 0) return 0;
|
||||
mean = mean || 0;
|
||||
stdev = stdev == null ? 1 : stdev;
|
||||
const z = (Math.log(value) - mean) / stdev;
|
||||
return Math.exp(-0.5 * z * z) / (stdev * SQRT2PI * value);
|
||||
}
|
||||
|
||||
export function cumulativeLogNormal(value, mean, stdev) {
|
||||
return cumulativeNormal(Math.log(value), mean, stdev);
|
||||
}
|
||||
|
||||
export function quantileLogNormal(p, mean, stdev) {
|
||||
return Math.exp(quantileNormal(p, mean, stdev));
|
||||
}
|
||||
|
||||
export default function(mean, stdev) {
|
||||
var mu,
|
||||
sigma,
|
||||
dist = {
|
||||
mean: function(_) {
|
||||
if (arguments.length) {
|
||||
mu = _ || 0;
|
||||
return dist;
|
||||
} else {
|
||||
return mu;
|
||||
}
|
||||
},
|
||||
stdev: function(_) {
|
||||
if (arguments.length) {
|
||||
sigma = _ == null ? 1 : _;
|
||||
return dist;
|
||||
} else {
|
||||
return sigma;
|
||||
}
|
||||
},
|
||||
sample: () => sampleLogNormal(mu, sigma),
|
||||
pdf: value => densityLogNormal(value, mu, sigma),
|
||||
cdf: value => cumulativeLogNormal(value, mu, sigma),
|
||||
icdf: p => quantileLogNormal(p, mu, sigma)
|
||||
};
|
||||
|
||||
return dist.mean(mean).stdev(stdev);
|
||||
}
|
||||
68
node_modules/vega-statistics/src/mixture.js
generated
vendored
Normal file
68
node_modules/vega-statistics/src/mixture.js
generated
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
import {random} from './random';
|
||||
|
||||
export default function(dists, weights) {
|
||||
var dist = {}, m = 0, w;
|
||||
|
||||
function normalize(x) {
|
||||
var w = [], sum = 0, i;
|
||||
for (i=0; i<m; ++i) { sum += (w[i] = (x[i]==null ? 1 : +x[i])); }
|
||||
for (i=0; i<m; ++i) { w[i] /= sum; }
|
||||
return w;
|
||||
}
|
||||
|
||||
dist.weights = function(_) {
|
||||
if (arguments.length) {
|
||||
w = normalize(weights = (_ || []));
|
||||
return dist;
|
||||
}
|
||||
return weights;
|
||||
};
|
||||
|
||||
dist.distributions = function(_) {
|
||||
if (arguments.length) {
|
||||
if (_) {
|
||||
m = _.length;
|
||||
dists = _;
|
||||
} else {
|
||||
m = 0;
|
||||
dists = [];
|
||||
}
|
||||
return dist.weights(weights);
|
||||
}
|
||||
return dists;
|
||||
};
|
||||
|
||||
dist.sample = function() {
|
||||
var r = random(),
|
||||
d = dists[m-1],
|
||||
v = w[0],
|
||||
i = 0;
|
||||
|
||||
// first select distribution
|
||||
for (; i<m-1; v += w[++i]) {
|
||||
if (r < v) { d = dists[i]; break; }
|
||||
}
|
||||
// then sample from it
|
||||
return d.sample();
|
||||
};
|
||||
|
||||
dist.pdf = function(x) {
|
||||
for (var p=0, i=0; i<m; ++i) {
|
||||
p += w[i] * dists[i].pdf(x);
|
||||
}
|
||||
return p;
|
||||
};
|
||||
|
||||
dist.cdf = function(x) {
|
||||
for (var p=0, i=0; i<m; ++i) {
|
||||
p += w[i] * dists[i].cdf(x);
|
||||
}
|
||||
return p;
|
||||
};
|
||||
|
||||
dist.icdf = function() {
|
||||
throw Error('Mixture icdf not supported.');
|
||||
};
|
||||
|
||||
return dist.distributions(dists).weights(weights);
|
||||
}
|
||||
190
node_modules/vega-statistics/src/normal.js
generated
vendored
Normal file
190
node_modules/vega-statistics/src/normal.js
generated
vendored
Normal file
@@ -0,0 +1,190 @@
|
||||
import {SQRT2, SQRT2PI} from './constants';
|
||||
import {random} from './random';
|
||||
|
||||
let nextSample = NaN;
|
||||
|
||||
export function sampleNormal(mean, stdev) {
|
||||
mean = mean || 0;
|
||||
stdev = stdev == null ? 1 : stdev;
|
||||
|
||||
let x = 0, y = 0, rds, c;
|
||||
if (nextSample === nextSample) {
|
||||
x = nextSample;
|
||||
nextSample = NaN;
|
||||
} else {
|
||||
do {
|
||||
x = random() * 2 - 1;
|
||||
y = random() * 2 - 1;
|
||||
rds = x * x + y * y;
|
||||
} while (rds === 0 || rds > 1);
|
||||
c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform
|
||||
x *= c;
|
||||
nextSample = y * c;
|
||||
}
|
||||
return mean + x * stdev;
|
||||
}
|
||||
|
||||
export function densityNormal(value, mean, stdev) {
|
||||
stdev = stdev == null ? 1 : stdev;
|
||||
const z = (value - (mean || 0)) / stdev;
|
||||
return Math.exp(-0.5 * z * z) / (stdev * SQRT2PI);
|
||||
}
|
||||
|
||||
// Approximation from West (2009)
|
||||
// Better Approximations to Cumulative Normal Functions
|
||||
export function cumulativeNormal(value, mean, stdev) {
|
||||
mean = mean || 0;
|
||||
stdev = stdev == null ? 1 : stdev;
|
||||
|
||||
let cd,
|
||||
z = (value - mean) / stdev,
|
||||
Z = Math.abs(z);
|
||||
|
||||
if (Z > 37) {
|
||||
cd = 0;
|
||||
} else {
|
||||
let sum, exp = Math.exp(-Z * Z / 2);
|
||||
if (Z < 7.07106781186547) {
|
||||
sum = 3.52624965998911e-02 * Z + 0.700383064443688;
|
||||
sum = sum * Z + 6.37396220353165;
|
||||
sum = sum * Z + 33.912866078383;
|
||||
sum = sum * Z + 112.079291497871;
|
||||
sum = sum * Z + 221.213596169931;
|
||||
sum = sum * Z + 220.206867912376;
|
||||
cd = exp * sum;
|
||||
sum = 8.83883476483184e-02 * Z + 1.75566716318264;
|
||||
sum = sum * Z + 16.064177579207;
|
||||
sum = sum * Z + 86.7807322029461;
|
||||
sum = sum * Z + 296.564248779674;
|
||||
sum = sum * Z + 637.333633378831;
|
||||
sum = sum * Z + 793.826512519948;
|
||||
sum = sum * Z + 440.413735824752;
|
||||
cd = cd / sum;
|
||||
} else {
|
||||
sum = Z + 0.65;
|
||||
sum = Z + 4 / sum;
|
||||
sum = Z + 3 / sum;
|
||||
sum = Z + 2 / sum;
|
||||
sum = Z + 1 / sum;
|
||||
cd = exp / sum / 2.506628274631;
|
||||
}
|
||||
}
|
||||
return z > 0 ? 1 - cd : cd;
|
||||
}
|
||||
|
||||
// Approximation of Probit function using inverse error function.
|
||||
export function quantileNormal(p, mean, stdev) {
|
||||
if (p < 0 || p > 1) return NaN;
|
||||
return (mean || 0) + (stdev == null ? 1 : stdev) * SQRT2 * erfinv(2 * p - 1);
|
||||
}
|
||||
|
||||
// Approximate inverse error function. Implementation from "Approximating
|
||||
// the erfinv function" by Mike Giles, GPU Computing Gems, volume 2, 2010.
|
||||
// Ported from Apache Commons Math, http://www.apache.org/licenses/LICENSE-2.0
|
||||
function erfinv(x) {
|
||||
// beware that the logarithm argument must be
|
||||
// commputed as (1.0 - x) * (1.0 + x),
|
||||
// it must NOT be simplified as 1.0 - x * x as this
|
||||
// would induce rounding errors near the boundaries +/-1
|
||||
let w = - Math.log((1 - x) * (1 + x)), p;
|
||||
|
||||
if (w < 6.25) {
|
||||
w -= 3.125;
|
||||
p = -3.6444120640178196996e-21;
|
||||
p = -1.685059138182016589e-19 + p * w;
|
||||
p = 1.2858480715256400167e-18 + p * w;
|
||||
p = 1.115787767802518096e-17 + p * w;
|
||||
p = -1.333171662854620906e-16 + p * w;
|
||||
p = 2.0972767875968561637e-17 + p * w;
|
||||
p = 6.6376381343583238325e-15 + p * w;
|
||||
p = -4.0545662729752068639e-14 + p * w;
|
||||
p = -8.1519341976054721522e-14 + p * w;
|
||||
p = 2.6335093153082322977e-12 + p * w;
|
||||
p = -1.2975133253453532498e-11 + p * w;
|
||||
p = -5.4154120542946279317e-11 + p * w;
|
||||
p = 1.051212273321532285e-09 + p * w;
|
||||
p = -4.1126339803469836976e-09 + p * w;
|
||||
p = -2.9070369957882005086e-08 + p * w;
|
||||
p = 4.2347877827932403518e-07 + p * w;
|
||||
p = -1.3654692000834678645e-06 + p * w;
|
||||
p = -1.3882523362786468719e-05 + p * w;
|
||||
p = 0.0001867342080340571352 + p * w;
|
||||
p = -0.00074070253416626697512 + p * w;
|
||||
p = -0.0060336708714301490533 + p * w;
|
||||
p = 0.24015818242558961693 + p * w;
|
||||
p = 1.6536545626831027356 + p * w;
|
||||
} else if (w < 16.0) {
|
||||
w = Math.sqrt(w) - 3.25;
|
||||
p = 2.2137376921775787049e-09;
|
||||
p = 9.0756561938885390979e-08 + p * w;
|
||||
p = -2.7517406297064545428e-07 + p * w;
|
||||
p = 1.8239629214389227755e-08 + p * w;
|
||||
p = 1.5027403968909827627e-06 + p * w;
|
||||
p = -4.013867526981545969e-06 + p * w;
|
||||
p = 2.9234449089955446044e-06 + p * w;
|
||||
p = 1.2475304481671778723e-05 + p * w;
|
||||
p = -4.7318229009055733981e-05 + p * w;
|
||||
p = 6.8284851459573175448e-05 + p * w;
|
||||
p = 2.4031110387097893999e-05 + p * w;
|
||||
p = -0.0003550375203628474796 + p * w;
|
||||
p = 0.00095328937973738049703 + p * w;
|
||||
p = -0.0016882755560235047313 + p * w;
|
||||
p = 0.0024914420961078508066 + p * w;
|
||||
p = -0.0037512085075692412107 + p * w;
|
||||
p = 0.005370914553590063617 + p * w;
|
||||
p = 1.0052589676941592334 + p * w;
|
||||
p = 3.0838856104922207635 + p * w;
|
||||
} else if (Number.isFinite(w)) {
|
||||
w = Math.sqrt(w) - 5.0;
|
||||
p = -2.7109920616438573243e-11;
|
||||
p = -2.5556418169965252055e-10 + p * w;
|
||||
p = 1.5076572693500548083e-09 + p * w;
|
||||
p = -3.7894654401267369937e-09 + p * w;
|
||||
p = 7.6157012080783393804e-09 + p * w;
|
||||
p = -1.4960026627149240478e-08 + p * w;
|
||||
p = 2.9147953450901080826e-08 + p * w;
|
||||
p = -6.7711997758452339498e-08 + p * w;
|
||||
p = 2.2900482228026654717e-07 + p * w;
|
||||
p = -9.9298272942317002539e-07 + p * w;
|
||||
p = 4.5260625972231537039e-06 + p * w;
|
||||
p = -1.9681778105531670567e-05 + p * w;
|
||||
p = 7.5995277030017761139e-05 + p * w;
|
||||
p = -0.00021503011930044477347 + p * w;
|
||||
p = -0.00013871931833623122026 + p * w;
|
||||
p = 1.0103004648645343977 + p * w;
|
||||
p = 4.8499064014085844221 + p * w;
|
||||
} else {
|
||||
p = Infinity;
|
||||
}
|
||||
|
||||
return p * x;
|
||||
}
|
||||
|
||||
export default function(mean, stdev) {
|
||||
var mu,
|
||||
sigma,
|
||||
dist = {
|
||||
mean: function(_) {
|
||||
if (arguments.length) {
|
||||
mu = _ || 0;
|
||||
return dist;
|
||||
} else {
|
||||
return mu;
|
||||
}
|
||||
},
|
||||
stdev: function(_) {
|
||||
if (arguments.length) {
|
||||
sigma = _ == null ? 1 : _;
|
||||
return dist;
|
||||
} else {
|
||||
return sigma;
|
||||
}
|
||||
},
|
||||
sample: () => sampleNormal(mu, sigma),
|
||||
pdf: value => densityNormal(value, mu, sigma),
|
||||
cdf: value => cumulativeNormal(value, mu, sigma),
|
||||
icdf: p => quantileNormal(p, mu, sigma)
|
||||
};
|
||||
|
||||
return dist.mean(mean).stdev(stdev);
|
||||
}
|
||||
17
node_modules/vega-statistics/src/numbers.js
generated
vendored
Normal file
17
node_modules/vega-statistics/src/numbers.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
export default function*(values, valueof) {
|
||||
if (valueof == null) {
|
||||
for (let value of values) {
|
||||
if (value != null && value !== '' && (value = +value) >= value) {
|
||||
yield value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let index = -1;
|
||||
for (let value of values) {
|
||||
value = valueof(value, ++index, values);
|
||||
if (value != null && value !== '' && (value = +value) >= value) {
|
||||
yield value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
node_modules/vega-statistics/src/quantiles.js
generated
vendored
Normal file
12
node_modules/vega-statistics/src/quantiles.js
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import numbers from './numbers';
|
||||
import {ascending, quantileSorted} from 'd3-array';
|
||||
|
||||
export default function(array, p, f) {
|
||||
var values = Float64Array.from(numbers(array, f));
|
||||
|
||||
// don't depend on return value from typed array sort call
|
||||
// protects against undefined sort results in Safari (vega/vega-lite#4964)
|
||||
values.sort(ascending);
|
||||
|
||||
return p.map(_ => quantileSorted(values, _));
|
||||
}
|
||||
5
node_modules/vega-statistics/src/quartiles.js
generated
vendored
Normal file
5
node_modules/vega-statistics/src/quartiles.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import quantiles from './quantiles';
|
||||
|
||||
export default function(array, f) {
|
||||
return quantiles(array, [0.25, 0.50, 0.75], f);
|
||||
}
|
||||
5
node_modules/vega-statistics/src/random.js
generated
vendored
Normal file
5
node_modules/vega-statistics/src/random.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
export var random = Math.random;
|
||||
|
||||
export function setRandom(r) {
|
||||
random = r;
|
||||
}
|
||||
29
node_modules/vega-statistics/src/regression/exp.js
generated
vendored
Normal file
29
node_modules/vega-statistics/src/regression/exp.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
import ols from './ols';
|
||||
import {points, visitPoints} from './points';
|
||||
import rSquared from './r-squared';
|
||||
|
||||
export default function(data, x, y) {
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [xv, yv, ux, uy] = points(data, x, y);
|
||||
let YL = 0, XY = 0, XYL = 0, X2Y = 0, n = 0, dx, ly, xy;
|
||||
|
||||
visitPoints(data, x, y, (_, dy) => {
|
||||
dx = xv[n++];
|
||||
ly = Math.log(dy);
|
||||
xy = dx * dy;
|
||||
|
||||
YL += (dy * ly - YL) / n;
|
||||
XY += (xy - XY) / n;
|
||||
XYL += (xy * ly - XYL) / n;
|
||||
X2Y += (dx * xy - X2Y) / n;
|
||||
});
|
||||
|
||||
const [c0, c1] = ols(XY / uy, YL / uy, XYL / uy, X2Y / uy),
|
||||
predict = x => Math.exp(c0 + c1 * (x - ux));
|
||||
|
||||
return {
|
||||
coef: [Math.exp(c0 - c1 * ux), c1],
|
||||
predict: predict,
|
||||
rSquared: rSquared(data, x, y, uy, predict)
|
||||
};
|
||||
}
|
||||
26
node_modules/vega-statistics/src/regression/linear.js
generated
vendored
Normal file
26
node_modules/vega-statistics/src/regression/linear.js
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
import ols from './ols';
|
||||
import {visitPoints} from './points';
|
||||
import rSquared from './r-squared';
|
||||
|
||||
// Adapted from d3-regression by Harry Stevens
|
||||
// License: https://github.com/HarryStevens/d3-regression/blob/master/LICENSE
|
||||
export default function(data, x, y) {
|
||||
let X = 0, Y = 0, XY = 0, X2 = 0, n = 0;
|
||||
|
||||
visitPoints(data, x, y, (dx, dy) => {
|
||||
++n;
|
||||
X += (dx - X) / n;
|
||||
Y += (dy - Y) / n;
|
||||
XY += (dx * dy - XY) / n;
|
||||
X2 += (dx * dx - X2) / n;
|
||||
});
|
||||
|
||||
const coef = ols(X, Y, XY, X2),
|
||||
predict = x => coef[0] + coef[1] * x;
|
||||
|
||||
return {
|
||||
coef: coef,
|
||||
predict: predict,
|
||||
rSquared: rSquared(data, x, y, Y, predict)
|
||||
};
|
||||
}
|
||||
114
node_modules/vega-statistics/src/regression/loess.js
generated
vendored
Normal file
114
node_modules/vega-statistics/src/regression/loess.js
generated
vendored
Normal file
@@ -0,0 +1,114 @@
|
||||
import ols from './ols';
|
||||
import {points} from './points';
|
||||
import {median} from 'd3-array';
|
||||
|
||||
const maxiters = 2,
|
||||
epsilon = 1e-12;
|
||||
|
||||
// Adapted from science.js by Jason Davies
|
||||
// Source: https://github.com/jasondavies/science.js/blob/master/src/stats/loess.js
|
||||
// License: https://github.com/jasondavies/science.js/blob/master/LICENSE
|
||||
export default function(data, x, y, bandwidth) {
|
||||
const [xv, yv, ux, uy] = points(data, x, y, true),
|
||||
n = xv.length,
|
||||
bw = Math.max(2, ~~(bandwidth * n)), // # nearest neighbors
|
||||
yhat = new Float64Array(n),
|
||||
residuals = new Float64Array(n),
|
||||
robustWeights = new Float64Array(n).fill(1);
|
||||
|
||||
for (let iter = -1; ++iter <= maxiters; ) {
|
||||
const interval = [0, bw - 1];
|
||||
|
||||
for (let i = 0; i < n; ++i) {
|
||||
const dx = xv[i],
|
||||
i0 = interval[0],
|
||||
i1 = interval[1],
|
||||
edge = (dx - xv[i0]) > (xv[i1] - dx) ? i0 : i1;
|
||||
|
||||
let W = 0, X = 0, Y = 0, XY = 0, X2 = 0,
|
||||
denom = 1 / Math.abs(xv[edge] - dx || 1); // avoid singularity!
|
||||
|
||||
for (let k = i0; k <= i1; ++k) {
|
||||
const xk = xv[k],
|
||||
yk = yv[k],
|
||||
w = tricube(Math.abs(dx - xk) * denom) * robustWeights[k],
|
||||
xkw = xk * w;
|
||||
|
||||
W += w;
|
||||
X += xkw;
|
||||
Y += yk * w;
|
||||
XY += yk * xkw;
|
||||
X2 += xk * xkw;
|
||||
}
|
||||
|
||||
// linear regression fit
|
||||
const [a, b] = ols(X / W, Y / W, XY / W, X2 / W);
|
||||
yhat[i] = a + b * dx;
|
||||
residuals[i] = Math.abs(yv[i] - yhat[i]);
|
||||
|
||||
updateInterval(xv, i + 1, interval);
|
||||
}
|
||||
|
||||
if (iter === maxiters) {
|
||||
break;
|
||||
}
|
||||
|
||||
const medianResidual = median(residuals);
|
||||
if (Math.abs(medianResidual) < epsilon) break;
|
||||
|
||||
for (let i = 0, arg, w; i < n; ++i){
|
||||
arg = residuals[i] / (6 * medianResidual);
|
||||
// default to epsilon (rather than zero) for large deviations
|
||||
// keeping weights tiny but non-zero prevents singularites
|
||||
robustWeights[i] = (arg >= 1) ? epsilon : ((w = 1 - arg * arg) * w);
|
||||
}
|
||||
}
|
||||
|
||||
return output(xv, yhat, ux, uy);
|
||||
}
|
||||
|
||||
// weighting kernel for local regression
|
||||
function tricube(x) {
|
||||
return (x = 1 - x * x * x) * x * x;
|
||||
}
|
||||
|
||||
// advance sliding window interval of nearest neighbors
|
||||
function updateInterval(xv, i, interval) {
|
||||
let val = xv[i],
|
||||
left = interval[0],
|
||||
right = interval[1] + 1;
|
||||
|
||||
if (right >= xv.length) return;
|
||||
|
||||
// step right if distance to new right edge is <= distance to old left edge
|
||||
// step when distance is equal to ensure movement over duplicate x values
|
||||
while (i > left && (xv[right] - val) <= (val - xv[left])) {
|
||||
interval[0] = ++left;
|
||||
interval[1] = right;
|
||||
++right;
|
||||
}
|
||||
}
|
||||
|
||||
// generate smoothed output points
|
||||
// average points with repeated x values
|
||||
function output(xv, yhat, ux, uy) {
|
||||
const n = xv.length, out = [];
|
||||
let i = 0, cnt = 0, prev = [], v;
|
||||
|
||||
for (; i<n; ++i) {
|
||||
v = xv[i] + ux;
|
||||
if (prev[0] === v) {
|
||||
// average output values via online update
|
||||
prev[1] += (yhat[i] - prev[1]) / (++cnt);
|
||||
} else {
|
||||
// add new output point
|
||||
cnt = 0;
|
||||
prev[1] += uy;
|
||||
prev = [v, yhat[i]];
|
||||
out.push(prev);
|
||||
}
|
||||
}
|
||||
prev[1] += uy;
|
||||
|
||||
return out;
|
||||
}
|
||||
27
node_modules/vega-statistics/src/regression/log.js
generated
vendored
Normal file
27
node_modules/vega-statistics/src/regression/log.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
import ols from './ols';
|
||||
import {visitPoints} from './points';
|
||||
import rSquared from './r-squared';
|
||||
|
||||
// Adapted from d3-regression by Harry Stevens
|
||||
// License: https://github.com/HarryStevens/d3-regression/blob/master/LICENSE
|
||||
export default function(data, x, y) {
|
||||
let X = 0, Y = 0, XY = 0, X2 = 0, n = 0;
|
||||
|
||||
visitPoints(data, x, y, (dx, dy) => {
|
||||
++n;
|
||||
dx = Math.log(dx);
|
||||
X += (dx - X) / n;
|
||||
Y += (dy - Y) / n;
|
||||
XY += (dx * dy - XY) / n;
|
||||
X2 += (dx * dx - X2) / n;
|
||||
});
|
||||
|
||||
const coef = ols(X, Y, XY, X2),
|
||||
predict = x => coef[0] + coef[1] * Math.log(x);
|
||||
|
||||
return {
|
||||
coef: coef,
|
||||
predict: predict,
|
||||
rSquared: rSquared(data, x, y, Y, predict)
|
||||
};
|
||||
}
|
||||
8
node_modules/vega-statistics/src/regression/ols.js
generated
vendored
Normal file
8
node_modules/vega-statistics/src/regression/ols.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
// Ordinary Least Squares
|
||||
export default function(uX, uY, uXY, uX2) {
|
||||
const delta = uX2 - uX * uX,
|
||||
slope = Math.abs(delta) < 1e-24 ? 0 : (uXY - uX * uY) / delta,
|
||||
intercept = uY - slope * uX;
|
||||
|
||||
return [intercept, slope];
|
||||
}
|
||||
44
node_modules/vega-statistics/src/regression/points.js
generated
vendored
Normal file
44
node_modules/vega-statistics/src/regression/points.js
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
export function points(data, x, y, sort) {
|
||||
data = data.filter(d => {
|
||||
let u = x(d), v = y(d);
|
||||
return u != null && (u = +u) >= u && v != null && (v = +v) >= v;
|
||||
});
|
||||
|
||||
if (sort) {
|
||||
data.sort((a, b) => x(a) - x(b));
|
||||
}
|
||||
|
||||
const n = data.length,
|
||||
X = new Float64Array(n),
|
||||
Y = new Float64Array(n);
|
||||
|
||||
// extract values, calculate means
|
||||
let i = 0, ux = 0, uy = 0, xv, yv, d;
|
||||
for (d of data) {
|
||||
X[i] = xv = +x(d);
|
||||
Y[i] = yv = +y(d);
|
||||
++i;
|
||||
ux += (xv - ux) / i;
|
||||
uy += (yv - uy) / i;
|
||||
}
|
||||
|
||||
// mean center the data
|
||||
for (i=0; i<n; ++i) {
|
||||
X[i] -= ux;
|
||||
Y[i] -= uy;
|
||||
}
|
||||
|
||||
return [X, Y, ux, uy];
|
||||
}
|
||||
|
||||
export function visitPoints(data, x, y, callback) {
|
||||
let i = -1, u, v;
|
||||
|
||||
for (let d of data) {
|
||||
u = x(d);
|
||||
v = y(d);
|
||||
if (u != null && (u = +u) >= u && v != null && (v = +v) >= v) {
|
||||
callback(u, v, ++i);
|
||||
}
|
||||
}
|
||||
}
|
||||
118
node_modules/vega-statistics/src/regression/poly.js
generated
vendored
Normal file
118
node_modules/vega-statistics/src/regression/poly.js
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
import linear from './linear';
|
||||
import {points} from './points';
|
||||
import quad from './quad';
|
||||
import rSquared from './r-squared';
|
||||
|
||||
// Adapted from d3-regression by Harry Stevens
|
||||
// License: https://github.com/HarryStevens/d3-regression/blob/master/LICENSE
|
||||
// ... which was adapted from regression-js by Tom Alexander
|
||||
// Source: https://github.com/Tom-Alexander/regression-js/blob/master/src/regression.js#L246
|
||||
// License: https://github.com/Tom-Alexander/regression-js/blob/master/LICENSE
|
||||
export default function(data, x, y, order) {
|
||||
// use more efficient methods for lower orders
|
||||
if (order === 1) return linear(data, x, y);
|
||||
if (order === 2) return quad(data, x, y);
|
||||
|
||||
const [xv, yv, ux, uy] = points(data, x, y),
|
||||
n = xv.length,
|
||||
lhs = [],
|
||||
rhs = [],
|
||||
k = order + 1;
|
||||
|
||||
let i, j, l, v, c;
|
||||
|
||||
for (i=0; i<k; ++i) {
|
||||
for (l=0, v=0; l<n; ++l) {
|
||||
v += Math.pow(xv[l], i) * yv[l];
|
||||
}
|
||||
lhs.push(v);
|
||||
|
||||
c = new Float64Array(k);
|
||||
for (j=0; j<k; ++j) {
|
||||
for (l=0, v=0; l<n; ++l) {
|
||||
v += Math.pow(xv[l], i + j);
|
||||
}
|
||||
c[j] = v;
|
||||
}
|
||||
rhs.push(c);
|
||||
}
|
||||
rhs.push(lhs);
|
||||
|
||||
const coef = gaussianElimination(rhs),
|
||||
predict = x => {
|
||||
x -= ux;
|
||||
let y = uy + coef[0] + coef[1] * x + coef[2] * x * x;
|
||||
for (i=3; i<k; ++i) y += coef[i] * Math.pow(x, i);
|
||||
return y;
|
||||
};
|
||||
|
||||
return {
|
||||
coef: uncenter(k, coef, -ux, uy),
|
||||
predict: predict,
|
||||
rSquared: rSquared(data, x, y, uy, predict)
|
||||
};
|
||||
}
|
||||
|
||||
function uncenter(k, a, x, y) {
|
||||
const z = Array(k);
|
||||
let i, j, v, c;
|
||||
|
||||
// initialize to zero
|
||||
for (i=0; i<k; ++i) z[i] = 0;
|
||||
|
||||
// polynomial expansion
|
||||
for (i=k-1; i>=0; --i) {
|
||||
v = a[i];
|
||||
c = 1;
|
||||
z[i] += v;
|
||||
for (j=1; j<=i; ++j) {
|
||||
c *= (i + 1 - j) / j; // binomial coefficent
|
||||
z[i-j] += v * Math.pow(x, j) * c;
|
||||
}
|
||||
}
|
||||
|
||||
// bias term
|
||||
z[0] += y;
|
||||
|
||||
return z;
|
||||
}
|
||||
|
||||
// Given an array for a two-dimensional matrix and the polynomial order,
|
||||
// solve A * x = b using Gaussian elimination.
|
||||
function gaussianElimination(matrix) {
|
||||
const n = matrix.length - 1,
|
||||
coef = [];
|
||||
|
||||
let i, j, k, r, t;
|
||||
|
||||
for (i = 0; i < n; ++i) {
|
||||
r = i; // max row
|
||||
for (j = i + 1; j < n; ++j) {
|
||||
if (Math.abs(matrix[i][j]) > Math.abs(matrix[i][r])) {
|
||||
r = j;
|
||||
}
|
||||
}
|
||||
|
||||
for (k = i; k < n + 1; ++k) {
|
||||
t = matrix[k][i];
|
||||
matrix[k][i] = matrix[k][r];
|
||||
matrix[k][r] = t;
|
||||
}
|
||||
|
||||
for (j = i + 1; j < n; ++j) {
|
||||
for (k = n; k >= i; k--) {
|
||||
matrix[k][j] -= (matrix[k][i] * matrix[i][j]) / matrix[i][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (j = n - 1; j >= 0; --j) {
|
||||
t = 0;
|
||||
for (k = j + 1; k < n; ++k) {
|
||||
t += matrix[k][j] * coef[k];
|
||||
}
|
||||
coef[j] = (matrix[n][j] - t) / matrix[j][j];
|
||||
}
|
||||
|
||||
return coef;
|
||||
}
|
||||
31
node_modules/vega-statistics/src/regression/pow.js
generated
vendored
Normal file
31
node_modules/vega-statistics/src/regression/pow.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
import ols from './ols';
|
||||
import {visitPoints} from './points';
|
||||
import rSquared from './r-squared';
|
||||
|
||||
// Adapted from d3-regression by Harry Stevens
|
||||
// License: https://github.com/HarryStevens/d3-regression/blob/master/LICENSE
|
||||
export default function(data, x, y) {
|
||||
let X = 0, Y = 0, XY = 0, X2 = 0, YS = 0, n = 0;
|
||||
|
||||
visitPoints(data, x, y, (dx, dy) => {
|
||||
const lx = Math.log(dx),
|
||||
ly = Math.log(dy);
|
||||
++n;
|
||||
X += (lx - X) / n;
|
||||
Y += (ly - Y) / n;
|
||||
XY += (lx * ly - XY) / n;
|
||||
X2 += (lx * lx - X2) / n;
|
||||
YS += (dy - YS) / n;
|
||||
});
|
||||
|
||||
const coef = ols(X, Y, XY, X2),
|
||||
predict = x => coef[0] * Math.pow(x, coef[1]);
|
||||
|
||||
coef[0] = Math.exp(coef[0]);
|
||||
|
||||
return {
|
||||
coef: coef,
|
||||
predict: predict,
|
||||
rSquared: rSquared(data, x, y, YS, predict)
|
||||
};
|
||||
}
|
||||
42
node_modules/vega-statistics/src/regression/quad.js
generated
vendored
Normal file
42
node_modules/vega-statistics/src/regression/quad.js
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
import {points} from './points';
|
||||
import rSquared from './r-squared';
|
||||
|
||||
export default function(data, x, y) {
|
||||
const [xv, yv, ux, uy] = points(data, x, y),
|
||||
n = xv.length;
|
||||
|
||||
let X2 = 0, X3 = 0, X4 = 0, XY = 0, X2Y = 0,
|
||||
i, dx, dy, x2;
|
||||
|
||||
for (i=0; i<n;) {
|
||||
dx = xv[i];
|
||||
dy = yv[i++];
|
||||
x2 = dx * dx;
|
||||
X2 += (x2 - X2) / i;
|
||||
X3 += (x2 * dx - X3) / i;
|
||||
X4 += (x2 * x2 - X4) / i;
|
||||
XY += (dx * dy - XY) / i;
|
||||
X2Y += (x2 * dy - X2Y) / i;
|
||||
}
|
||||
|
||||
const X2X2 = X4 - (X2 * X2),
|
||||
d = (X2 * X2X2 - X3 * X3),
|
||||
a = (X2Y * X2 - XY * X3) / d,
|
||||
b = (XY * X2X2 - X2Y * X3) / d,
|
||||
c = -a * X2,
|
||||
predict = x => {
|
||||
x = x - ux;
|
||||
return a * x * x + b * x + c + uy;
|
||||
};
|
||||
|
||||
// transform coefficients back from mean-centered space
|
||||
return {
|
||||
coef: [
|
||||
c - b * ux + a * ux * ux + uy,
|
||||
b - 2 * a * ux,
|
||||
a
|
||||
],
|
||||
predict: predict,
|
||||
rSquared: rSquared(data, x, y, uy, predict)
|
||||
};
|
||||
}
|
||||
17
node_modules/vega-statistics/src/regression/r-squared.js
generated
vendored
Normal file
17
node_modules/vega-statistics/src/regression/r-squared.js
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
import {visitPoints} from './points';
|
||||
|
||||
// Adapted from d3-regression by Harry Stevens
|
||||
// License: https://github.com/HarryStevens/d3-regression/blob/master/LICENSE
|
||||
export default function(data, x, y, uY, predict) {
|
||||
let SSE = 0, SST = 0;
|
||||
|
||||
visitPoints(data, x, y, (dx, dy) => {
|
||||
const sse = dy - predict(dx),
|
||||
sst = dy - uY;
|
||||
|
||||
SSE += sse * sse;
|
||||
SST += sst * sst;
|
||||
});
|
||||
|
||||
return 1 - SSE / SST;
|
||||
}
|
||||
62
node_modules/vega-statistics/src/sampleCurve.js
generated
vendored
Normal file
62
node_modules/vega-statistics/src/sampleCurve.js
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
// subdivide up to accuracy of 0.1 degrees
|
||||
const MIN_RADIANS = 0.1 * Math.PI / 180;
|
||||
|
||||
// Adaptively sample an interpolated function over a domain extent
|
||||
export default function(f, extent, minSteps, maxSteps) {
|
||||
minSteps = minSteps || 25;
|
||||
maxSteps = Math.max(minSteps, maxSteps || 200);
|
||||
|
||||
const point = x => [x, f(x)],
|
||||
minX = extent[0],
|
||||
maxX = extent[1],
|
||||
span = maxX - minX,
|
||||
stop = span / maxSteps,
|
||||
prev = [point(minX)],
|
||||
next = [];
|
||||
|
||||
if (minSteps === maxSteps) {
|
||||
// no adaptation, sample uniform grid directly and return
|
||||
for (let i = 1; i < maxSteps; ++i) {
|
||||
prev.push(point(minX + (i / minSteps) * span));
|
||||
}
|
||||
prev.push(point(maxX));
|
||||
return prev;
|
||||
} else {
|
||||
// sample minimum points on uniform grid
|
||||
// then move on to perform adaptive refinement
|
||||
next.push(point(maxX));
|
||||
for (let i = minSteps; --i > 0;) {
|
||||
next.push(point(minX + (i / minSteps) * span));
|
||||
}
|
||||
}
|
||||
|
||||
let p0 = prev[0],
|
||||
p1 = next[next.length - 1];
|
||||
|
||||
while (p1) {
|
||||
// midpoint for potential curve subdivision
|
||||
const pm = point((p0[0] + p1[0]) / 2);
|
||||
|
||||
if (pm[0] - p0[0] >= stop && angleDelta(p0, pm, p1) > MIN_RADIANS) {
|
||||
// maximum resolution has not yet been met, and
|
||||
// subdivision midpoint sufficiently different from endpoint
|
||||
// save subdivision, push midpoint onto the visitation stack
|
||||
next.push(pm);
|
||||
} else {
|
||||
// subdivision midpoint sufficiently similar to endpoint
|
||||
// skip subdivision, store endpoint, move to next point on the stack
|
||||
p0 = p1;
|
||||
prev.push(p1);
|
||||
next.pop();
|
||||
}
|
||||
p1 = next[next.length - 1];
|
||||
}
|
||||
|
||||
return prev;
|
||||
}
|
||||
|
||||
function angleDelta(p, q, r) {
|
||||
const a0 = Math.atan2(r[1] - p[1], r[0] - p[0]),
|
||||
a1 = Math.atan2(q[1] - p[1], q[0] - p[0]);
|
||||
return Math.abs(a0 - a1);
|
||||
}
|
||||
65
node_modules/vega-statistics/src/uniform.js
generated
vendored
Normal file
65
node_modules/vega-statistics/src/uniform.js
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
import {random} from './random';
|
||||
|
||||
export function sampleUniform(min, max) {
|
||||
if (max == null) {
|
||||
max = (min == null ? 1 : min);
|
||||
min = 0;
|
||||
}
|
||||
return min + (max - min) * random();
|
||||
}
|
||||
|
||||
export function densityUniform(value, min, max) {
|
||||
if (max == null) {
|
||||
max = (min == null ? 1 : min);
|
||||
min = 0;
|
||||
}
|
||||
return (value >= min && value <= max) ? 1 / (max - min) : 0;
|
||||
}
|
||||
|
||||
export function cumulativeUniform(value, min, max) {
|
||||
if (max == null) {
|
||||
max = (min == null ? 1 : min);
|
||||
min = 0;
|
||||
}
|
||||
return value < min ? 0 : value > max ? 1 : (value - min) / (max - min);
|
||||
}
|
||||
|
||||
export function quantileUniform(p, min, max) {
|
||||
if (max == null) {
|
||||
max = (min == null ? 1 : min);
|
||||
min = 0;
|
||||
}
|
||||
return (p >= 0 && p <= 1) ? min + p * (max - min) : NaN;
|
||||
}
|
||||
|
||||
export default function(min, max) {
|
||||
var a, b,
|
||||
dist = {
|
||||
min: function(_) {
|
||||
if (arguments.length) {
|
||||
a = _ || 0;
|
||||
return dist;
|
||||
} else {
|
||||
return a;
|
||||
}
|
||||
},
|
||||
max: function(_) {
|
||||
if (arguments.length) {
|
||||
b = _ == null ? 1 : _;
|
||||
return dist;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
},
|
||||
sample: () => sampleUniform(a, b),
|
||||
pdf: value => densityUniform(value, a, b),
|
||||
cdf: value => cumulativeUniform(value, a, b),
|
||||
icdf: p => quantileUniform(p, a, b)
|
||||
};
|
||||
|
||||
if (max == null) {
|
||||
max = (min == null ? 1 : min);
|
||||
min = 0;
|
||||
}
|
||||
return dist.min(min).max(max);
|
||||
}
|
||||
Reference in New Issue
Block a user