Skip to content

Instantly share code, notes, and snippets.

@stablemarkets
Last active March 2, 2019 21:51
Show Gist options
  • Save stablemarkets/cdb5830870b079401d59c69ab96e2df8 to your computer and use it in GitHub Desktop.
Save stablemarkets/cdb5830870b079401d59c69ab96e2df8 to your computer and use it in GitHub Desktop.
# full code at
# https://github.com/stablemarkets/BayesianTutorials/blob/master/MH_with_caching/
# Author: Arman Oganisian
library(microbenchmark)
library(LaplacesDemon)
library(MASS)
source("HelperFunctions.R") # contains mh_vanilla and mh_cache
set.seed(10)
# hyper-parameters and true values
lambda<-c(0,0,0,0)
phi<-10
true_beta <- matrix(c(0,2,1,-2),ncol=1)
N<-50000
# simulate covariates
X1 <- rnorm(N)
X2 <- rnorm(N)
X3 <- rnorm(N)
X <- model.matrix(~ X1 + X2 + X3)
# simulate outcome
Y <- rbinom(n = N, size = 1, prob = invlogit( X %*% true_beta ) )
## Run benchmark
bench<-microbenchmark(
# Run Vanialla Metropolis
MH_vanilla = mh_vanilla(beta_0 = c(0,0,0,0), # initial value
p=4, # number of parameters
phi = phi, lambda = lambda, #hyperparameters
X = X, Y = Y, # Data
#iterations and proposal variance
mh_trials=1000, jump_v=.2 ),
# Run Metropolis with Cache
MH_cache = mh_cache(beta_0 = c(0,0,0,0),
phi = phi, lambda = lambda, X = X, Y = Y,
mh_trials=1000, jump_v=.2, p=4),
times = 10)
bench
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment