一个JAGS模型,其中包括离散值的总和斯坦版本 - 这可能吗?(Stan version of a

2019-10-21 15:15发布

我试图运行斯坦这一模式。 我有它的运行JAGS版本(返回高度自相关的参数),我知道如何把它作为制定双指数(两分率),这没有问题可能运行的CDF。 不过,我想用这个版本作为起点相似,但更复杂的模型。

现在我有怀疑,这样的模式是不可能的斯坦。 因为通过采取一个布尔值的总和离散介绍的也许,斯坦可能无法计算梯度。

有谁知道这是否是这种情况,或者如果我做了错误的方式别的东西在这个模型? 我贴我得到下面的模型代码中的错误。

提前一月非常感谢

Model:

data {
    int y[11]; 
    int reps[11];
    real soas[11]; 

}
parameters {
    real<lower=0.001,upper=0.200> v1;
    real<lower=0.001,upper=0.200> v2;

}


model {
    real dif[11,96];
    real cf[11];

    real p[11];

    real t1[11,96];
    real t2[11,96];

    for (i in 1:11){
        for (r in 1:reps[i]){     
            t1[i,r]  ~ exponential(v1);
            t2[i,r]  ~ exponential(v2);
            dif[i,r] <-  (t1[i,r]+soas[i]<=(t2[i,r]));

            }
        cf[i] <- sum(dif[i]);
        p[i]  <-cf[i]/reps[i];
        y[i] ~ binomial(reps[i],p[i]); 
    }

}

下面是一些虚拟的数据:

psy_dat = { 
         'soas' :  numpy.array(range(-100,101,20)),
            'y' :  [47, 46, 62, 50, 59, 47, 36, 13, 7, 2, 1],
         'reps' :  [48, 48, 64, 64, 92, 92, 92, 64, 64, 48, 48]
          }

这里是错误的:

DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal): Left-hand side of sampling statement (~) contains a non-linear transform of a parameter or local variable.
You must call increment_log_prob() with the log absolute determinant of the Jacobian of the transform.
Sampling Statement left-hand-side expression:
get_base1(get_base1(t1,i,"t1",1),r,"t1",2) ~ exponential_log(...)
Warning (non-fatal): Left-hand side of sampling statement (~) contains a non-linear transform of a parameter or local variable.
You must call increment_log_prob() with the log absolute determinant of the Jacobian of the transform.
Sampling Statement left-hand-side expression:
get_base1(get_base1(t2,i,"t2",1),r,"t2",2) ~ exponential_log(...)

并在运行时:

Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
 stan::prob::exponential_log(N4stan5agrad3varE): Random variable is nan:0, but must not be nan!
 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
 Rejecting proposed initial value with zero density.


Initialization between (-2, 2) failed after 100 attempts. 
 Try specifying initial values, reducing ranges of constrained values, or   reparameterizing the model

下面是这个模型的工作JAGS版本:

   model {
   for ( n in 1 : N  ) { 
     for (r in 1 : reps[n]){
       t1[r,n] ~ dexp(v1)
       t2[r,n] ~ dexp(v2)
       c[r,n] <- (1.0*((t1[r,n]+durs[n])<=t2[r,n]))
     } 
     p[n] <- max((min(sum(c[,n]) /  (reps[n]),0.99999999999999)),   1-0.99999999999999)) 
     y[n] ~ dbin(p[n],reps[n])
   }

   v1 ~ dunif(0.0001,0.2)
   v2 ~ dunif(0.0001,0.2)
   }

关于MIN()和MAX():看到这个职位https://stats.stackexchange.com/questions/130978/observed-node-inconsistent-when-binomial-success-rate-exactly-one?noredirect=1 #comment250046_130978 。

Answer 1:

我真不知道你想什么模型来估算(这将是最好的,如果你发布尖齿代码),但你有什么上面不能在斯坦工作。 斯坦是,你必须声明,然后定义对象的感觉更接近C ++。 在你斯坦计划,你有两个声明real t1[11,96]; real t2[11,96]; real t1[11,96]; real t2[11,96]; 但没有定义t1t2 。 因此,它们initalized到NaN ,当你做t1[i,r] ~ exponential(v1); 该被解析为像for(i in 1:11) for(r in 1:reps[i]) lp__ += log(v1) - v1 * NaN其中lp__是保持对数后验的值的内部符号,这变成NaN,它不能做参数的都市风格的更新。

也许你意味着t1t2是未知参数,在这种情况下,他们应该在声明parameters块。 下面将帖子斯坦计划是有效的,应该工作,但它可能不是你心目中的程序(它没有很多的意义,我和间断dif可能会从取样从这个后验分布排除斯坦有效率的)。 data { int<lower=1> N; int y[N]; int reps[N]; real soas[N]; } parameters { real<lower=0.001,upper=0.200> v1; real<lower=0.001,upper=0.200> v2; real t1[N,max(reps)]; real t2[N,max(reps)]; } model { for (i in 1:N) { real dif[reps[i]]; for (r in 1:reps[i]) { dif[r] <- (t1[i,r]+soas[i]) <= t2[i,r]; } y[i] ~ binomial(reps[i], (1.0 + sum(dif)) / (1.0 + reps[i])); } to_array_1d(t1) ~ exponential(v1); to_array_1d(t2) ~ exponential(v2); }



文章来源: Stan version of a JAGS model which includes a sum of discrete values - Is it possible?