Statistics

Namespace

JXG.Math.Statistics namespace with functions for mathematical statistics. Most functions are like in the statistics package R.

Methods

Own

(static) TheilSenRegression(coords) → {Array}

The Theil-Sen estimator can be used to determine a more robust linear regression of a set of sample points than least squares regression in JXG.Math.Numerics.regressionPolynomial.

If the function should be applied to an array a of points, a the coords array can be generated with JavaScript array.map:

JXG.Math.Statistics.TheilSenRegression(a.map(el => el.coords));

Example

var board = JXG.JSXGraph.initBoard('jxgbox', { boundingbox: [-6,6,6,-6], axis : true });
var a=[];
a[0]=board.create('point', [0,0]);
a[1]=board.create('point', [3,0]);
a[2]=board.create('point', [0,3]);

board.create('line', [
    () => JXG.Math.Statistics.TheilSenRegression(a.map(el => el.coords))
  ],
  {strokeWidth:1, strokeColor:'black'});

Parameters

Name Type Description
coords Array

Array of JXG.Coords.

Returns

A stdform array of the regression line.

Type
Array

Details

Source
math/statistics.js, line 665

(static) abs(arr) → {Array|Number}

Determines the absolute value of every given value.

Parameters

Name Type Description
arr Array | Number

Returns

Type
Array | Number

Details

Source
math/statistics.js, line 369

(static) add(arr1, arr2) → {Array|Number}

Adds up two (sequences of) values. If one value is an array and the other one is a number the number is added to every element of the array. If two arrays are given and the lengths don't match the shortest length is taken.

Parameters

Name Type Description
arr1 Array | Number
arr2 Array | Number

Returns

Type
Array | Number

Details

Source
math/statistics.js, line 400

(static) boxplot(arr, coefopt) → {Array}

Compute the quartiles for the boxplot element, i.e. [min, 25% quartile, median 75% quartile, max, outliers]. NaN entries are ignored. Data array arr need not be sorted.

Parameters

Name Type Attributes Default Description
arr Array
coef Number <optional>
1.5

factor for the interquartile range. If 0: no outliers

Returns

quartile data: [min, 25%, 50%, 75%, max, [outliers]]

Type
Array

Details

See
Source
math/statistics.js, line 207

(static) div(arr1, arr2) → {Array|Number}

Divides two (sequences of) values. If two arrays are given and the lengths don't match the shortest length is taken.

Parameters

Name Type Description
arr1 Array | Number

Dividend

arr2 Array | Number

Divisor

Returns

Type
Array | Number

Details

Source
math/statistics.js, line 441

(static) divide()

Details

Deprecated
Use JXG.Math.Statistics.div instead.
Source
math/statistics.js, line 479

(static) generateGaussian(mean, stdDev) → {Number}

Generate values of a standard normal random variable with the Marsaglia polar method, see https://en.wikipedia.org/wiki/Marsaglia_polar_method. See also D. E. Knuth, The art of computer programming, vol 2, p. 117.

Parameters

Name Type Description
mean Number

mean value of the normal distribution

stdDev Number

standard deviation of the normal distribution

Returns

value of a standard normal random variable

Type
Number

Details

Source
math/statistics.js, line 700

(static) histogram(x, opt)

Compute the histogram of a dataset. Optional parameters can be supplied through a JavaScript object with the following default values:

{
  bins: 10,          // Number of bins
  range: false,      // false or array. The lower and upper range of the bins.
                     // If not provided, range is simply [min(x), max(x)].
                     // Values outside the range are ignored.
  density: false,    // If true, normalize the counts by dividing by sum(counts)
  cumulative: false
}

The function returns an array containing two arrays. The first array is of length bins+1 containing the start values of the bins. The last entry contains the end values of the last bin.

The second array contains the counts of each bin.

Example

let board = JXG.JSXGraph.initBoard('jxgbox',
      { boundingbox: [-1.7, .5, 20, -.03], axis: true});
 let board2 = JXG.JSXGraph.initBoard('jxgbox2',
      { boundingbox: [-1.6, 1.1, 20, -.06], axis: true});

  let runs = [
      [0.5, 1.0, 'brown'],
      [1.0, 2.0, 'red'],
      [2.0, 2.0, 'orange'],
      [3.0, 2.0, 'yellow'],
      [5.0, 1.0, 'green'],
      [9.0, 0.5, 'black'],
      [7.5, 1.0, 'purple'],
  ]

  let labelY = .4
  runs.forEach((run,i) => {
      board.create('segment',[[7,labelY-(i/50)],[9,labelY-(i/50)]],{strokeColor:run[2]})
      board.create('text',[10,labelY-(i/50),`k=${run[0]}, θ=${run[1]}`])

      // density
      let x = Array(50000).fill(0).map(() => JXG.Math.Statistics.randomGamma(run[0],run[1]))
      let res = JXG.Math.Statistics.histogram(x, { bins: 50, density: true, cumulative: false, range: [0, 20] });
      board.create('curve', [res[1], res[0]], { strokeColor: run[2], strokeWidth:2});

      // cumulative density
      res = JXG.Math.Statistics.histogram(x, { bins: 50, density: true, cumulative: true, range: [0, 20] });
      res[0].unshift(0)  // add zero to front so cumulative starts at zero
      res[1].unshift(0)
      board2.create('curve', [res[1], res[0]], { strokeColor: run[2], strokeWidth:2 });
  });

