SlideShare a Scribd company logo
1 of 6
Download to read offline
Programming Assignment # 6: Using the Componentwise
Metropolis-Hastings Algorithm to Sample from the Joint Posterior
Distribution for the Parameters of a Binomial Regression Model
Markov Chain Monte Carlo (MCMC) methods provide a way to sample
from a distribution (e.g., the joint posterior distribution for the parameters of a
Bayesian model). These methods are useful when analytic solutions for param-
eter estimations do not exist. If the Markov chain is long, the sampled random
variables are (approximately) identically distributed, but they are not indepen-
dent because in a Markov chain each random variable depends on the previous
one. However, because the Ergodic Theorem applies to MCMC methods, the
chains converge (with probability one) to the stationary distribution, which for
our purposes is the Bayesian joint posterior distribution.
MCMC methods are frequently implemented using a Gibbs sampler. This,
however, requires knowledge of the parameters’ conditional distributions, which
are frequently not available. In this case, another MCMC method, called
the Metropolis-Hastings algorithm, can be used. The Metropolis-Hastings al-
gorithm is a type of acceptance/rejection method. It requires a candidate-
generating distribution, also called proposal distribution. Ideally, the proposal
distribution should be similar to the posterior distribution, but any distribution
with the same support as the posterior is possible. If f(y|x) denotes the pro-
posal density, and p(x) the posterior density, the acceptance probability α(y|x)
is calculated as
α(y|x) = min 1,
f(x|y)p(y)
f(y|x)p(x)
.
One then generates U~U[0, 1] and selects the proposal y if U ≤ α and otherwise
rejects the proposal and remains in state x. Thus, the proposal is accepted with
probability α(y|x) and rejected with probability 1−α(y|x). If the proposed step
is sampled as Y |X~U(X −a, X +a), the proposal densities in the numerator and
denominator cancel. Moreover, it is enough to use the kernel of the posterior
density because α uses the ratio of two values of p(.).
The Metropolis-Hastings algorithm generalizes to multidimensional distri-
butions. In the multidimensional case, there are two types of algorithms —
the “regular” algorithm and the “componentwise” algorithm. Whereas the
“regular” algorithm computes a full proposal vector at each step, the “com-
ponentwise” algorithm, which is implemented in the example discussed in this
assignment, updates each component at a time, so that the proposals for all the
components are evaluated, i.e., accepted or rejected, in turn.
The assignment considers the binomial regression model yi~Bi(mi, θi), with
y denoting the vector of the number of successes and m the vector of the number
of trials; θ is given by
θi =
1
1 + e[−(α+βxi)]
,
with x denoting a vector containing the integers from 1 to 10. The m-vector is
(5, 4, 8, 6, 3, 10, 7, 12, 5, 4), and the y-vector is (0, 0, 0, 1, 3, 5, 4, 9, 3, 3).
1
A function called “bireg” was written to simulate sampling from the joint
posterior distribution for the parameters α and β. The function returns the
posterior means of these parameters. The function takes the vectors y, x, and
m as input arguments, as well as the starting values of the parameters (defaults:
α = 1, β = 1), the step sizes of the proposals (defaults: step size of the proposal
for α = 1, for β = 0.2), the number of steps of the chain (default = 10,000), and
the length of the “burn-in” period (default = 5000), i.e., the number of initial
values of the chain to be discarded. Burning-in improves the approximation for
the parameters of the posterior distribution because the initial observations, i.e.,
the observations before the chains have converged to the stationary distribution,
are discarded from the estimation of probabilities and expectations derived from
the posterior.
For practical reasons, a flat, uniform (that is, completely noninformative)
prior was chosen. The choice of this prior also reflects the lack of prior knowl-
edge about previously observed or expected values for α and β. The choice of
this prior makes the posterior distribution synonymous to the likelihood, and
in this writeup, as well as in the program, the two expressions are used ex-
changeably. Under omission of the binomial coefficient, which cancels out when
taking the ratio of the posterior distributions for the calculation of the accep-
tance probability, the log posterior density for the binomial regression model
is
p(α, β|y) =
n
i=1
yi log(θi) + (mi − yi) log(1 − θi).
This log posterior distribution was used to implement the componentwise Metro-
polis-Hastings algorithm, which generates a proposal for each of the two param-
eters (α and β) in turn, and then decides, based on the acceptance probability,
whether to accept or reject the proposal. (The decision about acceptance or
rejection is made with the help of sampling from a log standard uniform dis-
tribution, similar to the procedure described above.) This mechanism ensures
that the probability of acceptance depends on the ratio of the kernels of the
posterior densities. The choice of the step size (“radius”) of the proposal for a
component is critical for rapid convergence to the stationary distribution. The
step size is denoted a, and both the default values for aα and aβ were tuned to
produce acceptance probabilities between 0.3 and 0.6 (~0.46 for α and ~0.36 for
β).
The program records the Markov chains for α and β. An index/counter
that is running during the execution of the program is used to inform the user
about what step the program is currently executing. The goal is to sample
from the log likelihood until all samples, after a burn-in period, are derived
from the stationary distribution. In order to check whether stationarity had
been achieved, the program was run with different starting values (-3, -1, 0,
1, and 3 for both α and β) and with different numbers of steps (10,000 and
100,000); the burn-in period was always half the length of the whole chain.
All these configuations produced posterior means of about -3.8 (α) and 0.59
(β). The following graphics were obtained with starting values of 1 for both
2
−10 −8 −6 −4 −2
0.20.40.60.81.01.21.4
mchain[(burnin + 1):nrep, alpha]
mchain[(burnin+1):nrep,beta]
Figure 1: Scatterplot of α and β.
α and β, chains of length 100,000, and burn-in periods of 50,000 steps. A
scatterplot of α and β is shown in Figure 1. It shows the values of α and β after
the burn-in period. Diagnostic plots (time series plots of α and β, histograms
of α and β, and autocorrelation functions of α and β) are shown in Figure
2. The time series plots show convergence to the stationary distribution, and
the histograms do not have any unusual shapes. However, the autocorrelation
functions show considerable positive autocorrelation up to a lag of about 100.
(For greater lags (between about 160 and 230) the autocorrelation becomes
slightly negative.) This high autocorrelation is potentially problematic. If the
correlation between adjacent observations is high, a larger sample size is required
to obtain reasonable numerical accuracy, in addition to the requirement of a
much longer burn-in. However, the various configuations of staring values and
chain lengths that produced nearly identical posterior means, as well as the time
series and histograms, strongly suggest that the samples for the posterior means
were derived from the stationary distribution.
The good fit between the parameter θ of the binomial regression function,
which depends on the parameters α and β, and the probabilities of success
based on the data (y/m) is shown in Figure 3. Only success probability 5, which
equals 1 and is an exception to the trend of generally gradually increasing success
3
0 10000 20000 30000 40000 50000
−10−6−4−2
Index
mchain[(burnin+1):nrep,alpha]
0 10000 20000 30000 40000 50000
0.20.61.01.4
Indexmchain[(burnin+1):nrep,beta]
Histogram of mchain[(burnin + 1):nrep, alpha]
mchain[(burnin + 1):nrep, alpha]
Frequency
−10 −8 −6 −4 −2
05001500
Histogram of mchain[(burnin + 1):nrep, beta]
mchain[(burnin + 1):nrep, beta]
Frequency
0.2 0.4 0.6 0.8 1.0 1.2 1.4
050010001500
0 50 100 150 200 250
0.00.40.8
Lag
ACF
Series mchain[(burnin + 1):nrep, alpha]
0 50 100 150 200 250
0.00.40.8
Lag
ACF
Series mchain[(burnin + 1):nrep, beta]
Figure 2: Diagnostic plots of the results of a run of the Metropolis-Hastings
algorithm for the binomial regression model of the assignment: time series plots,
histograms, and autocorrelation functions of α and β.
4
x x x
x
x
x
x
x
x
x
2 4 6 8 10
0.00.20.40.60.81.0
x
y/m,theta
Figure 3: Probabilities of success (yi/mi; crosses) and parameters θi (dashed
line) of the binomial regression model.
probabilities, is located far away from the monotonically increasing regression
line.
The code for the “bireg” function is shown below:
# Function that uses the componentwise Metropolis-Hastings algorithm to
# sample from the joint likelihood for alpha and beta of a binomial
# regression model and returns the (posterior) means of these parameters.
#
# Required arguments:
# y - vector of number of successes
# x - vector of series of trials
# m - vector of number of trials
#
# Optional arguments:
# start - vector (length 2) of parameter (alpha, beta) starting values
# avec - vector (length 2) determining the step sizes of the proposals
# for alpha and beta
# nrep - chain length
5
# burnin - number of observations to discard
#
# Returns the (posterior) means of the parameters alpha and beta.
#
bireg <- function(y, x, m, start = c(1, 1), avec = c(1, 0.2),
nrep = 10000, burnin = 5000) {
if(mode(y) != "numeric" || mode(x) != "numeric" || mode(m) != "numeric"
|| mode(start) != "numeric" || mode(nrep) != "numeric"
|| mode(avec) != "numeric" || mode(burnin) != "numeric")
stop("All arguments must be numeric!")
logpost <- function(alpha, beta, x, m, y) {
# logarithm of the joint posterior distribution
theta <- 1/(1+exp(-(alpha+beta*x)))
return(sum(y*log(theta) + (m-y)*log(1-theta)))
}
MHstep <- function(pars, avec, x, m, y){
# implementation of the componentwise Metropolis-Hastings algorithm
res <- pars
for(i in 1:2) {
prop <- res
prop[i] <- res[i] + 2 * avec[i] * (runif(1) - 0.5)
# generate a proposal for a component/parameter
if(log(runif(1)) < logpost(prop[1], prop[2], x, m, y)
- logpost(res[1], res[2], x, m, y))
res[i] <- prop[i]
# conditional for acceptance
}
return(res) # return parameters
}
mchain <- matrix(NA, nrow=nrep, ncol=2)
# initialization of Markov chain matrix
mchain[1,] <- start # parameter starting values
for(i in 2:nrep) {
print(i) # index of current step
mchain[i,] <- MHstep(mchain[i - 1, ], avec, x, m, y)
# execution of the Metropolis-Hastings algorithm
}
return(list(alpha = mean(mchain[(burnin+1):nrep, 1]),
beta = mean(mchain[(burnin+1):nrep, 2])))
# return the means of the parameters alpha and beta
}
6

