%matplotlib inline import numpy as np import matplotlib.pyplot as plt from scipy import stats # use seaborn plotting defaults # If this causes an error, you can comment it out. import seaborn as sns sns.set() from fig_code import linear_data_sample x, y, dy = linear_data_sample() def model(theta, x): # the `theta` argument is a list of parameter values, e.g., theta = [m, b] for a line pass def ln_likelihood(theta, x, y, dy): # we will pass the parameters (theta) to the model function # the other arguments are the data pass def ln_prior(theta): pass def ln_posterior(theta, x, y, dy): return ln_prior(theta) + ln_likelihood(theta, x, y, dy) def run_mcmc(ln_posterior, nsteps, ndim, theta0, stepsize, args=()): """ Run a Markov Chain Monte Carlo Parameters ---------- ln_posterior: callable our function to compute the posterior nsteps: int the number of steps in the chain theta0: list the starting guess for theta stepsize: float a parameter controlling the size of the random step e.g. it could be the width of the Gaussian distribution args: tuple (optional) additional arguments passed to ln_posterior """ # Create the array of size (nsteps, ndims) to hold the chain # Initialize the first row of this with theta0 # Create the array of size nsteps to hold the log-likelihoods for each point # Initialize the first entry of this with the log likelihood at theta0 # Loop for nsteps for i in range(1, nsteps): # Randomly draw a new theta from the proposal distribution. # for example, you can do a normally-distributed step by utilizing # the np.random.randn() function # Calculate the probability for the new state # Compare it to the probability of the old state # Using the acceptance probability function # (remember that you've computed the log probability, not the probability!) # Chose a random number r between 0 and 1 to compare with p_accept # If p_accept>1 or p_accept>r, accept the step # Save the position to the i^th row of the chain # Save the probability to the i^th entry of the array # Else, do not accept the step # Set the position and probability are equal to the previous values pass # Return the chain and probabilities