Python 如何舍入模型中的PyMC3先验参数并与之进行比较?

Python 如何舍入模型中的PyMC3先验参数并与之进行比较?,python,theano,curve-fitting,bayesian,pymc3,Python,Theano,Curve Fitting,Bayesian,Pymc3,如果它看起来比需要的复杂,我会提前道歉。试图让它比这更简单,但有些问题更难表述。这样就尽可能接近我的处境。我有一些数据,我想用一个类似于下面描述的函数来拟合。但是,在实现PYMC3FIT时,正如在第二个代码部分中一样,我遇到了与这两个问题相关的错误 # some constants and fixed things rin = 1. rout = 1000. tmin = 10. x = np.arange(0.1, 10, 0.2) # parameters to be fitted a =

如果它看起来比需要的复杂,我会提前道歉。试图让它比这更简单,但有些问题更难表述。这样就尽可能接近我的处境。我有一些数据,我想用一个类似于下面描述的函数来拟合。但是,在实现PYMC3FIT时,正如在第二个代码部分中一样,我遇到了与这两个问题相关的错误

# some constants and fixed things
rin = 1.
rout = 1000.
tmin = 10.
x = np.arange(0.1, 10, 0.2)

# parameters to be fitted
a = 4.0
b = 0.5
c = 10

# define r_struct, depends on b, but is one value
r_struct = 0.1**(1./b)

# nrad below, the number of radial points,  
# depends on r_struct and the constant rout
# QUESTION 1 here
nrad = round(r_struct, -1) + round((rout - r_struct)/10., -1) * 2

# define the radial bins, logarithmically spaced
rbins = np.logspace(np.log10(rin), np.log10(rout), num=nrad+1, endpoint=True)
rad = 10**(( np.log10(rbins[1:]) + np.log10(rbins[:-1]))/2.)
t = rad**(-b)

# QUESTION 2 here
# t cannot go below tmin, so I do this
t[t < tmin] = tmin

# then I want to know where the limit of r_struct is here
iout = rad > r_struct
iin = rad <= r_struct

# depending on if rad is past r_struct or not
# I do two different things.
c1  = c_struct * (rad[iin]/50.)**(-1.*a)
taper = np.exp( -(rad[iout]/rad[iout][0])**(2.-a) )
c2 = c_taper * taper/taper[0]
tau = np.append(c1, c2) 

y_true = J0( x[:, np.newaxis] * rad) * (t * (1.0 - np.exp(-1.*tau) ) )
y_true = y_true.sum(axis=1)

y_obs = y_true + np.random.normal(size=len(x))*50
y_error = np.ones_like(y_true)*0.1*y_true.max()

plt.errorbar(x,y_obs, yerr= y_error, color='r', marker='.', ls='None', label='Observed')
plt.plot(x,y_true, 'b', marker='None', ls='-', lw=1, label='True')
plt.xlabel('x'); plt.ylabel('y'); plt.legend()
#一些常量和固定的东西
rin=1。
rout=1000。
tmin=10。
x=np.arange(0.1,10,0.2)
#要安装的参数
a=4.0
b=0.5
c=10
#定义r_结构,取决于b,但是一个值
r_结构=0.1**(1./b)
#下面是nrad,径向点的数量,
#取决于r_结构和常量路由
#这里是问题1
nrad=圆形(r_结构,-1)+圆形(r_结构)/10.,-1)*2
#定义径向箱,以对数间隔
rbins=np.logspace(np.log10(rin),np.log10(rout),num=nrad+1,endpoint=True)
rad=10**(np.log10(rbins[1:])+np.log10(rbins[:-1]))/2。)
t=rad**(-b)
#这里是问题2
#t不能低于tmin,所以我这样做
t[tr\u结构
iin=rad r_结构
iin=rad 17 nrad=round(r_结构,-1)+round((rout-r_结构)/10.,-1)*2
18 rbins=np.logspace(np.log10(rin),np.log10(rout),num=nrad+1,endpoint=True)
TypeError:类型TensorVariable未定义_round__方法
问题2:如何与使用PyMC3 Previor创建的数组进行比较?代码在比较时失败

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-12-7f77e70d147a> in <module>()
     26     t = rad**(-b)
     27
---> 28     t[t < tmin] = tmin
     29     iout = rad > r_struct
     30     iin = rad <= r_struct
---------------------------------------------------------------------------
AssertionError回溯(上次最近的调用)
在()
26 t=rad**(-b)
27
--->28 t[tr_结构

30 iin=rad它是否使用
step=pm.Metropolis()
运行?我怀疑
round
操作的不连续性对于区分计算图是有问题的。如果重新参数化,使
b
从一开始就是离散的,那么如果不指定步骤,PyMC将自动在该参数上使用Metropolis,在其他参数上使用NUTS。感谢@merv的建议。然而,
b
需要是连续的,只是我用它来定义一些离散的东西(在我的模型中,
b
定义了径向温度依赖性)。感谢添加错误消息。我可以看出你现在遇到的是theano问题,而不是PyMC3问题。我建议您熟悉一下,因为它们通常需要专门的操作方法,而您经常会用到这些方法。所有依赖于您的房车(
a、b、c
)的东西都将属于这种类型。例如,要对一个张量变量进行四舍五入,可以使用
theano.tensor.round()
。类似地,可以对
使用
theano.tensor.lt
theano.tensor.gt
进行四舍五入,对
=/code>使用
theano.tensor.ge
进行四舍五入。
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-fcf897a21b89> in <module>()
     15     r_struct = 0.1**(1./b)
     16
---> 17     nrad = round(r_struct, -1) + round((rout - r_struct)/10., -1) * 2
     18     rbins = np.logspace(np.log10(rin), np.log10(rout), num=nrad+1, endpoint=True)

TypeError: type TensorVariable doesn't define __round__ method
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-12-7f77e70d147a> in <module>()
     26     t = rad**(-b)
     27
---> 28     t[t < tmin] = tmin
     29     iout = rad > r_struct
     30     iin = rad <= r_struct