More Related Content

What's hot

Vandrongelen2018 2 kalmanfiler
Vandrongelen2018 2 kalmanfilerVandrongelen2018 2 kalmanfiler
Vandrongelen2018 2 kalmanfilerRamesh Bk
 
Isen 614 project presentation
Isen 614 project presentationIsen 614 project presentation
Isen 614 project presentationVanshaj Handoo
 
Preconditioning in Large-scale VDA
Preconditioning in Large-scale VDAPreconditioning in Large-scale VDA
Preconditioning in Large-scale VDAJoseph Parks
 
Alam afrizal tambahan
Alam afrizal tambahanAlam afrizal tambahan
Alam afrizal tambahanAlam Afrizal
 
Panoramic Imaging using SIFT and SURF
Panoramic Imaging using SIFT and SURFPanoramic Imaging using SIFT and SURF
Panoramic Imaging using SIFT and SURFEric Jansen
 
AcceleratingBayesianInference
AcceleratingBayesianInferenceAcceleratingBayesianInference
AcceleratingBayesianInferenceLuca Carniato
 
Modeling and quantification of uncertainties in numerical aerodynamics
Modeling and quantification of uncertainties in numerical aerodynamicsModeling and quantification of uncertainties in numerical aerodynamics
Modeling and quantification of uncertainties in numerical aerodynamicsAlexander Litvinenko
 
