我用PyMc收到一个错误消息,我不明白

2024-04-23 10:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的设置和错误消息:

创建假数据

  a=0.5
  b=-0.01
  s=1

ydata=np.empty((100, 100))

n, m =np.shape(ydata)
for t in range(1, n+1):
    phi_prob=s*((t**(-b))/a-1)
    process=Beta('process_%s' % str(t-1), value=0.5, alpha=s, beta=phi_prob)
    for i in range(m):
        ydata[t-1][i]=process.random()
#giving it some missing values, as that is part of the real problem I try to solve

for i, j in zip([choice(range(n)) for i in range(4*n)], [choice(range(m)) for j in range(4*m)]):
    ydata[i][j]=None

y_df=pd.DataFrame(ydata)
ydata=y_df.fillna(98) #making sure that it's all numeric, or else I get an error that I do understand
masked_data = np.ma.masked_equal(np.array(ydata), value=98)

构建模型:

alpha=Beta("alpha", 1, 1)
time=np.arange(1, len(masked_data)+1)

@pymc.stochastic(observed=False)
def beta(value=-0.01):
    if (value >=0 or value <-0.5):
        return -np.inf #the constraint that beta must be between -0.5 and 0: note that log(0)=-inf 
    return -1.5 * np.log(1 + value ** 2)

@pymc.stochastic(observed=False)
def sigma(value=1):
    return -np.log(abs(value))

# Define phi as a deterministic function of alpha, beta and time
@pymc.deterministic
def phi_model(t=np.arange(1, 100), alpha=alpha, beta=beta, sigma=sigma):
    return sigma*((t**(-beta))/alpha-1)

y=np.empty(len(time), dtype=object) #numpy.empty(N, dtype=object)

下面是模型中抛出错误的部分:

for t in time:
    data=masked_data[time-1]
    y[t-1] = Beta('y %s' % str(t-1), value=masked_data[t-1], alpha=sigma, beta=phi_model, observed=True)

下面是错误:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-412-a64e9ce685cf> in <module>()
      1 for t in time:
      2     data=masked_data[time-1]
----> 3     y[t-1] = Beta('y %s' % str(t-1), value=masked_data[t-1], alpha=sigma, beta=phi_model, observed=True)

/usr/lib/pymodules/python2.7/pymc/distributions.pyc in __init__(self, *args, **kwds)
    268                 random = debug_wrapper(random)
    269             else:
--> 270                 Stochastic.__init__(self, logp=logp, random=random, logp_partial_gradients = logp_partial_gradients, dtype=dtype, **arg_dict_out)
    271 
    272     new_class.__name__ = name

/usr/lib/pymodules/python2.7/pymc/PyMCObjects.pyc in __init__(self, logp, doc, name, parents, random, trace, value, dtype, rseed, observed, cache_depth, plot, verbose, isdata, check_logp, logp_partial_gradients)
    703                         dtype=dtype,
    704                         plot=plot,
--> 705                         verbose=verbose)
    706 
    707         # self._logp.force_compute()

/usr/lib/pymodules/python2.7/pymc/Node.pyc in __init__(self, doc, name, parents, cache_depth, trace, dtype, plot, verbose)
    199         self.extended_children = set()
    200 
--> 201         Node.__init__(self, doc, name, parents, cache_depth, verbose=verbose)
    202 
    203         if self.dtype is None:

/usr/lib/pymodules/python2.7/pymc/Node.pyc in __init__(self, doc, name, parents, cache_depth, verbose)
    117 
    118         # Initialize
--> 119         self.parents = parents
    120 
    121     def _get_parents(self):

/usr/lib/pymodules/python2.7/pymc/Node.pyc in _set_parents(self, new_parents)
    138 
    139         # Get new lazy function
--> 140         self.gen_lazy_function()
    141 
    142     parents = property(_get_parents, _set_parents, doc="Self's parents: the variables referred to in self's declaration.")

/usr/lib/pymodules/python2.7/pymc/PyMCObjects.pyc in gen_lazy_function(self)
    743                                     ultimate_args = self.extended_parents | set([self]),
    744                                     cache_depth = self._cache_depth)
--> 745         self._logp.force_compute()
    746 
    747 

LazyFunction.pyx in pymc.LazyFunction.LazyFunction.force_compute (pymc/LazyFunction.c:2411)()

/usr/lib/pymodules/python2.7/pymc/distributions.pyc in wrapper(**kwds)
   2770     def wrapper(**kwds):
   2771         value = kwds.pop('value')
-> 2772         return f(value, **kwds)
   2773 
   2774     if arguments is None:

/usr/lib/pymodules/python2.7/pymc/distributions.pyc in beta_like(x, alpha, beta)
    708     # except ZeroProbability:
    709     #     return -np.Inf
--> 710     return flib.beta_like(x, alpha, beta)
    711 
    712 beta_grad_like = {'value' : flib.beta_grad_x,

error: (nb==1 || nb==len(x)) failed for hidden nb: beta_like:nb=99

Tags: inselfalphafordatavaluelibusr