PyMC3-自定义theano Op进行数值积分

时间:2018-01-04 11:29:37

标签: python-3.x python-2.7 pymc pymc3

我使用PyMC3进行参数估计,使用必须定义的特定似然函数。我用Google搜索并发现我应该使用densitydist方法来实现用户定义的似然函数,但它不起作用。如何在PyMC3中合并用户定义的似然函数,并找出我的模型的最大posteriori(MAP)估计值?我的代码如下。这里L是我的似然函数的分析形式。我有一些关于某些对象的径向速度(vr)和位置(r)的观测数据,这些数据是从excel文件导入的。

data_ = np.array(pandas.read_excel('aaa.xlsx',header=None))
gamma=3.77;
G = 4.302*10**-6;
rmin = 3.0;
R = 95.7;
vr=data_[:,1];
r= data_[:,0];
h= np.pi;

class integrateOut(theano.Op):
 def __init__(self,f,t,t0,tf,*args,**kwargs):
    super(integrateOut,self).__init__()
    self.f = f
    self.t = t
    self.t0 = t0
    self.tf = tf

 def make_node(self,*inputs):
    self.fvars=list(inputs)

    try:
        self.gradF = tt.grad(self.f,self.fvars)
    except:
        self.gradF = None
    return theano.Apply(self,self.fvars,[tt.dscalar().type()])

 def perform(self,node, inputs, output_storage):

    args = tuple(inputs)
    f = theano.function([self.t]+self.fvars,self.f)
    output_storage[0][0] = quad(f,self.t0,self.tf,args=args)[0]

 def grad(self,inputs,grads):
    return [integrateOut(g,self.t,self.t0,self.tf)(*inputs)*grads[0] \
        for g in self.gradF] 

basic_model = pm.Model()
with basic_model:
   M=[]
   beta=[]
   interval=0.01*10**12
   M=pm.Uniform('M', 
               lower=0.5*10**12,upper=3.50*10**12,transform='interval')
   beta=pm.Uniform('beta',lower=2.001,upper=2.999,transform='interval')
   gamma=3.77
   logp=[]
   arr=[]
   vnew=[]
   rnew=[]
   theta = tt.scalar('theta')
   beta = tt.scalar('beta')
   z = tt.cos(theta)**(2*( (gamma/(beta - 2)) - 3/2) + 3)    
   intZ = integrateOut(z,theta,-(np.pi)/2,(np.pi)/2)(beta)
   gradIntZ = tt.grad(intZ,[beta])
   funcIntZ = theano.function([beta],intZ)
   funcGradIntZ = theano.function([beta],gradIntZ)  
   for j in np.arange(0,59,1):
     vnew.append(vr[j]+(0.05*vr[j]*float(dm.Decimal(rm.randrange(1, 
     20))/10)));
     rnew.append(r[j]+(0.05*r[j]*float(dm.Decimal(rm.randrange(1, 
     20))/10)));
   vn=np.array(vnew)
   rn=np.array(rnew)
   for beta in np.arange (2.01,2.99,0.01):
     for M in np.arange (0.5,2.50,0.01):
         i=np.arange(0,59,1)
         q =( gamma/(beta - 2)) - 3/2
         B = (G*M*10**12)/((beta -2 )*( R**(3 - beta)))
         K = (gamma - 3)/((rmin**(3 - gamma))*funcIntZ(beta)*m.sqrt(2*B))
         logp= -np.log(K*((1 -(( 1/(2*B) )*((vn[i]**2)*rn[i]**(beta - 
                          2))))**(q+1))*(rn[i]**(1-gamma +(beta/2))))
         arr.append(logp.sum())
   def logp_func(rn,vn):
      return min(np.array(arr))  
   logpvar = pm.DensityDist("logpvar", logp_func, observed={"rn": rn,"vn":vn})
    start = pm.find_MAP(model=basic_model)
    step = pm.Metropolis()
    basicmodeltrace = pm.sample(10000, step=step, 
    start=start,random_seed=1,progressbar=True)
    print(pm.summary(basicmodeltrace))
    map_estimate = pm.find_MAP(model=basic_model)
    print(map_estimate)

我收到以下错误消息:

 ValueError: Cannot compute test value: input 0 (theta) of Op 
 Elemwise{cos,no_inplace}(theta) missing default value.  
 Backtrace when that variable is created:

由于数值积分不起作用,我无法得到输出。我使用自定义theano op来获取Custom Theano Op to do numerical integration得到的数值积分代码。如果我运行它单独输入特定的beta值,但不在模型中,则集成有效。

1 个答案:

答案 0 :(得分:0)

我对您的代码做了一些更改,但这仍然不起作用,但我希望它更接近解决方案。请check this thread,因为有人正在努力解决基本相同的问题。

class integrateOut(theano.Op):
    def __init__(self, f, t, t0, tf,*args, **kwargs):
        super(integrateOut,self).__init__()
        self.f = f
        self.t = t
        self.t0 = t0
        self.tf = tf

    def make_node(self, *inputs):
        self.fvars=list(inputs)
        try:
            self.gradF = tt.grad(self.f, self.fvars)
        except:
            self.gradF = None
        return theano.Apply(self, self.fvars, [tt.dscalar().type()])

    def perform(self,node, inputs, output_storage):

        args = tuple(inputs)
        f = theano.function([self.t] + self.fvars,self.f)
        output_storage[0][0] = quad(f, self.t0, self.tf, args=args)[0]

    def grad(self,inputs,grads):
        return [integrateOut(g, self.t, self.t0, self.tf)(*inputs)*grads[0] \
                for g in self.gradF]


gamma = 3.77
G = 4.302E-6
rmin = 3.0
R = 95.7
vr = data[:,1]
r = data[:,0]
h = np.pi
interval =  1E10

vnew = []
rnew = []
for j in np.arange(0,59,1):
    vnew.append(vr[j]+(0.05*vr[j] * float(dm.Decimal(rm.randrange(1, 20))/10)))
    rnew.append(r[j]+(0.05*r[j] * float(dm.Decimal(rm.randrange(1, 20))/10)))
vn = np.array(vnew)
rn = np.array(rnew)

def integ(gamma, beta, theta):
    z = tt.cos(theta)**(2*((gamma/(beta - 2)) - 3/2) + 3)    
    return integrateOut(z, theta, -(np.pi)/2, (np.pi)/2)(beta)

with pm.Model() as basic_model:

    M = pm.Uniform('M', lower=0.5*10**12, upper=3.50*10**12)
    beta = pm.Uniform('beta', lower=2.001, upper=2.999)
    theta = pm.Normal('theta', 0, 10**2)

    def logp_func(rn,vn):
        q = (gamma/(beta - 2)) - 3/2
        B = (G*M*1E12) / ((beta -2 )*(R**(3 - beta)))
        K = (gamma - 3) / ((rmin**(3 - gamma)) * integ(gamma, beta, theta) * (2*B)**0.5)
        logp = - np.log(K*((1 -((1/(2*B))*((vn**2)*rn**(beta - 
                        2))))**(q+1))*(rn**(1-gamma +(beta/2))))
        return logp.sum()

    logpvar = pm.DensityDist("logpvar", logp_func, observed={"rn": rn,"vn":vn})
    start = pm.find_MAP()
    #basicmodeltrace = pm.sample()
    print(start)