2023-01-31 17:56:00 +01:00

83 lines
2.6 KiB
Markdown

---
layout: default
---
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
<h1 id="function-variance">Function variance <a href="#function-variance" title="Permalink">#</a></h1>
Compute the variance of a matrix or a list with values.
In case of a multidimensional array or matrix, the variance over all
elements will be calculated.
Additionally, it is possible to compute the variance along the rows
or columns of a matrix by specifying the dimension as the second argument.
Optionally, the type of normalization can be specified as the final
parameter. The parameter `normalization` can be one of the following values:
- 'unbiased' (default) The sum of squared errors is divided by (n - 1)
- 'uncorrected' The sum of squared errors is divided by n
- 'biased' The sum of squared errors is divided by (n + 1)
Note that older browser may not like the variable name `var`. In that
case, the function can be called as `math['var'](...)` instead of
`math.var(...)`.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.variance(a, b, c, ...)
math.variance(A)
math.variance(A, normalization)
math.variance(A, dimension)
math.variance(A, dimension, normalization)
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`array` | Array &#124; Matrix | A single matrix or or multiple scalar values
`normalization` | string | Determines how to normalize the variance. Choose 'unbiased' (default), 'uncorrected', or 'biased'. Default value: 'unbiased'.
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
* | The variance
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
Type | Description
---- | -----------
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
```js
math.variance(2, 4, 6) // returns 4
math.variance([2, 4, 6, 8]) // returns 6.666666666666667
math.variance([2, 4, 6, 8], 'uncorrected') // returns 5
math.variance([2, 4, 6, 8], 'biased') // returns 4
math.variance([[1, 2, 3], [4, 5, 6]]) // returns 3.5
math.variance([[1, 2, 3], [4, 6, 8]], 0) // returns [4.5, 8, 12.5]
math.variance([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 4]
math.variance([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.5, 2]
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[mean](mean.html),
[median](median.html),
[max](max.html),
[min](min.html),
[prod](prod.html),
[std](std.html),
[sum](sum.html)