Parameters

Name Type Description
x Array
opt Object

Optional parameters

Returns

Array [bin, counts] Array bins contains start values of bins, array counts contains the number of entries of x which are contained in each bin.

Details

Source
math/statistics.js, line 1421

(static) max(arr) → {Number}

Extracts the maximum value from the array.

Parameters

Name Type Description
arr Array

Returns

The highest number from the array. It returns NaN if not every element could be interpreted as a number and -Infinity if an empty array is given or no element could be interpreted as a number.

Type
Number

Details

Source
math/statistics.js, line 337

(static) mean(arr) → {Number}

Determines the mean value of the values given in an array.

Parameters

Name Type Description
arr Array

Returns

Type
Number

Details

Source
math/statistics.js, line 86

(static) median(arr) → {Number}

The median of a finite set of values is the value that divides the set into two equal sized subsets.

Parameters

Name Type Description
arr Array

The set of values.

Returns

Type
Number

Details

Source
math/statistics.js, line 101

(static) min(arr) → {Number}

Extracts the minimum value from the array.

Parameters

Name Type Description
arr Array

Returns

The lowest number from the array. It returns NaN if not every element could be interpreted as a number and Infinity if an empty array is given or no element could be interpreted as a number.

Type
Number

Details

Source
math/statistics.js, line 349

(static) mod(arr1, arr2, mathopt) → {Array|Number}

Divides two (sequences of) values and returns the remainder. If two arrays are given and the lengths don't match the shortest length is taken.

Parameters

Name Type Attributes Default Description
arr1 Array | Number

Dividend

arr2 Array | Number

Divisor

math Boolean <optional>
false

Mathematical mod or symmetric mod? Default is symmetric, the JavaScript % operator.

Returns

Type
Array | Number

Details

Source
math/statistics.js, line 493

(static) multiply(arr1, arr2) → {Array|Number}

Multiplies two (sequences of) values. If one value is an array and the other one is a number the number is multiplied to every element of the array. If two arrays are given and the lengths don't match the shortest length is taken.

Parameters

Name Type Description
arr1 Array | Number
arr2 Array | Number

Returns

Type
Array | Number

Details

Source
math/statistics.js, line 544

(static) percentile(arr, percentile) → {Number|Array}

The P-th percentile ( \(0 < P \leq 100\) ) of a list of \(N\) ordered values (sorted from least to greatest) is the smallest value in the list such that no more than \(P\) percent of the data is strictly less than the value and at least \(P\) percent of the data is less than or equal to that value. See https://en.wikipedia.org/wiki/Percentile.

Here, the linear interpolation between closest ranks method is used.

Parameters

Name Type Description
arr Array

The set of values, need not be ordered.

percentile Number | Array

One or several percentiles

Returns

Depending if a number or an array is the input for percentile, a number or an array containing the percentiles is returned.

Type
Number | Array

Details

Source
math/statistics.js, line 158

(static) prod(arr) → {Number}

Multiplies all elements of the given array.

Parameters

Name Type Description
arr Array

An array of numbers.

Returns

Type
Number

Details

Source
math/statistics.js, line 69

(static) randomBeta(alpha, beta)

Generate value of a random variable with beta distribution with shape parameters alpha and beta. See https://en.wikipedia.org/wiki/Beta_distribution.

Parameters

Name Type Description
alpha Number

\(< 0\)

beta Number

\(< 0\)

Returns

Number

Details

Source
math/statistics.js, line 986

(static) randomBinomial(n, p)

Generate values for a random variable in binomial distribution with parameters \(n\) and \(p\). See https://en.wikipedia.org/wiki/Binomial_distribution. It uses algorithm BG from https://dl.acm.org/doi/pdf/10.1145/42372.42381.

Example

let board = JXG.JSXGraph.initBoard('jxgbox',
    { boundingbox: [-1.7, .5, 30, -.03], axis: true });

let runs = [
    [0.5, 20, 'blue'],
    [0.7, 20, 'green'],
    [0.5, 40, 'red'],
];

