Skip to content

poisson

Poisson

Bases: Marginal

Poisson marginal estimator

This subclass behaves like Marginal but assumes each gene follows a Poisson distribution with mean mu_j(x) that depends on covariates x via the provided formula object.

The allowed formula keys are 'mean', defaulting to a single mean term if a string formula is supplied.

Examples:

>>> from scdesigner.distributions import Poisson
>>> import scdesigner.datasets
>>>
>>> pancreas = scdesigner.datasets.pancreas()
>>>
>>> sim = Poisson(formula="~ bs(pseudotime, df=5)")
>>> sim.setup_data(pancreas)
>>> sim.fit(max_epochs=1, verbose=False)
>>>
>>> # evaluate p(y | x) and mu(x)
>>> y, x = next(iter(sim.loader))
>>> l = sim.likelihood((y, x))
>>> y_hat = sim.predict(x)
>>>
>>> # convert to quantiles and back
>>> u = sim.uniformize(y, x)
>>> x_star = sim.invert(u, x)
Source code in scdesigner/src/scdesigner/distributions/poisson.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
class Poisson(Marginal):
    """Poisson marginal estimator

    This subclass behaves like `Marginal` but assumes each gene follows a
    Poisson distribution with mean `mu_j(x)` that depends on covariates `x`
    via the provided `formula` object.

    The allowed formula keys are 'mean', defaulting to a single `mean` term
    if a string formula is supplied.

    Examples
    --------
    >>> from scdesigner.distributions import Poisson
    >>> import scdesigner.datasets
    >>>
    >>> pancreas = scdesigner.datasets.pancreas()
    >>>
    >>> sim = Poisson(formula="~ bs(pseudotime, df=5)")
    >>> sim.setup_data(pancreas)
    >>> sim.fit(max_epochs=1, verbose=False)
    >>>
    >>> # evaluate p(y | x) and mu(x)
    >>> y, x = next(iter(sim.loader))
    >>> l = sim.likelihood((y, x))
    >>> y_hat = sim.predict(x)
    >>>
    >>> # convert to quantiles and back
    >>> u = sim.uniformize(y, x)
    >>> x_star = sim.invert(u, x)
    """
    def __init__(self, formula: Union[Dict, str], **kwargs):
        formula = standardize_formula(formula, allowed_keys=['mean'])
        super().__init__(formula, **kwargs)

    def setup_optimizer(
            self,
            optimizer_class: Optional[callable] = torch.optim.Adam,
            **optimizer_kwargs,
    ):
        if self.loader is None:
            raise RuntimeError("self.loader is not set (call setup_data first)")

        def nll(batch):
            return -self.likelihood(batch).sum()

        self.predict = GLMPredictor(
            n_outcomes=self.n_outcomes,
            feature_dims=self.feature_dims,
            loss_fn=nll,
            optimizer_class=optimizer_class,
            optimizer_kwargs=optimizer_kwargs,
            device=self.device,
        )

    def _initialize_parameters(self, **kwargs):
        beta, _ = initialize_parameters(
            self.train_loader,
            self.n_outcomes,
            self.feature_dims["mean"],
            p_disp=1,
        )
        self.predict.coefs["mean"].data.copy_(beta)

    def likelihood(self, batch) -> torch.Tensor:
        """Compute the log-likelihood"""
        y, x = batch
        params = self.predict(x)
        mu = params.get("mean")
        return y * torch.log(mu) - mu - torch.lgamma(y + 1)

    def invert(self, u: torch.Tensor, x: Dict[str, torch.Tensor]) -> torch.Tensor:
        """Invert pseudoobservations."""
        mu, u = self._local_params(x, u)
        y = poisson(mu).ppf(u)
        return torch.from_numpy(y).float()

    def uniformize(self, y: torch.Tensor, x: Dict[str, torch.Tensor], epsilon=1e-6) -> torch.Tensor:
        """Return uniformized pseudo-observations for counts y given covariates x."""
        # cdf values using scipy's parameterization
        mu, y = self._local_params(x, y)
        u1 = poisson(mu).cdf(y)
        u2 = np.where(y > 0, poisson(mu).cdf(y - 1), 0)
        v = np.random.uniform(size=y.shape)
        u = np.clip(v * u1 + (1 - v) * u2, epsilon, 1 - epsilon)
        return torch.from_numpy(u).float()

    def _local_params(self, x, y=None) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
        params = self.predict(x)
        mu = params.get('mean')
        if y is None:
            return _to_numpy(mu)
        return _to_numpy(mu, y)

invert(u, x)

Invert pseudoobservations.

Source code in scdesigner/src/scdesigner/distributions/poisson.py
80
81
82
83
84
def invert(self, u: torch.Tensor, x: Dict[str, torch.Tensor]) -> torch.Tensor:
    """Invert pseudoobservations."""
    mu, u = self._local_params(x, u)
    y = poisson(mu).ppf(u)
    return torch.from_numpy(y).float()

likelihood(batch)

Compute the log-likelihood

Source code in scdesigner/src/scdesigner/distributions/poisson.py
73
74
75
76
77
78
def likelihood(self, batch) -> torch.Tensor:
    """Compute the log-likelihood"""
    y, x = batch
    params = self.predict(x)
    mu = params.get("mean")
    return y * torch.log(mu) - mu - torch.lgamma(y + 1)

uniformize(y, x, epsilon=1e-06)

Return uniformized pseudo-observations for counts y given covariates x.

Source code in scdesigner/src/scdesigner/distributions/poisson.py
86
87
88
89
90
91
92
93
94
def uniformize(self, y: torch.Tensor, x: Dict[str, torch.Tensor], epsilon=1e-6) -> torch.Tensor:
    """Return uniformized pseudo-observations for counts y given covariates x."""
    # cdf values using scipy's parameterization
    mu, y = self._local_params(x, y)
    u1 = poisson(mu).cdf(y)
    u2 = np.where(y > 0, poisson(mu).cdf(y - 1), 0)
    v = np.random.uniform(size=y.shape)
    u = np.clip(v * u1 + (1 - v) * u2, epsilon, 1 - epsilon)
    return torch.from_numpy(u).float()