[Python] how to set truncated t-distribution prior in bambi

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    %pip install bambi==0.15.0
    %pip install numpy==1.26.4


    import bambi as bmb
    import polars as pl
    from polars import col, lit
    import pandas as pd
    import numpy as np
    import arviz as az

    np.random.seed(42)
    num_rows = int(10e3)

    # Generate random data
    shape, scale = 10, 6 # Gamma parameters to achieve mean of 60
    revenue = np.random.gamma(shape, scale, size=num_rows)

    # Create Polars DataFrame
    pl_a = pl.DataFrame({
    "recipe": "A",
    "revenue": revenue
    })

    # Generate random data
    shape, scale = 11, 6 # Gamma parameters to achieve mean of 66
    revenue = np.random.gamma(shape, scale, size=num_rows)

    # Create Polars DataFrame
    pl_b = pl.DataFrame({
    "recipe": "B",
    "revenue": revenue
    })

    pl_ab = pl.concat([pl_a, pl_b])

    m_lm = bmb.Model(
    "revenue ~ recipe",
    pl_ab.to_pandas(),
    family="gaussian",
    priors={
    "Intercept": bmb.Prior("Normal", mu=60, sigma=2),
    "recipe": bmb.Prior("Normal", mu=0, sigma=2),
    "sigma": bmb.Prior("StudentT", nu=3, mu=20, sigma=5)
    # bmb.Prior("HalfStudentT", nu=3, sigma=5)
    }
    )

    m_lm.fit()


    In the above code I can specify a StudentT or HalfStudentT prior for sigma, but I am unsuccessful in setting a truncated StudentT with a specified mu.

    I have failed to set a truncated prior in every way I could imagine:

    m_trunc = bmb.Model(
    "revenue ~ recipe",
    pl_ab.to_pandas(),
    family="gaussian",
    priors={
    "Intercept": bmb.Prior("Normal", mu=60, sigma=2),
    "recipe": bmb.Prior("Normal", mu=0, sigma=2),
    "sigma": bmb.Prior(
    "Truncated",
    prior=bmb.Prior("StudentT", nu=3, mu=20, sigma=5),
    lower=0, upper=np.inf
    )
    }
    )

    m_trunc.fit()


    TypeError: Truncated.dist() missing 1 required positional argument: 'dist'

    m_trunc = bmb.Model(
    "revenue ~ recipe",
    pl_ab.to_pandas(),
    family="gaussian",
    priors={
    "Intercept": bmb.Prior("Normal", mu=60, sigma=2),
    "recipe": bmb.Prior("Normal", mu=0, sigma=2),
    "sigma": bmb.Prior(
    "Truncated",
    dist="StudentT",
    nu=3, mu=0, sigma=1,
    lower=0, upper=np.inf
    )
    }
    )

    m_trunc.fit()


    TypeError: 'str' object is not callable

    m_trunc = bmb.Model(
    "revenue ~ recipe",
    pl_ab.to_pandas(),
    family="gaussian",
    priors={
    "Intercept": bmb.Prior("Normal", mu=60, sigma=2),
    "recipe": bmb.Prior("Normal", mu=0, sigma=2),
    "sigma": bmb.Prior(
    "StudentT", nu=3, mu=0, sigma=1, lower=0, upper=5 # Truncation applied here
    )
    }
    )

    m_trunc.fit()


    TypeError: RandomVariable.make_node() got an unexpected keyword argument 'lower'

    import pymc as pm

    m_trunc = bmb.Model(
    "revenue ~ recipe",
    pl_ab.to_pandas(),
    family="gaussian",
    priors={
    "Intercept": bmb.Prior("Normal", mu=60, sigma=2),
    "recipe": bmb.Prior("Normal", mu=0, sigma=2),
    "sigma": bmb.Prior(
    pm.Bound(pm.StudentT, lower=0, upper=5), # Bound the StudentT prior
    nu=3, mu=0, sigma=1 # Parameters for StudentT
    )
    }
    )

    m_trunc.fit()


    AttributeError: module 'pymc' has no attribute 'Bound'

    Continue reading...

Compartilhe esta Página