R/deprecated/RMHMC_boot_proposal.R

Defines functions RMHMC_boot_proposal

#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 bootstrap resampling of the change statistics to estimate the Fisher information matrix, rather than just using the observed information
#updates the momentum matrix at each step when the position is updated
#note that this means the HMC dynamics are wrong - so should only be used for a small number of steps e.g. 1 
#Does not do fixed point iteration.


#' @export

RMHMC_boot_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){(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=1, #the number of leapfrog steps
                               epsilon = NULL, #size of the leapfrog stepchange_off = NULL,
                               change_on = NULL,
                               resamples, #number of resamples to take,
                               ...
){
  #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 prior + 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)/prior(q))
    }
    else{prior_deriv <- prior_grad(q)/prior(q)}
    
    if(sum(is.na(prior_deriv)) != 0){
      prior_deriv <- rep(0,length(q))
    }
    #derivative of change statistics on top
    top_deriv <- Reduce('+',change_on)
    #derivative of change statistics on bottom
    tmp <- lapply(change_on,function(x){
      (x)/(1 + exp(-sum(q*x)))})
    bottom_deriv <- Reduce('+',tmp)
    
    #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
  momentum_assign <- function(q){
    
    resamples = lapply(1:resamples,function(x){sample(change_on,length(change_on),replace = T)})
    momentum_sigma <- lapply(1:length(resamples),function(x){q_grad(q,change_on = resamples[[x]])})
    momentum_sigma <- lapply(momentum_sigma,function(x){outer(x,x)})
    momentum_sigma <- Reduce("+",momentum_sigma)/length(momentum_sigma)
      
    #check if singular
    if(abs(det(momentum_sigma)) < 10**(-6)){
      for(i in 10**seq(-6,20,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)
    
    assign("momentum_sigma",momentum_sigma, envir = parent.env(environment()))
    assign("momentum_sigma_inv",momentum_sigma_inv, envir = parent.env(environment()))
    # print("Momentum is :")
    # print(momentum_sigma)
    # print("Momentum inversed is:")
    # print(momentum_sigma_inv)
    #print("Momentum Assigned")
    
    return()
  }
  #debug(momentum_assign)
  momentum_assign(q)
  
  
  #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,change_on,change_off)
  
  #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,change_on,change_off)
      
      #Update covariance matrix:
      momentum_assign(q)
    }
  }
  #Do final updates
  q <- q + epsilon*(momentum_sigma_inv%*%p)
  p <- p - (epsilon/2)*q_grad(q,change_on,change_off)
  
  #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))
}
duncan-clark/Blolog documentation built on June 22, 2022, 7:57 a.m.