1 |
|
2 |
|
3 | # Function variance
|
4 |
|
5 | Compute the variance of a matrix or a list with values.
|
6 | In case of a (multi dimensional) array or matrix, the variance over all
|
7 | elements will be calculated.
|
8 |
|
9 | Additionally, it is possible to compute the variance along the rows
|
10 | or columns of a matrix by specifying the dimension as the second argument.
|
11 |
|
12 | Optionally, the type of normalization can be specified as the final
|
13 | parameter. The parameter `normalization` can be one of the following values:
|
14 |
|
15 | - 'unbiased' (default) The sum of squared errors is divided by (n - 1)
|
16 | - 'uncorrected' The sum of squared errors is divided by n
|
17 | - 'biased' The sum of squared errors is divided by (n + 1)
|
18 |
|
19 |
|
20 | Note that older browser may not like the variable name `var`. In that
|
21 | case, the function can be called as `math['var'](...)` instead of
|
22 | `math.var(...)`.
|
23 |
|
24 |
|
25 | ## Syntax
|
26 |
|
27 | ```js
|
28 | math.variance(a, b, c, ...)
|
29 | math.variance(A)
|
30 | math.variance(A, normalization)
|
31 | math.variance(A, dimension)
|
32 | math.variance(A, dimension, normalization)
|
33 | ```
|
34 |
|
35 | ### Parameters
|
36 |
|
37 | Parameter | Type | Description
|
38 | --------- | ---- | -----------
|
39 | `array` | Array | Matrix | A single matrix or or multiple scalar values
|
40 | `normalization` | string | Determines how to normalize the variance. Choose 'unbiased' (default), 'uncorrected', or 'biased'. Default value: 'unbiased'.
|
41 |
|
42 | ### Returns
|
43 |
|
44 | Type | Description
|
45 | ---- | -----------
|
46 | * | The variance
|
47 |
|
48 |
|
49 | ## Examples
|
50 |
|
51 | ```js
|
52 | math.variance(2, 4, 6) // returns 4
|
53 | math.variance([2, 4, 6, 8]) // returns 6.666666666666667
|
54 | math.variance([2, 4, 6, 8], 'uncorrected') // returns 5
|
55 | math.variance([2, 4, 6, 8], 'biased') // returns 4
|
56 |
|
57 | math.variance([[1, 2, 3], [4, 5, 6]]) // returns 3.5
|
58 | math.variance([[1, 2, 3], [4, 6, 8]], 0) // returns [4.5, 8, 12.5]
|
59 | math.variance([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 4]
|
60 | math.variance([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.5, 2]
|
61 | ```
|
62 |
|
63 |
|
64 | ## See also
|
65 |
|
66 | [mean](mean.md),
|
67 | [median](median.md),
|
68 | [max](max.md),
|
69 | [min](min.md),
|
70 | [prod](prod.md),
|
71 | [std](std.md),
|
72 | [sum](sum.md)
|