A Covariance Matrix is a type of matrix used to describe the covariance values between two items in a random vector. It is also known as the variance-covariance matrix because the variance of each element is represented along the matrix’s major diagonal and the covariance is represented among the non-diagonal elements.
It’s particularly important in fields like data science, machine learning, and finance, where understanding relationships between multiple variables is crucial and comes in handy when it comes to stochastic modeling and principal component analysis.
It helps us to identify the direction of the relationship(positive or negative) between variables. Covariance matrix are also essential to understand the high- dimensional datasets.
Mathematical Definition
The variance-covariance matrix is a square matrix with diagonal elements that represent the variance and the non-diagonal components that express covariance.
- The covariance of a variable can take any real value- positive, negative, or zero.
- A positive covariance suggests that the two variables have a positive relationship, whereas a negative covariance indicates that they do not.
- If two elements do not vary together, they have a zero covariance.
Learn More: Diagonal Matrix
Covariance Matrix Example
Let's say there are 2 data sets, X = [10, 5] and Y = [3, 9]. The variance of Set X = 6.5 and the variance of Set Y = 9. The covariance between both variables is -15. The covariance matrix is as follows:
\begin{bmatrix} Variance~of~Set~X & Covariance~of~Both~Sets\\ Covariance~of~Both~Sets& Variance~of~Set~Y \end{bmatrix}=\begin{bmatrix} 6.5 & -15\\ -15& 9 \end{bmatrix}
The general form of a covariance matrix is given as follows:
.png)
Where,
- Sample Variance: var(x1) = \frac{\sum_{1}^{n}\left ( x_{i} -\overline{x}\right )^{2} }{n-1}
- Sample Covarinace: cov(x1, y1) = \frac{\sum_{1}^{n}\left (x_{i} -\overline{x}\right )\left(y_{i}-\overline{y}\right)}{n-1}
- Population Variance: var(xn) = \frac{\sum_{1}^{n}\left ( x_{i} -\mu\right )^{2} }{n}
- Population Covariance: cov(xn, yn) = \frac{\sum_{1}^{n}\left ( x_{i} -\mu_{x}\right )\left ( y_{i}-\mu_{y} \right ) }{n}
Here,
- μ is the Mean of Population
- \overline x is the Mean of the Sample
- n is the Number of observations
- xi is the Observation in Dataset x
Let's see the format of the Covariance Matrix of 2 ⨯ 2 and 3 ⨯ 3.
2 ⨯ 2 Covariance Matrix
We know that in a 2 ⨯ 2 matrix there are two rows and two columns. Hence, the 2 ⨯ 2 Covariance Matrix can be expressed as \begin{bmatrix}\mathrm{var(x)}& \mathrm{cov(x,y)} \\\mathrm{cov(x,y)} &\mathrm{var(y)}\end{bmatrix}
3 ⨯ 3 Covariance Matrix
In a 3⨯3 Matrix, there are 3 rows and 3 columns. We know that in a Covariance Matrix, the diagonal elements are variance, and the non-diagonal elements are covariance. Hence, a 3⨯3 Covariance Matrix can be given as \begin{bmatrix}\mathrm{var(x)}&\mathrm{cov(x,y)} &\mathrm{cov(x,z)} \\\mathrm{cov(x,y)} &\mathrm{var(y)} &\mathrm{cov(y,z)} \\\mathrm{cov(x,z)} &\mathrm{cov(y,z)} &\mathrm{var(z)} \\\end{bmatrix}
How to Find the Covariance Matrix?
The dimensions of a covariance matrix are determined by the number of variables in a given data set. If there are only two variables in a set, then the covariance matrix would have two rows and two columns. Similarly, if a data set has three variables, then its covariance matrix would have three rows and three columns.
The data pertains to the marks scored by Anna, Caroline, and Laura in Psychology and History. Make a covariance matrix.
Student | Psychology(X) | History(Y) |
---|
Anna | 80 | 70 |
Caroline | 63 | 20 |
Laura | 100 | 50 |
The following steps have to be followed:
Step 1: Find the mean of variable X. Sum up all the observations in variable X and divide the sum obtained with the number of terms. Thus, (80 + 63 + 100)/3 = 81.
Step 2: Subtract the mean from all observations. (80 - 81), (63 - 81), (100 - 81).
Step 3: Take the squares of the differences obtained above and then add them up. Thus, (80 - 81)2 + (63 - 81)2 + (100 - 81)2.
Step 4: Find the variance of X by dividing the value obtained in Step 3 by 1 less than the total number of observations. var(X) = [(80 - 81)2 + (63 - 81)2 + (100 - 81)2] / (3 - 1) = 343.
Step 5: Similarly, repeat steps 1 to 4 to calculate the variance of Y. Var(Y) = 633.333
Step 6: Choose a pair of variables.
Step 7: Subtract the mean of the first variable (X) from all observations; (80 - 81), (63 - 81), (100 - 81).
Step 8: Repeat the same for variable Y; (70 - 47), (20 - 47), (50 - 47).
Step 9: Multiply the corresponding terms: (80 - 81)(70 - 47), (63 - 81)(20 - 47), (100 - 81)(50 - 47).
Step 10: Find the covariance by adding these values and dividing them by (n - 1). Cov(X, Y) = [(80 - 81)(70 - 47) + (63 - 81)(20 - 47) + (100 - 81)(50 - 47)]/(3-1) = 260.
Step 11: Use the general formula for the covariance matrix to arrange the terms. The matrix becomes: \begin{bmatrix} 343 & 260\\ 260& 633.333 \end{bmatrix}
Properties of the Covariance Matrix
The Properties of the Covariance Matrix are mentioned below:
- A covariance matrix is always square, implying that the number of rows in a covariance matrix is always equal to the number of columns in it.
- A covariance matrix is always symmetric, implying that the transpose of a covariance matrix is always equal to the original matrix.
- A covariance matrix is always positive and semi-definite.
- The eigenvalues of a covariance matrix are always real and non-negative.
Read More,
Real-Life Application of the Covariance Matrix
- Stock Market - Investors use a covariance matrix to see how different stocks move together.
- Weather Forecasting - Meterologist use it to study that how temprature, humidity and wind speed are related over time.
- Image Processing - In facial recognization, covariance helps us to indentify pattern and diffrence between images by analyzing pixel relation.
- Economics - Economists use to analyse how different economic indicators move together.
Solved Examples on Covariance Matrix
Example 1: The marks scored by 3 students in Physics and Biology are given below:
Student | Physics(X) | Biology(Y) |
---|
A | 92 | 80 |
B | 60 | 30 |
C | 100 | 70 |
Calculate the Covariance Matrix from the above data.
Solution:
Sample covariance matrix is given by \frac{\sum_{1}^{n}\left ( x_{i} -\overline{x}\right )^{2} }{n-1} .
Here, μx = 84, n = 3
var(x) = [(92 - 84)2 + (60 - 84)2 + (100 - 84)2] / (3 - 1) = 448
Also, μy = 60, n = 3
var(y) = [(80 - 60)2 + (30 - 60)2 + (70 - 60)2] / (3 - 1) = 700
Now, cov(x, y) = cov(y, x) = [(92 - 84)(80 - 60) + (60 - 84)(30 - 60) + (100 - 84)(70 - 60)] / (3 - 1) = 520.
The population covariance matrix is given as: \begin{bmatrix} 448 & 520\\ 520& 700 \end{bmatrix}
Example 2. Prepare the population covariance matrix from the following table:
Age | Number of People |
---|
29 | 68 |
26 | 60 |
30 | 58 |
35 | 40 |
Solution:
Population variance is given by \frac{\sum_{1}^{n}\left ( x_{i} -\mu\right )^{2} }{n} .
Here, μx = 56.5, n = 4
var(x) = [(68 - 56.5)2 + (60 - 56.5)2 + (58 - 56.5)2 + (40 - 56.5)2 ] / 4 = 104.75
Also, μy = 30, n = 4
var(y) = [(29 - 30)2 + (26 - 30)2 + (30 - 30)2 + (35 - 30)2] / 4 = 10. 5
Now, cov(x, y) = \frac{\sum_{1}^{4}\left ( x_{i} -\mu_{x}\right )\left ( y_{i}-\mu_{y} \right ) }{4}
cov(x, y) = -27
The population covariance matrix is given as: \begin{bmatrix} 104.7 &-27 \\ -27& 10.5 \end{bmatrix}
Example 3. Interpret the following covariance matrix:
\begin{bmatrix} & X & Y & Z\\ X & 60 & 32 & -4\\ Y & 32 & 30 & 0\\ Z & -4 & 0 & 80 \end{bmatrix}
Solution:
- The diagonal elements 60, 30, and 80 indicate the variance in data sets X, Y, and Z respectively. Y shows the lowest variance whereas Z displays the highest variance.
- The covariance for X and Y is 32. As this is a positive number it means that when X increases (or decreases) Y also increases (or decreases)
- The covariance for X and Z is -4. As it is a negative number it implies that when X increases Z decreases and vice-versa.
- The covariance for Y and Z is 0. This means that there is no predictable relationship between the two data sets.
Example 4. Find the sample covariance matrix for the following data:
X | Y | Z |
---|
75 | 10.5 | 45 |
65 | 12.8 | 65 |
22 | 7.3 | 74 |
15 | 2.1 | 76 |
18 | 9.2 | 56 |
Solution:
Sample covariance matrix is given by \frac{\sum_{1}^{n}\left ( x_{i} -\overline{x}\right )^{2} }{n-1} .
n = 5,
- μx = 22.4, var(X) = 321.2 / (5 - 1) = 80.3
- μy = 12.58, var(Y) = 132.148 / 4 = 33.037
- μz = 64, var(Z) = 570 / 4 = 142.5
Now, cov(X, Y) = \frac{\sum_{1}^{5}\left ( x_{i} -22.4\right )\left ( y_{i}-12.58\right ) }{5-1} = -11.76
⇒ cov(X, Z) = \frac{\sum_{1}^{5}\left ( x_{i} -22.4\right )\left ( z_{i}-64 \right ) }{5-1} = 34.97
⇒ cov(Y, Z) = \frac{\sum_{1}^{5}\left ( y_{i} -12.58\right )\left ( z_{i}-64 \right ) }{5-1} = -40.87
The covariance matrix is given as:
\begin{bmatrix} 80.3 & -13.865 &14.25 \\ -13.865 & 33.037 & -39.5250\\ 14.25 & -39.5250 & 142.5 \end{bmatrix}
Practice Problems on Covariance Matrix
Problem 1: Given two sets of data points: X = [2, 4, 6, 8, 10] and Y = [1, 3, 5, 7, 9], calculate the covariance between X and Y.
Problem 2: Calculate the covariance matrix for the following dataset:
Problem 3: Given the covariance matrix:
\Sigma = \begin{bmatrix} 4 & -2 & 0 \\ -2 & 3 & 1 \\ 0 & 1 & 5 \\ \end{bmatrix}
Problem 4: Identify the variances and covariances between the variables.
Problem 5: Handling Missing Data: How do you compute the covariance matrix when some data points are missing? Provide an example with missing values.
Problem 6: Covariance Matrix in Multivariate Normal Distribution: Explain the role of the covariance matrix in defining the shape of the multivariate normal distribution.
Problem 7: Visualizing Covariance Matrix: Plot the covariance matrix for a dataset using Python. Provide a code example and explain the visualization.
Problem 8: Covariance Matrix for Time Series Data: How do you calculate and interpret the covariance matrix for time series data? Provide a sample time series dataset and the corresponding covariance matrix.
Conclusion
- The covariance matrix is a fundamental concept in statistics and data analysis, providing insight into the relationships between multiple variables.
- It captures the extent to which two variables change together and is essential for the various applications such as the Principal Component Analysis (PCA), financial analysis and multivariate statistics.
- By understanding and interpreting the covariance matrix, one can make more informed decisions and uncover deeper patterns in data.
Similar Reads
Maths Mathematics, often referred to as "math" for short. It is the study of numbers, quantities, shapes, structures, patterns, and relationships. It is a fundamental subject that explores the logical reasoning and systematic approach to solving problems. Mathematics is used extensively in various fields
5 min read
Basic Arithmetic
What are Numbers?Numbers are symbols we use to count, measure, and describe things. They are everywhere in our daily lives and help us understand and organize the world.Numbers are like tools that help us:Count how many things there are (e.g., 1 apple, 3 pencils).Measure things (e.g., 5 meters, 10 kilograms).Show or
15+ min read
Arithmetic OperationsArithmetic Operations are the basic mathematical operationsâAddition, Subtraction, Multiplication, and Divisionâused for calculations. These operations form the foundation of mathematics and are essential in daily life, such as sharing items, calculating bills, solving time and work problems, and in
9 min read
Fractions - Definition, Types and ExamplesFractions are numerical expressions used to represent parts of a whole or ratios between quantities. They consist of a numerator (the top number), indicating how many parts are considered, and a denominator (the bottom number), showing the total number of equal parts the whole is divided into. For E
7 min read
What are Decimals?Decimals are numbers that use a decimal point to separate the whole number part from the fractional part. This system helps represent values between whole numbers, making it easier to express and measure smaller quantities. Each digit after the decimal point represents a specific place value, like t
10 min read
ExponentsExponents are a way to show that a number (base) is multiplied by itself many times. It's written as a small number (called the exponent) to the top right of the base number.Think of exponents as a shortcut for repeated multiplication:23 means 2 x 2 x 2 = 8 52 means 5 x 5 = 25So instead of writing t
9 min read
PercentageIn mathematics, a percentage is a figure or ratio that signifies a fraction out of 100, i.e., A fraction whose denominator is 100 is called a Percent. In all the fractions where the denominator is 100, we can remove the denominator and put the % sign.For example, the fraction 23/100 can be written a
5 min read
Algebra
Variable in MathsA variable is like a placeholder or a box that can hold different values. In math, it's often represented by a letter, like x or y. The value of a variable can change depending on the situation. For example, if you have the equation y = 2x + 3, the value of y depends on the value of x. So, if you ch
5 min read
Polynomials| Degree | Types | Properties and ExamplesPolynomials are mathematical expressions made up of variables (often represented by letters like x, y, etc.), constants (like numbers), and exponents (which are non-negative integers). These expressions are combined using addition, subtraction, and multiplication operations.A polynomial can have one
9 min read
CoefficientA coefficient is a number that multiplies a variable in a mathematical expression. It tells you how much of that variable you have. For example, in the term 5x, the coefficient is 5 â it means 5 times the variable x.Coefficients can be positive, negative, or zero. Algebraic EquationA coefficient is
8 min read
Algebraic IdentitiesAlgebraic Identities are fundamental equations in algebra where the left-hand side of the equation is always equal to the right-hand side, regardless of the values of the variables involved. These identities play a crucial role in simplifying algebraic computations and are essential for solving vari
14 min read
Properties of Algebraic OperationsAlgebraic operations are mathematical processes that involve the manipulation of numbers, variables, and symbols to produce new results or expressions. The basic algebraic operations are:Addition ( + ): The process of combining two or more numbers to get a sum. For example, 3 + 5 = 8.Subtraction (â)
3 min read
Geometry
Lines and AnglesLines and Angles are the basic terms used in geometry. They provide a base for understanding all the concepts of geometry. We define a line as a 1-D figure that can be extended to infinity in opposite directions, whereas an angle is defined as the opening created by joining two or more lines. An ang
9 min read
Geometric Shapes in MathsGeometric shapes are mathematical figures that represent the forms of objects in the real world. These shapes have defined boundaries, angles, and surfaces, and are fundamental to understanding geometry. Geometric shapes can be categorized into two main types based on their dimensions:2D Shapes (Two
2 min read
Area and Perimeter of Shapes | Formula and ExamplesArea and Perimeter are the two fundamental properties related to 2-dimensional shapes. Defining the size of the shape and the length of its boundary. By learning about the areas of 2D shapes, we can easily determine the surface areas of 3D bodies and the perimeter helps us to calculate the length of
10 min read
Surface Areas and VolumesSurface Area and Volume are two fundamental properties of a three-dimensional (3D) shape that help us understand and measure the space they occupy and their outer surfaces.Knowing how to determine surface area and volumes can be incredibly practical and handy in cases where you want to calculate the
10 min read
Points, Lines and PlanesPoints, Lines, and Planes are basic terms used in Geometry that have a specific meaning and are used to define the basis of geometry. We define a point as a location in 3-D or 2-D space that is represented using coordinates. We define a line as a geometrical figure that is extended in both direction
14 min read
Coordinate Axes and Coordinate Planes in 3D spaceIn a plane, we know that we need two mutually perpendicular lines to locate the position of a point. These lines are called coordinate axes of the plane and the plane is usually called the Cartesian plane. But in real life, we do not have such a plane. In real life, we need some extra information su
6 min read
Trigonometry & Vector Algebra
Trigonometric RatiosThere are three sides of a triangle Hypotenuse, Adjacent, and Opposite. The ratios between these sides based on the angle between them is called Trigonometric Ratio. The six trigonometric ratios are: sine (sin), cosine (cos), tangent (tan), cotangent (cot), cosecant (cosec), and secant (sec).As give
4 min read
Trigonometric Equations | Definition, Examples & How to SolveTrigonometric equations are mathematical expressions that involve trigonometric functions (such as sine, cosine, tangent, etc.) and are set equal to a value. The goal is to find the values of the variable (usually an angle) that satisfy the equation.For example, a simple trigonometric equation might
9 min read
Trigonometric IdentitiesTrigonometric identities play an important role in simplifying expressions and solving equations involving trigonometric functions. These identities, which include relationships between angles and sides of triangles, are widely used in fields like geometry, engineering, and physics. Some important t
10 min read
Trigonometric FunctionsTrigonometric Functions, often simply called trig functions, are mathematical functions that relate the angles of a right triangle to the ratios of the lengths of its sides.Trigonometric functions are the basic functions used in trigonometry and they are used for solving various types of problems in
6 min read
Inverse Trigonometric Functions | Definition, Formula, Types and Examples Inverse trigonometric functions are the inverse functions of basic trigonometric functions. In mathematics, inverse trigonometric functions are also known as arcus functions or anti-trigonometric functions. The inverse trigonometric functions are the inverse functions of basic trigonometric function
11 min read
Inverse Trigonometric IdentitiesInverse trigonometric functions are also known as arcus functions or anti-trigonometric functions. These functions are the inverse functions of basic trigonometric functions, i.e., sine, cosine, tangent, cosecant, secant, and cotangent. It is used to find the angles with any trigonometric ratio. Inv
9 min read
Calculus
Introduction to Differential CalculusDifferential calculus is a branch of calculus that deals with the study of rates of change of functions and the behaviour of these functions in response to infinitesimal changes in their independent variables.Some of the prerequisites for Differential Calculus include:Independent and Dependent Varia
6 min read
Limits in CalculusIn mathematics, a limit is a fundamental concept that describes the behaviour of a function or sequence as its input approaches a particular value. Limits are used in calculus to define derivatives, continuity, and integrals, and they are defined as the approaching value of the function with the inp
12 min read
Continuity of FunctionsContinuity of functions is an important unit of Calculus as it forms the base and it helps us further to prove whether a function is differentiable or not. A continuous function is a function which when drawn on a paper does not have a break. The continuity can also be proved using the concept of li
13 min read
DifferentiationDifferentiation in mathematics refers to the process of finding the derivative of a function, which involves determining the rate of change of a function with respect to its variables.In simple terms, it is a way of finding how things change. Imagine you're driving a car and looking at how your spee
2 min read
Differentiability of a Function | Class 12 MathsContinuity or continuous which means, "a function is continuous at its domain if its graph is a curve without breaks or jumps". A function is continuous at a point in its domain if its graph does not have breaks or jumps in the immediate neighborhood of the point. Continuity at a Point: A function f
11 min read
IntegrationIntegration, in simple terms, is a way to add up small pieces to find the total of something, especially when those pieces are changing or not uniform.Imagine you have a car driving along a road, and its speed changes over time. At some moments, it's going faster; at other moments, it's slower. If y
3 min read
Probability and Statistics
Basic Concepts of ProbabilityProbability is defined as the likelihood of the occurrence of any event. It is expressed as a number between 0 and 1, where 0 is the probability of an impossible event and 1 is the probability of a sure event.Concepts of Probability are used in various real life scenarios : Stock Market : Investors
7 min read
Bayes' TheoremBayes' Theorem is a mathematical formula used to determine the conditional probability of an event based on prior knowledge and new evidence. It adjusts probabilities when new information comes in and helps make better decisions in uncertain situations.Bayes' Theorem helps us update probabilities ba
13 min read
Probability Distribution - Function, Formula, TableA probability distribution is a mathematical function or rule that describes how the probabilities of different outcomes are assigned to the possible values of a random variable. It provides a way of modeling the likelihood of each outcome in a random experiment.While a Frequency Distribution shows
13 min read
Descriptive StatisticStatistics is the foundation of data science. Descriptive statistics are simple tools that help us understand and summarize data. They show the basic features of a dataset, like the average, highest and lowest values and how spread out the numbers are. It's the first step in making sense of informat
5 min read
What is Inferential Statistics?Inferential statistics is an important tool that allows us to make predictions and conclusions about a population based on sample data. Unlike descriptive statistics, which only summarize data, inferential statistics let us test hypotheses, make estimates, and measure the uncertainty about our predi
7 min read
Measures of Central Tendency in StatisticsCentral tendencies in statistics are numerical values that represent the middle or typical value of a dataset. Also known as averages, they provide a summary of the entire data, making it easier to understand the overall pattern or behavior. These values are useful because they capture the essence o
11 min read
Set TheorySet theory is a branch of mathematics that deals with collections of objects, called sets. A set is simply a collection of distinct elements, such as numbers, letters, or even everyday objects, that share a common property or rule.Example of SetsSome examples of sets include:A set of fruits: {apple,
3 min read
Practice