mc_harmonic 1¶

Query¶

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

Output¶

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.randn()
    for i in range(1,n_steps):
        x_new = traj[i-1] + np.random.randn()
        if np.random.rand() < np.exp(-(x_new**2 - traj[i-1]**2)/(2*k*T)):
            traj[i] = x_new
        else:
            traj[i] = traj[i-1]
    return traj

Evaluate¶

Go to Google Form