Hello all,

I am currently working on a project and some internal comments suggested that I directly control for theoretical posteriors as covariates into my analysis. I have the entire theoretical model completed, but I have reached my knowledge limit with Stata in trying to produce such variables using direct computational methods in Stata. So, now I need help! Do not worry I have made a few attempts myself without success.

Below is a snippet of the relevant variables needed to produce the Bayesian posterior that I am needing.

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input str24 label_participant float p byte corrected_signal float(p_score_10 p_score_11 p_score_12 p_score_13 p_score_14 p_score_15 bayes_score_10 bayes_score_11 bayes_score_12 bayes_score_13 bayes_score_14 bayes_score_15)
"558a035bfdf99b2d75651378" .6  8   0   0  0   0   0   0 . . . . . .
"558de4bffdf99b0cad9683ba" .9  5   0   0  0   0   0   0 . . . . . .
"5591827dfdf99b4fccbdfb21" .6  9   0   0  0   0   0   0 . . . . . .
"559385f5fdf99b78e49c58e9" .9 15 .15  .2 .2 .11 .06 .04 . . . . . .
"559ef308fdf99b2616ff2a4f" .6 19   0 .05 .1 .45 .35 .05 . . . . . .
"55a43278fdf99b02ff6caf89" .6 13 .15  .2 .2  .2 .08   0 . . . . . .
"55a55bd7fdf99b790558627c" .9 10 .05  .1 .7  .1 .05   0 . . . . . .
"55b237e6fdf99b19ea79d2f7" .9  .   0   0  0   0   0   0 . . . . . .
"55b42b7cfdf99b5244217cf4" .6 11   0   0  0   0   0 .05 . . . . . .
"55b47001fdf99b615b12d66c" .6  .   0   0  0   0  .1  .2 . . . . . .
"55bd22d0fdf99b374ec0711a" .6  7   0   0 .2 .35  .3  .1 . . . . . .
end
Please note that the variables p_score* and bayes_score* index from 0 to 20 (21 states of the world) in the full data set and I am using the ones in the dataex output purely for reference purposes.

Here is a quick breakdown of what each variable stands for:

1. label_participant : id for each subject in my experiment
2. p : probability participant's 'corrected_signal' equals their true score on a quiz
3. corrected_signal : the signal a participant observed after their priors but before their posteriors
4. p_score_`k' : participant's prior probability they got a score of `k' for k = [0, 20]
5. bayes_score_`k' : the theoretical posterior probability of getting score `k' if updating as if they were a pure or exact Bayesian

Now, there are two formulas from the theoretical model needed to produce bayes_score_`k' for each `k' (beware pseudo Stata code incoming).

1. The posterior belief that a Bayesian thinks their true score is equal to the signal received:

bayes_score_`k=corrected_signal' = (p * p_score_`k=corrected_signal') / ((p * p_score_`k=corrected_signal') + sum_{i = -5}^{i = 5}{((1-p)/10) * p_score_`k=corrected_signal + i'})

2. The posterior belief that a Bayesian thinks their true score is equal to any other score other than the signal they received

bayes_score_`k=/=corrected_signal' = (((1-p)/10) * p_score_`k=/=corrected_signal') / ((p * p_score_`k=corrected_signal') + sum_{i = -5}^{i = 5}{((1-p)/10) * p_score_`k=corrected_signal + i'}) for each k =/= corrected_signal


I am wanting to produce some sort of nested for loop with if conditions that can produce all of this in one go. Is this possible? Any help would be greatly appreciated!

Thanks in advance!

Nicholas