Skip to content

Metaclass

pyrenew helper classes

Model

Model(**kwargs)

Abstract base class for models

Source code in pyrenew/metaclass.py
97
98
99
@abstractmethod
def __init__(self, **kwargs) -> None:  # numpydoc ignore=GL08
    pass

model

model(**kwargs) -> tuple

Alias for the sample method.

Parameters:

Name Type Description Default
**kwargs

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

{}

Returns:

Type Description
tuple
Source code in pyrenew/metaclass.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def model(self, **kwargs) -> tuple:
    """
    Alias for the sample method.

    Parameters
    ----------
    **kwargs
        Additional keyword arguments passed through to
        internal `sample` calls, should there be any.

    Returns
    -------
    tuple
    """
    return self.sample(**kwargs)

posterior_predictive

posterior_predictive(
    rng_key: ArrayLike | None = None,
    numpyro_predictive_args: dict = {},
    **kwargs,
) -> dict

A wrapper of numpyro.infer.util.Predictive to generate posterior predictive samples.

Parameters:

Name Type Description Default
rng_key ArrayLike | None

Random key for the Predictive function call. Defaults to None.

None
numpyro_predictive_args dict

Dictionary of arguments to be passed to the numpyro.infer.util.Predictive constructor.

{}
**kwargs

Additional named arguments passed to the __call__() method of numpyro.infer.util.Predictive.

{}

Returns:

Type Description
dict
Source code in pyrenew/metaclass.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def posterior_predictive(
    self,
    rng_key: ArrayLike | None = None,
    numpyro_predictive_args: dict = {},
    **kwargs,
) -> dict:
    """
    A wrapper of [`numpyro.infer.util.Predictive`][] to generate
    posterior predictive samples.

    Parameters
    ----------
    rng_key
        Random key for the Predictive function call. Defaults to None.
    numpyro_predictive_args
        Dictionary of arguments to be passed to the
        [`numpyro.infer.util.Predictive`][] constructor.
    **kwargs
        Additional named arguments passed to the
        `__call__()` method of
        [`numpyro.infer.util.Predictive`][].

    Returns
    -------
    dict
    """
    if self.mcmc is None:
        raise ValueError(
            "No posterior samples available. Run model with model.run()."
        )

    if rng_key is None:
        rand_int = np.random.randint(np.iinfo(np.int64).min, np.iinfo(np.int64).max)
        rng_key = jr.key(rand_int)

    predictive = Predictive(
        model=self.model,
        posterior_samples=self.mcmc.get_samples(),
        **numpyro_predictive_args,
    )

    return predictive(rng_key, **kwargs)

print_summary

print_summary(prob: float = 0.9, exclude_deterministic: bool = True) -> None

A wrapper of numpyro.infer.mcmc.MCMC.print_summary.

Parameters:

Name Type Description Default
prob float

The width of the credible interval to show. Default 0.9

0.9
exclude_deterministic bool

Whether to print deterministic sites in the summary. Defaults to True.

True

Returns:

Type Description
None
Source code in pyrenew/metaclass.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
def print_summary(
    self,
    prob: float = 0.9,
    exclude_deterministic: bool = True,
) -> None:
    """
    A wrapper of [`numpyro.infer.mcmc.MCMC.print_summary`][].

    Parameters
    ----------
    prob
        The width of the credible interval to show. Default 0.9
    exclude_deterministic
        Whether to print deterministic sites in the summary.
        Defaults to True.

    Returns
    -------
    None
    """
    return self.mcmc.print_summary(prob, exclude_deterministic)

prior_predictive

prior_predictive(
    rng_key: ArrayLike | None = None,
    numpyro_predictive_args: dict = {},
    **kwargs,
) -> dict

A wrapper for numpyro.infer.util.Predictive to generate prior predictive samples.

Parameters:

Name Type Description Default
rng_key ArrayLike | None

Random key for the Predictive function call. Default None.

None
numpyro_predictive_args dict

Dictionary of arguments to be passed to the numpyro.infer.util.Predictive constructor. Default None.

{}
**kwargs

Additional named arguments passed to the __call__() method of numpyro.infer.util.Predictive.

{}

Returns:

