Skip to content

Observation

NegativeBinomialObservation

NegativeBinomialObservation(
    name: str, concentration_rv: RandomVariable, eps: float = 1e-10
)

Bases: RandomVariable

Negative Binomial observation

Default constructor

Parameters:

Name Type Description Default
name str

Name for the numpyro variable.

required
concentration_rv RandomVariable

Random variable from which to sample the positive concentration parameter of the negative binomial. This parameter is sometimes called k, phi, or the "dispersion" or "overdispersion" parameter, despite the fact that larger values imply that the distribution becomes more Poissonian, while smaller ones imply a greater degree of dispersion.

required
eps float

Small value to add to the predicted mean to prevent numerical instability. Defaults to 1e-10.

1e-10

Returns:

Type Description
None
Source code in pyrenew/observation/negativebinomial.py
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
def __init__(
    self,
    name: str,
    concentration_rv: RandomVariable,
    eps: float = 1e-10,
) -> None:
    """
    Default constructor

    Parameters
    ----------
    name
        Name for the numpyro variable.
    concentration_rv
        Random variable from which to sample the positive concentration
        parameter of the negative binomial. This parameter is sometimes
        called k, phi, or the "dispersion" or "overdispersion" parameter,
        despite the fact that larger values imply that the distribution
        becomes more Poissonian, while smaller ones imply a greater degree
        of dispersion.
    eps
        Small value to add to the predicted mean to prevent numerical
        instability. Defaults to 1e-10.

    Returns
    -------
    None
    """

    NegativeBinomialObservation.validate(concentration_rv)

    self.name = name
    self.concentration_rv = concentration_rv
    self.eps = eps

sample

sample(mu: ArrayLike, obs: ArrayLike | None = None, **kwargs) -> ArrayLike

Sample from the negative binomial distribution

Parameters:

Name Type Description Default
mu ArrayLike

Mean parameter of the negative binomial distribution.

required
obs ArrayLike | None

Observed data, by default None.

None
**kwargs

Additional keyword arguments passed through to internal sample calls, should there be any.

{}

Returns:

Type Description
ArrayLike
Source code in pyrenew/observation/negativebinomial.py
 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
def sample(
    self,
    mu: ArrayLike,
    obs: ArrayLike | None = None,
    **kwargs,
) -> ArrayLike:
    """
    Sample from the negative binomial distribution

    Parameters
    ----------
    mu
        Mean parameter of the negative binomial distribution.
    obs
        Observed data, by default None.
    **kwargs
        Additional keyword arguments passed through to internal sample calls, should there be any.

    Returns
    -------
    ArrayLike
    """
    concentration = self.concentration_rv.sample()

    negative_binomial_sample = numpyro.sample(
        name=self.name,
        fn=dist.NegativeBinomial2(
            mean=mu + self.eps,
            concentration=concentration,
        ),
        obs=obs,
    )

    return negative_binomial_sample

validate staticmethod

validate(concentration_rv: RandomVariable) -> None

Check that the concentration_rv is actually a RandomVariable

Parameters:

Name Type Description Default
concentration_rv RandomVariable

RandomVariable from which to sample the positive concentration parameter of the negative binomial.

required

Returns:

Type Description
None
Source code in pyrenew/observation/negativebinomial.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@staticmethod
def validate(concentration_rv: RandomVariable) -> None:
    """
    Check that the concentration_rv is actually a RandomVariable

    Parameters
    ----------
    concentration_rv
        RandomVariable from which to sample the positive concentration
        parameter of the negative binomial.

    Returns
    -------
    None
    """
    assert isinstance(concentration_rv, RandomVariable)
    return None

PoissonObservation

PoissonObservation(name: str, eps: float = 1e-08)

Bases: RandomVariable

Poisson observation process

Default Constructor

Parameters:

Name Type Description Default
name str required
eps float

Small value added to the rate parameter to avoid zero values. Defaults to 1e-8.

1e-08

Returns:

Type Description
None
Source code in pyrenew/observation/poisson.py
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
def __init__(
    self,
    name: str,
    eps: float = 1e-8,
) -> None:
    """
    Default Constructor

    Parameters
    ----------
    name
        Passed to [`numpyro.primitives.sample`][].
    eps
        Small value added to the rate parameter to avoid zero values.
        Defaults to 1e-8.

    Returns
    -------
    None
    """

    self.name = name
    self.eps = eps

    return None

sample

sample(mu: ArrayLike, obs: ArrayLike | None = None, **kwargs) -> ArrayLike

Sample from the Poisson process

Parameters:

Name Type Description Default
mu ArrayLike

Rate parameter of the Poisson distribution.

required
obs ArrayLike | None

Observed data. Defaults to None.

None
**kwargs

Additional keyword arguments passed through to internal sample calls, should there be any.

{}

Returns:

Type Description
ArrayLike
Source code in pyrenew/observation/poisson.py
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
def sample(
    self,
    mu: ArrayLike,
    obs: ArrayLike | None = None,
    **kwargs,
) -> ArrayLike:
    """
    Sample from the Poisson process

    Parameters
    ----------
    mu
        Rate parameter of the Poisson distribution.
    obs
        Observed data. Defaults to None.
    **kwargs
        Additional keyword arguments passed through to internal sample calls, should there be any.

    Returns
    -------
    ArrayLike
    """

    poisson_sample = numpyro.sample(
        name=self.name,
        fn=dist.Poisson(rate=mu + self.eps),
        obs=obs,
    )
    return poisson_sample