# Ordered\_logistic\_lpmf

**URL:** <https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799>\
**Category:** Modeling\
**Created:** [July 19, 2019, 11:26am UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799 "2019-07-19T11:26:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabio](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/fabio/32/935_2.png) [@fabio](https://discourse.mc-stan.org/u/fabio)\
**Post date:** [July 19, 2019, 11:26am UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/1 "2019-07-19T11:26:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [July 19, 2019, 12:11pm UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/2 "2019-07-19T12:11:15Z")

</div>

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](https://github.com/stan-dev/math/issues/1248#issuecomment-494350329).

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

---

<div class="post-metadata">

**Author:** ![fabio](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/fabio/32/935_2.png) [@fabio](https://discourse.mc-stan.org/u/fabio)\
**Post date:** [July 19, 2019, 12:21pm UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/3 "2019-07-19T12:21:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![erognli](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/erognli/32/5181_2.png) [@erognli](https://discourse.mc-stan.org/u/erognli)\
**Post date:** [September 5, 2019, 4:08pm UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/4 "2019-09-05T16:08:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpbl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cpbl/32/7177_2.png) [@cpbl](https://discourse.mc-stan.org/u/cpbl)\
**Post date:** [February 18, 2020, 10:10pm UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/5 "2020-02-18T22:10:52Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![mcol](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mcol/32/5598_2.png) [@mcol](https://discourse.mc-stan.org/u/mcol)\
**Post date:** [February 18, 2020, 10:59pm UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/6 "2020-02-18T22:59:39Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![mcol](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mcol/32/5598_2.png) [@mcol](https://discourse.mc-stan.org/u/mcol)\
**Post date:** [February 18, 2020, 11:01pm UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/7 "2020-02-18T23:01:02Z")

</div>

Or you could consider using cmdstanr/cmdstanpy.

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [February 19, 2020, 1:32am UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/8 "2020-02-19T01:32:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpbl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/cpbl/32/7177_2.png) [@cpbl](https://discourse.mc-stan.org/u/cpbl)\
**Post date:** [February 19, 2020, 2:24am UTC](https://discourse.mc-stan.org/t/ordered-logistic-lpmf/9799/9 "2020-02-19T02:24:07Z")

</div>

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?
