Skip to content

bernoulli

Bernoulli

Bases: Marginal

Bernoulli marginal estimator

This subclass behaves like Marginal but assumes each feature follows a Bernoulli distribution with success probability theta_j(x) that depends on covariates x through the formula argument.

The allowed formula keys are 'mean' (interpreted as the logit of the success probability when used with a GLM link). If a string formula is provided, it is taken to specify the mean model.

Examples:

>>> from scdesigner.distributions import Bernoulli
>>> import scdesigner.datasets
>>>
>>> pancreas = scdesigner.datasets.pancreas()
>>>
>>> sim = Bernoulli(formula="~ pseudotime")
>>> sim.setup_data(pancreas)
>>> sim.fit(max_epochs=1, verbose=False)
>>>
>>> # evaluate p(y | x) and theta(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/bernoulli.py
 9
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
class Bernoulli(Marginal):
    """Bernoulli marginal estimator

    This subclass behaves like `Marginal` but assumes each feature follows a
    Bernoulli distribution with success probability `theta_j(x)` that depends
    on covariates `x` through the `formula` argument.

    The allowed formula keys are 'mean' (interpreted as the logit of the
    success probability when used with a GLM link). If a string formula is
    provided, it is taken to specify the `mean` model.

    Examples
    --------
    >>> from scdesigner.distributions import Bernoulli
    >>> import scdesigner.datasets
    >>>
    >>> pancreas = scdesigner.datasets.pancreas()
    >>>
    >>> sim = Bernoulli(formula="~ pseudotime")
    >>> sim.setup_data(pancreas)
    >>> sim.fit(max_epochs=1, verbose=False)
    >>>
    >>> # evaluate p(y | x) and theta(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)")

        link_fns = {"mean": torch.sigmoid}
        def nll(batch):
            return -self.likelihood(batch).sum()
        self.predict = GLMPredictor(
            n_outcomes=self.n_outcomes,
            feature_dims=self.feature_dims,
            link_fns=link_fns,
            loss_fn=nll,
            optimizer_class=optimizer_class,
            optimizer_kwargs=optimizer_kwargs,
            device=self.device,
        )

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

    def invert(self, u: torch.Tensor, x: Dict[str, torch.Tensor]) -> torch.Tensor:
        """Invert pseudoobservations."""
        theta, u = self._local_params(x, u)
        y = bernoulli(theta).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."""
        theta, y = self._local_params(x, y)
        u1 =  bernoulli(theta).cdf(y)
        u2 = np.where(y > 0,  bernoulli(theta).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) -> Tuple:
        params = self.predict(x)
        theta = params.get('mean')
        if y is None:
            return _to_numpy(theta)
        return _to_numpy(theta, y)

invert(u, x)

Invert pseudoobservations.

Source code in scdesigner/src/scdesigner/distributions/bernoulli.py
72
73
74
75
76
def invert(self, u: torch.Tensor, x: Dict[str, torch.Tensor]) -> torch.Tensor:
    """Invert pseudoobservations."""
    theta, u = self._local_params(x, u)
    y = bernoulli(theta).ppf(u)
    return torch.from_numpy(y).float()

likelihood(batch)

Compute the log-likelihood

Source code in scdesigner/src/scdesigner/distributions/bernoulli.py
65
66
67
68
69
70
def likelihood(self, batch) -> torch.Tensor:
    """Compute the log-likelihood"""
    y, x = batch
    params = self.predict(x)
    theta = params.get("mean")
    return y * torch.log(theta) + (1 - y) * torch.log(1 - theta)

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

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

Source code in scdesigner/src/scdesigner/distributions/bernoulli.py
78
79
80
81
82
83
84
85
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."""
    theta, y = self._local_params(x, y)
    u1 =  bernoulli(theta).cdf(y)
    u2 = np.where(y > 0,  bernoulli(theta).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()