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
19
votes
2 answers

How to get "proportion of variance" vector from princomp in R

This should be very basic and I hope someone can help me. I ran a principal component analysis with the following call: pca <- princomp(....) summary(pca) Summary pca returns this description: PC1 PC2 PC3 Standard…
Neeraj Bhatnagar
  • 341
  • 1
  • 2
  • 6
16
votes
8 answers

Calculating Standard Deviation & Variance in C++

So, I've posted a few times and previously my problems were pretty vague. I started C++ this week and have been doing a little project. I'm trying to calculate standard deviation & variance. My code loads a file of 100 integers and puts them into an…
Jack
  • 321
  • 2
  • 5
  • 16
16
votes
5 answers

Why is the var() function giving me a different answer than my calculated variance?

I wasn't sure if this should go in SO or some other .SE, so I will delete if this is deemed to be off-topic. I have a vector and I'm trying to calculate the variance "by hand" (meaning based on the definition of variance but still performing the…
pocketlizard
  • 379
  • 2
  • 4
  • 11
16
votes
1 answer

Creating instances of a covariant type class from instances of a non-covariant one

Suppose I've got a simple type class whose instances will give me a value of some type: trait GiveMeJustA[X] { def apply(): X } And I've got some instances: case class Foo(s: String) case class Bar(i: Int) implicit object GiveMeJustAFoo extends…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
15
votes
1 answer

How can I combine the typeclass pattern with subtyping?

Suppose I'm using the typeclass pattern in Scala. Here's how I make a class C part of the typeclass Foo: Welcome to Scala version 2.9.0.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26). scala> trait Foo[T] { def foo(t: T) } defined trait…
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
15
votes
2 answers

Can a model have both high bias and high variance? Overfitting and Underfitting?

As I understand it when creating a supervised learning model, our model may have high bias if we are making very simple assumptions (for example if our function is linear) which cause the algorithm to miss relationships between our features and…
Alaa Awad
  • 3,612
  • 6
  • 25
  • 35
15
votes
3 answers

Homoscedascity test for Two-Way ANOVA

I've been using var.test and bartlett.test to check basic ANOVA assumptions, among others, homoscedascity (homogeniety, equality of variances). Procedure is quite simple for One-Way ANOVA: bartlett.test(x ~ g) # where x is numeric, and g is a…
aL3xa
  • 35,415
  • 18
  • 79
  • 112
15
votes
4 answers

ref and out parameters in C# and cannot be marked as variant

What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass: IVariant { public virtual R DoSomething( ref A args…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
14
votes
5 answers

Conditional typing in generic method

Consider the following (heavily simplified) code: public T Function() { if (typeof(T) == typeof(string)) { return (T) (object) "hello"; } ... } It's kind of absurd to first cast to object, then to T. But the compiler has no…
Hugo Sereno Ferreira
  • 8,600
  • 7
  • 46
  • 92
14
votes
2 answers

Variance annotations in type aliases

Recently I've noticed that variance annotations can be used in type aliases. Here is example from Predef: type Function[-A, +B] = Function1[A, B] And I started to think, where it could be used. Obviously, you can't change variance to opposite, or…
4e6
  • 10,696
  • 4
  • 52
  • 62
13
votes
2 answers

What's the theory behind computing variance of an image?

I am trying to compute the blurriness of an image by using LaplacianFilter. According to this article: https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/ I have to compute the variance of the output image. The problem is I don't…
denis631
  • 1,765
  • 3
  • 17
  • 38
12
votes
7 answers

Covariance and Contravariance on the same type argument

The C# spec states that an argument type cannot be both covariant and contravariant at the same time. This is apparent when creating a covariant or contravariant interface you decorate your type parameters with "out" or "in" respectively. There is…
William Edmondson
  • 3,619
  • 3
  • 32
  • 41
11
votes
4 answers

How to find cumulative variance or standard deviation in R

I have a column X in a data frame, for which I need to find the cumulative standard deviation. X Cumulative.SD 1 - 4 2.12 5 2.08 6 2.16 9 2.91
Praveen Chougale
  • 373
  • 3
  • 11
11
votes
5 answers

Covariance and Contravariance with Func in generics

I need more information about variance in generics and delegates. The following code snippet does not compile: Error CS1961 Invalid variance: The type parameter 'TIn' must be covariantly valid on 'Test.F(Func)'. 'TIn' is …
Robertas
  • 1,164
  • 3
  • 11
  • 26
11
votes
2 answers

When using covariance notations or generic bounds in Scala

In Scala variance can be defined with variance operators like + and - on the generic type argument. For example the List type is covariant in the standard library. class List[+A] So a function with a covariant list can be defined like this: def…
Julian
  • 33,915
  • 22
  • 119
  • 174
1
2
3
51 52