# Generating and utilizing random interger in the model block

**URL:** <https://discourse.mc-stan.org/t/generating-and-utilizing-random-interger-in-the-model-block/30484>\
**Category:** Modeling\
**Created:** [February 21, 2023, 8:47am UTC](https://discourse.mc-stan.org/t/generating-and-utilizing-random-interger-in-the-model-block/30484 "2023-02-21T08:47:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miracle](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Miracle](https://discourse.mc-stan.org/u/Miracle)\
**Post date:** [February 21, 2023, 8:47am UTC](https://discourse.mc-stan.org/t/generating-and-utilizing-random-interger-in-the-model-block/30484/1 "2023-02-21T08:47:46Z")

</div>

Can someone please help with this? I have a data chain ‘mu’ that I want to randomly select the values at each iteration and utilize the selected values i the model block. So I created the transformed data block where a random integer is created as an index to be used to select the corresponding value of mu. But so far, it seems that the model block has been utilizing only one index for mu. How can I get the index to change at each iteration?  
data {  
int N;  
int K;  
int s1;  
int s2;  
real x[N];  
real y[N];  
real mu[9999];//Use when mu data is used  
int id[N];  
//real id\_l[K];  
}

transformed data {  
real unif = uniform\_rng(0,1);  
real idn = ceil(unif \* 9999);  
int idx;  
for (n in 1:9999) {  
if (n == idn) {  
idx = n;  
break;  
}  
}  
}

parameters {

real beta;  
real\<lower=0\> sigma\_1;  
real\<lower=0\> sigma\_2;  
//real\<lower=0\> mu;//use when estimating mu  
real\<lower=1\> muu;  
real\<lower=0\> sigmau;  
vector[K] U\_raw;  
}

transformed parameters {  
real nu[N];  
//real U[N];  
//real k;  
vector[K] U=U\_raw\*sigmau+muu;  
// U = vector();  
//U=U\_rep[id\_l U\_];

```
    for(n in 1:N){
      
      nu[n]=U[id[n]]+beta*x[n]; #first model with log transformed data
       //nu[n]=exp(U[id[n]])*(x[n])^beta;##second model with original data values
       
      }
      
}

```

model {

```
    U_raw ~std_normal();
    for(n in 1:s1){
      
      y[n] ~ logistic(nu[n],sigma_1);
     
      
    }
    for(n in s2:N){
      
         
      //y[n] ~ logistic(nu[n]+mu,sigma_2);#Use this if we are estimating mu
      print("idx",idx);
      
      y[n] ~ logistic(nu[n]+mu[idx],sigma_2);
    
      }
   }

```

---

<div class="post-metadata">

**Author:** ![WardBrian](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wardbrian/32/12078_2.png) [@WardBrian](https://discourse.mc-stan.org/u/WardBrian)\
**Post date:** [February 21, 2023, 2:33pm UTC](https://discourse.mc-stan.org/t/generating-and-utilizing-random-interger-in-the-model-block/30484/2 "2023-02-21T14:33:07Z")

</div>

I do not believe it is possible to do what you’re describing in Stan. The `transformed data` block is only run once per sampling run, as you’ve noted, and `rng` functions cannot be used in the model block directly.

---

<div class="post-metadata">

**Author:** ![Miracle](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Miracle](https://discourse.mc-stan.org/u/Miracle)\
**Post date:** [February 22, 2023, 8:14am UTC](https://discourse.mc-stan.org/t/generating-and-utilizing-random-interger-in-the-model-block/30484/3 "2023-02-22T08:14:05Z")

</div>

Okay, thank you for the information

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [February 23, 2023, 2:35pm UTC](https://discourse.mc-stan.org/t/generating-and-utilizing-random-interger-in-the-model-block/30484/4 "2023-02-23T14:35:08Z")

</div>

Just for extra context: even if you were sure that this random subsampling of data between iterations actually makes sense and the corresponding Markov chain corresponded to some well-defined posterior distribution (which is far from obvious, but not impossible), there will be a remaining technical problem: Stan’s algorithm requires adaptation of the mass matrix and step size to work. This adaptation is generally sensitive to specific data (the same model with different data often requires quite different step size/mass). So even if you went to Stan’s C++ codebase and interleaved Stan’s HMC steps with your data subsampling (which would be possible in C++) your approach would very likely mess-up the adaptation making HMC perform poorly.

---

<div class="post-metadata">

**Author:** ![Miracle](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Miracle](https://discourse.mc-stan.org/u/Miracle)\
**Post date:** [February 24, 2023, 6:45am UTC](https://discourse.mc-stan.org/t/generating-and-utilizing-random-interger-in-the-model-block/30484/5 "2023-02-24T06:45:32Z")

</div>

I understand your point. Thanks for the additional information
