Understand unbiased Parameter When Computing Variance and Standard-deviation in Pytorch – Pytorch Tutorial

By | October 13, 2022

When we are computing variance and standard-deviation of a pytorch tensor, we will see unbiased parameter.

For example:

torch.var_mean(input, dim, unbiased, keepdim=False, *, out=None)
torch.var(input, dim, unbiased, keepdim=False, *, out=None) 
torch.std(input, dim, unbiased, keepdim=False, *, out=None)

How to understand the effect of unbiased? In this tutorial, we will introduce it.

As to torch.var_mean() function, when unbiased = True, it means Bessel’s correction will be used will be used. However, it is so abstract.

How to understand unbiased when computing variance and standard-deviation?

  • When unbiased = True, we will compute population variance.
  • When unbiased = False, we will compute sample variance.

Sample Variance vs Population Variance

We will use an example to evaluate.

import torch
import numpy as np

x_len = torch.from_numpy(np.arange(10.0))

var_1, mean_1 = torch.var_mean(x_len, unbiased=False)
var_2, mean_2 = torch.var_mean(x_len, unbiased=True)

print(var_1, mean_1)
print(var_2, mean_2)

print("population variance: unbiased=True")
var_11 = torch.square(x_len - mean_1)/x_len.shape[0]
var_11 = var_11.sum()

print("sample variance: unbiased=False")
var_22 = torch.square(x_len - mean_1)/(x_len.shape[0]-1)
var_22 = var_22.sum()

Run this code, we will see:

tensor(8.2500, dtype=torch.float64) tensor(4.5000, dtype=torch.float64)
tensor(9.1667, dtype=torch.float64) tensor(4.5000, dtype=torch.float64)
population variance: unbiased=True
tensor(8.2500, dtype=torch.float64)
sample variance: unbiased=False
tensor(9.1667, dtype=torch.float64)

We can find:

unbiased=False, sample variance = 9.1667

unbiased=True, population variance = 8.2500

Leave a Reply

Your email address will not be published. Required fields are marked *