Skip to content

Commit ec4821e

Browse files
aadya940brandonwillard
authored andcommitted
Add sampler class for F-distribution in tensor.random.basic
1 parent 22e9d62 commit ec4821e

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

aesara/tensor/random/basic.py

+46
Original file line numberDiff line numberDiff line change
@@ -2194,6 +2194,51 @@ def __call__(self, x, **kwargs):
21942194
random = uniform
21952195

21962196

2197+
class FRV(RandomVariable):
2198+
r"""An F-distribution continuous random variable.
2199+
2200+
The probability density function for `F` in terms of its degrees of freedom parameters `dfn` and `dfd` is:
2201+
2202+
.. math::
2203+
2204+
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})}
2205+
2206+
for :math:`x > 0`, :math:`dfn > 0`, and :math:`dfd > 0`.
2207+
2208+
"""
2209+
name = "f"
2210+
ndim_supp = 0
2211+
ndims_params = [0, 0]
2212+
dtype = "floatX"
2213+
_print_name = ("F", "\\operatorname{F}")
2214+
2215+
def __call__(self, dfn=1, dfd=1, size=None, **kwargs):
2216+
r"""Draw samples from an F-distribution.
2217+
2218+
Signature
2219+
---------
2220+
2221+
`() -> ()`
2222+
2223+
Parameters
2224+
----------
2225+
dfn
2226+
Degrees of freedom parameter for the numerator. Must be positive.
2227+
dfd
2228+
Degrees of freedom parameter for the denominator. Must be positive.
2229+
size
2230+
Sample shape. If the given size is, e.g. `(m, n, k)` then `m * n * k`
2231+
independent, identically distributed random variables are
2232+
returned. Default is `None` in which case a single random variable
2233+
is returned.
2234+
2235+
"""
2236+
return super().__call__(dfn, dfd, size=size, **kwargs)
2237+
2238+
2239+
f = FRV()
2240+
2241+
21972242
__all__ = [
21982243
"permutation",
21992244
"choice",
@@ -2242,4 +2287,5 @@ def __call__(self, x, **kwargs):
22422287
"rayleigh",
22432288
"power",
22442289
"zipf",
2290+
"f",
22452291
]

tests/tensor/random/test_basic.py

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
choice,
2828
dirichlet,
2929
exponential,
30+
f,
3031
gamma,
3132
gengamma,
3233
geometric,
@@ -1513,3 +1514,16 @@ def test_pickle():
15131514
a_unpkl = pickle.loads(a_pkl)
15141515

15151516
assert a_unpkl.owner.op._props() == sample_a.owner.op._props()
1517+
1518+
1519+
@pytest.mark.parametrize(
1520+
"dfn, dfd",
1521+
[
1522+
(1, 1),
1523+
(2, 2),
1524+
(5, 10),
1525+
(10, 5),
1526+
],
1527+
)
1528+
def test_f_samples(dfn, dfd):
1529+
compare_sample_values(f, dfn, dfd)

0 commit comments

Comments
 (0)