Effects of Weight Approximation Methods on Performance of Digital Beamforming...
Effects of Weight Approximation Methods on Performance of Digital Beamforming...Effects of Weight Approximation Methods on Performance of Digital Beamforming...
Effects of Weight Approximation Methods on Performance of Digital Beamforming...IOSR Journals
 
Slides TSALBP ACO 2008
Slides TSALBP ACO 2008Slides TSALBP ACO 2008
Slides TSALBP ACO 2008Manuel ChiSe
 
Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]
Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]
Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]AI Robotics KR
 
Numerical methods for 2 d heat transfer
Numerical methods for 2 d heat transferNumerical methods for 2 d heat transfer
Numerical methods for 2 d heat transferArun Sarasan
 
Time alignment techniques for experimental sensor data
Time alignment techniques for experimental sensor dataTime alignment techniques for experimental sensor data
Time alignment techniques for experimental sensor dataIJCSES Journal
 
TurnerBottoneStanekNIPS2013
TurnerBottoneStanekNIPS2013TurnerBottoneStanekNIPS2013
TurnerBottoneStanekNIPS2013Clay Stanek
 

What's hot (19)

Vandrongelen2018 2 kalmanfiler
Vandrongelen2018 2 kalmanfilerVandrongelen2018 2 kalmanfiler
Vandrongelen2018 2 kalmanfiler
 
