Factor Metropolis X2X4 patch.py
From Werner KRAUTH
(Difference between revisions)
| Revision as of 23:00, 10 June 2024 Werner (Talk | contribs) ← Previous diff |
Revision as of 23:01, 10 June 2024 Werner (Talk | contribs) Next diff → |
||
| Line 2: | Line 2: | ||
| import random | import random | ||
| import matplotlib.pyplot as plt | import matplotlib.pyplot as plt | ||
| - | + | ||
| def u(x): return x ** 2 / 2.0 + x ** 4 / 4.0 | def u(x): return x ** 2 / 2.0 + x ** 4 / 4.0 | ||
| - | + | ||
| def u2(pos): return 0.5 * pos ** 2 | def u2(pos): return 0.5 * pos ** 2 | ||
| - | + | ||
| def u4(pos): return 0.25 * pos ** 4 | def u4(pos): return 0.25 * pos ** 4 | ||
| - | + | ||
| x = 0.0 | x = 0.0 | ||
| delta = 0.1 | delta = 0.1 | ||
| - | + | ||
| data = [] | data = [] | ||
| n_samples = 10 ** 6 | n_samples = 10 ** 6 | ||
| Line 23: | Line 23: | ||
| x = new_x | x = new_x | ||
| data.append(x) | data.append(x) | ||
| - | |||
| plt.title('Factorized Metropolis algorithm (patch), anharmonic oscillator' ) | plt.title('Factorized Metropolis algorithm (patch), anharmonic oscillator' ) | ||
| plt.xlabel('$x$') | plt.xlabel('$x$') | ||
Revision as of 23:01, 10 June 2024
import math
import random
import matplotlib.pyplot as plt
def u(x): return x ** 2 / 2.0 + x ** 4 / 4.0
def u2(pos): return 0.5 * pos ** 2
def u4(pos): return 0.25 * pos ** 4
x = 0.0
delta = 0.1
data = []
n_samples = 10 ** 6
for i in range(n_samples):
new_x = x + random.uniform(-delta, delta)
delta_u2 = u2(new_x) - u2(x)
delta_u4 = u4(new_x) - u4(x)
Upsilon2 = random.uniform(0.0, 1.0)
Upsilon4 = random.uniform(0.0, 1.0)
if Upsilon2 < math.exp(-delta_u2) and Upsilon4 < math.exp(-delta_u4):
x = new_x
data.append(x)
plt.title('Factorized Metropolis algorithm (patch), anharmonic oscillator' )
plt.xlabel('$x$')
plt.ylabel('$\pi(x)$')
plt.hist(data, bins=100, density=True,label='data')
XValues = []
YValues = []
for i in range(-1000,1000):
x = i / 400.0
XValues.append(x)
YValues.append(math.exp(- u(x)) / 1.93525)
plt.plot(XValues, YValues, label='theory')
plt.legend(loc='upper right')
plt.show()
