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

Draft of switch StudentT cdf to use tfp's betainc #1475

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion numpyro/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -1909,18 +1909,26 @@ def variance(self):
return jnp.broadcast_to(var, self.batch_shape)

def cdf(self, value):
try:
from tensorflow_probability.substrates.jax.math import betainc as betainc_fn
except ImportError:
from jax.scipy.special import betainc as betainc_fn

# Ref: https://en.wikipedia.org/wiki/Student's_t-distribution#Related_distributions
# X^2 ~ F(1, df) -> df / (df + X^2) ~ Beta(df/2, 0.5)
scaled = (value - self.loc) / self.scale
scaled_squared = scaled * scaled
beta_value = self.df / (self.df + scaled_squared)

# when scaled < 0, returns 0.5 * Beta(df/2, 0.5).cdf(beta_value)
# when scaled > 0, returns 1 - 0.5 * Beta(df/2, 0.5).cdf(beta_value)
scaled_sign_half = 0.5 * jnp.sign(scaled)
return (
0.5
+ scaled_sign_half
- 0.5 * jnp.sign(scaled) * betainc(0.5 * self.df, 0.5, beta_value)
- 0.5
* jnp.sign(scaled)
* betainc_fn(0.5 * jnp.asarray(self.df), 0.5, jnp.asarray(beta_value))
)

def icdf(self, q):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"jaxns==1.0.0",
"optax>=0.0.6",
"pyyaml", # flax dependency
"tensorflow_probability>=0.15.0",
"tensorflow_probability>=0.17.0",
],
"examples": [
"arviz",
Expand Down