0
$\begingroup$

I am learning about the Metropolis Monte Carlo methods for applications in statistical thermodynamics and I wrote a small portion of code for the purposes of figuring something out. Turns out the dynamics of my loop were more interesting than expected. The loop is:

import numpy as np
tot = 0
k = 0.5
n = 100000
for i in range(n):
    rng = np.random.rand()
    if rng > k:
        tot+=rng
        k = rng
    else:
        tot-=rng
        k = rng
print(tot)

When you run this code for large n, it seems to converge to the figure 1/6*n (so for n = 1000, you would expect to get a value that falls around 166).

I understand why the output value is positive, since for a given random number that is greater than the preceding k value, you can't get a subsequent value for 'rng' that takes away more than what was added. This effectively introduces a positive pressure on the system.

Furthermore, if a large number (a number close to one) was just added, you'll likely end up with a number taken away (since k becomes rng, which was large (close to one) so the chances of the next random number being greater than it is low, so the chances of the subsequent contribution to the 'tot' value being negative is high). This constraint imposes a 'braking force' on the system.

Honestly when I first saw this I thought it would be trivial to decipher, but what appeared to be simple at first has left me scratching my head. If someone that's better than me at maths could find an analytical solution to this (in the limit that n is large), I'd be very happy to see it.

Lastly, perhaps this post is in the wrong thread. If so, please could you let me know of a more suitable location to post it!

$\endgroup$
1
  • $\begingroup$ If you ever need to generate random numbers in Python without access to numpy, consider using random.random instead of numpy.random.rand. $\endgroup$
    – J.G.
    Commented Jan 30, 2022 at 15:00

1 Answer 1

1
$\begingroup$

Since k is updated regardless, it’s just uniformly distributed. The probability of moving down is of course $k$, with an expected change of $-k/2$; the up possibility is similar. As such,

$$\begin{align} \mathrm{E}(\Delta tot)&=\int_0^1 \left(\frac{-k}2k+\frac{1+k}2(1-k)\right)\,dk\\ &=\int_0^1 \left(\frac12-k^2\right)\,dk=\frac12-\frac13=\frac16 \end{align}$$

Intuitive understanding might be aided by recognizing that the expected change when $k=1/2$ (as initially) is $1/4$, since the two directions are equally likely but the upward movement is on average three times as large.

$\endgroup$
2
  • $\begingroup$ Oh wow, that is so clever. So it's like a two state system with a probability of moving up or down multiplied by the associated outcome. I understand the probabilities, but I didn't think to assume that the random number would fall in the midpoint, that's very clever. What I don't get now is the integration being from 0 to 1. Does this mean you're implicitly assuming that for large n, every value of k will get covered, therefore we integrate across all these values? $\endgroup$ Commented Jan 30, 2022 at 15:07
  • $\begingroup$ @Ihatecoding: You don’t quite “assume” the midpoint; it’s just obviously the expected value conditioned on one movement direction. Yes, k is just uniformly distributed since it’s always the previous value of rng; the assumption of large n comes in only in that we ignore the (negative) correlation between each overlapping pair of successive steps and the non-random initial k. $\endgroup$ Commented Jan 30, 2022 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .