rmcelreath / rethinking

Statistical Rethinking course and book package
2.18k stars 604 forks source link

Can't work "Full luxury Bayes" from 2023 lecture 12 #449

Closed richardCrandall closed 3 days ago

richardCrandall commented 5 days ago

I'm working my way through Statistical Rethinking and have run into some trouble running the models in the bonus material of the 2023 lecture 12. I initially had a lot of trouble replicating the posteriors shown on the slides, but that may be due to sampling differences since we're not using a set.seed(). However, when I attempted to run the "full luxury Bayes" model at the end of the bonus material, Stan couldn't run the model at all.

Before attempting to run the chains, I got an error message saying, "line 27, column 8: Unreachable statement (following a reject, fatal_error, break, continue, or return) found, is this intended?"

The code at line 27, column 8 of the Stan text file is the next line after a return line, which from what I gather from ChatGPT is part of the problem: Stan can't run two different returns as it terminates after the first return. Here is the Stan code I'm seeing:

        return normal_lpdf( X | mu , sigma ); // line 26
        for ( i in 1:size(Y) ) {
            p[i] = a[g[start+i-1]] + bxy * X[start+i-1] + bzy * Z[g[start+i-1]] + buy * u[g[start+i-1]];
            p[i] = inv_logit(p[i]);
        }
        return bernoulli_lpmf( Y | p );

Once the code tried to run, it produced these errors: "Chain 1 Unrecoverable error evaluating the log probability at the initial value. Chain 1 Exception: Exception: normal_lpdf: Random variable has size = 200, but Location parameter has size 1; and they must be the same size." It also referred to line 27, and then referred to this section of the Stan code as well:

target += reduce_sum( reducer , Y , 1 , 
            Xbar,
            Ng,
            Z,
            a,
            X,
            g,
            u,
            z,
            bzy,
            buy,
            bxy,
            aX,
            bux,
            abar,
            tau,
            sigma )

For reference, here is the full R code that I am using, from the 2023 lecture 12 slides & video:

N_groups <- 30
N_id <- 200
a0 <- (-2)
bZY <- (-0.5)
g <- sample(1:N_groups,size=N_id,replace=TRUE) # sample into groups
Ug <- rnorm(N_groups,1.5) # group confounds
X <- rnorm(N_id, Ug[g] ) # individual varying trait
Z <- rnorm(N_groups) # group varying trait (observed)
Y <- rbern(N_id, p=inv_logit( a0 + X + Ug[g] + bZY*Z[g] ) )
dat <- list(Y=Y,X=X,g=g,Ng=N_groups,Z=Z)
xbar <- sapply( 1:N_groups , function(j) mean(X[g==j]) )
dat$Xbar <- xbar
mru <- ulam(
    alist(
        # Y model
        Y ~ bernoulli(p),
        logit(p) <- a[g] + bxy*X + bzy*Z[g] + buy*u[g],
        transpars> vector[Ng]:a <<- abar + z*tau,
        # X model
        X ~ normal(mu,sigma),
        mu <- aX + bux*u[g],
        vector[Ng]:u ~ normal(0,1),
        # priors
        z[g] ~ dnorm(0,1),
        c(aX,bxy,buy,bzy) ~ dnorm(0,1),
        bux ~ dexp(1),
        abar ~ dnorm(0,1),
        tau ~ dexp(1),
        sigma ~ dexp(1)
    ) , data=dat , chains=4 , cores=4 , sample=TRUE )

I'm not sure if there was a code update in Stan/R/Rethinking/etc. that makes the code no longer work, if it's something unique to my computer, if it's an error on my part, etc. Any help would be greatly appreciated.

rmcelreath commented 4 days ago

The code samples fine for me. Perhaps you need to update Stan?

richardCrandall commented 4 days ago

The code samples fine for me. Perhaps you need to update Stan?

I've spent some time trying to reinstall Stan but I think I made things worse. Now I have trouble running ulam() models that ran fine before. I tried updating Cmdstanr, and when that seemed to break things I tried uninstalling Cmdstanr, deleting Cmdstan files, and uninstalling Rethinking, and then reinstalling said packages. It still seems broken. I'm getting a long error message; please see below for snippets:

In file included from stan/lib/stan_math/lib/tbb_2020.3/include/tbb/tbb_profiling.h:123,
                 from stan/lib/stan_math/lib/tbb_2020.3/include/tbb/task.h:36,
                 from stan/lib/stan_math/lib/tbb_2020.3/include/tbb/task_arena.h:23,
                 from stan/lib/stan_math/stan/math/prim/core/init_threadpool_tbb.hpp:18,
                 from src/cmdstan/arguments/arg_num_threads.hpp:5,
                 from src/cmdstan/command.hpp:8,
                 from src/cmdstan/main.cpp:1:
stan/lib/stan_math/lib/tbb_2020.3/include/tbb/atomic.h:422:24: warning: template-id not allowed for constructor in C++20 [-Wtemplate-id-cdtor]
  422 |     constexpr atomic<T>(const atomic<T>& rhs): internal::atomic_impl<T>(rhs) {}
      |                        ^
In file included from stan/lib/stan_math/stan/math/prim/prob/std_normal_ccdf_log.hpp:5,
                 from stan/lib/stan_math/stan/math/prim/prob.hpp:331,
                 from stan/lib/stan_math/stan/math/prim.hpp:16,
                 from stan/src/stan/io/dump.hpp:7,
                 from src/cmdstan/command_helper.hpp:10,
                 from src/cmdstan/command.hpp:13:
stan/lib/stan_math/stan/math/prim/prob/std_normal_lccdf.hpp: In function 'stan::return_type_t<T> stan::math::std_normal_lccdf(const T_y&)':
stan/lib/stan_math/stan/math/prim/prob/std_normal_lccdf.hpp:52: note: '-Wmisleading-indentation' is disabled from this point onwards, since column-tracking was disabled due to the size of the code/headers
   52 |     } else if (y_dbl > 8.25) {

stan/lib/stan_math/stan/math/prim/prob/std_normal_lccdf.hpp:52: note: adding '-flarge-source-files' will allow for more column-tracking support, at the expense of compilation time and memory

AI tells me:

The error you encountered while running cmdstanr::install_cmdstan() appears to be related to compatibility issues with the Intel Threading Building Blocks (TBB) library used by CmdStan. Specifically, the error message in the attached file indicates warnings about C++20 features (e.g., template-id not allowed for constructor) in TBB's atomic.h file. This issue typically arises due to conflicts between the version of TBB bundled with CmdStan and the C++ compiler or standard being used.

I've tried doing what AI told me in order to tell my computer to use C++17 instead of C++20 but nothing seems to work. I get the same error message I quoted from above.

My only thought is that maybe R, Cmdstanr, or something else was updated recently to cause these problems -- either that, or I've just screwed something up very badly (which is very possible, as I'm not a computer expert).

For reference:

R version 4.4.2 (2024-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets 
[7] methods   base     

other attached packages:
[1] rethinking_2.42     posterior_1.6.0     cmdstanr_0.8.1.9000
rmcelreath commented 3 days ago

Hm at least new error messages is some kind of progress? Whatever is wrong, it has nothing to do with my package. I have no idea how to help you. Maybe someone on the Stan forums could help.

richardCrandall commented 3 days ago

Hm at least new error messages is some kind of progress? Whatever is wrong, it has nothing to do with my package. I have no idea how to help you. Maybe someone on the Stan forums could help.

Got it, I'll go in that direction. Thanks for taking the time to help a stranger on the internet. I've been reading through your book and listening to your lectures and they've both been a wonderful resource.