class_docstr: Creates a Wishart distribution parameterized by a symmetric positive definite matrix :math:`\Sigma`,
  or its Cholesky decomposition :math:`\mathbf{\Sigma} = \mathbf{L}\mathbf{L}^\top`
  
  Example:
      >>> # xdoctest: +SKIP("FIXME: scale_tril must be at least two-dimensional")
      >>> m = Wishart(torch.Tensor([2]), covariance_matrix=torch.eye(2))
      >>> m.sample()  # Wishart distributed with mean=`df * I` and
      >>>             # variance(x_ij)=`df` for i != j and variance(x_ij)=`2 * df` for i == j
  
  Args:
      df (float or Tensor): real-valued parameter larger than the (dimension of Square matrix) - 1
      covariance_matrix (Tensor): positive-definite covariance matrix
      precision_matrix (Tensor): positive-definite precision matrix
      scale_tril (Tensor): lower-triangular factor of covariance, with positive-valued diagonal
  Note:
      Only one of :attr:`covariance_matrix` or :attr:`precision_matrix` or
      :attr:`scale_tril` can be specified.
      Using :attr:`scale_tril` will be more efficient: all computations internally
      are based on :attr:`scale_tril`. If :attr:`covariance_matrix` or
      :attr:`precision_matrix` is passed instead, it is only used to compute
      the corresponding lower triangular matrices using a Cholesky decomposition.
      'torch.distributions.LKJCholesky' is a restricted Wishart distribution.[1]
  
  **References**
  
  [1] Wang, Z., Wu, Y. and Chu, H., 2018. `On equivalence of the LKJ distribution and the restricted Wishart distribution`.
  [2] Sawyer, S., 2007. `Wishart Distributions and Inverse-Wishart Sampling`.
  [3] Anderson, T. W., 2003. `An Introduction to Multivariate Statistical Analysis (3rd ed.)`.
  [4] Odell, P. L. & Feiveson, A. H., 1966. `A Numerical Procedure to Generate a SampleCovariance Matrix`. JASA, 61(313):199-203.
  [5] Ku, Y.-C. & Bloomfield, P., 2010. `Generating Random Wishart Matrices with Fractional Degrees of Freedom in OX`.
class_docstr_tok: ["Creates","a","Wishart","distribution","parameterized","by","a","symmetric","positive","definite","matrix",":","math",":","`","\\Sigma","`",",","or","its","Cholesky","decomposition",":","math",":","`","\\mathbf","{","\\Sigma","}","=","\\mathbf","{","L","}","\\mathbf","{","L","}","^\\top","`","Example",":",">",">",">","#","xdoctest",":","+SKIP","(","``","FIXME",":","scale_tril","must","be","at","least","two-dimensional","''",")",">",">",">","m","=","Wishart","(","torch.Tensor","(","[","2","]",")",",","covariance_matrix=torch.eye","(","2",")",")",">",">",">","m.sample","(",")","#","Wishart","distributed","with","mean=","`","df","*","I","`","and",">",">",">","#","variance","(","x_ij",")","=","`","df","`","for","i","!","=","j","and","variance","(","x_ij",")","=","`","2","*","df","`","for","i","==","j","Args",":","df","(","float","or","Tensor",")",":","real-valued","parameter","larger","than","the","(","dimension","of","Square","matrix",")","-","1","covariance_matrix","(","Tensor",")",":","positive-definite","covariance","matrix","precision_matrix","(","Tensor",")",":","positive-definite","precision","matrix","scale_tril","(","Tensor",")",":","lower-triangular","factor","of","covariance",",","with","positive-valued","diagonal","Note",":","Only","one","of",":","attr",":","`","covariance_matrix","`","or",":","attr",":","`","precision_matrix","`","or",":","attr",":","`","scale_tril","`","can","be","specified",".","Using",":","attr",":","`","scale_tril","`","will","be","more","efficient",":","all","computations","internally","are","based","on",":","attr",":","`","scale_tril","`",".","If",":","attr",":","`","covariance_matrix","`","or",":","attr",":","`","precision_matrix","`","is","passed","instead",",","it","is","only","used","to","compute","the","corresponding","lower","triangular","matrices","using","a","Cholesky","decomposition",".","'torch.distributions.LKJCholesky","'","is","a","restricted","Wishart","distribution",".","[","1","]","*","*","References","*","*","[","1","]","Wang",",","Z.",",","Wu",",","Y.","and","Chu",",","H.",",","2018",".","`","On","equivalence","of","the","LKJ","distribution","and","the","restricted","Wishart","distribution","`",".","[","2","]","Sawyer",",","S.",",","2007",".","`","Wishart","Distributions","and","Inverse-Wishart","Sampling","`",".","[","3","]","Anderson",",","T.","W.",",","2003",".","`","An","Introduction","to","Multivariate","Statistical","Analysis","(","3rd","ed.",")","`",".","[","4","]","Odell",",","P.","L.","&","Feiveson",",","A.","H.",",","1966",".","`","A","Numerical","Procedure","to","Generate","a","SampleCovariance","Matrix","`",".","JASA",",","61","(","313",")",":199-203",".","[","5","]","Ku",",","Y.-C.","&","Bloomfield",",","P.",",","2010",".","`","Generating","Random","Wishart","Matrices","with","Fractional","Degrees","of","Freedom","in","OX","`","."]
docstr: .. warning::
      In some cases, sampling algorithm based on Bartlett decomposition may return singular matrix samples.
      Several tries to correct singular samples are performed by default, but it may end up returning
      singular matrix samples. Singular samples may return `-inf` values in `.log_prob()`.
      In those cases, the user should validate the samples and either fix the value of `df`
      or adjust `max_try_correction` value for argument in `.rsample` accordingly.