Isen 614 project presentation
Isen 614 project presentationIsen 614 project presentation
Isen 614 project presentation
 
Data Analysis Assignment Help
Data Analysis Assignment HelpData Analysis Assignment Help
Data Analysis Assignment Help
 
Chapter26
Chapter26Chapter26
Chapter26
 
Preconditioning in Large-scale VDA
Preconditioning in Large-scale VDAPreconditioning in Large-scale VDA
Preconditioning in Large-scale VDA
 
solver (1)
solver (1)solver (1)
solver (1)
 
Alam afrizal tambahan
Alam afrizal tambahanAlam afrizal tambahan
Alam afrizal tambahan
 
Cu24631635
Cu24631635Cu24631635
Cu24631635
 
Panoramic Imaging using SIFT and SURF
Panoramic Imaging using SIFT and SURFPanoramic Imaging using SIFT and SURF
Panoramic Imaging using SIFT and SURF
 
AcceleratingBayesianInference
AcceleratingBayesianInferenceAcceleratingBayesianInference
AcceleratingBayesianInference
 
Modeling and quantification of uncertainties in numerical aerodynamics
Modeling and quantification of uncertainties in numerical aerodynamicsModeling and quantification of uncertainties in numerical aerodynamics
Modeling and quantification of uncertainties in numerical aerodynamics
 
AR model
AR modelAR model
AR model
 
Effects of Weight Approximation Methods on Performance of Digital Beamforming...
Effects of Weight Approximation Methods on Performance of Digital Beamforming...Effects of Weight Approximation Methods on Performance of Digital Beamforming...
Effects of Weight Approximation Methods on Performance of Digital Beamforming...
 
Slides TSALBP ACO 2008
Slides TSALBP ACO 2008Slides TSALBP ACO 2008
Slides TSALBP ACO 2008
 
Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]
Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]
Sensor Fusion Study - Ch3. Least Square Estimation [강소라, Stella, Hayden]
 
Paper 6 (azam zaka)
Paper 6 (azam zaka)Paper 6 (azam zaka)
Paper 6 (azam zaka)
 
Numerical methods for 2 d heat transfer
Numerical methods for 2 d heat transferNumerical methods for 2 d heat transfer
Numerical methods for 2 d heat transfer
 
Time alignment techniques for experimental sensor data
Time alignment techniques for experimental sensor dataTime alignment techniques for experimental sensor data
Time alignment techniques for experimental sensor data
 
TurnerBottoneStanekNIPS2013
TurnerBottoneStanekNIPS2013TurnerBottoneStanekNIPS2013
TurnerBottoneStanekNIPS2013
 

Similar to Using the Componentwise Metropolis-Hastings Algorithm to Sample from the Joint Posterior Distribution for the Parameters of a Binomial Regression Model

Aristotle boyd martin-peci_poster_2017
Aristotle boyd martin-peci_poster_2017Aristotle boyd martin-peci_poster_2017
Aristotle boyd martin-peci_poster_2017Aristotle Boyd-Martin
 
Deal_Final_Paper_PY525f15
Deal_Final_Paper_PY525f15Deal_Final_Paper_PY525f15
Deal_Final_Paper_PY525f15Jacob Deal
 
Partha Sengupta_structural analysis.pptx
Partha Sengupta_structural analysis.pptxPartha Sengupta_structural analysis.pptx
Partha Sengupta_structural analysis.pptxJimmyPhoenix2
 
MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION
MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION
MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION ijscai
 
