The covariance of two random variables X and Y is the expectation of their product, minus the product of their expectations:

CovXY = EXY - EX EY

Covariance is of course analogous to the variance of a single random variable. The covariance function of a random process is its autocovariance; if this random process forms a vector, then the autocovariance of this vector forms its covariance matrix.

In object-oriented terminology, covariance relates to rewritten methods, and what types are allowed. The Eiffel FAQ provides this example of two classes: a PARENT class and a CHILD class, with the child class rewriting the foo method.

   class PARENT
      feature
         foo (arg: A) is ...
   end; -- PARENT

   class CHILD
      inherit
         PARENT redefine foo
      feature
         foo (arg: B) is ...
    end; -- CHILD

The question is: what relationship between the types A and B should be allowed? There are 3 possible answers:

  • They're always the same
  • B is a subtype of A -- this is covariance, since the classes and the method parameters move (vary) in the same "direction", ie down the inheritance hierarchy.
  • A is a subtype of B -- the contravariance rule. The classes and the parameters move in different directions.

Sather is contravariant, while Eiffel is covariant.

Log in or register to write something here or to contact authors.