Skip to content

Commit

Permalink
Add sampler class for F-distribution in tensor.random.basic
Browse files Browse the repository at this point in the history
  • Loading branch information
aadya940 authored and brandonwillard committed Sep 21, 2023
1 parent 22e9d62 commit ec4821e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
46 changes: 46 additions & 0 deletions aesara/tensor/random/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,51 @@ def __call__(self, x, **kwargs):
random = uniform


class FRV(RandomVariable):
r"""An F-distribution continuous random variable.
The probability density function for `F` in terms of its degrees of freedom parameters `dfn` and `dfd` is:
.. math::
f(x; dfn, dfd) = \frac{\sqrt{\frac{(dfn \cdot x)^{dfn} \cdot dfd^{dfd}}{(dfn \cdot x + dfd)^{dfn + dfd}}}}{x \cdot \text{Beta}(\frac{dfn}{2}, \frac{dfd}{2})}
for :math:`x > 0`, :math:`dfn > 0`, and :math:`dfd > 0`.
"""
name = "f"
ndim_supp = 0
ndims_params = [0, 0]
dtype = "floatX"
_print_name = ("F", "\\operatorname{F}")

def __call__(self, dfn=1, dfd=1, size=None, **kwargs):
r"""Draw samples from an F-distribution.
Signature
---------
`() -> ()`
Parameters
----------
dfn
Degrees of freedom parameter for the numerator. Must be positive.
dfd
Degrees of freedom parameter for the denominator. Must be positive.
size
Sample shape. If the given size is, e.g. `(m, n, k)` then `m * n * k`
independent, identically distributed random variables are
returned. Default is `None` in which case a single random variable
is returned.
"""
return super().__call__(dfn, dfd, size=size, **kwargs)


f = FRV()


__all__ = [
"permutation",
"choice",
Expand Down Expand Up @@ -2242,4 +2287,5 @@ def __call__(self, x, **kwargs):
"rayleigh",
"power",
"zipf",
"f",
]
14 changes: 14 additions & 0 deletions tests/tensor/random/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
choice,
dirichlet,
exponential,
f,
gamma,
gengamma,
geometric,
Expand Down Expand Up @@ -1513,3 +1514,16 @@ def test_pickle():
a_unpkl = pickle.loads(a_pkl)

assert a_unpkl.owner.op._props() == sample_a.owner.op._props()


@pytest.mark.parametrize(
"dfn, dfd",
[
(1, 1),
(2, 2),
(5, 10),
(10, 5),
],
)
def test_f_samples(dfn, dfd):
compare_sample_values(f, dfn, dfd)

0 comments on commit ec4821e

Please sign in to comment.