# Global variable in functions block

**URL:** <https://discourse.mc-stan.org/t/global-variable-in-functions-block/1127>\
**Category:** General\
**Tags:** stanc\
**Created:** [July 2, 2017, 11:27pm UTC](https://discourse.mc-stan.org/t/global-variable-in-functions-block/1127 "2017-07-02T23:27:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhomass](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bhomass/32/407_2.png) [@bhomass](https://discourse.mc-stan.org/u/bhomass)\
**Post date:** [July 2, 2017, 11:27pm UTC](https://discourse.mc-stan.org/t/global-variable-in-functions-block/1127/1 "2017-07-02T23:27:02Z")

</div>

I want a global variable (which is a vector in this case), which I can declare once and use throughout my functions block. The compiler is not accepting it. Is there such provision in stan?

---

<div class="post-metadata">

**Author:** ![syclik](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/syclik/32/6_2.png) [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Post date:** [July 3, 2017, 2:43am UTC](https://discourse.mc-stan.org/t/global-variable-in-functions-block/1127/2 "2017-07-03T02:43:11Z")

</div>

No. There are no global variables.

We, the Stan development team, have discussed things like this, but it’s never gotten further. Part of it is because the cons outweigh the pros, but also because it’s lower priority (even if we were to design it properly) and we’re just tackling other things.

We’re always looking for more contributors. If that’s of interest, it would help to explain why it’s useful, how exactly that would be specified. If you have time to work on it to help Stan, then even better!

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 4, 2017, 1:22am UTC](https://discourse.mc-stan.org/t/global-variable-in-functions-block/1127/3 "2017-07-04T01:22:37Z")

</div>

I think at most we’d add closures so that function bodies could implicitly depend on all variables in scope.

---

<div class="post-metadata">

**Author:** ![irelamb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/irelamb/32/17831_2.png) [@irelamb](https://discourse.mc-stan.org/u/irelamb)\
**Post date:** [August 8, 2023, 12:03pm UTC](https://discourse.mc-stan.org/t/global-variable-in-functions-block/1127/4 "2023-08-08T12:03:58Z")

</div>

What if one would like to use the reduce\_sum function to speed up computation time, but at the same time he/she wish to store some intermediate values computed in the partial\_sum function? In that case being able to access and update a global variable could be useful.

For example, I am using a finite mixture model and I would like to store the value of the log likelihood for a certain mixture component and for each data point. at the same time, I am using the reduce\_sum function to add partial sums to the total log likelihood, and those values are computed inside partial\_sums. How should I write the program to still take advantage of the reduce\_sum speed up while accessing the probabilities for a data point to be generated by a given component?

---

<div class="post-metadata">

**Author:** ![irelamb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/irelamb/32/17831_2.png) [@irelamb](https://discourse.mc-stan.org/u/irelamb)\
**Post date:** [August 8, 2023, 12:14pm UTC](https://discourse.mc-stan.org/t/global-variable-in-functions-block/1127/5 "2023-08-08T12:14:50Z")

</div>

I guess one should use map\_rect instead of reduce\_sum in this case.
