class_docstr: Distribution over coalescent times given irregular sampled ``leaf_times``
  and piecewise constant coalescent rates defined on a regular time grid.
  
  This assumes a piecewise constant base coalescent rate specified on time
  intervals ``(-inf,1]``, ``[1,2]``, ..., ``[T-1,inf)``,  where ``T =
  rate_grid.size(-1)``. Leaves may be sampled at arbitrary real times, but
  are commonly sampled in the interval ``[0, T]``.
  
  Sample values will be sorted sets of binary coalescent times. Each sample
  ``value`` will have cardinality ``value.size(-1) = leaf_times.size(-1) -
  1``, so that phylogenies are complete binary trees.  This distribution can
  thus be batched over multiple samples of phylogenies given fixed (number
  of) leaf times, e.g. over phylogeny samples from BEAST or MrBayes.
  
  This distribution implements :meth:`log_prob` but not ``.sample()``.
  
  See also :class:`~pyro.distributions.CoalescentRateLikelihood`.
  
  **References**
  
  [1] J.F.C. Kingman (1982)
      "On the Genealogy of Large Populations"
      Journal of Applied Probability
  [2] J.F.C. Kingman (1982)
      "The Coalescent"
      Stochastic Processes and their Applications
  [3] A. Popinga, T. Vaughan, T. Statler, A.J. Drummond (2014)
      "Inferring epidemiological dynamics with Bayesian coalescent inference:
      The merits of deterministic and stochastic models"
      https://arxiv.org/pdf/1407.1792.pdf
  
  :param torch.Tensor leaf_times: Tensor of times of sampling events, i.e.
      leaf nodes in the phylogeny. These can be arbitrary real numbers with
      arbitrary order and duplicates.
  :param torch.Tensor rate_grid: Tensor of base coalescent rates (pairwise
      rate of coalescence). For example in a simple SIR model this might be
      ``beta S / I``. The rightmost dimension is time, and this tensor
      represents a (batch of) rates that are piecewise constant in time.
class_docstr_tok: ["Distribution","over","coalescent","times","given","irregular","sampled","``","leaf_times","``","and","piecewise","constant","coalescent","rates","defined","on","a","regular","time","grid",".","This","assumes","a","piecewise","constant","base","coalescent","rate","specified","on","time","intervals","``","(","-inf,1","]","``",",","``","[","1,2","]","``",",","...",",","``","[","T-1",",","inf",")","``",",","where","``","T","=","rate_grid.size","(","-1",")","``",".","Leaves","may","be","sampled","at","arbitrary","real","times",",","but","are","commonly","sampled","in","the","interval","``","[","0",",","T","]","``",".","Sample","values","will","be","sorted","sets","of","binary","coalescent","times",".","Each","sample","``","value","``","will","have","cardinality","``","value.size","(","-1",")","=","leaf_times.size","(","-1",")","-","1","``",",","so","that","phylogenies","are","complete","binary","trees",".","This","distribution","can","thus","be","batched","over","multiple","samples","of","phylogenies","given","fixed","(","number","of",")","leaf","times",",","e.g",".","over","phylogeny","samples","from","BEAST","or","MrBayes",".","This","distribution","implements",":","meth",":","`","log_prob","`","but","not","``",".sample","(",")","``",".","See","also",":","class",":","`","~pyro.distributions.CoalescentRateLikelihood","`",".","*","*","References","*","*","[","1","]","J.F.C",".","Kingman","(","1982",")","``","On","the","Genealogy","of","Large","Populations","''","Journal","of","Applied","Probability","[","2","]","J.F.C",".","Kingman","(","1982",")","``","The","Coalescent","''","Stochastic","Processes","and","their","Applications","[","3","]","A.","Popinga",",","T.","Vaughan",",","T.","Statler",",","A.J",".","Drummond","(","2014",")","``","Inferring","epidemiological","dynamics","with","Bayesian","coalescent","inference",":","The","merits","of","deterministic","and","stochastic","models","''","https",":","\/\/arxiv.org\/pdf\/1407.1792.pdf",":","param","torch.Tensor","leaf_times",":","Tensor","of","times","of","sampling","events",",","i.e",".","leaf","nodes","in","the","phylogeny",".","These","can","be","arbitrary","real","numbers","with","arbitrary","order","and","duplicates",".",":","param","torch.Tensor","rate_grid",":","Tensor","of","base","coalescent","rates","(","pairwise","rate","of","coalescence",")",".","For","example","in","a","simple","SIR","model","this","might","be","``","beta","S","\/","I","``",".","The","rightmost","dimension","is","time",",","and","this","tensor","represents","a","(","batch","of",")","rates","that","are","piecewise","constant","in","time","."]
docstr: Computes likelihood as in equations 7-8 of [3].
  
  This has time complexity ``O(T + S N log(N))`` where ``T`` is the
  number of time steps, ``N`` is the number of leaves, and ``S =
  sample_shape.numel()`` is the number of samples of ``value``.
  
  :param torch.Tensor value: A tensor of coalescent times. These denote
      sets of size ``leaf_times.size(-1) - 1`` along the trailing
      dimension and should be sorted along that dimension.
  :returns: Likelihood ``p(coal_times | leaf_times, rate_grid)``
  :rtype: torch.Tensor