Jgrass-NewAge: Kriging component
Jgrass-NewAge: Kriging componentJgrass-NewAge: Kriging component
Jgrass-NewAge: Kriging componentNiccolò Tubini
 
Robust and real time detection of
Robust and real time detection ofRobust and real time detection of
Robust and real time detection ofcsandit
 
Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...
Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...
Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...csandit
 
Quantum algorithm for solving linear systems of equations
 Quantum algorithm for solving linear systems of equations Quantum algorithm for solving linear systems of equations
Quantum algorithm for solving linear systems of equationsXequeMateShannon
 
Prob and statistics models for outlier detection
Prob and statistics models for outlier detectionProb and statistics models for outlier detection
Prob and statistics models for outlier detectionTrilochan Panigrahi
 
tw1979 Exercise 3 Report
tw1979 Exercise 3 Reporttw1979 Exercise 3 Report
tw1979 Exercise 3 ReportThomas Wigg
 
Introduction to Support Vector Machines
Introduction to Support Vector MachinesIntroduction to Support Vector Machines
Introduction to Support Vector MachinesSilicon Mentor
 
Variable step size of lms algorithem using partical swarm optimization
Variable step size of lms algorithem using partical swarm optimizationVariable step size of lms algorithem using partical swarm optimization
Variable step size of lms algorithem using partical swarm optimizationeSAT Publishing House
 
An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...Zac Darcy
 
An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...Zac Darcy
 
Probabilistic Error Bounds for Reduced Order Modeling M&C2015
Probabilistic Error Bounds for Reduced Order Modeling M&C2015Probabilistic Error Bounds for Reduced Order Modeling M&C2015
Probabilistic Error Bounds for Reduced Order Modeling M&C2015Mohammad
 

Similar to Using the Componentwise Metropolis-Hastings Algorithm to Sample from the Joint Posterior Distribution for the Parameters of a Binomial Regression Model (20)

Aristotle boyd martin-peci_poster_2017
Aristotle boyd martin-peci_poster_2017Aristotle boyd martin-peci_poster_2017
Aristotle boyd martin-peci_poster_2017
 
G05313347
G05313347G05313347
G05313347
 
Bootstrap2up
Bootstrap2upBootstrap2up
Bootstrap2up
 
Deal_Final_Paper_PY525f15
Deal_Final_Paper_PY525f15Deal_Final_Paper_PY525f15
Deal_Final_Paper_PY525f15
 
Partha Sengupta_structural analysis.pptx
Partha Sengupta_structural analysis.pptxPartha Sengupta_structural analysis.pptx
Partha Sengupta_structural analysis.pptx
 
MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION
MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION
MARGINAL PERCEPTRON FOR NON-LINEAR AND MULTI CLASS CLASSIFICATION
 
RS
RSRS
RS
 
Economia01
Economia01Economia01
Economia01
 
Economia01
Economia01Economia01
Economia01
 
Jgrass-NewAge: Kriging component
Jgrass-NewAge: Kriging componentJgrass-NewAge: Kriging component
Jgrass-NewAge: Kriging component
 
Robust and real time detection of
Robust and real time detection ofRobust and real time detection of
Robust and real time detection of
 
Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...
Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...
Robust and Real Time Detection of Curvy Lanes (Curves) Having Desired Slopes ...
 
Quantum algorithm for solving linear systems of equations
 Quantum algorithm for solving linear systems of equations Quantum algorithm for solving linear systems of equations
Quantum algorithm for solving linear systems of equations
 
Prob and statistics models for outlier detection
Prob and statistics models for outlier detectionProb and statistics models for outlier detection
Prob and statistics models for outlier detection
 
tw1979 Exercise 3 Report
tw1979 Exercise 3 Reporttw1979 Exercise 3 Report
tw1979 Exercise 3 Report
 
Introduction to Support Vector Machines
Introduction to Support Vector MachinesIntroduction to Support Vector Machines
Introduction to Support Vector Machines
 
Variable step size of lms algorithem using partical swarm optimization
Variable step size of lms algorithem using partical swarm optimizationVariable step size of lms algorithem using partical swarm optimization
Variable step size of lms algorithem using partical swarm optimization
 
An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...
 