docstr_tok: ["..","warning",":",":","In","some","cases",",","sampling","algorithm","based","on","Bartlett","decomposition","may","return","singular","matrix","samples",".","Several","tries","to","correct","singular","samples","are","performed","by","default",",","but","it","may","end","up","returning","singular","matrix","samples",".","Singular","samples","may","return","`","-inf","`","values","in","`",".log_prob","(",")","`",".","In","those","cases",",","the","user","should","validate","the","samples","and","either","fix","the","value","of","`","df","`","or","adjust","`","max_try_correction","`","value","for","argument","in","`",".rsample","`","accordingly","."]
code: def rsample(self, sample_shape=torch.Size(), max_try_correction=None):
      r"""
      .. warning::
          In some cases, sampling algorithm based on Bartlett decomposition may return singular matrix samples.
          Several tries to correct singular samples are performed by default, but it may end up returning
          singular matrix samples. Singular samples may return `-inf` values in `.log_prob()`.
          In those cases, the user should validate the samples and either fix the value of `df`
          or adjust `max_try_correction` value for argument in `.rsample` accordingly.
      """
  
      if max_try_correction is None:
          max_try_correction = (
              3 if torch._C._get_tracing_state() else 10
          )
  
      sample_shape = torch.Size(sample_shape)
      sample = self._bartlett_sampling(sample_shape)
  
      # Below part is to improve numerical stability temporally and should be removed in the future
      is_singular = self.support.check(sample)
      if self._batch_shape:
          is_singular = is_singular.amax(self._batch_dims)
  
      if torch._C._get_tracing_state():
          # Less optimized version for JIT
          for _ in range(max_try_correction):
              sample_new = self._bartlett_sampling(sample_shape)
              sample = torch.where(is_singular, sample_new, sample)
  
              is_singular = ~self.support.check(sample)
              if self._batch_shape:
                  is_singular = is_singular.amax(self._batch_dims)
  
      else:
          # More optimized version with data-dependent control flow.
          if is_singular.any():
              warnings.warn("Singular sample detected.")
  
              for _ in range(max_try_correction):
                  sample_new = self._bartlett_sampling(
                      is_singular[is_singular].shape
                  )
                  sample[is_singular] = sample_new
  
                  is_singular_new = ~self.support.check(sample_new)
                  if self._batch_shape:
                      is_singular_new = is_singular_new.amax(
                          self._batch_dims
                      )
                  is_singular[is_singular.clone()] = is_singular_new
  
                  if not is_singular.any():
                      break
  
      return sample
  
imports: import math
  import warnings
  from numbers import Number
  from typing import Optional, Union
  import torch
  from torch import nan
  from torch.distributions import constraints
  from torch.distributions.exp_family import ExponentialFamily
  from torch.distributions.multivariate_normal import _precision_to_scale_tril
  from torch.distributions.utils import lazy_property
label_desc: Use image node_id 10 for calling the Wishart obj's underlying member method code with example usage: obj.rsample(sample_shape, max_try_correction) and returns: sample