stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
723 stars 183 forks source link

Using the wiener_lpdf() function in the cmdstanr package to implement a diffusion model, the construction of a Markov chain was unsuccessful #3073

Closed luckyjlb closed 1 month ago

luckyjlb commented 1 month ago

I encountered the following problem when running the code related to the seven parameter diffusion model using the latest version 2.35 of the cmdstanr package. After repeated modifications, it still persists. I really can't find the reason. I hope to receive everyone's help.Thank you. The code I am running mainly comes from https://valentinpratz.de/posts/2022-12-29-stan-wiener_full-level-1/ And I made corresponding adjustments. The current code does not report any errors, but building a Markov chain was unsuccessful.

example

I used a new version of the function wiener_lpdf (rt [i] | a, t0, w, v [cnd [i]], sv, sw, st0) I mainly learned from the following link. https://github.com/stan-dev/math/pull/2822 example.zip image

code:

library(cmdstanr) load("L1dat.RData") data <- Level1dat summary(data) et_cmdstan_path(path=NULL) chains = 4

initial values

init.stan = function(){ L = list() for (i in 1:chains) { L[[i]] = list( sv = 1 + runif(1, -0.1, 0.1), sw = 0.1 + runif(1, -0.05, 0.05), st0 = 0.2 + runif(1, -0.05, 0.05) ) } return(L) }

Compiling model

mc <- cmdstan_model("Level1.stan")

mc<-cmdstan_model(file)

mc <- cmdstan_model("D:\seven\level1\level1.stan") stan.data = list( N = nrow(data), Ncnds = 3, rt = data$RT, stim = as.numeric(data$Stim), resp = as.numeric(data$Resp), cnd = as.numeric(data$Cond), a = 1,
t0 = 0.3, w = 0.45, v = c(3.5, 2.5, 1.5) ) outdir = "fits" dir.create(outdir, recursive = T, showWarnings = FALSE) m <- mc$sample(data=stan.data, init = init.stan(), refresh = 25, iter_sampling = 250, iter_warmup = 250, chains = chains, parallel_chains = 4, output_dir = outdir, output_basename = "fit", save_warmup = TRUE )

issues

unning MCMC with 4 parallel chains...

Chain 2 Assertion failed: index >= 0 && index < size(), file stan/lib/stan_math/lib/eigen_3.4.0/Eigen/src/Core/DenseCoeffsBase.h, line 410Warning: Chain 2 finished unexpectedly!

Chain 1 Assertion failed: index >= 0 && index < size(), file stan/lib/stan_math/lib/eigen_3.4.0/Eigen/src/Core/DenseCoeffsBase.h, line 410Warning: Chain 1 finished unexpectedly!

Chain 3 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:

Chain 3 Exception: wiener_lpdf: Inter-trial variability in A-priori bias = 0.999729, but must be smaller than 2*(A-priori bias) = 0.9 (in 'C:/Users/13201/AppData/Local/Temp/RtmpYrSjfs/model-6fc8368c44a1.stan', line 29, column 8 to column 72)

Chain 3 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,

Chain 3 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.

Chain 3 Chain 4 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:

Chain 4 Exception: wiener_lpdf: Inter-trial variability in A-priori bias = 0.999978, but must be smaller than 2*(A-priori bias) = 0.9 (in 'C:/Users/13201/AppData/Local/Temp/RtmpYrSjfs/model-6fc8368c44a1.stan', line 29, column 8 to column 72)

Chain 4 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,

Chain 4 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.

Expected output

I hope to generate Markov chains normally.Hope to receive help.

Current version:

cmdstanr packages 2.35

SteveBronder commented 1 month ago

This would probably be better posted on the Stan discourse forums

https://discourse.mc-stan.org/