splisosm.likelihood
===================

.. py:module:: splisosm.likelihood

.. autoapi-nested-parse::

   Likelihood functions and pvalue-approximation utilities.



Functions
---------

.. autoapisummary::

   splisosm.likelihood.liu_sf
   splisosm.likelihood.log_prob_fastdm
   splisosm.likelihood.log_prob_fastmult
   splisosm.likelihood.log_prob_fastmult_batched
   splisosm.likelihood.log_prob_fastmvn
   splisosm.likelihood.log_prob_fastmvn_batched


Module Contents
---------------

.. py:function:: liu_sf(t, lambs, dofs = None, deltas = None, kurtosis = False)

   Compute p-values for weighted sums of chi-squared variables using the Liu approximation.

   Let :math:`X = \sum_{i=1}^{d} \lambda_i * \chi^2(h_i, \delta_i)` be a linear combination of
   `d` chi-squared random variables, each with degree of freedom :math:`h_i` and noncentrality
   :math:`\delta_i`, this function approximates :math:``Pr(X > t)`` using the
   Liu moment-matching approach :cite:`liu2009new`.

   :param t: Shape (n,), observed test statistic.
   :param lambs: Shape (d,), weights of each chi-squared component.
   :param dofs: Shape (d,), degrees of freedom for each component. Defaults to all ones.
   :param deltas: Shape (d,), noncentrality parameters for each component. Defaults to all zeros.
   :param kurtosis: If True, uses kurtosis matching proposed in :cite:`lee2012optimal`; otherwise uses the original
                    skewness matching :cite:`liu2009new`.

   :returns: P-values of shape (n,) computed as ``Pr(X > t)``.
   :rtype: numpy.ndarray

   .. rubric:: Notes

   From https://github.com/limix/chiscore/blob/master/chiscore/_liu.py


.. py:function:: log_prob_fastdm(concentration, counts, mask = None)

   Compute optimized Dirichlet-multinomial log-likelihood.

   :param concentration: Isoform concentration tensor of shape (n_isos, n_spots).
   :param counts: Isoform count tensor of shape (n_isos, n_spots).
   :param mask: Spot mask of shape (n_spots,). Entries with value ``1``
                are treated as masked and are ignored when computing likelihood.

   :returns: Scalar log-likelihood.
   :rtype: torch.Tensor


.. py:function:: log_prob_fastmult(probs, counts, mask = None)

   Compute optimized multinomial log-likelihood.

   :param probs: Isoform ratio tensor of shape (n_isos, n_spots).
   :param counts: Isoform count tensor of shape (n_isos, n_spots).
   :param mask: Spot mask of shape (n_spots,). Entries with value ``1``
                are treated as masked and are ignored when computing likelihood.

   :returns: Scalar log-likelihood.
   :rtype: torch.Tensor


.. py:function:: log_prob_fastmult_batched(probs, counts, mask = None)

   Compute batched optimized multinomial log-likelihood.

   :param probs: Batched isoform ratio tensor of shape (batch_size, n_isos, n_spots).
   :param counts: Batched isoform count tensor of shape (batch_size, n_isos, n_spots).
   :param mask: Mask tensor of shape (batch_size, n_spots). Entries with value ``1``
                are treated as masked and are ignored when computing likelihood.

   :returns: Log-likelihood values of shape (batch_size,).
   :rtype: torch.Tensor


.. py:function:: log_prob_fastmvn(locs, cov_eigvals, cov_eigvecs, data, mask = None)

   Compute multivariate normal log-likelihood with eigendecomposition.

   Classes along the first axis are treated as independent,
   and the log-likelihood is computed as the sum of log-likelihoods for each class.

   :param locs: Mean tensor of shape (n_classes, n_spots).
   :param cov_eigvals: Covariance eigenvalues of shape (n_classes, n_spots) or (1, n_spots).
   :param cov_eigvecs: Covariance eigenvectors of shape (n_classes, n_spots, n_spots) or (1, n_spots, n_spots).
   :param data: Observation tensor of shape (n_classes, n_spots).
   :param mask: Spot mask of shape (n_spots,). Entries with value ``1``
                are treated as masked and are ignored when computing likelihood.

   :returns: Scalar log-likelihood.
   :rtype: torch.Tensor

   :raises ValueError: If covariance eigendecomposition shapes are incompatible with `data`.


.. py:function:: log_prob_fastmvn_batched(locs, cov_eigvals, cov_eigvecs, data, mask = None, residual_eigval = None)

   Compute batched multivariate normal log-likelihood with eigendecomposition.

   Classes along the second axis are treated as independent,
   and the log-likelihood is computed as the sum of log-likelihoods for each class.

   Supports both full-rank and low-rank covariance approximations.  In the
   **full-rank** case (``residual_eigval=None``) the covariance is
   ``C = V diag(c) V^T`` and both ``cov_eigvals`` and ``cov_eigvecs`` have
   ``n_spots`` entries in their last dimension.

   In the **low-rank** case (``residual_eigval`` provided) the covariance is
   approximated as

   .. math::

       C \approx V_k \operatorname{diag}(c_k) V_k^T
                 + d\,(I - V_k V_k^T)

   where ``V_k`` (n_spots × k) contains the top-k eigenvectors, ``c_k`` (k)
   the corresponding eigenvalues, and ``d`` is the scalar residual eigenvalue
   (the noise-only contribution for the uncaptured modes).  The log-likelihood
   is then computed via the Woodbury identity.

   :param locs: Batched mean tensor of shape (batch_size, n_classes, n_spots).
   :param cov_eigvals: Full-rank: shape (batch_size, n_classes, n_spots).
                       Low-rank:  shape (batch_size, n_classes, k) with k ≤ n_spots.
   :param cov_eigvecs: Full-rank: shape (batch_size, n_classes, n_spots, n_spots).
                       Low-rank:  shape (batch_size, n_classes, n_spots, k).
   :param data: Batched observation of shape (batch_size, n_classes, n_spots).
   :param mask: Batched mask tensor of shape (batch_size, n_spots). Entries with value ``1``
                are treated as masked and are ignored when computing likelihood.
   :type mask: torch.Tensor, optional
   :param residual_eigval: Low-rank residual eigenvalue ``d`` of shape (batch_size, n_classes, 1).
                           When ``None`` (default), full-rank computation is used.
   :type residual_eigval: torch.Tensor, optional

   :returns: Log-likelihood values of shape (batch_size,).
   :rtype: torch.Tensor

   :raises ValueError: If covariance decomposition shapes are incompatible with input sizes.


