"""What W5 measures: a simple random walk on Z^5 ever comes back to where it started with probability 1 - 1/(5 W5), about 0.135; most walks drift away for good. Drawn: the distance from home of real (seeded) walks of 36 steps. Seven are the first seeds that never came back in those steps (faint); one is the first seed whose walk is home for the first time at step 36 (the field's colour).""" import math import random from pathlib import Path import kit W5 = 0.23126162496804623574 # the README's value assert abs(1 - 1 / (5 * W5) - 0.13518) < 5e-6 # the README's return probability T = 36 def distances(seed): """Distance from the origin after each of T steps of the walk with this seed.""" rng, pos, out = random.Random(seed), [0] * 5, [0.0] for _ in range(T): pos[rng.randrange(5)] += rng.choice((-1, 1)) out.append(math.sqrt(sum(x * x for x in pos))) return out came_back = next(d for d in map(distances, range(10 ** 6)) if d[-1] == 0 and all(d[1:-1])) away = [d for d in map(distances, range(100)) if all(d[1:])][:7] assert len(away) == 7 x0, x1, base, top = 40, 600, 250, 36 m = max(max(d) for d in away + [came_back]) pts = lambda d: [(x0 + (x1 - x0) * t / T, base - (base - top) * v / m) for t, v in enumerate(d)] body = kit.line(x0, base, x1, base, 'faint-s', 2) # home: distance 0 body += ''.join(kit.polyline(pts(d), 'faint-s', 2) for d in away) body += kit.polyline(pts(came_back), 'f-s', 3.5) body += kit.dot(x0, base, 7, 'ink') + kit.dot(x1, base, 9, 'f') kit.write(__file__, 'physics-astronomy', body, label='walks on Z⁵, distance from home · ~0.135 return')