An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...An Improved Iterative Method for Solving General System of Equations via Gene...
An Improved Iterative Method for Solving General System of Equations via Gene...
 
Probabilistic Error Bounds for Reduced Order Modeling M&C2015
Probabilistic Error Bounds for Reduced Order Modeling M&C2015Probabilistic Error Bounds for Reduced Order Modeling M&C2015
Probabilistic Error Bounds for Reduced Order Modeling M&C2015
 

More from Thomas Templin

Design Optimization and Carpet Plot
Design Optimization and Carpet PlotDesign Optimization and Carpet Plot
Design Optimization and Carpet PlotThomas Templin
 
Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...
Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...
Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...Thomas Templin
 
An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...
An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...
An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...Thomas Templin
 
Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...
Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...
Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...Thomas Templin
 
Automated Detection of Frustration presentation
Automated Detection of Frustration presentationAutomated Detection of Frustration presentation
Automated Detection of Frustration presentationThomas Templin
 
Automated Detection of Frustration paper
Automated Detection of Frustration paperAutomated Detection of Frustration paper
Automated Detection of Frustration paperThomas Templin
 
Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...
Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...
Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...Thomas Templin
 
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)Thomas Templin
 
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...Thomas Templin
 

More from Thomas Templin (9)

Design Optimization and Carpet Plot
Design Optimization and Carpet PlotDesign Optimization and Carpet Plot
Design Optimization and Carpet Plot
 
Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...
Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...
Troubleshooting and Enhancement of Inverted Pendulum System Controlled by DSP...
 
An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...
An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...
An Overview of Superconductivity with Special Attention on Thermodynamic Aspe...
 
Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...
Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...
Equipping the MeArm Open Source Robot Arm with Mobile, Sensory, and Enhanced ...
 
Automated Detection of Frustration presentation
Automated Detection of Frustration presentationAutomated Detection of Frustration presentation
Automated Detection of Frustration presentation
 
