How to Calculate Variance in MATLAB?


In this article, we will explore how to calculate variance in MATLAB. In mathematics, variance is a statistical tool used to measure the degree of dispersion of a set of data points around its average value. It is widely used to quantify the diversity or variability of a set of data points.

We can compute variance of a data set by using the following formula:

$\mathrm{Var=\frac{\displaystyle\sum\limits_{i=1}^n (x_i −\bar{x})^2}{n}}$

Where, xi is the individual data points, is the average of data set, and n is the total number of data points in the set.

The following sections of this articles explain how to calculate the variance of a data set with the help of example MATLAB programs.

Calculate Variance in MATLAB

MATLAB provides a built−in function ‘var’ to compute the variance of a set of data point to quantify its dispersion around its average value. The ‘var’ function can have several different syntaxes based on different use cases. Let us discuss each of these syntaxes individually.

Calculate Simple Variance

To calculate the variance of a set of data points, we can use the following default syntax of the of the ‘var’ function:

Variance = var(A);

Where, A is an array or vector of data points.

The following MATLAB program demonstrates the implementation of the default syntax of the ‘var’ function.

Example

% MATLAB code for calculating variance of a data set
% Create a vector of data points
A = [2, 4, 8, 10, 12, 16, 25];
% Calculate the variance of the data set
Variance = var(A);
% Display the original vector and its variance
disp('The input vector is:');
disp(A);
disp('Variance of the vector is:');
disp(Variance);

Output

The input vector is:
    2    4    8   10   12   16   25
Variance of the vector is:
60.333

Code Explanation

In this MATLAB code, firstly we create a vector of data points ‘A’. Then, we use the ‘var’ function to compute the variance of the vector ‘A’. Finally, we display the original vector and its variance by using the ‘disp’ function.

Calculate Weighted Variance

We use the following syntax of the ‘var’ function to compute the weighted variance of a set of data points:

Variance = var(A, w);

Here, A is the vector of data points, and w is the weight vector.

Consider the following MATAB program to understand the implementation of this syntax.

Example

% MATLAB code for calculating weighted variance of a data set
% Create a vector of data points
A = [2, 4, 8, 10, 12, 16, 25];
% Create a weight vector
w = [1, 2, 5, 4, 3, 7, 6];
% Calculate the weighted variance of the data set
Variance = var(A, w);
% Display the original vector and its variance
disp('The input vector is:');
disp(A);
disp('Weighted variance of the vector is:');
disp(Variance);

Output

The input vector is:
     2     4     8    10    12    16    25

Weighted variance of the vector is:
   48.3367

Code Explanation

In this MATLAB code, we start by creating a vector of data points ‘A’ and a weight vector ‘w’. It is to be noted that size of the input vector and weight vector must be the same. Next, we use the ‘var’ function with ‘w’ as the second argument to compute the weighted variance of the vector ‘A’. Finally, we display the original vector and its weighted variance by using the ‘disp’ function.

Calculate Variance along All Dimensions

The following syntax of the ‘var’ function is utilized to compute the variance of a data set along its all dimensions:

Variance = var(A, 0, 'all');

Consider the following MATLAB program to understand the implementation of this syntax.

Example

% MATLAB code for calculating variance of a data set along all dimensions
% Create an array of data points
A = [2, 4, 8; 10, 12, 16; 25, 30, 35];
% Calculate the variance of the data set along all dimensions
Variance = var(A, 0, 'all');	% ‘0’ indicates sample variance
% Display the input array and its variance along all dimensions
disp('The input array is:');
disp(A);
disp('Variance of the array along all dimensions is:');
disp(Variance);

Output

The input array is:
     2     4     8
    10    12    16
    25    30    35

Variance of the array along all dimensions is:
  136.6944

Code Explanation

In this MATLAB code, we start by defining an input array of data points ‘A’. Next, we use the ‘var’ function to calculate the variance of the array ‘A’ along its all dimensions using the ‘all’ option. Finally, we display the input array and its variance along all dimensions by using the ‘disp’ function.

Calculate Variance along a Specific Dimension

The following syntax of the ‘var’ function is used to compute the variance of a data set along a specific dimension:

Variance = var(A, 0, dim);

Here, if dim = 1, the variance will be calculated along the rows of the array, and if dim = 2, the variance will be calculated along the columns of the array.

The following MATLAB program illustrates the implementation of this syntax of the ‘var’ function.

Example

% MATLAB code for calculating variance of a data set along a specific dimension
% Create an array of data points
A = [2, 4, 8; 10, 12, 16; 25, 30, 35];
% Calculate the variance of the data set along rows
Variance_r = var(A, 0, 1);
% Calculate the variance of the data set along columns
Variance_c = var(A, 0, 2);
% Display the input array and its variance
disp('The input array is:');
disp(A);
disp('Variance of the array along rows is:');
disp(Variance_r);
disp('Variance of the array along columns is:');
disp(Variance_c);

Output

The input array is:
     2     4     8
    10    12    16
    25    30    35

Variance of the array along rows is:
  136.3333  177.3333  192.3333

Variance of the array along columns is:
    9.3333
    9.3333
   25.0000

Code Explanation

In this MATLAB code, we define an input array of data points ‘A’. Next, we use the ‘var’ function to calculate the variance of the array ‘A’ with ‘dim = 1’ along its rows and ‘dim = 2’ along the columns respectively. Finally, we display the input array and its variance along rows and columns by using the ‘disp’ function.

Conclusion

Hence, this is all about calculating variance of a data set in MATALB. MATLAB provides a built−in function ‘var’ to compute the variance of a set of data points. In above sections of this article, we have explained all the syntaxes of the ‘var’ function used to compute variance for different use cases.

Updated on: 07-Aug-2023

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements