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
11
votes
8 answers

Python: Variance of a list of defined numbers

I am trying to make a function that prints the variance of a list of defined numbers: grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5] So far, I have tried proceeding on making these three functions: def grades_sum(my_list): …
GiamPy
  • 3,543
  • 3
  • 30
  • 51
10
votes
2 answers

Scala type members variance

Consider this short snippet: trait Table[+A] { type RowType = Seq[A] } Scala 2.11.7 compiler gives the following error: covariant type A occurs in invariant position in type Seq[A] of type RowType Why is A considered to be in invariant position…
Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
10
votes
3 answers

Calculating variance of an image python efficiently

I'm working on a project in which need to get the variance of an image. Currently I'm taking 2 approaches (both work but are very slow): Calculating the variance for each pixel individually: This is the code using numpy, varianceMatrix is the…
est.tenorio
  • 362
  • 3
  • 11
10
votes
3 answers

R, filter matrix based on variance cut-offs

See edit below Using R, I would like to filter a matrix (of gene expression data) and keep only the rows (genes/probes) that have values with high variance. For example, I'd like to only keep the rows that have values in the bottom and top…
Todd
  • 568
  • 2
  • 6
  • 15
10
votes
3 answers

Variance rules in C#

The Exact rules for variance validity are a bit vague and not specific. I'm going to list the rules for what makes a type valid-covariantly, and attach some queries and personal annotations to each of those rules. A type is valid covariantly if it…
Garrett Biermann
  • 537
  • 1
  • 4
  • 12
9
votes
1 answer

Scala UpperBound and LowerBound concept

Below is the code I am trying to run: class Student { def printDetails = println("I am a student") def printSomeOtherDetails = println("I love Studying") } class ComputerScienceStudent extends Student { override def printDetails = println("I…
Sudipta Deb
  • 1,040
  • 3
  • 23
  • 43
9
votes
1 answer

variance annotation, keeping track "positive" and "negative" positions by Scala compiler

In Programming in Scala page 436, the author gives an example of the compiler checking that each type parameter is only used in positions that are classified appropriately. abstract class Cat[-T, +U] { def meow[W^-](volume: T^-, listener: Cat[U^+,…
platypus
  • 1,165
  • 5
  • 27
  • 47
8
votes
1 answer

Cumulative variance explained for NMDS in R

Is there a way to determine the cumulative variance explained (metric fit or R^2m) from an NMDS object with the function metaMDS? The object returns values for stress, scores, points, but I do not see variance. The function comes from the vegan…
Becca
  • 107
  • 2
  • 8
8
votes
4 answers

Using "adjust" option for lonely PSUs in R survey package: why don't single-PSU strata variances change when data in other strata change?

I have survey data from a stratified simple random sampling design where some strata contain only a single sampling unit (even though the strata population size may be >1). These are referred to as "lonely PSUs" in the R survey package…
Bryan
  • 103
  • 2
  • 7
8
votes
1 answer

How can I highlight variance over a ggplot?

I can't find out how should I put up this ques so I used this method. I have a latitude-longitude dataset. The image posted below is what I want to produce.. This is my dataset: Latitude Longitude 21.06941667 71.07952778 21.06941667…
ayush
  • 343
  • 3
  • 20
8
votes
1 answer

Can I have a type that's both, covariant and contravariant, i.e. fully fungible/changeable with sub and super types?

Can I have a type (for now forgetting its semantics) that can be covariant as well as contravariant? for example: public interface Foo { void DoFooWith(T arg); } Off to Eric Lippert's blog for the meat and potatoes of variance in C#…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
8
votes
3 answers

Understanding delegate contravariance usefulness

So I have a delegate defined as: public delegate void MyDelegate(T myParameter); Resharper suggests that I should make T contravariant as follows: public delegate void MyDelegate(T myParameter); Now, I have a hard time understanding what…
AxiomaticNexus
  • 6,190
  • 3
  • 41
  • 61
7
votes
2 answers

Simulate data for logistic regression with fixed r2

I would like to simulate data for a logistic regression where I can specify its explained variance beforehand. Have a look at the code below. I simulate four independent variables and specify that each logit coefficient should be of size…
Lion Behrens
  • 199
  • 1
  • 11
7
votes
4 answers

Calculating Population Standard Deviation in R

Looking for a way to calculate Population Standard Deviation in R -- using greater than 10 samples. Unable to extract the source C code in R to find the method of calculation. # Sample Standard Deviation # Note: All the below match with 10 or less…
eyeOfTheStorm
  • 351
  • 1
  • 5
  • 15
7
votes
2 answers

Can I "pimp my library" with an analogue of TraversableLike.map that has nicely variant types?

Suppose I want to add functionality like map to a Scala List, something along the lines of list mapmap f, which applies the function f to each element of list twice. (A more serious example might be implementing a parallel or distributed map, but I…
Scott Morrison
  • 3,100
  • 24
  • 39
1 2
3
51 52