Automated Detection of Frustration paper
Automated Detection of Frustration paperAutomated Detection of Frustration paper
Automated Detection of Frustration paper
 
Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...
Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...
Deliberations on and Suggestions for Revising Canon Four of the Code of Ethic...
 
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (report)
 
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...
Feature Detection in Aerial Images for Post-Disaster Needs Assessment (presen...
 

Recently uploaded

From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 

Recently uploaded (20)

From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 

Using the Componentwise Metropolis-Hastings Algorithm to Sample from the Joint Posterior Distribution for the Parameters of a Binomial Regression Model

  • 1. Programming Assignment # 6: Using the Componentwise Metropolis-Hastings Algorithm to Sample from the Joint Posterior Distribution for the Parameters of a Binomial Regression Model Markov Chain Monte Carlo (MCMC) methods provide a way to sample from a distribution (e.g., the joint posterior distribution for the parameters of a Bayesian model). These methods are useful when analytic solutions for param- eter estimations do not exist. If the Markov chain is long, the sampled random variables are (approximately) identically distributed, but they are not indepen- dent because in a Markov chain each random variable depends on the previous one. However, because the Ergodic Theorem applies to MCMC methods, the chains converge (with probability one) to the stationary distribution, which for our purposes is the Bayesian joint posterior distribution. MCMC methods are frequently implemented using a Gibbs sampler. This, however, requires knowledge of the parameters’ conditional distributions, which are frequently not available. In this case, another MCMC method, called the Metropolis-Hastings algorithm, can be used. The Metropolis-Hastings al- gorithm is a type of acceptance/rejection method. It requires a candidate- generating distribution, also called proposal distribution. Ideally, the proposal distribution should be similar to the posterior distribution, but any distribution with the same support as the posterior is possible. If f(y|x) denotes the pro- posal density, and p(x) the posterior density, the acceptance probability α(y|x) is calculated as α(y|x) = min 1, f(x|y)p(y) f(y|x)p(x) . One then generates U~U[0, 1] and selects the proposal y if U ≤ α and otherwise rejects the proposal and remains in state x. Thus, the proposal is accepted with probability α(y|x) and rejected with probability 1−α(y|x). If the proposed step is sampled as Y |X~U(X −a, X +a), the proposal densities in the numerator and denominator cancel. Moreover, it is enough to use the kernel of the posterior density because α uses the ratio of two values of p(.). The Metropolis-Hastings algorithm generalizes to multidimensional distri- butions. In the multidimensional case, there are two types of algorithms — the “regular” algorithm and the “componentwise” algorithm. Whereas the “regular” algorithm computes a full proposal vector at each step, the “com- ponentwise” algorithm, which is implemented in the example discussed in this assignment, updates each component at a time, so that the proposals for all the components are evaluated, i.e., accepted or rejected, in turn. The assignment considers the binomial regression model yi~Bi(mi, θi), with y denoting the vector of the number of successes and m the vector of the number of trials; θ is given by θi = 1 1 + e[−(α+βxi)] , with x denoting a vector containing the integers from 1 to 10. The m-vector is (5, 4, 8, 6, 3, 10, 7, 12, 5, 4), and the y-vector is (0, 0, 0, 1, 3, 5, 4, 9, 3, 3). 1
  • 2. A function called “bireg” was written to simulate sampling from the joint posterior distribution for the parameters α and β. The function returns the posterior means of these parameters. The function takes the vectors y, x, and m as input arguments, as well as the starting values of the parameters (defaults: α = 1, β = 1), the step sizes of the proposals (defaults: step size of the proposal for α = 1, for β = 0.2), the number of steps of the chain (default = 10,000), and the length of the “burn-in” period (default = 5000), i.e., the number of initial values of the chain to be discarded. Burning-in improves the approximation for the parameters of the posterior distribution because the initial observations, i.e., the observations before the chains have converged to the stationary distribution, are discarded from the estimation of probabilities and expectations derived from the posterior. For practical reasons, a flat, uniform (that is, completely noninformative) prior was chosen. The choice of this prior also reflects the lack of prior knowl- edge about previously observed or expected values for α and β. The choice of this prior makes the posterior distribution synonymous to the likelihood, and in this writeup, as well as in the program, the two expressions are used ex- changeably. Under omission of the binomial coefficient, which cancels out when taking the ratio of the posterior distributions for the calculation of the accep- tance probability, the log posterior density for the binomial regression model is p(α, β|y) = n i=1 yi log(θi) + (mi − yi) log(1 − θi). This log posterior distribution was used to implement the componentwise Metro- polis-Hastings algorithm, which generates a proposal for each of the two param- eters (α and β) in turn, and then decides, based on the acceptance probability, whether to accept or reject the proposal. (The decision about acceptance or rejection is made with the help of sampling from a log standard uniform dis- tribution, similar to the procedure described above.) This mechanism ensures that the probability of acceptance depends on the ratio of the kernels of the posterior densities. The choice of the step size (“radius”) of the proposal for a component is critical for rapid convergence to the stationary distribution. The step size is denoted a, and both the default values for aα and aβ were tuned to produce acceptance probabilities between 0.3 and 0.6 (~0.46 for α and ~0.36 for β). The program records the Markov chains for α and β. An index/counter that is running during the execution of the program is used to inform the user about what step the program is currently executing. The goal is to sample from the log likelihood until all samples, after a burn-in period, are derived from the stationary distribution. In order to check whether stationarity had been achieved, the program was run with different starting values (-3, -1, 0, 1, and 3 for both α and β) and with different numbers of steps (10,000 and 100,000); the burn-in period was always half the length of the whole chain. All these configuations produced posterior means of about -3.8 (α) and 0.59 (β). The following graphics were obtained with starting values of 1 for both 2
  • 3. −10 −8 −6 −4 −2 0.20.40.60.81.01.21.4 mchain[(burnin + 1):nrep, alpha] mchain[(burnin+1):nrep,beta] Figure 1: Scatterplot of α and β. α and β, chains of length 100,000, and burn-in periods of 50,000 steps. A scatterplot of α and β is shown in Figure 1. It shows the values of α and β after the burn-in period. Diagnostic plots (time series plots of α and β, histograms of α and β, and autocorrelation functions of α and β) are shown in Figure 2. The time series plots show convergence to the stationary distribution, and the histograms do not have any unusual shapes. However, the autocorrelation functions show considerable positive autocorrelation up to a lag of about 100. (For greater lags (between about 160 and 230) the autocorrelation becomes slightly negative.) This high autocorrelation is potentially problematic. If the correlation between adjacent observations is high, a larger sample size is required to obtain reasonable numerical accuracy, in addition to the requirement of a much longer burn-in. However, the various configuations of staring values and chain lengths that produced nearly identical posterior means, as well as the time series and histograms, strongly suggest that the samples for the posterior means were derived from the stationary distribution. The good fit between the parameter θ of the binomial regression function, which depends on the parameters α and β, and the probabilities of success based on the data (y/m) is shown in Figure 3. Only success probability 5, which equals 1 and is an exception to the trend of generally gradually increasing success 3
  • 4. 0 10000 20000 30000 40000 50000 −10−6−4−2 Index mchain[(burnin+1):nrep,alpha] 0 10000 20000 30000 40000 50000 0.20.61.01.4 Indexmchain[(burnin+1):nrep,beta] Histogram of mchain[(burnin + 1):nrep, alpha] mchain[(burnin + 1):nrep, alpha] Frequency −10 −8 −6 −4 −2 05001500 Histogram of mchain[(burnin + 1):nrep, beta] mchain[(burnin + 1):nrep, beta] Frequency 0.2 0.4 0.6 0.8 1.0 1.2 1.4 050010001500 0 50 100 150 200 250 0.00.40.8 Lag ACF Series mchain[(burnin + 1):nrep, alpha] 0 50 100 150 200 250 0.00.40.8 Lag ACF Series mchain[(burnin + 1):nrep, beta] Figure 2: Diagnostic plots of the results of a run of the Metropolis-Hastings algorithm for the binomial regression model of the assignment: time series plots, histograms, and autocorrelation functions of α and β. 4
  • 5. x x x x x x x x x x 2 4 6 8 10 0.00.20.40.60.81.0 x y/m,theta Figure 3: Probabilities of success (yi/mi; crosses) and parameters θi (dashed line) of the binomial regression model. probabilities, is located far away from the monotonically increasing regression line. The code for the “bireg” function is shown below: # Function that uses the componentwise Metropolis-Hastings algorithm to # sample from the joint likelihood for alpha and beta of a binomial # regression model and returns the (posterior) means of these parameters. # # Required arguments: # y - vector of number of successes # x - vector of series of trials # m - vector of number of trials # # Optional arguments: # start - vector (length 2) of parameter (alpha, beta) starting values # avec - vector (length 2) determining the step sizes of the proposals # for alpha and beta # nrep - chain length 5
  • 6. # burnin - number of observations to discard # # Returns the (posterior) means of the parameters alpha and beta. # bireg <- function(y, x, m, start = c(1, 1), avec = c(1, 0.2), nrep = 10000, burnin = 5000) { if(mode(y) != "numeric" || mode(x) != "numeric" || mode(m) != "numeric" || mode(start) != "numeric" || mode(nrep) != "numeric" || mode(avec) != "numeric" || mode(burnin) != "numeric") stop("All arguments must be numeric!") logpost <- function(alpha, beta, x, m, y) { # logarithm of the joint posterior distribution theta <- 1/(1+exp(-(alpha+beta*x))) return(sum(y*log(theta) + (m-y)*log(1-theta))) } MHstep <- function(pars, avec, x, m, y){ # implementation of the componentwise Metropolis-Hastings algorithm res <- pars for(i in 1:2) { prop <- res prop[i] <- res[i] + 2 * avec[i] * (runif(1) - 0.5) # generate a proposal for a component/parameter if(log(runif(1)) < logpost(prop[1], prop[2], x, m, y) - logpost(res[1], res[2], x, m, y)) res[i] <- prop[i] # conditional for acceptance } return(res) # return parameters } mchain <- matrix(NA, nrow=nrep, ncol=2) # initialization of Markov chain matrix mchain[1,] <- start # parameter starting values for(i in 2:nrep) { print(i) # index of current step mchain[i,] <- MHstep(mchain[i - 1, ], avec, x, m, y) # execution of the Metropolis-Hastings algorithm } return(list(alpha = mean(mchain[(burnin+1):nrep, 1]), beta = mean(mchain[(burnin+1):nrep, 2]))) # return the means of the parameters alpha and beta } 6