# Deterministic and random variables in Stan

Hello everybody!
First of all, I apologize if I am asking something obvious, I have very little experience working with Stan and in general with Bayesian statistics.
I want to create a model of peer assessment. Given a set of grades by a peer X to a group of students, and given a set of grades by a different peer Y to that same group of students, I am computing in the transformed data block the mean and variance of the resulting differences. In other words: if X has assessed v,w and z with grades G^{x}_{v}, G^{x}_{w}, G^{x}_{z}; and Y has assessed these same students with grades G^{y}_{v}, G^{y}_{w}, G^{y}_{z}, I am computing the mean \mu_{xy} = \frac{\sum_{i}G^{x}_{i} - G^{y}_{i}}{|\{G^{x}\}\cap\{G^{y}\}|}, where the denominator denotes the number of peers assessed both by X and Y. Similarly, the variance is computed as \sigma_{xy} = \frac{\sum_{i}(G^{x}_{i} - G^{y}_{i} - \mu_{xy})^{2}}{|\{G^{x}\}\cap\{G^{y}\}|}.

I intend now to use \sigma_{xy} and \mu_{xy} as the parameters of a normal distribution such that b \sim \mathcal{N}(\mu_{xy}, \sigma_{xy}), where b is a new variable.
Seeing this as a PGM, we would have the set of grades G^{x}_{v}, G^{x}_{w}, G^{x}_{z}, G^{y}_{v}, G^{y}_{w}, G^{y}_{z} as observed nodes, all of them parents of data-derived \mu_{xy} and \sigma_{xy}, which are likewise parents of an unobserved child b. As you may notice, given the deterministic definitions of \mu_{xy} and \sigma_{xy}, their existence depends on |\{G^{x}\}\cap\{G^{y}\}| \neq 0. Does Stan support such a model? Could I define b as a parameter?

1 Like

Hello!

No worries - so if I’m understanding you correctly you’d like to create a PGM that looks a little like this:
(the set of grades) → data-derived mean and variance → your distribution b
I’m unsure if Stan support graphical models so hopefully someone can step in here, but yes you can define b as a distribution and since you have a fixed mean and variance (based on the context you’ve told us!) it should be fairly straightforward to sample from. :)
The one consideration I might have about your latter question, is if you have any reason to believe your set of grades follow a particular distribution - that might affect how you go about sampling from your distribution b.

1 Like

Bit out of my depth but is this related to multi membership models? If so the brms package has multimember model support so may be helpful

2 Likes

First of all, thank you both very much! I finally was able to make the model work! Regarding @n97 's question about the distribution followed by the grades, yes, the data that I gathered shows what seems to be something similar to a gaussian centred around 7 on a scale from 0 to 10.
I was not thinking from the perspective of multi-membership terms, @stevebronder , but that link did turn out to be useful for another project I’m in, so thank you very much for the hint!

1 Like