Questions tagged [variance]

In probability and statistics, the variance is a measure of the spread of a set of numbers.

In computer science, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations.

About Type Ordering

Let's say that a class P (parent) is inherited by a class C (child).

We can denote this fact as: P > C.

Informally, the P is "larger" than C since it "contains" all the possible instances or objects or C, but can also contain some other objects that are not C. Child is always parent but not necessarily the other way around.

Variance in Generics

Let's say that there is a generic type G that has a single generic parameter T, denoted by: G{T}.

  • If G{P} > G{C}, then T is co-variant (it preserves the original inheritance ordering).
  • If G{P} < G{C}, then T is contra-variant (it reverses the original inheritance ordering).
  • If G{P} and G{C} are type-unrelated, then T is invariant (it "breaks" the inheritance).

So the variance is the property of transformation (in this case: genetic parameter T of G), not the original type hierarchy (P and C).

C# Examples

The generic parameter T of IEnumerable<out T> is covariant (as denoted by "out" keyword), meaning you can "forget" that the collection contains Cs and just treat it as if it contains Ps. For example:

IEnumerable<C> ec = ...;
IEnumerable<P> ep = ec;

The generic parameter T of Action<in T> is contravariant (as denoted by "in" keyword), meaning that an action on P can also be applied to C. For example:

Action<P> ap = ...;
Action<C> ac = ap;

The generic parameter T is contravariant and generic parameter TResult is covariant in Func<in T, out TResult>. Each generic parameter is considered a different "transformation" with its own variance. This lets you write a code like this:

Func<P, C> fpc = ...;
Func<C, P> fcp = fpc;

And finally, the generic parameter T of IList<T> is considered invariant, so IList<P> and IList<C> are considered unrelated types (there is no assignment compatibility in either direction).

Variance in Mathematics

Variance is a descriptor of a probability distribution, it denotes how far away the numbers are from the expected value, the mean. The variance of a random variable or distribution is calculated as the mean of the squared deviation of that variable from its expected value or mean.

If a random variable X has the expected value (mean) μ = E[X] then the variance of X is given by:

enter image description here

or

enter image description here

See the Wikipedia article for more information.

768 questions
78
votes
14 answers

Rolling variance algorithm

I'm trying to find an efficient, numerically stable algorithm to calculate a rolling variance (for instance, a variance over a 20-period rolling window). I'm aware of the Welford algorithm that efficiently computes the running variance for a stream…
Abiel
  • 5,251
  • 9
  • 54
  • 74
74
votes
4 answers

What's the difference between A<:B and +B in Scala?

What's the difference between [A <: B] and [+B] in Scala?
jsadfeew
  • 2,247
  • 1
  • 18
  • 25
44
votes
5 answers

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism?

I am trying to figure out the exact meaning of the words Covariance and Contravariance from several articles online and questions on StackOverflow, and from what I can understand, it's only another word for polymorphism. Am I correct with the above…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
40
votes
4 answers

When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

@uncheckedVariance can be used to bridge the gap between Scala's declaration site variance annotations and Java's invariant generics. scala> import java.util.Comparator import java.util.Comparator scala> trait Foo[T] extends…
retronym
  • 54,768
  • 12
  • 155
  • 168
39
votes
9 answers

How can I calculate the variance of a list in python?

If I have a list like this: results=[-14.82381293, -0.29423447, -13.56067979, -1.6288903, -0.31632439, 0.53459687, -1.34069996, -1.61042692, -4.03220519, -0.24332097] I want to calculate the variance of this list in Python which is the…
minks
  • 2,859
  • 4
  • 21
  • 29
35
votes
4 answers

How does Java's use-site variance compare to C#'s declaration site variance?

My understand is that specifying variance for generics in C# happens at the type declaration level: when you're creating your generic type, you specify the variance for the type arguments. In Java, on the other hand, variance is specified where a…
munificent
  • 11,946
  • 2
  • 38
  • 55
29
votes
3 answers

Python scikit learn Linear Model Parameter Standard Error

I am working with sklearn and specifically the linear_model module. After fitting a simple linear as in import pandas as pd import numpy as np from sklearn import linear_model randn = np.random.randn X = pd.DataFrame(randn(10,3),…
Ryan
  • 655
  • 2
  • 8
  • 12
28
votes
6 answers

Why is there no parameter contra-variance for overriding?

C++ and Java support return-type covariance when overriding methods. Neither, however, support contra-variance in parameter types - instead, it translates to overloading (Java) or hiding (C++). Why is that? It seems to me that there is no harm in…
Oak
  • 26,231
  • 8
  • 93
  • 152
27
votes
1 answer

Difference between Variance, Covariance, Contravariance and Bivariance in TypeScript

Could you please explain using small and simple TypeScript examples what is Variance, Covariance, Contravariance and Bivariance?
24
votes
4 answers

Population Variance in r

How can I calculate the population variance of my data using R? I read there is a package called popvar but I have the Version 0.99.892 and I don't find the package
YazminRios
  • 389
  • 1
  • 2
  • 10
23
votes
1 answer

Finding the dimension with highest variance using scikit-learn PCA

I need to use pca to identify the dimensions with the highest variance of a certain set of data. I'm using scikit-learn's pca to do it, but I can't identify from the output of the pca method what are the components of my data with the highest…
Alberto A
  • 1,160
  • 4
  • 17
  • 35
22
votes
7 answers

How can I simply calculate the rolling/moving variance of a time series in python?

I have a simple time series and I am struggling to estimate the variance within a moving window. More specifically, I cannot figure some issues out relating to the way of implementing a sliding window function. For example, when using NumPy and…
Barry
  • 377
  • 2
  • 4
  • 7
21
votes
4 answers

Does I re-implement I if I is convertible to I by variance conversion?

interface ICloneable { T Clone(); } class Base : ICloneable { public Base Clone() { return new Base(); } } class Derived : Base, ICloneable { new public Derived Clone() { return new Derived(); } } Given these…
Michael Liu
  • 52,147
  • 13
  • 117
  • 150
20
votes
2 answers

Var(x) and cov(x, x) don't give the same result in numpy

A property of the covariance is, that cov(x, x) = var(x) However, in numpy I don't get the same result. from numpy import var, cov x = range(10) y = var(x) z = cov(x, x)[0][1] print y, z Am I doing something wrong here? How can I obtain the…
Randomtheories
  • 1,220
  • 18
  • 22
20
votes
3 answers

How do I print the variance of an lm in R without computing from the Standard Error by hand?

Simple question really! I am running lots of linear regressions of y~x and want to obtain the variance for each regression without computing it from hand from the Standard Error output given in the summary.lm command. Just to save a bit of time…
Sarah
  • 789
  • 3
  • 12
  • 29
1
2 3
51 52