Ordered_logistic_lpmf

Starting from the example of Bürkner & Vuorre (Ordinal Regression Modells in Psychology: A Tutorial)page 12, I use ``stancode(fit_sc1)``` to retrieve the following model:

fit_sc1 <- brm(formula = rating ~ 1+ belief, data = stemcell, family = cumulative())

to understand what’s going on in Ordinal Models.

Messing with the code I changed

for (n in 1:N) {
    target += ordered_logistic_lpmf(Y[n]| mu[n], temp_Intercept);
  }

to

 target += ordered_logistic_lpmf(Y| mu, temp_Intercept);

to try the vectorization.With the vectorization I get 10x slower code, 3951 divergences, \hat{R} \gt 4 and low ESS.
I’m totally OK with non-vectorized code… but what is happening here and what am I missing here?

This is due to a bug in the vectorised code (my fault actually!). For that combination of inputs, the derivatives aren’t being calculated properly. There’s more context on the issue here.

This bug has been fixed in the 2.20 release. In the interim, you can also fix this yourself.

First find the ordered_logistic header file with the command:

system.file("include", "stan", "math", "prim", "mat", "prob", 
            "ordered_logistic_lpmf.hpp", 
            package = "StanHeaders")

If you open up that file, on line 139 you’ll see:

        ops_partials.edge2_.partials_vec_[n](0) = d;

Change that to:

        ops_partials.edge2_.partials_vec_[n](0) += d;

i.e., change “=” to “+=”

This code was overwriting the derivatives w.r.t to the vector of cutpoints, rather than accumulating them for each observation. Let me know if that works for you or not

3 Likes

It works like a charms! And moreover… it’s 3x faster than the unvectorized code. Thanks @andrjohns!

2 Likes

Thanks! I have a graded response model that suddenly started acting strange over summer. Good to know why!

From a struggling newbie: I installed Stan through RStan (and separately PyStan). I found the relevant file (this seemed clearest to me: https://github.com/stan-dev/math/commit/d0640b309f496f315b11c4e363e845fc1fdf5c9d ) and edited it, but how do I recompile the code, without overwriting my edit by updating the package? I’m on GNU/Linux.

Or… Is there a way I can be running 2.20?

My best guess for recompiling was to do the “Installing RStan from source” on https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Linux and hope that it uses the code downloaded inside my R folder. But I tried, and it overwrote my bugfix.

For the R side, I would just manually fix that header in your StanHeaders installation, which is probably under ~/R/somethingsomething/StanHeaders/inst/include folder (sorry I’m not at my PC so I’m going by memory).

Or you could consider using cmdstanr/cmdstanpy.

A couple of posts up I wrote out a guide for how to update the file for an existing RStan installation. You just need to update the file in your R package directory and continue as normal, no re-installation needed

Oh, thank you both @mcol and @andrjohns !!
I see; I only need to recompile my own .stan files, not the whole installation. Great! Things are running an order of magnitude faster and the results are no longer terrible !!

I wish I could have the last three days back, though. Is there a mechanism to back-port this simple fix to the version that people are downloading and installing?

1 Like