let labelY = .4;
runs.forEach((run, i) => {
    board.create('segment', [[7, labelY - (i / 50)], [9, labelY - (i / 50)]], { strokeColor: run[2] });
    board.create('text', [10, labelY - (i / 50), `p=${run[0]}, n=${run[1]}`]);

    let x = Array(50000).fill(0).map(() => JXG.Math.Statistics.randomBinomial(run[1], run[0]));
    let res = JXG.Math.Statistics.histogram(x, {
        bins: 40,
        density: true,
        cumulative: false,
        range: [0, 40]
    });
    board.create('curve', [res[1], res[0]], { strokeColor: run[2] });
});

Parameters

Name Type Description
n Number

Number of trials (n >= 0)

p Number

Probability (0 <= p <= 1)

Returns

Number Integer value of a random variable in binomial distribution

Details

Source
math/statistics.js, line 1120

(static) randomChisquare(k)

Generate value of a random variable with chi-square distribution with k degrees of freedom. See https://en.wikipedia.org/wiki/Chi-squared_distribution.

Parameters

Name Type Description
k Number

\(>0\)

Returns

Number

Details

Source
math/statistics.js, line 1008

(static) randomExponential(lambda)

Generate value of a random variable with exponential distribution, i.e. \(f(x; \lambda) = \lambda * e^(-\lambda x)\) if \(x \geq 0\) and \(f(x; \lambda) = 0\) if \(x < 0\). See https://en.wikipedia.org/wiki/Exponential_distribution. Algorithm: D.E. Knuth, TAOCP 2, p. 128.

Example

let board = JXG.JSXGraph.initBoard('JXGbox',
      { boundingbox: [-.5, 1.5, 5, -.1], axis: true});

  let runs = [
      [0.5, 'red'],
      [1.0, 'green'],
      [1.5, 'blue'],
  ]

  let labelY = 1
  runs.forEach((run,i) => {
      board.create('segment',[[1.8,labelY-(i/20)],[2.3,labelY-(i/20)]],{strokeColor:run[1]})
      board.create('text',[2.5,labelY-(i/20),`λ=${run[0]}`])

      let x = Array(50000).fill(0).map(() => JXG.Math.Statistics.randomExponential(run[0]))
      let res = JXG.Math.Statistics.histogram(x, { bins: 40, density: true, cumulative: false, range: false });
      board.create('curve', [res[1], res[0]], { strokeColor: run[1], strokeWidth:2});
  })

Parameters

Name Type Description
lambda Number

\(> 0\)

Returns

Number

Details

Source
math/statistics.js, line 845

(static) randomF(d1, d2)

Generate value of a random variable with F-distribution with d1 and d2 degrees of freedom. See https://en.wikipedia.org/wiki/F-distribution.

Parameters

Name Type Description
d1 Number

\(>0\)

d2 Number

\(>0\)

Returns

Number

Details

Source
math/statistics.js, line 1026

(static) randomGamma(a, bopt, topt)

Generate value of a random variable with gamma distribution of order alpha. See https://en.wikipedia.org/wiki/Gamma_distribution. Algorithm: D.E. Knuth, TAOCP 2, p. 129.

Example

let board = JXG.JSXGraph.initBoard('jxgbox',
      { boundingbox: [-1.7, .5, 20, -.03], axis: true});

  let runs = [
      [0.5, 1.0, 'brown'],
      [1.0, 2.0, 'red'],
      [2.0, 2.0, 'orange'],
      [3.0, 2.0, 'yellow'],
      [5.0, 1.0, 'green'],
      [9.0, 0.5, 'black'],
      [7.5, 1.0, 'purple'],
  ]

  let labelY = .4
  runs.forEach((run,i) => {
      board.create('segment',[[7,labelY-(i/50)],[9,labelY-(i/50)]],{strokeColor:run[2]})
      board.create('text',[10,labelY-(i/50),`k=${run[0]}, θ=${run[1]}`])

      // density
      let x = Array(50000).fill(0).map(() => JXG.Math.Statistics.randomGamma(run[0],run[1]))
      let res = JXG.Math.Statistics.histogram(x, { bins: 50, density: true, cumulative: false, range: [0, 20] });
      board.create('curve', [res[1], res[0]], { strokeColor: run[2]});

  });

Parameters

Name Type Attributes Default Description
a Number

shape, \( > 0\)

b Number <optional>
1

scale, \( > 0\)

t Number <optional>
0

threshold

Returns

Number

Details

Source
math/statistics.js, line 925

(static) randomGeometric(p)

Generate values for a random variable in geometric distribution with probability \(p\). See https://en.wikipedia.org/wiki/Geometric_distribution.

Parameters

Name Type Description
p Number

(0 <= p <= 1)

Returns

Number

Details

Source
math/statistics.js, line 1194

(static) randomHypergeometric(good, bad, samples)

