Bayesian Analysis - A First Example

Similar documents
2D Plotting with Matlab

to have roots with negative real parts, the necessary and sufficient conditions are that:

Introduction to MatLab

New Mexico Tech Hyd 510

Phonon dispersion relation and density of states of a simple cubic lattice

An Introduction to MatLab

Fast Fourier Transform Discrete-time windowing Discrete Fourier Transform Relationship to DTFT Relationship to DTFS Zero padding

Fin System, Inc. Company Report. Temperature Profile Calculators. Team 1 J. C. Stewards, Lead A. B. Williams, Documentation M. D.

Fast Fourier Transform Discrete-time windowing Discrete Fourier Transform Relationship to DTFT Relationship to DTFS Zero padding

Solutions. Chapter 1. Problem 1.1. Solution. Simulate free response of damped harmonic oscillator

Solutions - Homework #1

Overview of Bode Plots Transfer function review Piece-wise linear approximations First-order terms Second-order terms (complex poles & zeros)

Computational Statistics with Matlab. Mark Steyvers

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

COMP MATLAB.

Solutions for examination in TSRT78 Digital Signal Processing,

Solving Differential Equations on 2-D Geometries with Matlab

Package bivariate. December 10, 2018

GRAPHIC WEEK 7 DR. USMAN ULLAH SHEIKH DR. MUSA MOHD MOKJI DR. MICHAEL TAN LOONG PENG DR. AMIRJAN NAWABJAN DR. MOHD ADIB SARIJARI

Introduction to Signal Analysis Parts I and II

Experiment A11 Chaotic Double Pendulum Procedure

Solutions. Chapter 5. Problem 5.1. Solution. Consider the driven, two-well Duffing s oscillator. which can be written in state variable form as

Homework 1 Solutions

Matlab Instruction Primer; Chem 691, Spring 2016

Problem (a) θ 0 = 0

BIOE 198MI Biomedical Data Analysis. Spring Semester Lab 4: Introduction to Probability and Statistics

STAT2201 Assignment 3 Semester 1, 2017 Due 13/4/2017

EE266 Homework 3 Solutions

Solutions to Homework 8 - Continuous-Time Markov Chains

How to make a presentation with L A TEX?

EE364b Homework 4. L(y,ν) = (1/2) y x ν(1 T y 1), minimize (1/2) y x 2 2 subject to y 0, 1 T y = 1,

1 Mechanistic and generative models of network structure

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

1 TIME OF ARRIVAL LOCATION TECHNIQUE

Using Probability to do Statistics.

LAB 2: DTFT, DFT, and DFT Spectral Analysis Summer 2011

EE 224 Lab: Graph Signal Processing

Overview of Discrete-Time Fourier Transform Topics Handy Equations Handy Limits Orthogonality Defined orthogonal

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Run bayesian parameter estimation

Notes perso. - cb1 1. Version 3 - November u(t)e i2πft dt u(t) =

Physics 241 Class #10 Outline (9/25/2013) Plotting Handle graphics Numerical derivatives and functions Next time numerical integrals

Temperature measurement

ECE531 Homework Assignment Number 2

Solutions Manual Internal Combustion Engines: Applied Thermosciences

Computational Foundations of Cognitive Science

Inference, Models and Simulation for Complex Systems CSCI Lecture 6 13 September 2011 Prof. Aaron Clauset

1 A brief primer on probability distributions

LAB 1: MATLAB - Introduction to Programming. Objective:

Probability review (week 3) Solutions

Chapter 7 Statistics, Probability, and Interpolation

Solutions - Homework # 3

Homework 6 MA/CS 375, Fall 2005 Due December 15

Bayesian course - problem set 5 (lecture 6)

Week 4 Concept of Probability

AMS 27L LAB #6 Winter 2009

Bayesian Statistics Part III: Building Bayes Theorem Part IV: Prior Specification

Bfh Ti Control F Ws 2008/2009 Lab Matlab-1

Simple numerical analysis of 2D Ising model

Statistical methods. Mean value and standard deviations Standard statistical distributions Linear systems Matrix algebra

Tutorial on Approximate Bayesian Computation

