Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loc-Scale variant of Chi-sq distribution #76

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions jaxampler/_src/rvs/chi2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@


class Chi2(ContinuousRV):
def __init__(self, nu: Numeric | Any, name: Optional[str] = None) -> None:
shape, self._nu = jx_cast(nu)
def __init__(
self,
nu: Numeric | Any,
loc: Numeric | Any = 0.0,
scale: Numeric | Any = 1.0,
name: Optional[str] = None,
) -> None:
shape, self._nu, self._loc, self._scale = jx_cast(nu, loc, scale)
self.check_params()
super().__init__(name=name, shape=shape)

Expand All @@ -37,19 +43,39 @@ def check_params(self) -> None:

@partial(jit, static_argnums=(0,))
def logpdf_x(self, x: Numeric) -> Numeric:
return jax_chi2.logpdf(x, self._nu)
return jax_chi2.logpdf(
x=x,
df=self._nu,
loc=self._loc,
scale=self._scale,
)

@partial(jit, static_argnums=(0,))
def pdf_x(self, x: Numeric) -> Numeric:
return jax_chi2.pdf(x, self._nu)
return jax_chi2.pdf(
x=x,
df=self._nu,
loc=self._loc,
scale=self._scale,
)

@partial(jit, static_argnums=(0,))
def logcdf_x(self, x: Numeric) -> Numeric:
return jax_chi2.logcdf(x, self._nu)
return jax_chi2.logcdf(
x=x,
df=self._nu,
loc=self._loc,
scale=self._scale,
)

@partial(jit, static_argnums=(0,))
def cdf_x(self, x: Numeric) -> Numeric:
return jax_chi2.cdf(x, self._nu)
return jax_chi2.cdf(
x=x,
df=self._nu,
loc=self._loc,
scale=self._scale,
)

@partial(jit, static_argnums=(0,))
def logppf_x(self, x: Numeric) -> Numeric:
Expand All @@ -59,10 +85,10 @@ def rvs(self, shape: tuple[int, ...], key: Optional[Array] = None) -> Array:
if key is None:
key = self.get_key()
new_shape = shape + self._shape
return jax.random.chisquare(key, self._nu, shape=new_shape)
return self._loc + self._scale * jax.random.chisquare(key, self._nu, shape=new_shape)

def __repr__(self) -> str:
string = f"Chi2(nu={self._nu}"
string = f"Chi2(nu={self._nu}, loc={self._loc}, scale={self._scale}"
if self._name is not None:
string += f", name={self._name}"
string += ")"
Expand Down