class_docstr: Implements Welford's online scheme for estimating (co)variance (see :math:`[1]`).
  Useful for adapting diagonal and dense mass structures for HMC.
  
  **References**
  
  [1] `The Art of Computer Programming`,
  Donald E. Knuth
class_docstr_tok: ["Implements","Welford","'s","online","scheme","for","estimating","(","co",")","variance","(","see",":","math",":","`","[","1","]","`",")",".","Useful","for","adapting","diagonal","and","dense","mass","structures","for","HMC",".","*","*","References","*","*","[","1","]","`","The","Art","of","Computer","Programming","`",",","Donald","E.","Knuth"]
code: def get_covariance(self, regularize=True):
      if self.n_samples < 2:
          raise RuntimeError(
              "Insufficient samples to estimate covariance"
          )
      cov = self._m2 / (self.n_samples - 1)
      if regularize:
          # Regularization from stan
          scaled_cov = (self.n_samples / (self.n_samples + 5.0)) * cov
          shrinkage = 1e-3 * (5.0 / (self.n_samples + 5.0))
          if self.diagonal:
              cov = scaled_cov + shrinkage
          else:
              scaled_cov.view(-1)[
                  :: scaled_cov.size(0) + 1
              ] += shrinkage
              cov = scaled_cov
      return cov
  
file_path: pyro/pyro/ops/welford.py
imports: import torch
label_desc: Use image node_id 4 for calling the WelfordCovariance obj's underlying member method code with example usage: obj.get_covariance(regularize) and returns: cov