Lesson 11: Mass-Spring, Resonance and ode45

Is the World Going to End in 2029?

LAB 2 1. Measurement of 2. Binomial Distribution

Likelihood and Bayesian Inference for Proportions

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Math 515 Fall, 2008 Homework 2, due Friday, September 26.

Lecture 5b: Starting Matlab

Linear System Theory

Likelihood and Bayesian Inference for Proportions

S(c)/ c = 2 J T r = 2v, (2)

Spatial discrete hazards using Hierarchical Bayesian Modeling

MATLAB crash course 1 / 27. MATLAB crash course. Cesar E. Tamayo Economics - Rutgers. September 27th, /27

CS 361: Probability & Statistics

Task 1: Open ArcMap and activate the Spatial Analyst extension.

9.07 MATLAB Tutorial. Camilo Lamus. October 1, 2010

CS 361: Probability & Statistics

Hot Spot / Point Density Analysis: Kernel Smoothing

THE CRYSTAL BALL SCATTER CHART

MAE 143B - Homework 7

Lab 1 Uniform Motion - Graphing and Analyzing Motion

Lab 13: Ordinary Differential Equations

Data Assimilation Research Testbed Tutorial

Introduction to Bayesian Inference

Computer projects for Mathematical Statistics, MA 486. Some practical hints for doing computer projects with MATLAB:

Markov Chain Monte Carlo

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

Introduction to ArcMap

EE368B Image and Video Compression

MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

Assignment 6, Math 575A

Monte Carlo Composition Inversion Acceptance/Rejection Sampling. Direct Simulation. Econ 690. Purdue University

OKLAHOMA STATE UNIVERSITY

Matlab for Review. NDSU Matlab Review pg 1

Coulomb s Law Mini-Lab

MOHO PRO 12 Step-by-step demonstration script

Right-truncated data. STAT474/STAT574 February 7, / 44

Dr. Iyad Jafar. Adapted from the publisher slides

LEARNING WITH MINITAB Chapter 12 SESSION FIVE: DESIGNING AN EXPERIMENT

Curve Fitting Re-visited, Bishop1.2.5

Transcription:

Bayesian Analysis - A First Example This script works through the example in Hoff (29), section 1.2.1 We are interested in a single parameter: θ, the fraction of individuals in a city population with with a specific infectious disease. Contents Matlab Preliminaries Sampling Distribution Prior The Posterior Matlab Finals Publishing Notes We have a small sample of 2 individuals to provide empirical evidence for our estimation of theta. Let y be the observed number of infected individuals in the sample. n=2; Matlab Preliminaries Set random number seed. This will allow you to EXACTLY replicate your results in re-runs. Use 37 to get my results exactly. rand( state,37); % set arbitrary seed for uniform draws randn( state,37); % set arbitrary seed for normal draws Sampling Distribution If we knew theta, than a reasonable sampling model (and likelihood function) for the total number of infected citizens would be the binomial (n, θ) density. Thus: p (y θ) = ( n y) θ y (1 θ) n y, θ [, 1], y = 1, 2,..., n (1) Let s plot the bin(2,theta) for a few values of theta: n=2; %our assumed sample size th1=.5; 1

th2=.1; th3=.2; th4=.5; R=1; %number of draws - your choice y1=binornd(2,th1,r,1); %this gives us an R by 1 vector of proportions. y2=binornd(2,th2,r,1); y3=binornd(2,th3,r,1); y4=binornd(2,th4,r,1); % Plot the results in a single figure, but with separate windows figure(1); subplot(2,2,1); %call first window of a 2 x 2 grid seq=:1:2; %determine x-values over which to plot [counts,bincenters]=hist(y1,seq); %pcaptures # of obs. for each x-value in "counts", and captures the integers %over which we wish to plot in "bincenters" (so bincenters = seq); bar(bincenters,counts/r); %generates actual plot title( $\theta=.5$, interpreter, latex, fontsize,15); set(gca, Xlim,[-1 2]); set(gca, Ylim,[.5]); set(gca, XTick,:2:2); subplot(2,2,2); %fill second window seq=:1:2; [counts,bincenters]=hist(y2,seq); bar(bincenters,counts/r); title( $\theta=.1$, interpreter, latex, fontsize,15); set(gca, Xlim,[-1 2]); set(gca, Ylim,[.5]); set(gca, XTick,:2:2); subplot(2,2,3); %fill third window seq=:1:2; [counts,bincenters]=hist(y3,seq); bar(bincenters,counts/r); title( $\theta=.2$, interpreter, latex, fontsize,15); set(gca, Xlim,[-1 2]); set(gca, Ylim,[.5]); 2

