import numpy as np
def run_mc(n_steps, T, k):
"""
This function performs metropolis monte carlo sampling of a harmonic potential at temperature T
and returns the trajectory
"""
[insert]
return traj
import numpy as np
def run_mc(n_steps, T, k):
"""
This function performs metropolis monte carlo sampling of a harmonic potential at temperature T
and returns the trajectory
"""
traj = np.zeros(n_steps)
traj[0] = np.random.normal(0, 1)
for i in range(1, n_steps):
x_new = np.random.normal(traj[i-1], 1)
if np.random.rand() < np.exp(-k*(x_new**2 - traj[i-1]**2)/T):
traj[i] = x_new
else:
traj[i] = traj[i-1]
return traj
Go to Google Form