docstr_tok: ["Computes","likelihood","as","in","equations","7-8","of","[","3","]",".","This","has","time","complexity","``","O","(","T","+","S","N","log","(","N",")",")","``","where","``","T","``","is","the","number","of","time","steps",",","``","N","``","is","the","number","of","leaves",",","and","``","S","=","sample_shape.numel","(",")","``","is","the","number","of","samples","of","``","value","``",".",":","param","torch.Tensor","value",":","A","tensor","of","coalescent","times",".","These","denote","sets","of","size","``","leaf_times.size","(","-1",")","-","1","``","along","the","trailing","dimension","and","should","be","sorted","along","that","dimension",".",":","returns",":","Likelihood","``","p","(","coal_times","|","leaf_times",",","rate_grid",")","``",":","rtype",":","torch.Tensor"]
code: def log_prob(self, value):
      """
      Computes likelihood as in equations 7-8 of [3].
  
      This has time complexity ``O(T + S N log(N))`` where ``T`` is the
      number of time steps, ``N`` is the number of leaves, and ``S =
      sample_shape.numel()`` is the number of samples of ``value``.
  
      :param torch.Tensor value: A tensor of coalescent times. These denote
          sets of size ``leaf_times.size(-1) - 1`` along the trailing
          dimension and should be sorted along that dimension.
      :returns: Likelihood ``p(coal_times | leaf_times, rate_grid)``
      :rtype: torch.Tensor
      """
      if self._validate_args:
          self._validate_sample(value)
      coal_times = value
      phylogeny = _make_phylogeny(self.leaf_times, coal_times)
  
      # Compute survival factors for closed intervals.
      cumsum = self.rate_grid.cumsum(-1)
      cumsum = torch.nn.functional.pad(cumsum, (1, 0), value=0)
      integral = _interpolate_gather(
          cumsum, phylogeny.times[..., 1:]
      )  # ignore the final lonely leaf
      integral = integral[..., :-1] - integral[..., 1:]
      integral = integral.clamp(
          min=torch.finfo(integral.dtype).tiny
      )  # avoid nan
      log_prob = -(phylogeny.binomial[..., 1:-1] * integral).sum(-1)
  
      # Compute density of coalescent events.
      i = coal_times.floor().clamp(min=0, max=self.duration - 1).long()
      rates = phylogeny.coal_binomial * _gather(self.rate_grid, -1, i)
      log_prob = log_prob + safe_log(rates).sum(-1)
  
      batch_shape = broadcast_shape(self.batch_shape, value.shape[:-1])
      log_prob = log_prob.expand(batch_shape)
      return log_prob
  
imports: import functools
  import weakref
  from collections import namedtuple
  import torch
  from torch.distributions import constraints
  from pyro.distributions.util import broadcast_shape, is_validation_enabled
  from pyro.ops.special import safe_log
  from .torch_distribution import TorchDistribution
label_desc: Use image node_id 5 for calling the CoalescentTimesWithRate obj's underlying member method code with example usage: obj.log_prob(value) and returns: log_prob