Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fail_fast: true

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.14.1
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- id: ruff-format
types_or: [ python, pyi ]
- repo: https://github.com/MarcoGorelli/madforhooks
rev: 0.4.1
hooks:
- id: no-print-statements
4 changes: 1 addition & 3 deletions distributions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"""
PyTensor powered distributions.
"""
"""PyTensor powered distributions."""

__version__ = "0.1.0"
2 changes: 1 addition & 1 deletion distributions/asymmetriclaplace.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(mu, b, kappa):
Expand Down
2 changes: 1 addition & 1 deletion distributions/bernoulli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytensor.tensor as pt
from pytensor.tensor.xlogx import xlogx

from .helper import ppf_bounds_disc, cdf_bounds
from distributions.helper import cdf_bounds, ppf_bounds_disc


def mean(p):
Expand Down
2 changes: 1 addition & 1 deletion distributions/beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pytensor.tensor.special import betaln
from pytensor.tensor.xlogx import xlogy0

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(alpha, beta):
Expand Down
4 changes: 2 additions & 2 deletions distributions/betabinomial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytensor.tensor as pt

from .helper import cdf_bounds, discrete_entropy, ppf_bounds_disc
from .optimization import find_ppf_discrete
from distributions.helper import cdf_bounds, discrete_entropy
from distributions.optimization import find_ppf_discrete


def mean(n, alpha, beta):
Expand Down
4 changes: 2 additions & 2 deletions distributions/betascaled.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytensor.tensor as pt
from pytensor.tensor.math import betaincinv
from pytensor.tensor.special import betaln
from pytensor.tensor.xlogx import xlogy0
from pytensor.tensor.math import betaincinv

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(alpha, beta, lower, upper):
Expand Down
5 changes: 2 additions & 3 deletions distributions/binomial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import pytensor.tensor as pt
from pytensor.tensor.xlogx import xlogy0

from .helper import cdf_bounds, discrete_entropy, ppf_bounds_disc
from .normal import entropy as normal_entropy
from .optimization import find_ppf_discrete
from distributions.helper import cdf_bounds, discrete_entropy
from distributions.optimization import find_ppf_discrete


def mean(n, p):
Expand Down
2 changes: 1 addition & 1 deletion distributions/cauchy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(alpha, beta):
Expand Down
4 changes: 2 additions & 2 deletions distributions/chisquared.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont, cdf_bounds
from pytensor.tensor.xlogx import xlogy0

from distributions.helper import cdf_bounds, ppf_bounds_cont


def mean(nu):
return nu
Expand Down
2 changes: 1 addition & 1 deletion distributions/discreteuniform.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import cdf_bounds, discrete_entropy, ppf_bounds_disc
from distributions.helper import cdf_bounds, discrete_entropy, ppf_bounds_disc


def mean(lower, upper):
Expand Down
8 changes: 4 additions & 4 deletions distributions/exgaussian.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pytensor.tensor as pt

from .helper import logdiffexp, continuous_entropy
from .optimization import find_ppf
from .normal import logcdf as normal_logcdf
from .normal import logpdf as normal_logpdf
from distributions.helper import continuous_entropy, logdiffexp
from distributions.normal import logcdf as normal_logcdf
from distributions.normal import logpdf as normal_logpdf
from distributions.optimization import find_ppf


def mean(mu, sigma, nu):
Expand Down
2 changes: 1 addition & 1 deletion distributions/exponential.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont, cdf_bounds
from distributions.helper import cdf_bounds, ppf_bounds_cont


def mean(lam):
Expand Down
2 changes: 1 addition & 1 deletion distributions/gamma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(alpha, beta):
Expand Down
3 changes: 1 addition & 2 deletions distributions/geometric.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytensor.tensor as pt
from pytensor.tensor.xlogx import xlogy0

from .helper import cdf_bounds, ppf_bounds_disc
from distributions.helper import cdf_bounds, ppf_bounds_disc


def mean(p):
Expand Down
2 changes: 1 addition & 1 deletion distributions/gumbel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import cdf_bounds, ppf_bounds_cont
from distributions.helper import cdf_bounds, ppf_bounds_cont


def mean(mu, beta):
Expand Down
2 changes: 1 addition & 1 deletion distributions/halfcauchy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont, cdf_bounds
from distributions.helper import ppf_bounds_cont


def mean(beta):
Expand Down
2 changes: 1 addition & 1 deletion distributions/halfnormal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(sigma):
Expand Down
10 changes: 5 additions & 5 deletions distributions/halfstudentt.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytensor.tensor as pt
from pytensor.tensor.special import betaln
from pytensor.tensor.math import betaincinv
from pytensor.tensor.special import betaln

from .helper import cdf_bounds, ppf_bounds_cont
from .halfnormal import entropy as halfnormal_entropy
from .halfnormal import cdf as halfnormal_cdf
from .halfnormal import logpdf as halfnormal_logpdf
from distributions.halfnormal import cdf as halfnormal_cdf
from distributions.halfnormal import entropy as halfnormal_entropy
from distributions.halfnormal import logpdf as halfnormal_logpdf
from distributions.helper import cdf_bounds, ppf_bounds_cont


def mean(nu, sigma):
Expand Down
8 changes: 2 additions & 6 deletions distributions/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,12 @@ def discrete_entropy(min_x, max_x, logpdf, *params):


def from_tau(tau):
"""
Convert precision (tau) to standard deviation (sigma).
"""
"""Convert precision (tau) to standard deviation (sigma)."""
sigma = 1 / pt.sqrt(tau)
return sigma


