Skip to content

var, svar

Variance, sample variance

var

Variance

var x    var[x]

Where x is a numeric list, returns its variance as a float atom. var ignores nulls.

q)var 2 3 5 7
3.6875
q)var 2 3 5 0n 7
3.6875
q)trade:([]sym:`IBM`MSFT`IBM`MSFT`IBM;price:101 52 103 50 102f)
q)select var price by sym from trade
sym | price
----| ---------
IBM | 0.6666667
MSFT| 1

var is an aggregate function. Where sqr:{x*x}, it is equivalent to:

{avg[sqr x]-sqr[avg x]}

Since 4.1t 2022.04.15, var can also traverse columns of tables and general, anymap, and nested lists.

q)M:get`:m77 set m:(2 3;4 0N;1 7)
q)var m
1.555556 4
q)var M
1.555556 4
q)T:get`:tab/ set t:flip`a`b!flip m
q)var t
a| 1.555556
b| 4
q)var T
a| 1.555556
b| 4

var is a multithreaded primitive.

svar

Sample variance

svar x    svar[x]

Where x is a numeric list, returns its sample variance as a float atom. svar ignores nulls.

\[svar(x)=\frac{n}{n-1}var(x)\]

where \(n\) is the number of non-null items in x.

q)var 2 3 5 7
3.6875
q)svar 2 3 5 7
4.916667
q)svar 2 3 5 0n 7
4.916667
q)select svar price by sym from trade
sym | price
----| -----
IBM | 1
MSFT| 2

svar is an aggregate function, equivalent to {var[x]*n%-1+n:count[x]-sum null x}.

Since 4.1t 2022.04.15, svar can also traverse columns of tables and general, anymap, and nested lists.

q)M:get`:m77 set m:(2 3;4 0N;1 7)
q)svar m
2.333333 8
q)svar M
2.333333 8
q)T:get`:tab/ set t:flip`a`b!flip m
q)svar t
a| 2.333333
b| 8
q)svar T
a| 2.333333
b| 8

svar is a multithreaded primitive.

Domain and range

The following shows the resulting output (range) type given the input (domain) type of x. For the character codes of the datatypes, refer to Datatypes.

domain: B G X H I J E F C S P M D Z N U V T
range:  f . f f f f f f f . f f f f f f f f

cov, scov

Mathematics

Covariance, Variance