import torch


def compute_var_and_mean_sq(lst):
  """Compute variance and mean square of a list of samples."""
  num_samples = len(lst)
  tensor = torch.stack(lst)
  mean = torch.mean(tensor, 0, keepdim=True)
  # estimate variance
  var = (tensor - mean).pow(2).sum(0) / (num_samples - 1)
  # estimate E[x^2]. cannot estimate E[x]^2 without bias
  square = tensor.pow(2).mean(0)
  return var.mean(0).mean(0), square.mean(0).mean(0)