I am trying to simplify and optimize an old loop code that I wrote. Given that Fold and NestWhile use the results of the last iteration in the new iteration, I would like to use them to get rid of the Do constructions in my version. The capture is certain operations. The example is a very simple template with a much more complicated structure.

For those familiar with Bayesian inference codes / MCMCs, this mimics a hybrid sample of Gibbs and Metropolis-Hastings WITHOUT the statistical properties, I'm only interested in mechanics. For those who are not, I include a verbal description and an example of work coding.

Use SetDelayed to initialize parameters and vectors (variables) because they are used to store the value of the last iteration. I also define a posterior distribution (objective) and a proposal, which will suggest candidate values. I define U to be used in a subsequent control step and empty vectors to store the values of each loop. To simplify, the length of c and the number of iterations is set to 10. I know that some definitions are redundant, they help me track the process.

```
a: = 1
b: = 1
c: = table[1, {i, 1, 10}];
U: = RandomVariate[UniformDistribution[{0, 1}]]astore: = {}
bstore: = {}
F[a_, b_, c_] : = RandomVariate[NormalDistribution[a + Mean[c], ABS[b]]]Sun[a_, b_, c_] : = RandomVariate[NormalDistribution[b + Mean[c], ABS[a]]]submit[a_, b_, c_] : = PDF[Studentdistribution[ABS[StudentTDistribution[Abs[Distribucióndelestudiante[Abdominales[StudentTDistribution[Abs[b]], c]shore up[a_, b_, c_] : = PDF[Normaldistribution[aAbs[NormalDistribution[aAbs[Distribuciónnormal[aAbs[NormalDistribution[aAbs[b]], c]
```

The first two steps of each iteration of the loop are a drawing of a and b of the distributions f and g. The third step is a cursor on the vector c (written as a loop inside a loop). For each element j in c a candidate value is drawn (test) and when the next control step is applied,

$$ frac {post (test) * prop (c_j)} {post (c_j) * prop (test)}> U (0,1) $$

If the test lasts, then $ c_j $ It is replaced by test in vector c. Otherwise, nothing happens (the logarithms are for numerical reasons).

```
Do[{a = f[a, b, c],
b = g[a, b, c],
Do[{ex = c[[j]],
test = RandomVariate[Normaldistribution[aAbs[NormalDistribution[aAbs[Distribuciónnormal[aAbs[NormalDistribution[aAbs[b]]],
Yes[Log[post[a, b, test]*shore up[a, b, ex]]-
Log in[submit[post[enviar[post[a, b, ex]*shore up[a, b, test]]> Log[U],
c = ReplacePart[c, j -> test], Nothing]}, {j, 1, 10}],
astore = Append[astore, a],
bstore = Append[bstore, b]
}, {i, 1, 10}]
```

The code should store a and b for each iteration and update the vector c correctly.

I would like

1) Improve speed (so this can be a horse race between different constructions)

2) Use Fold and / or NestList that are made for such operations.

Any other improvement is also welcome. Finally, there is a considerable complication in c that includes the walkers, but for now I will stick to that.

If you need more information by all means, ask and I will add it.