Class: Univariate

Univariate(args)

Univariate statistical distribution analysis tool It works by sorting data into bins. This bin size depends on absolute & relative precision of the incoming data. Thus, very large samples can be processed fast with reasonable memory usage.

Constructor

new Univariate(args)

Parameters:
Name Type Description
args Object
Properties
Name Type Attributes Description
base Number <optional>
Must be between 1 and 1.5. Default is 1.001
precision Number <optional>
Must be positive. Default is 1E-9.
bins Array <optional>
See addWeighted() for description
Source:

Methods

add(…data) → {Univariate}

Add value(s) to sample.
Parameters:
Name Type Attributes Description
data Number <repeatable>
Number(s) to add to sample
Source:
Returns:
this (chainable)
Type
Univariate
Examples
for (let i=0; i<10000; i++)
   stat.add(-Math.log(Math.random()));
// creates exponential distribution
stat.add( 1,2,3,4,5,6 );
// a d6

addWeighted(pairs) → {Univariate}

Add values to sample, with weights.
Parameters:
Name Type Description
pairs Array.<Array.<number>> Each pair is an array with two numbers: [ value, quantity ]. Negative quantity is allowed and means we're erasing data.
Source:
Returns:
this (chainable)
Type
Univariate
Example
stat.addWeighted( [ [0.1, 5], [0.2, 4], [0.3, 3] ] )
// adds 0.1 x 5, 0.2 x 4, 0.3 x 3

cdf(x) → {Number}

Cumulative distribution function, i.e. P(value < x). This is the inverse of quantile.
Parameters:
Name Type Description
x Number
Source:
Returns:
probability
Type
Number

clone(argsopt) → {Univariate}

create a copy of sample object, possibly modifying precision settings and/or filtering data.
Parameters:
Name Type Attributes Description
args Object <optional>
Properties
Name Type Attributes Description
precision Number <optional>
Override absolute precision
base Number <optional>
Override relative precision
min Number <optional>
Filter values less than this
max Number <optional>
Filter values greater than this
ltrim Number <optional>
Filter values less than Xth percentile
rtrim Number <optional>
Filter values greater than 100-Xth percentile
winsorize Boolean <optional>
If a data point doesn't fit the bounds, truncate it instead of discarding.
transform function <optional>
Apply function to sample data
Source:
Returns:
copy of the original object
Type
Univariate

count() → {Integer}

Number of values in the sample.
Source:
Returns:
count
Type
Integer

E(fun) → {Number}

Calculate expected value of a given function over the sample.
Parameters:
Name Type Description
fun function
Source:
Returns:
Type
Number

getBins()

Returns a sorted list of pairs containing numbers in the sample and their respective counts. See addWeighted().
Source:

histogram(args) → {Array}

Histogram based on the sample
Parameters:
Name Type Description
args Object
Properties
Name Type Attributes Description
count Integer <optional>
Number of bars in the histogram. Default is 10.
scale Number <optional>
If given, make sure it's the height of the highest bar.
Source:
Returns:
Array of triplets: [barHeight, leftBorder, rightBorder ]. rightBorder equals to the next bar's leftBorder.
Type
Array

kurtosis() → {Number|undefined}

Kurtosis is a measure of how much of the distribution is contained in the "tails". Equals to 4th standardized moment minus 3, with a correction.
Source:
Returns:
Type
Number | undefined

max() → {Number}

Maximal value in the sample. This value is somewhat rounded up to guarantee it is greater than _any_ value in the sample.
Source:
Returns:
Maximum value
Type
Number

mean() → {Number}

Average value of the sample.
Source:
Returns:
Type
Number

median() → {Number}

Returns x such that half of the sample is less than x. Same as quantile(0.5).
Source:
Returns:
x
Type
Number

min() → {Number}

Minimal value in the sample. This value is somewhat rounded down to guarantee it is less than _any_ value in the sample.
Source:
Returns:
Minimum value
Type
Number

moment(power, offsetopt) → {Number}

Moment of nth power, i.e. E((x-offset)**power)
Parameters:
Name Type Attributes Description
power Integer Power to raise to.
offset Number <optional>
Number to subtract. Default is mean.
Source:
Returns:
Type
Number

momentAbs(power, offsetopt) → {Number}

Absolute moment of nth power, i.e. E(|x-offset|**power)
Parameters:
Name Type Attributes Default Description
power Number 1 Power to raise to. May be fractional. Default is 1.
offset Number <optional>
Number to subtract. Default is mean.
Source:
Returns:
Type
Number

momentStd(power) → {Number}

Standardized moment of nth power, i.e. nth moment / stdev**n.
Parameters:
Name Type Description
power Integer
Source:
Returns:
Type
Number

percentile(p) → {Number}

Returns x such that P(value < x) === p%. Same as quantile(p/100).
Parameters:
Name Type Description
p Number
Source:
Returns:
x
Type
Number

quantile(p) → {Number}

A number x such that P(value <= x) === p
Parameters:
Name Type Description
p Number from 0 to 1
Source:
Returns:
value
Type
Number
Example
const stat = new Univariate();
 stat.add( 1,2,3,4,5 );
 stat.quantile( 0.2 ); // slightly greater than 1
 stat.quantile( 0.5 ); // 3

skewness() → {Number|undefined}

Skewness is a measure of the asymmetry of a distribution. Equals to 3rd standardized moment times n^2/(n-1)(n-2) correction Undefined if there are less than 3 data points.
Source:
Returns:
Type
Number | undefined

stdev() → {Number}

Standard deviation of the sample. Bessel's correction is used: stdev = sqrt( E<(x - E)**2> * n/(n-1) )
Source:
Returns:
Standard deviation
Type
Number

sumOf(fun) → {Number}

Sum of arbitrary function over the sample.
Parameters:
Name Type Description
fun function Number->Number
Source:
Returns:
Type
Number
Examples
stat.sumOf( x => 1 ); // same as stat.count()
  
stat.sumOf( x => x ); // same as stat.count() * stat.mean()

toJSON() → {Object}

Serialization of the sample.
Source:
Returns:
plain data structure that can serve as an argument to new().
Type
Object