{ "metadata": { "name": "", "signature": "sha256:4235a601a76130acb78c7fe8a4afb78321d1add561442d218869dc37a066cc57" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "import diymcmc" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We've written a convenience function to grab the test data:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x,y,sigma_y = diymcmc.get_data1()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Start by plotting the data -- just make an `errorbar()` plot with the provided data." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# MAKE PLOT" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What model should we use for the data? Why? Create a function to evaluate this model for a given choice of parameters and `x` data:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def model(pars, x):\n", " # the `pars` argument is a list of parameter values, e.g., pars = [m, b] for a line\n", " pass" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "We'll start with the assumption that the data are independent and identically distributed so that the likelihood is simply a product of Gaussians (one big Gaussian). We'll also assume that the uncertainties reported are correct, and that there are no uncertainties on the `x` data. We need to define a function that will evaluate the (ln)likelihood of the data, given a particular choice of your model parameters. A good way to structure this function is as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def ln_likelihood(pars, x, y, y_unc):\n", " # we will pass the parameters (pars) to the model function\n", " # the other arguments are the data\n", " pass " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What about priors? Remember your prior only depends on the model parameters, but be careful about what kind of prior you are specifying for each parameter. Do we need to properly normalize the probabilities?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def ln_prior(pars):\n", " pass" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can define a function that evaluates the (ln)posterior probability, which is just the sum of the ln prior and ln likelihood:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def ln_posterior(pars, x, y, y_unc):\n", " return ln_prior(pars) + ln_likelihood(pars, x, y, y_unc)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now write a function to actually run a Metropolis-Hastings MCMC sampler. Ford (2005) includes a great step-by-step walkthrough of the Metropolis-Hastings algorithm, and we'll base our code on that" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def run_mcmc(ln_posterior, nsteps, ndim, p0, stepsize, args=()):\n", " \n", " # Set up the chain, and initialize it\n", " # (need an array holding the positions at each step\n", " \n", " # Set up an array to hold the probabilities at each step\n", " \n", " # Calculate the probability for the first step\n", " \n", " # Loop for nsteps\n", " for i in np.linspace(1,nsteps-1,nsteps-1):\n", " # Randomly choose new model parameters for the trial state\n", " \n", " # Calculate the probability for the new state\n", " \n", " # Compare it to the probability of the old state\n", " # Using the acceptance probability function\n", " \n", " # Chose a random number u between 0 and 1 to compare with p_accept\n", " \n", " # If p_accept>1 or p_accept>u, accept the step\n", " # Save the position to the chain\n", " \n", " # Save the probability to that array\n", " \n", " # Else, do not accept the step\n", " # Set the position and probability are equal to the last value\n", " \n", " # Return the chain and probabilities" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now run the MCMC code on the data provided." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# run it!" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plot the position of the walker as a function of step number for each of the parameters. Are the chains converged? " ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make histograms of the samples for each parameter. Should you include all of the samples? " ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Report to us your constraints on the model parameters -- you have some freedom in interpreting what this means..." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }