If anyone cares, this is supposed to be a very simple implementation of a Metropolis algorithm for a uniform distribution. I want to plot the variance of the sampled values as a function of an acceptance ratio, a. The loop in question:
a = 0.1;
u[0] = RandomReal[];
While[a < 2.1,
k = 1;
While[k < 11,
u[k] = RandomReal[];
ut = u[k] - u[k - 1];
r = Exp[-a*ut];
If[r > RandomReal[], k = k + 1, k = k]];
ui = Table[u[k], {k, 10}];
m[a] = Sum[u[k], {k, 1, 10}]/10;
v[a] = Variance[ui];
a = a + 0.1]
This is working for the most part, however v[a] is undefined for a=0.7, 0.9, and a>1.5. The weird part is that if I check v[0.7] manually it returns a value, but if I make a table with v[a],{a,0.1,2,0.1} it shows as undefined at a=0.7. I want to know why I'm having trouble for these a values.
v,u,mas a lists of fixed length (if this length is known before the loop start) and to write into the list withv[[a]] = ...etc. – Henrik Schumacher Mar 14 '19 at 19:03It is a really bad idea to use floating point numbers for indexingYes. Actually in Ada (a very well designed and safe language), this will generate a compile error. May be the new Mathematica Lint tool that I saw few days ago in some post can catch such user error, I do not know. – Nasser Mar 14 '19 at 21:20