如何格式化观察到的数据以用于 pymc3 中的贝叶斯网络?
How do I format observed data for use in Bayesian networks in pymc3?
我很难理解观察到的数据在 pymc3 中是如何工作的。从我目前找到的信息来看,these two 示例对我所掌握的知识最有帮助,但我无法让我的模型发挥作用。
作为我正在尝试做的事情的一个例子,假设我有一家餐厅顾客的记录,以 5 分制的分类等级记录当天的温度,以及他们是否点了主食餐、配菜或饮料。我已经像这样设置了一些模拟数据:
import numpy as np
import pymc3 as pm
from theano import shared
no_of_root_categories = 5
no_of_samples = 1000
hot_day = np.random.randint(no_of_root_categories, size=no_of_samples)
option_labels = ["Main", "Side", "Beverage"]
meal_options = np.random.randint(2, size=(len(option_labels), no_of_samples))
我想将其建模为简单的贝叶斯网络,如下所示:
观察阴影节点的位置。
这是我得到的:
with pm.Model() as crashing_model:
shape = (no_of_samples, no_of_root_categories)
alpha = (1 / no_of_root_categories) * np.ones(shape)
root_prior = pm.Dirichlet("Temperature Rating Prior", alpha, shape=shape)
root = pm.Categorical('Temperature Rating', p=root_p, shape=no_of_samples, observed=hot_day)
for item, label in enumerate(option_labels):
node_data = meal_options[item, :]
theano_probs = shared(np.array(node_probs))
node_prior = pm.Beta(f"{label} Prior",
mu=root,
sigma=root,
shape=no_of_samples,
testval=np.random.randint(1, size=no_of_samples))
pm.Binomial(label, p=node_prior, n=no_of_samples, observed=node_data)
这有效,但是当我尝试时
with crashing_model:
trace = pm.sample(1000, random_seed=0)
Python 退出并出现 'Bad initial energy' 错误。
我可以创建一个似乎在没有潜在变量的情况下也能工作的模型
with pm.Model() as working_model: # seems to work
root_values = [np.where(hot_day == i)[0].tolist() for i in range(no_of_root_categories)]
root_p = [len(i) / 1000 for i in root_values]
root = pm.Categorical('Temperature Rating', p=root_p)
shared_proportions = shared(np.array([len(hot_day[i]) for i in root_values]))
for item, label in enumerate(option_labels):
node_probs = [sum([meal_options[item, idx] for idx in category]) / len(category) for category in root_values]
theano_probs = shared(np.array(node_probs))
pm.Binomial(label, p=theano_probs[root], n=shared_proportions[root])
但我不确定如何翻译我在那里所做的工作以处理潜在变量。任何帮助将不胜感激。
在 pymc3 discourse 的一些帮助下,我让它工作了。我以某种方式得到了我需要确保所有形状都相同的想法,但我根本不需要指定它们。以下代码适用于遇到问题的其他人。
with pm.Model() as model:
shape = (no_of_samples, no_of_root_categories)
alpha = np.ones(no_of_root_categories)
root_prior = pm.Dirichlet("Temperature Rating Prior", alpha)
root = pm.Categorical('Temperature Rating', p=root_p, observed=hot_day)
for item, label in enumerate(option_labels):
node_data = meal_options[item, :]
theano_alpha_probs = shared(np.ones(no_of_root_categories))
theano_beta_probs = shared(np.ones(no_of_root_categories))
node_prior = pm.Beta(f"{label} Prior",
alpha=theano_alpha_probs[root],
beta=theano_beta_probs[root],
testval=0.5)
pm.Bernoulli(label, p=node_prior, observed=node_data)
我很难理解观察到的数据在 pymc3 中是如何工作的。从我目前找到的信息来看,these two 示例对我所掌握的知识最有帮助,但我无法让我的模型发挥作用。
作为我正在尝试做的事情的一个例子,假设我有一家餐厅顾客的记录,以 5 分制的分类等级记录当天的温度,以及他们是否点了主食餐、配菜或饮料。我已经像这样设置了一些模拟数据:
import numpy as np
import pymc3 as pm
from theano import shared
no_of_root_categories = 5
no_of_samples = 1000
hot_day = np.random.randint(no_of_root_categories, size=no_of_samples)
option_labels = ["Main", "Side", "Beverage"]
meal_options = np.random.randint(2, size=(len(option_labels), no_of_samples))
我想将其建模为简单的贝叶斯网络,如下所示:
观察阴影节点的位置。
这是我得到的:
with pm.Model() as crashing_model:
shape = (no_of_samples, no_of_root_categories)
alpha = (1 / no_of_root_categories) * np.ones(shape)
root_prior = pm.Dirichlet("Temperature Rating Prior", alpha, shape=shape)
root = pm.Categorical('Temperature Rating', p=root_p, shape=no_of_samples, observed=hot_day)
for item, label in enumerate(option_labels):
node_data = meal_options[item, :]
theano_probs = shared(np.array(node_probs))
node_prior = pm.Beta(f"{label} Prior",
mu=root,
sigma=root,
shape=no_of_samples,
testval=np.random.randint(1, size=no_of_samples))
pm.Binomial(label, p=node_prior, n=no_of_samples, observed=node_data)
这有效,但是当我尝试时
with crashing_model:
trace = pm.sample(1000, random_seed=0)
Python 退出并出现 'Bad initial energy' 错误。
我可以创建一个似乎在没有潜在变量的情况下也能工作的模型
with pm.Model() as working_model: # seems to work
root_values = [np.where(hot_day == i)[0].tolist() for i in range(no_of_root_categories)]
root_p = [len(i) / 1000 for i in root_values]
root = pm.Categorical('Temperature Rating', p=root_p)
shared_proportions = shared(np.array([len(hot_day[i]) for i in root_values]))
for item, label in enumerate(option_labels):
node_probs = [sum([meal_options[item, idx] for idx in category]) / len(category) for category in root_values]
theano_probs = shared(np.array(node_probs))
pm.Binomial(label, p=theano_probs[root], n=shared_proportions[root])
但我不确定如何翻译我在那里所做的工作以处理潜在变量。任何帮助将不胜感激。
在 pymc3 discourse 的一些帮助下,我让它工作了。我以某种方式得到了我需要确保所有形状都相同的想法,但我根本不需要指定它们。以下代码适用于遇到问题的其他人。
with pm.Model() as model:
shape = (no_of_samples, no_of_root_categories)
alpha = np.ones(no_of_root_categories)
root_prior = pm.Dirichlet("Temperature Rating Prior", alpha)
root = pm.Categorical('Temperature Rating', p=root_p, observed=hot_day)
for item, label in enumerate(option_labels):
node_data = meal_options[item, :]
theano_alpha_probs = shared(np.ones(no_of_root_categories))
theano_beta_probs = shared(np.ones(no_of_root_categories))
node_prior = pm.Beta(f"{label} Prior",
alpha=theano_alpha_probs[root],
beta=theano_beta_probs[root],
testval=0.5)
pm.Bernoulli(label, p=node_prior, observed=node_data)