PyMC @observed decorator computed log-probability cannot be cast to float 错误
PyMC @observed decorator computed log-probability cannot be cast to float error
我在将自定义 @stochasitc 变量切换为 PyMC 中的 @observed 变量时遇到了一些问题。我有一个这样设置的随机变量:
def age_logp(age):
if age < 0 or age > 110:
return -np.inf
elif 0 <= age < 20:
prob = age_dist['0-20']
elif 20 <= age < 40:
prob = age_dist['20-40']
elif 40 <= age < 60:
prob = age_dist['40-60']
elif 60 <= age < 80:
prob = age_dist['60-80']
elif age >= 80:
prob = age_dist['80-inf']
return np.log(prob)
@pymc.stochastic
def age(value=0):
def logp(value):
return age_logp(value)
如何将其切换为观察到的变量?到目前为止我已经试过了:
@pymc.observed
def age(value=np.array([12, 43, 28, 39, 87, 26])):
return map(age_logp, value)
但是我得到了TypeError: age: computed log-probability [-1.639897119918809, -1.3394107752210402, -1.0876723486297752, -1.0876723486297752, -3.1235656450638758, -1.0876723486297752] cannot be cast to float
如何扩展 age
以获取观察值数组?
您已经正确地切换了一些东西,但是如果您想将多个观察值建模为独立的,您可以在联合对数似然中对它们求和:
@pymc.observed
def age(value=np.array([12, 43, 28, 39, 87, 26])):
return sum(map(age_logp, value))
如果您愿意,可以通过其他方式组合它们。关键是 age
应该 return 一个标量值。
我在将自定义 @stochasitc 变量切换为 PyMC 中的 @observed 变量时遇到了一些问题。我有一个这样设置的随机变量:
def age_logp(age):
if age < 0 or age > 110:
return -np.inf
elif 0 <= age < 20:
prob = age_dist['0-20']
elif 20 <= age < 40:
prob = age_dist['20-40']
elif 40 <= age < 60:
prob = age_dist['40-60']
elif 60 <= age < 80:
prob = age_dist['60-80']
elif age >= 80:
prob = age_dist['80-inf']
return np.log(prob)
@pymc.stochastic
def age(value=0):
def logp(value):
return age_logp(value)
如何将其切换为观察到的变量?到目前为止我已经试过了:
@pymc.observed
def age(value=np.array([12, 43, 28, 39, 87, 26])):
return map(age_logp, value)
但是我得到了TypeError: age: computed log-probability [-1.639897119918809, -1.3394107752210402, -1.0876723486297752, -1.0876723486297752, -3.1235656450638758, -1.0876723486297752] cannot be cast to float
如何扩展 age
以获取观察值数组?
您已经正确地切换了一些东西,但是如果您想将多个观察值建模为独立的,您可以在联合对数似然中对它们求和:
@pymc.observed
def age(value=np.array([12, 43, 28, 39, 87, 26])):
return sum(map(age_logp, value))
如果您愿意,可以通过其他方式组合它们。关键是 age
应该 return 一个标量值。