def to_tau(sigma):
"""
Convert standard deviation (sigma) to precision (tau).
"""
"""Convert standard deviation (sigma) to precision (tau)."""
tau = pt.power(sigma, -2)
return tau
2 changes: 1 addition & 1 deletion distributions/inversegamma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import cdf_bounds, ppf_bounds_cont
from distributions.helper import cdf_bounds, ppf_bounds_cont


def mean(alpha, beta):
Expand Down
2 changes: 1 addition & 1 deletion distributions/kumaraswamy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytensor.tensor as pt
from pytensor.tensor.xlogx import xlogy0

from .helper import cdf_bounds, ppf_bounds_cont, sf_bounds
from distributions.helper import cdf_bounds, ppf_bounds_cont, sf_bounds


def mean(a, b):
Expand Down
3 changes: 2 additions & 1 deletion distributions/laplace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytensor.tensor as pt
from .helper import ppf_bounds_cont, cdf_bounds

from distributions.helper import cdf_bounds, ppf_bounds_cont


def mean(mu, b):
Expand Down
2 changes: 1 addition & 1 deletion distributions/logistic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(mu, s):
Expand Down
2 changes: 1 addition & 1 deletion distributions/lognormal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import cdf_bounds, ppf_bounds_cont
from distributions.helper import cdf_bounds, ppf_bounds_cont


def mean(mu, sigma):
Expand Down
4 changes: 2 additions & 2 deletions distributions/negativebinomial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytensor.tensor as pt
from pytensor.tensor.xlogx import xlogy0

from .helper import cdf_bounds, discrete_entropy, sf_bounds
from .optimization import find_ppf_discrete
from distributions.helper import cdf_bounds, discrete_entropy, sf_bounds
from distributions.optimization import find_ppf_discrete


def mean(n, p):
Expand Down
2 changes: 1 addition & 1 deletion distributions/normal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont
from distributions.helper import ppf_bounds_cont


def mean(mu, sigma):
Expand Down
4 changes: 2 additions & 2 deletions distributions/optimization.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytensor.tensor as pt

from .helper import ppf_bounds_cont, ppf_bounds_disc
from .normal import ppf as normal_ppf
from distributions.helper import ppf_bounds_cont, ppf_bounds_disc


def find_ppf(q, lower, upper, cdf, *params):
"""
Compute the inverse CDF using the bisection method.

Uses iterative expansion for infinite bounds.

Note: We need to improve this method!!!
Expand Down
6 changes: 3 additions & 3 deletions distributions/poisson.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytensor.tensor as pt

from pytensor.tensor.xlogx import xlogy0
from .helper import cdf_bounds, discrete_entropy, sf_bounds
from .optimization import find_ppf_discrete

from distributions.helper import cdf_bounds, discrete_entropy, sf_bounds
from distributions.optimization import find_ppf_discrete


def mean(mu):
Expand Down
6 changes: 3 additions & 3 deletions distributions/skewnormal.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytensor.tensor as pt

from .optimization import find_ppf
from .halfnormal import entropy as halfnormal_entropy
from .normal import entropy as normal_entropy
from distributions.halfnormal import entropy as halfnormal_entropy
from distributions.normal import entropy as normal_entropy
from distributions.optimization import find_ppf


def mean(mu, sigma, alpha):
Expand Down
10 changes: 5 additions & 5 deletions distributions/studentt.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytensor.tensor as pt
from pytensor.tensor.special import betaln
from pytensor.tensor.math import betaincinv
from pytensor.tensor.special import betaln

from .helper import ppf_bounds_cont
from .normal import logcdf as normal_logpdf
from .normal import cdf as normal_logcdf
from .normal import entropy as normal_entropy
from distributions.helper import ppf_bounds_cont
from distributions.normal import cdf as normal_logcdf
from distributions.normal import entropy as normal_entropy
from distributions.normal import logcdf as normal_logpdf


def mean(nu, mu, sigma):
Expand Down
4 changes: 2 additions & 2 deletions distributions/wald.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytensor.tensor as pt

from .helper import cdf_bounds
from .optimization import find_ppf
from distributions.helper import cdf_bounds
from distributions.optimization import find_ppf


def mean(mu, lam):
Expand Down
17 changes: 6 additions & 11 deletions tests/helper_empirical.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"""
Common utilities for testing distributions against empirical samples.
For distributions not available in scipy.
"""
"""Common utilities for testing distributions not available in scipy."""

import numpy as np
import pytensor.tensor as pt
from numpy.testing import assert_allclose
from scipy.stats import skew, kurtosis
from scipy.integrate import quad
from scipy.stats import kurtosis, skew


def run_empirical_tests(
Expand All @@ -24,9 +21,7 @@ def run_empirical_tests(
quantiles_rtol=1e-4,
cdf_rtol=1e-4,
):
"""
Test a distribution against empirical samples for distributions not in scipy.
"""
"""Test a distribution against empirical samples for distributions not in scipy."""
rng_p = pt.random.default_rng(1)
rvs = p_dist.rvs(*p_params, size=sample_size, random_state=rng_p).eval()
sample_x = rvs[:20]
Expand Down Expand Up @@ -178,9 +173,9 @@ def run_empirical_tests(
mask = np.abs(pdf_vals) > 1e-4
if np.any(mask):
rel_error = np.abs(numerical_pdf[mask] - pdf_vals[mask]) / (np.abs(pdf_vals[mask]) + 1e-10)
assert np.all(
rel_error < 1e-3
), f"PDF doesn't match CDF derivative. Max rel error: {np.max(rel_error)}"
assert np.all(rel_error < 1e-3), (
f"PDF doesn't match CDF derivative. Max rel error: {np.max(rel_error)}"
)

# PPF-CDF inverse
x_vals = p_dist.ppf(q, *p_params).eval()
Expand Down
Loading