Type Description
dict
Source code in pyrenew/metaclass.py
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
def prior_predictive(
    self,
    rng_key: ArrayLike | None = None,
    numpyro_predictive_args: dict = {},
    **kwargs,
) -> dict:
    """
    A wrapper for [`numpyro.infer.util.Predictive`][]
    to generate prior predictive samples.

    Parameters
    ----------
    rng_key
        Random key for the Predictive function call.
        Default None.
    numpyro_predictive_args
        Dictionary of arguments to be passed to
        the [`numpyro.infer.util.Predictive`][]
        constructor. Default None.
    **kwargs
        Additional named arguments passed to the
        `__call__()` method of
        [`numpyro.infer.util.Predictive`][].

    Returns
    -------
    dict
    """

    if rng_key is None:
        rand_int = np.random.randint(np.iinfo(np.int64).min, np.iinfo(np.int64).max)
        rng_key = jr.key(rand_int)

    predictive = Predictive(
        model=self.model,
        posterior_samples=None,
        **numpyro_predictive_args,
    )

    return predictive(rng_key, **kwargs)

run

run(
    num_warmup,
    num_samples,
    rng_key: ArrayLike | None = None,
    nuts_args: dict = None,
    mcmc_args: dict = None,
    **kwargs,
) -> None

Runs the model

Parameters:

Name Type Description Default
nuts_args dict

Dictionary of arguments passed to the kernel numpyro.infer.hmc.NUTS constructor. Defaults to None.

None
mcmc_args dict

Dictionary of arguments passed to the MCMC runner numpyro.infer.mcmc.MCMC constructor. Defaults to None.

None

Returns:

Type Description
None
Source code in pyrenew/metaclass.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
def run(
    self,
    num_warmup,
    num_samples,
    rng_key: ArrayLike | None = None,
    nuts_args: dict = None,
    mcmc_args: dict = None,
    **kwargs,
) -> None:
    """
    Runs the model

    Parameters
    ----------
    nuts_args
        Dictionary of arguments passed to the kernel
        [`numpyro.infer.hmc.NUTS`][] constructor.
        Defaults to None.
    mcmc_args
        Dictionary of arguments passed to the MCMC runner
        [`numpyro.infer.mcmc.MCMC`][] constructor.
        Defaults to None.

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

    self._init_model(
        num_warmup=num_warmup,
        num_samples=num_samples,
        nuts_args=nuts_args,
        mcmc_args=mcmc_args,
    )
    if rng_key is None:
        rand_int = np.random.randint(np.iinfo(np.int64).min, np.iinfo(np.int64).max)
        rng_key = jr.key(rand_int)

    self.mcmc.run(rng_key=rng_key, **kwargs)

    return None

sample abstractmethod

sample(**kwargs) -> tuple

Sample method of the model.

The method design in the class should have at least kwargs.

Parameters:

Name Type Description Default
**kwargs

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

{}

Returns:

Type Description
tuple
Source code in pyrenew/metaclass.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
@abstractmethod
def sample(
    self,
    **kwargs,
) -> tuple:
    """
    Sample method of the model.

    The method design in the class should have at least kwargs.

    Parameters
    ----------
    **kwargs
        Additional keyword arguments passed through to internal
        `sample` calls, should there be any.

    Returns
    -------
    tuple
    """
    pass

RandomVariable

RandomVariable(**kwargs)

Abstract base class for latent and observed random variables.

Default constructor

Source code in pyrenew/metaclass.py
47
48
49
50
51
def __init__(self, **kwargs):
    """
    Default constructor
    """
    pass

__call__

__call__(**kwargs)

Alias for sample.

Source code in pyrenew/metaclass.py
83
84
85
86
87
def __call__(self, **kwargs):
    """
    Alias for `sample`.
    """
    return self.sample(**kwargs)

sample abstractmethod

sample(**kwargs) -> tuple

Sample method of the process

The method design in the class should have at least kwargs.

Parameters:

Name Type Description Default
**kwargs

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

{}

Returns:

Type Description
tuple
Source code in pyrenew/metaclass.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@abstractmethod
def sample(
    self,
    **kwargs,
) -> tuple:
    """
    Sample method of the process

    The method design in the class should have at least kwargs.

    Parameters
    ----------
    **kwargs
        Additional keyword arguments passed through to internal
        `sample` calls, should there be any.

    Returns
    -------
    tuple
    """
    pass

validate abstractmethod staticmethod

validate(**kwargs) -> None

Validation of kwargs to be implemented in subclasses.

Source code in pyrenew/metaclass.py
75
76
77
78
79
80
81
@staticmethod
@abstractmethod
def validate(**kwargs) -> None:
    """
    Validation of kwargs to be implemented in subclasses.
    """
    pass