Skip to content

Commit

Permalink
Adding Horseshoe Prior (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlheureux authored Sep 1, 2024
1 parent 20302bc commit 0520a5f
Show file tree
Hide file tree
Showing 2 changed files with 700 additions and 1 deletion.
31 changes: 30 additions & 1 deletion bambi/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@
import pytensor.tensor as pt
import pymc as pm

MAPPING = {"Cumulative": pm.Categorical, "StoppingRatio": pm.Categorical}

def horseshoe(name, tau_nu=3, lam_nu=1, dims=None):
"""Simulate a beta coefficient value with a horseshoe prior.
This is an internal function which is not supposed to be used by users.
This will be used only when a horseshoe prior is called for beta coefficients.
Parameters
----------
name: str
is the name of the parameters as registered in the PyMC model
tau_nu: int, float
Degrees of freedom of tau. Default: 3
lam_nu: int, float
Degrees of freedom of lam. Default: 1 (equivalent to a HalfCauchy)
dims: str
dimensions passed to PyMC. Default: None
Returns
------
np.ndarray
Array with the beta coefficient simulated.
"""
tau = pm.HalfStudentT(f"{name}_tau", nu=tau_nu)
lam = pm.HalfStudentT(f"{name}_lam", nu=lam_nu, dims=dims)
beta_raw = pm.Normal(f"{name}_raw", 0, 1, dims=dims)
beta = pm.Deterministic(name, beta_raw * tau**2 * lam**2, dims=dims)
return beta


MAPPING = {"Cumulative": pm.Categorical, "StoppingRatio": pm.Categorical, "Horseshoe": horseshoe}


def get_distribution(dist):
Expand Down
Loading

0 comments on commit 0520a5f

Please sign in to comment.