#This script does a Riemanniean Manifold Hamiltonian MCMC leapfrog proposal for the LOLOG model:
#simply updates the covariance matrix at each step compared to the regular HMC algorithm
#uses observed information at each step
#' @export
RMHMC_observed_proposal <- function(theta_0, #the current paramter value - starting position for HMC
net, #the observed network,
s, #the fixed edge permutation
formula_rhs, #rhs formula of model
prior = function(theta,net=net,change_on = change_on){(sum(abs(theta)<10) == length(theta))*((1/20)**length(theta))}, #prior function for theta
prior_grad = NULL, #specify prior derivative to get speed up
L_steps, #the number of leapfrog steps
epsilon = NULL, #size of the leapfrog step
epsilon_factor = 1, #used when epsilon is null to amend the generate "ideal epsilon"
momentum_sigma, #sigma used in momentum function generator
change_off = NULL,
change_on = NULL,
...
){
#Rename the theta_0 as q - in line with HMC literature
q <- theta_0
names(q) <- NULL
#Make the lolog formula
formula <- as.formula(paste("net ~",formula_rhs,sep = ""))
#calculate the change stats for the given permutaion and graph
if(is.null(change_off)){
tmp <- lolog_change_stats(net,s,formula_rhs)
change_on <- tmp$change_on
change_off <- tmp$change_off
}
#Calculate gradient of the log liklihood function for LOLOG
q_grad <- function(q,change_on=NULL,network=NULL){
if(!is.null(network) & (is.null(change_on)) ){
formula <- as.formula(paste("network ~",formula_rhs,sep = ""))
tmp <- lolog_change_stats(network,s,formula_rhs)
change_on <- tmp$change_on
}
if(is.null(prior_grad)){
prior_deriv <- (numDeriv::grad(prior,q,net=net,change_on = change_on)/prior(q,net=net,change_on = change_on))
}
else{prior_deriv <- prior_grad(q,net=net,change_on = change_on)/prior(q,net=net,change_on = change_on)}
if(sum(is.na(prior_deriv)) != 0){
prior_deriv <- rep(0,length(q))
}
#derivative of change statistics on top
top_deriv <- Reduce('+',change_on)
bottom_deriv <- bottom_deriv_helper_cpp(change_on,q)
#return their sum with the correct signs
return(-prior_deriv - top_deriv + bottom_deriv)
}
#specify a local momentum matrix if no momentum is supplied:
#it is the local covariance matrix at the starting point
#"ideal" dynamics under the assumption that the proposal distribution is Gaussian are sampling the momentum from the inverse covariance matrix
if(is.null(momentum_sigma)){
momentum_sigma = outer(q_grad(q,change_on = change_on),q_grad(q,change_on=change_on))
#check if singular
if(abs(det(momentum_sigma)) < 10**(-6)){
for(i in 10**seq(-6,10,1)){
if(abs(det(momentum_sigma))>10**(-6)){
break
}
else{
momentum_sigma <- momentum_sigma + diag(min(diag(momentum_sigma))*i,dim(momentum_sigma)[1])
}
}
}
momentum_sigma_inv <- solve(momentum_sigma)
}
#If epsilon is null put it as the mimimum eigen value of the momentum matrix:
if(is.null(epsilon)){
epsilon <- (min(eigen(momentum_sigma)$values)**0.5)*epsilon_factor
}
#Generate initial momentum
p_init <- MASS::mvrnorm(1,rep(0,length(q)),Sigma = momentum_sigma)
p <- p_init
#Do half momentum update fist
p <- p - (epsilon/2)*q_grad(q)
#Do L_steps full updates
if(L_steps != 1){
for(i in 1:(L_steps-1)){
q <- q + epsilon*(momentum_sigma_inv%*%p)
if(prior(q)==0){
return(list(proposal = as.vector(q), prob_factor = 0))
}
p <- p - epsilon*q_grad(q)
#print(q)
#print(p)
#print(momentum_sigma)
#print(det(momentum_sigma))
#print(momentum_sigma_inv)
#print(det(momentum_sigma_inv))
#Update covariance matrix:
momentum_sigma = outer(q_grad(q),q_grad(q))
#check if singular
if(abs(det(momentum_sigma)) < 10**(-6)){
for(i in 10:1){
if(abs(det(momentum_sigma))>10**(-6)){
break
}
else{
momentum_sigma <- momentum_sigma + diag(min(diag(momentum_sigma))*10**(-i),dim(momentum_sigma)[1])
}
}
}
momentum_sigma_inv <- solve(momentum_sigma)
}
}
#Do final updates
q <- q + epsilon*(momentum_sigma_inv%*%p)
p <- p - (epsilon/2)*q_grad(q)
#negate the momentum variable - does not actaully effect anything
p <- -p
#prob factor takes account of the joint distribution in the metropolis step
prob_factor = exp(0.5*t(p_init)%*%momentum_sigma_inv%*%p_init - 0.5*t(p)%*%momentum_sigma_inv%*%p)
return(list(proposal = as.vector(q), prob_factor = prob_factor))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.