Hi, I created a specific BayesianNetwork where user specify the probability distribution of a node as a lambda function.
class FunctionalCPD(BaseFactor):
def __init__(self, variable, fn, parents=[]):
self.fn = fn
self.parents = parents if parents else []
self.variables = [variable] + self.parents
#Example
cpd1 = FunctionalCPD("x1", lambda _: np.random.normal(0, 1))
cpd2 = FunctionalCPD(
"x2", lambda parent: np.random.normal(parent["x1"] + 2.0, 1), parents=["x1"]
)
This is useful because we can let the lambda function input parent data for its probability calculation or any further inference. This has the corresponding BayesianNetwork which is just collection of nodes where the probability distribution of a child node take the value for the parent as input.
Is there a way to use mcmc to fit this type of network given a data ?
I have tried the following :
def combined_model():
for node in sort_nodes:
if node in data.columns:
parents = self.get_cpds(node).parents
parent_data = (
{p: torch.tensor(data[p].values).float() for p in parents}
if parents
else None
)
with pyro.plate(f"data_{node}", len(data)):
self.get_cpds(node).fn(parent_data)
pyro.clear_param_store()
nuts_kernel = pyro.infer.NUTS(combined_model)
mcmc = pyro.infer.MCMC(nuts_kernel, num_samples=num_steps, warmup_steps=200)
mcmc.run()
posterior_samples = mcmc.get_samples()
return {var: posterior_samples[var].detach().numpy() for var in posterior_samples.keys()}
But there is no samples.