-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeterministic.py
89 lines (72 loc) · 2 KB
/
deterministic.py
1
2
3
4
5
6
7
8
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
# -*- coding: utf-8 -*-
# numpydoc ignore=GL08
from __future__ import annotations
import numpyro as npro
from jax.typing import ArrayLike
from pyrenew.metaclass import RandomVariable
class DeterministicVariable(RandomVariable):
"""
A deterministic (degenerate) random variable. Useful to pass fixed
quantities.
"""
def __init__(
self,
vars: ArrayLike,
label: str = "a_random_variable",
) -> None:
"""Default constructor
Parameters
----------
vars : ArrayLike
A tuple with arraylike objects.
label : str, optional
A label to assign to the process. Defaults to "a_random_variable"
Returns
-------
None
"""
self.validate(vars)
self.vars = vars
self.label = label
return None
@staticmethod
def validate(vars: ArrayLike) -> None:
"""
Validates inputted to DeterministicPMF
Parameters
----------
vars : ArrayLike
An ArrayLike object.
Returns
-------
None
Raises
------
Exception
If the inputted vars object is not a ArrayLike.
"""
if not isinstance(vars, ArrayLike):
raise Exception("vars is not a ArrayLike")
return None
def sample(
self,
record=True,
**kwargs,
) -> tuple:
"""
Retrieve the value of the deterministic Rv
Parameters
----------
record : bool, optional
Whether to record the value of the deterministic RandomVariable. Defaults to True.
**kwargs : dict, optional
Additional keyword arguments passed through to internal
sample calls, should there be any.
Returns
-------
tuple
Containing the stored values during construction.
"""
if record:
npro.deterministic(self.label, self.vars)
return (self.vars,)