I want to use this algorithm as a black-box, I'll be implementing it either in Python or R, but I don’t really understand it well to be able to turn it into a program.
How do we choose the initial values for parameters?
What exactly is q(xt, ·) & q(y,x), I understand these are probabilities, but how do we calculate them? and what function is f(y)?
and finally, Do I use the average of the values in the output vectors for each parameter?
Thanks in advance!
Metropolis-Hasting Algorithm for parameter estimation
P – total no. of parameters
N – total no. of iterations
t – iteration variable
- Initialize the parameter vector x0 = {x01, x02, …, X0P}
For iteration t = 1, 2, . . . N do
- Generate a candidate ‘y’ for next sample i.e. draw from the distribution q(xt, ·)
- Calculate acceptance ratio α(xt, y) using α(x, y) = min{1,(f(y) q(y,x))/(f(x) q(x,y) ) }
- Draw u ~ U[0, 1] i.e. Uniform Distribution
Accept/Reject state depending on acceptance ratio
If u < α(xt, y)
xt+1 = y // accept the proposal
else
xt+1 = xt // reject the proposal
Save xt1, xt2,…, xtP
Return {x11, x21, …., xN1} {x12, x22, …., xN2} . . {x1P, x2P, …., xNP}
EDIT1: My question seeks a clear-cut translation of, let's say, α(x, y) = min{1,(f(y) q(y,x))/(f(x) q(x,y) ) } How does this look like in a program? what exactly is the meaning of q(x, y)? how does the function look like? without any terminologies related to probability or statistics. Apologies if I was not clear before.
EDIT2: How should the functions 'proposal_func' & 'alpha_func' be implemented? (if rest of the program looks fine)
values <- rep(0, 100)
current <- 1
for (i in 1:length(values)) {
proposal <- proposal_func(current)
accept_ratio <- alpha_func(current, proposal)
u <- runif(1,0,1)
if (u < accept_ratio) {
values[i] <- proposal
current <- proposal
} else {
values[i] <- current
}
}
alpha_func <- function(current, proposal) {
}
proposal_func <- function(current) {
}