I am running a Markov Chain Monte Carlo algorithm for updating a density distribution. There is a specific section of my code which tries to fill a very large matrix thetha.mh
(of dimension 5000x2x60). the following code snippet is trying to fill the first dimension of my matrix, but the specific operation in second line of the loop is taking too much time.
Does anybody know of a better way to improve the efficiency of this operation?
fhatt=kde(x=theta.mh[m1:m,s,t-1], h=hpi(theta.mh[m1:m,s,t-1]))
p2=function(thetax)
dkde(thetax, fhatt)
fhatd=kde(x=deltat.mh[m1:m,s,t-1], h=hpi(deltat.mh[m1:m,s,t-1]))
p3=function(deltatx)
dkde(deltatx, fhatd)
# P1 and P2 are two distribution updated from the previous values of tetha.mh through kernel density estimation
h1=function(thetax)
log(p1(y[s,t],thetax)*p2(thetax))
# h1 is the the logarithm of the multiplication of p1 and p2 (laplace form estimate)
for(i1 in 2:5000)
{
thetas=rnorm(1,theta.mh[i1-1,s,t],stheta[i1,s,t])
r1[i1,s,t]=exp(h1(thetas)-h1(theta.mh[i1-1,s,t]))
if (r1[i1,s,t]>runif(1))
theta.mh[i1,s,t]=thetas
else {
theta.mh[i1,s,t]=theta.mh[i1-1,s,t]
}
}
Rcpp
andinline
to rewrite in C++ depending on your wizard skills \$\endgroup\$