Generate values for a random variable in hypergeometric distribution. Samples are drawn from a hypergeometric distribution with specified parameters, good (ways to make a good selection), bad (ways to make a bad selection), and samples (number of items sampled, which is less than or equal to good + bad).

Naive implementation with runtime O(samples).

Parameters

Name Type Description
good Number

ways to make a good selection

bad Number

ways to make a bad selection

samples Number

number of items sampled

Returns

Number

Details

Source
math/statistics.js, line 1279

(static) randomNormal(mean, stdDev)

Generate value of a standard normal random variable with given mean and standard deviation. Alias for JXG.Math.Statistics#generateGaussian

Example

let board = JXG.JSXGraph.initBoard('JXGbox',
      { boundingbox: [-5, 1.5, 5, -.03], axis: true});

  let runs = [
      [0, 0.2, 'blue'],
      [0, 1.0, 'red'],
      [0, 5.0, 'orange'],
      [-2,0.5, 'green'],
  ]

  let labelY = 1.2
  runs.forEach((run,i) => {
      board.create('segment',[[1.0,labelY-(i/20)],[2.0,labelY-(i/20)]],{strokeColor:run[2]})
      board.create('text',[2.5,labelY-(i/20),`μ=${run[0]}, σ2=${run[1]}`])

      let x = Array(50000).fill(0).map(() => JXG.Math.Statistics.randomNormal(run[0],Math.sqrt(run[1])))  // sqrt so Std Dev, not Variance
      let res = JXG.Math.Statistics.histogram(x, { bins: 40, density: true, cumulative: false, range: false });
      board.create('curve', [res[1], res[0]], { strokeColor: run[2], strokeWidth:2});
  })

Parameters

Name Type Description
mean Number
stdDev Number

Returns

Number

Details

See
Source
math/statistics.js, line 777

(static) randomPareto(gamma, k)

Generate values for a random variable in Pareto distribution with shape \(\gamma\) and scale \(k\). See https://en.wikipedia.org/wiki/Pareto_distribution. Method: use inverse transformation sampling.

Parameters

Name Type Description
gamma Number

shape (0 < gamma)

k Number

scale (0 < k < x)

Returns

Number

Details

Source
math/statistics.js, line 1256

(static) randomPoisson(mu)

Generate values for a random variable in Poisson distribution with mean \(\mu\). See https://en.wikipedia.org/wiki/Poisson_distribution.

Parameters

Name Type Description
mu Number

(0 < mu)

Returns

Number

Details

Source
math/statistics.js, line 1214

(static) randomT(nu)

Generate value of a random variable with Students-t-distribution with ν degrees of freedom. See https://en.wikipedia.org/wiki/Student%27s_t-distribution.

Parameters

Name Type Description
nu Number

\(>0\)

Returns

Number

Details

Source
math/statistics.js, line 1047

(static) randomUniform(a, b)

Generate value of a uniform distributed random variable in the interval [a, b].

Parameters

Name Type Description
a Number
b Number

Returns

Number

Details

Source
math/statistics.js, line 788

(static) range(arr) → {Array}

Determines the lowest and the highest value from the given array.

Parameters

Name Type Description
arr Array

Returns

The minimum value as the first and the maximum value as the second value.

Type
Array

Details

Source
math/statistics.js, line 359

(static) sd(arr) → {Number}

Determines the standard deviation which shows how much variation there is from the average value of a set of numbers.

Parameters

Name Type Description
arr Array

Returns

Type
Number

Details

Source
math/statistics.js, line 301

(static) subtract(arr1, arr2) → {Array|Number}

Subtracts two (sequences of) values. If two arrays are given and the lengths don't match the shortest length is taken.

Parameters

Name Type Description
arr1 Array | Number

Minuend

arr2 Array | Number

Subtrahend

Returns

Type
Array | Number

Details

Source
math/statistics.js, line 585

(static) sum(arr) → {Number}

Sums up all elements of the given array.

Parameters

Name Type Description
arr Array

An array of numbers.

Returns

Type
Number

Details

Source
math/statistics.js, line 52

(static) variance(arr) → {Number}

Bias-corrected sample variance. A variance is a measure of how far a set of numbers are spread out from each other.

Parameters

Name Type Description
arr Array

Returns

Type
Number

Details

Source
math/statistics.js, line 276

(static) weightedMean(arr, w) → {Number}

Weighted mean value is basically the same as JXG.Math.Statistics.mean but here the values are weighted, i.e. multiplied with another value called weight. The weight values are given as a second array with the same length as the value array..

Parameters

Name Type Description
arr Array

Set of alues.

w Array

Weight values.

Throws

If the dimensions of the arrays don't match.

Type
Error

Returns

Type
Number

Details

Source
math/statistics.js, line 315

Inherited

none

Details

Statistics

Source
math/statistics.js, line 39