与BUGS相比,STAN缓慢采样

时间:2016-05-30 10:08:25

标签: bayesian sampling winbugs stan

我正在尝试从WinBugs切换到Stan,因为我喜欢编程语言并且R中的使用对于Stan比WinBugs更好。我将我的分层贝叶斯模型重新编码为Stan语言,并应用了我在互联网上找到的所有推荐设置。但是,我的WinBugs采样器的速度是Stan采样器的20倍。 (所以我不是在谈论编译器,我知道它对Stan来说比较慢。)

我尝试在互联网上以任何方式加速采样,我向我的模型进行了矢量化,我在Stan的2个核心上运行我的链,而Bugs只运行1个链。我能找到的所有论坛都说Stan不会比Bugs慢得多。有谁知道为什么速度差异如此之大以及我如何加快采样速度?

我的BUGS和Stan的模型发布在下面。每个代码的前三行是代码的主要部分,其余的只是先行。 T和N都是150左右。

谢谢!

BUGS:

model
  {
    for( i in 1 : N)
    {
      for (t in 1 : T)
      {
        Y[i,t] ~ dnorm(mu[i,t],hy[i])
        mu[i,t] <-  alpha1[i] + rho[i] * X0[i,t] + phi[i,t] * X1[i,t] + beta1[i] * X2[i,t] + beta2[i] * X3[i,t]  + beta3[i] * X4[i,t] + beta4[i] * gt[i,t]
        phi[i,t] <- alpha2[i] + gamma1[i] * exp(gamma2[i]*pow(M[i] - P[i,t],2)) + beta5[i] * X5[i,t] + beta6[i] * X6[i,t]
      }
      alpha1[i] ~ dflat()
      hy[i] ~ dgamma(0,0)
      rho[i] ~ dflat()
      beta1[i] ~ dflat()
      beta2[i] ~ dflat()
      beta4[i] ~ dflat()
      alpha2[i] ~ dnorm(mu_alpha2, sigma_alpha2)
      gamma1[i] ~ dnorm(mu_gamma1, sigma_gamma1)
      gamma2[i] ~ dnorm(mu_gamma2, sigma_gamma2)
      M[i] ~ dnorm(mu_M, sigma_M)
      beta3[i] ~ dnorm(mu_beta3, sigma_beta3)
      beta5[i] ~ dnorm(mu_beta5, sigma_beta5)
      beta6[i] ~ dnorm(mu_beta6, sigma_beta6)
    }
  mu_alpha2 ~ dflat()
  mu_gamma1 ~ dflat()
  mu_gamma2 ~ dunif(-1.5,-0.5)
  mu_M ~ dunif(0.7,1.3)
  mu_beta3 ~ dflat()
  mu_beta5 ~ dflat()
  mu_beta6 ~ dflat()
  sigma_alpha2 ~ dgamma(0.1,0.1)
  sigma_gamma1 ~ dgamma(0.1,0.1)
  sigma_gamma2 ~ dgamma(0.1,0.1)
  sigma_M ~ dgamma(0.1,0.1)
  sigma_beta3 ~ dgamma(0.1,0.1)
  sigma_beta5 ~ dgamma(0.1,0.1)
  sigma_beta6 ~ dgamma(0.1,0.1)
}

斯坦:

transformed parameters {
  for (i in 1:N) {
    phi[i] <- alpha2[i] + gamma1[i] * exp(gamma2[i] * (M[i] - P[i]) .* (M[i] - P[i])) + beta4[i] * log(t) + beta5[i] * D4[i];
    mu[i] <- alpha1[i] + phi[i] .* X1[i] + rho[i] * X0[i] + beta1[i] * X2[i] + beta2[i] * X3[i] + beta3[i] * X4[i];
    sigma[i] <- sqrt(sigma_sq[i]);
  }

  sigma_beta3 <- sqrt(sigma_sq_beta3);
  sigma_gamma1 <- sqrt(sigma_sq_gamma1);
  sigma_gamma2 <- sqrt(sigma_sq_gamma2);
  sigma_M <- sqrt(sigma_sq_M);
  sigma_beta4 <- sqrt(sigma_sq_beta4);
  sigma_beta5 <- sqrt(sigma_sq_beta5);
  sigma_alpha2 <- sqrt(sigma_sq_alpha2);
}
model {
  // Priors
  alpha1 ~ normal(0,100);
  rho ~ normal(0,100);
  beta1 ~ normal(0,100);
  beta2 ~ normal(0,100);

  mu_beta3 ~ normal(0,100);
  mu_alpha2 ~ normal(0,100);
  mu_gamma1 ~ normal(0,100);
  mu_gamma2 ~ uniform(-1.5, -0.5);
  mu_M ~ uniform(0.7, 1.3);
  mu_beta4 ~ normal(0,100);
  mu_beta5 ~ normal(0,100);

  sigma_sq ~ inv_gamma(0.001, 0.001);
  sigma_sq_beta3 ~ inv_gamma(0.001, 0.001);
  sigma_sq_alpha2 ~ inv_gamma(0.001, 0.001);
  sigma_sq_gamma1 ~ inv_gamma(0.001, 0.001);
  sigma_sq_gamma2 ~ inv_gamma(0.001, 0.001);
  sigma_sq_M ~ inv_gamma(0.001, 0.001);
  sigma_sq_beta4 ~ inv_gamma(0.001, 0.001);
  sigma_sq_beta5 ~ inv_gamma(0.001, 0.001);

  // likelihoods

  beta3 ~ normal(mu_beta3, sigma_beta3);
  alpha2 ~ normal(mu_alpha2, sigma_alpha2);
  gamma1 ~ normal(mu_gamma1, sigma_gamma1);
  gamma2 ~ normal(mu_gamma2, sigma_gamma2);
  M ~ normal(mu_M, sigma_M);
  beta4 ~ normal(mu_beta4, sigma_beta4);
  beta5 ~ normal(mu_beta5, sigma_beta5);

  for (i in 1:N){
    Y[i] ~ normal(mu[i], sigma[i]);
  }
}

0 个答案:

没有答案