Dear
Perry and All nimble users
Thank you for your suggestion!
It is really useful for debugging.
I tried debugging with your "print() method".
Actually I can't understand why and where this error happens.
I set print() function anywhere in my code, then I found some curious situations.
1. Error occurs different places even the same seed value.
I attach two screenshots to this mail, they are the screenshots of when the error occurred.
As you can see, error occurred at different points.
2. Error happens between two print()s even these are next to each other!!!
In my code below, error occurs
print("vol ",vol, " i ",i)
print("std2_1 ", std, " i ",i)
between these 2 prints. I guess so because vol was printed but std was not printed just before Aborting error.
3. (I guess this can't be the source of error) "for function" acts in curious order.
In my code,
for() calculates calc_volume() from i=1 to i=t at first, then it calculates calc_2() from i=1 to i=t (I guess so from the order of print() in screenshots).
I'm actually despaired now.
It looks impossible to debug, I thought.
Thank you for reading!
It would be help if any comments or suggestions!
Thanks,
kei
This is my code,
calc_volume = nimbleFunction(
run = function(distance = double(0), radius = double(0), mat = double(2),A = double(1), AA = double(1),AAA = double(2),i = integer(0),j = integer(0)) {
rad_big = min(which(A - radius > 0))
rad_sma = rad_big-1
#if((rad_sma < 1) || (rad_sma > 297)) browser()
print(distance, " ", radius, " i ",i," j ",j)
small_rad_line = AAA[rad_sma,]
big_rad_line = AAA[rad_big,]
dist_big1 = min(which(small_rad_line - distance > 0))
dist_big2 = min(which(big_rad_line - distance > 0))
### distanceが0の時にエラーが出るかわからないからエラー処理は保留
dist_small1 = dist_big1-1
dist_small2 = dist_big2-1
a1 = AAA[rad_sma,dist_small1] # x of left low
a2 = AAA[rad_sma,dist_big1] # x of right low
A1 = AAA[rad_big,dist_small2] # x of left high
A2 = AAA[rad_big,dist_big2] # x of right high
aa1 = mat[rad_sma, dist_small1] # y of left low
aa2 = mat[rad_sma, dist_big1] # y of right low
AA1 = mat[rad_big, dist_small2] # y of left high
AA2 = mat[rad_big, dist_big2] # y of right high
rate1_low = (distance - a1)/(a2 - a1)
rate1_high = (distance - A1)/(A2 - A1)
rate2 = (radius - A[rad_sma])/(A[rad_big] - A[rad_sma])
x1 = (a2 - a1)*rate1_low + a1
x2 = (A2 - A1)*rate1_high + A1
y1 = (aa2 - aa1)*rate1_low + aa1
y2 = (AA2 - AA1)*rate1_high + AA1
x_mid = (x2 - x1)*rate2 + x1
y_mid = (y2 - y1)*rate2 + y1
print("y_mid ",y_mid, " i ",i," j ",j)
returnType(double(0))
return(y_mid)
})
calc_2 = nimbleFunction( # AAAとかmatもdoubleにしないとエラー
run = function(res_box = double(2), rho = double(0),i = integer(0), std = double(0)) {
print("res_box ",res_box[i, 1:5], " i ",i)
print("rho ",rho, " i ",i)
vol <- 120 + 20*log(rho*res_box[i, 1:5])/log(10) # log10(x) = log(x)/log(10)
print("vol ",vol, " i ",i)
print("std2_1 ", std, " i ",i) ←←← Error occurred here!!
returnType(double(1))
return(vol)
})
### My model
Particle_code_std2s <- nimbleCode({
## produce init particles
x[1,1] ~ dunif(-1500, 3000)
x[1,2] ~ dunif(-3000, 2000) # y
x[1,3] ~ dunif(0, 3500) # r
for(i in 2:t){
## forcast
x[i,1] ~ T(dnorm(x[i-1, 1], sd = sqrt(std_xy)) ,x_min, x_max) # nimbleでは明示的にsd= or tau=にしないといけない
x[i,2] ~ T(dnorm(x[i-1, 2], sd = sqrt(std_xy)) ,y_min, y_max) # invGは分散に対して。
x[i,3] ~ T(dnorm(x[i-1, 3], sd = sqrt(std_r)) ,0,100000) # 下限が0.1の時は収束した(普通のMCMCで)
d[i,1:5] <- sqrt((XX[1:5]-x[i,1])^2+(YY[1:5]-x[i,2])^2) # ちゃんとベクトルを扱うときは全部を指定する
for (j in 1:n_mic) { # ここでerror 'list' object cannot be coerced to type 'double'
res_box[i,j] <- calc_volume(distance = d[i,j], radius = x[i,3], mat = mat[1:297,1:171], A = A[1:297], AA = AA[1:297], AAA = AAA[1:297,1:171], i = i, j = j)
}
vol[i, 1:5] <- calc_2(res_box = res_box[1:t,1:5], rho = rho, i = i, std = std2_1)
Data[i,1] ~ ddexp(location = vol[i,1], scale = std2_1)
Data[i,2] ~ ddexp(location = vol[i,2], scale = std2_1)
Data[i,3] ~ ddexp(location = vol[i,3], scale = std2_1)
Data[i,4] ~ ddexp(location = vol[i,4], scale = std2_1)
Data[i,5] ~ ddexp(location = vol[i,5], scale = std2_1)
}
# priors
std_xy ~ dunif(0, 1000000)
std_r ~ dunif(0, 1000)
std2_1 ~ dunif(0,100)
rho ~ dunif(0, 100)
})
mat = as.matrix(read.csv("sound_volume_matrix_Log10_2.csv", header = FALSE))
A = unlist(read.csv("calculated_radius_matrix_Log10_2.csv", header=FALSE))
AA = unlist(read.csv("number_of_distance_Log10_2.csv", header=FALSE))
AAA = as.matrix(read.csv("radius_distance_matrix_Log10_2.csv", header=FALSE))
AAA[
is.na(AAA)] = 10000000#NULL
#AAA
x1 = 0 # マイクロホン座標
y1 = 0
x2 = 941.1171
y2 = 583.7835
x3 = 863.6608
y3 = -1402.1910
x4 = 1325.5312
y4 = -1126.3762
x5 = 1801.3538
y5 = -843.9334
XX = c(x1, x2, x3, x4, x5)
YY = c(y1, y2, y3, y4, y5)
x_min = min(XX) - 3000
x_max = max(XX) + 3000
y_min = min(YY) - 3000
y_max = max(YY) + 3000
res_box = array(rep(0,5))
vol = array(rep(0,5))
df = read.csv("df8_4_q25Log.csv", header = FALSE)[,-1] # 1列目はいらない列(何時のデータかが書いてある列)
df[1,] = 1# あえて1行目は使わない行が書いてある(尤度の比較とかが2行目からだから)
df2 <- data.frame( lapply(df, as.numeric) ) # dataをnumericに
Data = list(Data = df2)
time_point = nrow(df) #
n_mic = 5
Inits = list(std_xy = 1, std2_1 = 1, std2_2 = 1, std2_3 = 1, std2_4 = 1, std2_5 = 1,
std_r = 1, rho = 1, x = matrix(rep(1, 3*time_point), ncol = 3))
Const = list(x_min = x_min, x_max = x_max, y_min = y_min, y_max = y_max, XX = XX, YY = YY, mat = mat, A = A, AA = AA, AAA = AAA, n_mic = n_mic, t = time_point)
Particle_model <- nimbleModel(Particle_code_std2s, constants = Const,
data <- Data,inits = Inits,
check = TRUE)
cParticle_model <- compileNimble(Particle_model)
## watch the action
#res_box[i,j] <- calc_volume(distance = d[i,j], radius = x[i,3], mat = mat[1:297,1:171], A = A[1:297], AA = AA[1:297], AAA = AAA[1:297,1:171])
i = 19
#cParticle_model$vol[,i] = c()
cParticle_model$x[2,i] = 721.435
cParticle_model$rho
#cParticle_model$rad_big
cParticle_model$calculate("res_box[2, 3]")
cParticle_model$res_box[2, 3] = 35.1879
cParticle_model$calculate("vol[2, 3]")
cParticle_model$vol[2,]
n_par = 1 # the number of particles
particleconf <- configureMCMC(Particle_model,
node = NULL, #
print = TRUE, enableWAIC = TRUE) # enableWAIC = TRUEにしないと、runMCMCでWAIC=TRUEにできない
particleconf$addMonitors(c("std_xy", "std2_1","std_r", "rho")) #
## 相関が大きいからblock_samplerした方が収束が良くなる!alphaとbetaの時間自己相関も減少
particleconf$addSampler(target = c("std_xy", "std2_1","std_r", "rho"), type = "RW_PF_block",
control = list(adaptive = TRUE, propCov= diag(4), adaptScaleOnly = FALSE,adaptInterval = 50, # adaptScaleOnlyがFalseだとadoptiveがscaleだけじゃなくてpropcovも行う。
pfNparticles = n_par, pfType = "bootstrap",
latents = c("x"),
pfOptimizeNparticles = FALSE)) # adoptIntervalは小さいと意味ない,でも大きすぎてもいけない
### calc
Particleconf <- buildMCMC(particleconf) # xyrをxにまとめることでbuildできるようになった!
#cParticleconf <- compileNimble(Particleconf)
cParticleMCMC <- compileNimble(Particleconf, project = cParticle_model, resetFunctions = TRUE)
iter = 20
burn_in = 0
res = runMCMC(cParticleMCMC, niter = iter, nchains = 1, nburnin = burn_in, summary = TRUE, WAIC = TRUE, setSeed = 1) ←←←← Aborting error occourred when I did this line.
2024年5月20日月曜日 23:25:18 UTC+9 Perry de Valpine: