# Defining priors for dependent random variables in STAN

**URL:** <https://discourse.mc-stan.org/t/defining-priors-for-dependent-random-variables-in-stan/25167>\
**Category:** Modeling\
**Tags:** rstan, specification\
**Created:** [November 8, 2021, 4:59pm UTC](https://discourse.mc-stan.org/t/defining-priors-for-dependent-random-variables-in-stan/25167 "2021-11-08T16:59:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen0902](https://avatars.discourse-cdn.com/v4/letter/p/ea666f/32.png) [@Praveen0902](https://discourse.mc-stan.org/u/Praveen0902)\
**Post date:** [November 8, 2021, 4:59pm UTC](https://discourse.mc-stan.org/t/defining-priors-for-dependent-random-variables-in-stan/25167/1 "2021-11-08T16:59:30Z")

</div>

**Background** : I have a simulation model which has unobserved parameters. I created a metamodel using artificial neural networks (ANN) because the simulation model takes a lot of time. I am trying to estimate the unobserved parameters using Bayesian calibration, where priors are based on current knowledge, and the likelihood of observing data is being estimated from the metamodel.

**Query** : I have two random variables X and Y for which I am trying to get the posterior distribution using STAN. The prior distribution of X is uniform, _U(0,2)_. The prior for Y is also uniform, but it will always exceed X i.e., _Y ~ U(X,2)_. Since Y depends on X, how can I define the prior distribution for Y in STAN such that the constraint Y\>X holds? I am new to STAN, so I would appreciate any suggestions or guidance on how to proceed. Thank you so much!

* * *

```stan
// Xq is the vector of parameters for calibration
parameters {
  matrix<lower=-1, upper=1>[num_targets,num_inputs] Xq;
} 

```

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [November 8, 2021, 6:53pm UTC](https://discourse.mc-stan.org/t/defining-priors-for-dependent-random-variables-in-stan/25167/2 "2021-11-08T18:53:59Z")

</div>

Hi Praveen, welcome to Discourse! This is a good question!

I just want to clarify: you need a joint prior on X and Y such that

- the margin for X is uniform between zero and two.
- additionally, for any value of X the pushforward for Y comes from uniform(X, 2)
- and as a result, you’re expecting the histogram of the margin for Y (without conditioning on X) that looks like a concave-up curve increasing beginning at (0,0).

Here are two Stan programs that achieve this:

# Program 1

```stan
parameters {
  real<lower = 0, upper = 2> X;
  real<lower = 0, upper = 1> Y_1;
}
transformed parameters {
  real Y = Y_1*(2 - X) + X;
}

```

# Program 2:

```stan
parameters {
  real<lower = 0, upper = 2> X;
  real<lower = X, upper = 2> Y;
}
model {
  target += -log(2 - X);
}

```

The intuition here is that rather than a uniform probability density over the triangle that respects your parameter constraints, we need a density such that slices along values of constant X all have the same total probability as one another. These slices differ in length from the slice at X = 0 by a factor of \frac{2 - X}{2}, and so these regions of parameter space need to contain excess probability density according to the reciprocal of this fraction. Dropping the constant 2 in the denominator and taking the logarithm we get the second model above.
