Hi,
I saw on an other post that someone wanted to sample a variable from a non-adaptable distribution (ref). They use Theano, and I saw you switched for pytensor for pymc>5. I would like to define such a non-adaptable distribution using the posterior distribution of another model.
Some context on my study:
I am trying to implement Principal Stratification method using pymc, below is a simplified version. Model 1 predicts if a patient in the placebo arm will be alive at the end of the trial. Model 2 uses model 1 to estimate if treated patients would have been alive if they had been in the placebo arm. Then model 2 estimates the average of a score on the treated arm for patients that would have been alive if they were in the placebo arm.
with pm.Model() as model_1:
# Input data
x_p = pm.Data("x_p", df_placebo_t[columns_for_pred])
alive_p = pm.Data("alive_p", df_placebo_t[["alive_obs"]])
# Create the alpha and beta parameters and assume a normal distribution
alpha_p = pm.Normal('alpha_p', mu=0.0, tau=non_informative_std)
beta_p = pm.Normal('beta_p', mu=0.0, tau=non_informative_std, shape = len(columns_for_pred))
# The probability of being alive is modeled as a logistic function
p_p = pm.Deterministic('p_p', 1. / (1. + Tensor.exp(beta_p * x_p + alpha_p)))
# Create the bernoulli parameter which uses observed data to inform the algorithm
observed_p = pm.Bernoulli('obs', p_p, observed=alive_p) * w_p
# Sample
trace_placebo_survival = pm.sample(
n_samples,
tune=n_tune,
target_accept=0.99,
cores = n_cores,
random_seed=RANDOM_SEED,
)
with pm.Model() as model_2:
# Input data
x_treat = pm.Data("x_p", df_treated_t[columns_for_pred])
y = pm.Data("y", df_placebo_t["score"])
# Create the alpha and beta parameters and assume a normal distribution
alpha_static = ???
beta_static = ???
mask = pm.Deterministic('mask', 1. / (1. + Tensor.exp(beta_p * x_treat + alpha_p)))
# Assume distributions
mean_trend = pm.Normal("mean_trend", 0, 1)
sigma = pm.Normal("sigma", 1)
average = pm.Potential('likelihood',pm.logp(pm.Normal.dist(mu=mean_trend, sigma=sigma),value)*mask)
# Sample
trace_placebo_survival = pm.sample(
n_samples,
tune=n_tune,
target_accept=0.99,
cores = n_cores,
random_seed=RANDOM_SEED,
)
I hope this is clear… Note that I also found a way to use posterior as new prior (ref) but as the variable is included in the likelihood, it gets updated and I don’t want that to happen.
Many thanks for your help!