Skip to content

Commit

Permalink
Add .hypothesis/ directory to .gitignore
Browse files Browse the repository at this point in the history
and ppf and cdf to scipy.stats.uniform
  • Loading branch information
IlayMenahem committed Jan 16, 2024
1 parent c0d51e7 commit 390e903
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
.envrc
jax.iml
.bazelrc.user
.hypothesis/

# virtualenv/venv directories
/venv/
Expand Down
2 changes: 2 additions & 0 deletions docs/jax.scipy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ jax.scipy.stats.uniform

logpdf
pdf
cdf
ppf

jax.scipy.stats.gaussian_kde
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
19 changes: 19 additions & 0 deletions jax/_src/scipy/stats/uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import scipy.stats as osp_stats

from jax import lax
from jax import numpy as jnp
from jax.numpy import where, inf, logical_or
from jax._src.typing import Array, ArrayLike
from jax._src.numpy.util import _wraps, promote_args_inexact
Expand All @@ -32,3 +33,21 @@ def logpdf(x: ArrayLike, loc: ArrayLike = 0, scale: ArrayLike = 1) -> Array:
@_wraps(osp_stats.uniform.pdf, update_doc=False)
def pdf(x: ArrayLike, loc: ArrayLike = 0, scale: ArrayLike = 1) -> Array:
return lax.exp(logpdf(x, loc, scale))

@_wraps(osp_stats.uniform.cdf, update_doc=False)
def cdf(x: ArrayLike, loc: ArrayLike = 0, scale: ArrayLike = 1) -> Array:
x, loc, scale = promote_args_inexact("uniform.cdf", x, loc, scale)
zero, one = jnp.array(0, x.dtype), jnp.array(1, x.dtype)
conds = [lax.lt(x, loc), lax.gt(x, lax.add(loc, scale)), lax.ge(x, loc) & lax.le(x, lax.add(loc, scale))]
vals = [zero, one, lax.div(lax.sub(x, loc), scale)]

return jnp.select(conds, vals)

@_wraps(osp_stats.uniform.ppf, update_doc=False)
def ppf(q: ArrayLike, loc: ArrayLike = 0, scale: ArrayLike = 1) -> Array:
q, loc, scale = promote_args_inexact("uniform.ppf", q, loc, scale)
return where(
jnp.isnan(q) | (q < 0) | (q > 1),
jnp.nan,
lax.add(loc, lax.mul(scale, q))
)
2 changes: 2 additions & 0 deletions jax/scipy/stats/uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
from jax._src.scipy.stats.uniform import (
logpdf as logpdf,
pdf as pdf,
cdf as cdf,
ppf as ppf,
)
31 changes: 31 additions & 0 deletions tests/scipy_stats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,36 @@ def args_maker():
tol=1e-4)
self._CompileAndCheck(lax_fun, args_maker)

@genNamedParametersNArgs(3)
def testUniformCdf(self, shapes, dtypes):
rng = jtu.rand_default(self.rng())
scipy_fun = osp_stats.uniform.cdf
lax_fun = lsp_stats.uniform.cdf

def args_maker():
x, loc, scale = map(rng, shapes, dtypes)
return [x, loc, np.abs(scale)]

with jtu.strict_promotion_if_dtypes_match(dtypes):
self._CheckAgainstNumpy(scipy_fun, lax_fun, args_maker, check_dtypes=False,
tol=1e-5)
self._CompileAndCheck(lax_fun, args_maker)

@genNamedParametersNArgs(3)
def testUniformPpf(self, shapes, dtypes):
rng = jtu.rand_default(self.rng())
scipy_fun = osp_stats.uniform.ppf
lax_fun = lsp_stats.uniform.ppf

def args_maker():
q, loc, scale = map(rng, shapes, dtypes)
return [q, loc, np.abs(scale)]

with jtu.strict_promotion_if_dtypes_match(dtypes):
self._CheckAgainstNumpy(scipy_fun, lax_fun, args_maker, check_dtypes=False,
tol=1e-5)
self._CompileAndCheck(lax_fun, args_maker)

@genNamedParametersNArgs(4)
def testChi2LogPdf(self, shapes, dtypes):
rng = jtu.rand_positive(self.rng())
Expand All @@ -1058,6 +1088,7 @@ def args_maker():
tol=5e-4)
self._CompileAndCheck(lax_fun, args_maker)


@genNamedParametersNArgs(4)
def testChi2LogCdf(self, shapes, dtypes):
rng = jtu.rand_positive(self.rng())
Expand Down

0 comments on commit 390e903

Please sign in to comment.