set(gca, XTick,:2:2); subplot(2,2,4); %fill fourth window seq=:1:2; [counts,bincenters]=hist(y4,seq); bar(bincenters,counts/r); title( $\theta=.5$, interpreter, latex, fontsize,15); set(gca, Xlim,[-1 2]); set(gca, Ylim,[.5]); set(gca, XTick,:2:2); %Save figure in Matlab format to = c:\klaus\aaec6984\mlab\figures\binomexample ; %designate destination saveas(gcf,to);%"gcf" means "get current figure" % Export figure so it can be imported into latex to = c:\klaus\aaec6984\tex\module1\binomexample ; %designate destination print( -depsc,to);%this automatically selects the current figure.5 θ =.5.5 θ =.1.4.4.3.2.3.2.1.1 2 4 6 8 1 12 14 16 18 2.5 θ =.2 2 4 6 8 1 12 14 16 18 2.5 θ =.5.4.4.3.2.3.2.1.1 2 4 6 8 1 12 14 16 18 2 2 4 6 8 1 12 14 16 18 2 Prior Infection rates in other cities range from.5 to.2, with an average prevalence of.1. Thus, a reasonable prior distribution must satisfy the following 3

conditions: bound between and 1 continuous between bounds mean near.1 Even with these conditions, there are many priors to choose from. In such cases, we add a 4th condition: computational convenience. This leads to a beta prior. The beta(a,b) has mean a/(a+b) and mode (a-1)/(a-1+b-1). The density is given as: p (θ) = Γ (α + β) Γ (α) Γ (β) θα 1 (1 θ) β 1 θ [, 1] (2) The Beta (2, 2) might be a resonable choice. Let s draw from it and plot it (along with the posterior, below). a=2; b=2; pr=betarnd(a,b,r,1); Eth=mean(pr) stdth=std(pr) minth=min(pr) maxth=max(pr) Eth =.91192 stdth =.6115 minth =.5857 maxth =.43387 The Posterior Assume we observe y= (i.e. nobody of the 2 individuals is infected). It turns out that a Beta (α, β) prior for θ, combined with a Bin (θ, n) sampling distribution yields a Beta (α + y, β + n y). Knowing this, we can immediately draw from this posterior and compute its moments. 4

y=; a1=a+y; b1=b+n-y; po=betarnd(a1,b1,r,1); Eth=mean(po) stdth=std(po) minth=min(po) maxth=max(po) Eth =.47725 stdth =.32814 minth = 3.8681e-5 maxth =.2757 Let s plot the prior vs. the posterior [f1,x1]=ksdensity(pr, kernel, epanechnikov, npoints,2, support,[ 1]); [f2,x2]=ksdensity(po, kernel, epanechnikov, npoints,2, support,[ 1]); figure(2) plot(x1,f1, -b,x2,f2, -k, LineWidth,1); title( prior and posterior distributions ); xlabel( $\theta$, interpreter, latex, fontsize,12); ylabel( density ); h=legend( $p\left(\theta\right)$, $p\left(\theta y\right)$ ); set(h, interpreter, latex, fontsize,12); 5

16 14 prior and posterior distributions p (θ) p (θ y) 12 1 density 8 6 4 2.1.2.3.4.5.6.7 θ Matlab Finals save your numerical output, if any save c:\klaus\aaec6984\mlab\worksp\mod1_1a pr po; Publishing Notes after clicking on save and publish in the menu bar above, open the resulting.tex file in TexnicCenter. Before compiling, add the following commands at the top of the file, before the \begin{document} line: \usepackage{amsmath} \usepackage{epstopdf} 6