NimbleHMC

63 views
Skip to first unread message

Wala Draidi Areed

unread,
Nov 2, 2022, 1:29:46 AM11/2/22
to nimble-users
Hi,

I would like to use the nimbleHMC package for sampling, but I could not install the package from the code:

remotes::install_github("nimble-dev/nimbleHMC", subdir = "nimbleHMC")


Downloading GitHub repo nimble-dev/nimbleHMC@HEAD
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?

1: All                          
2: CRAN packages only            
3: None                          
4: igraph (1.3.0 -> 1.3.5) [CRAN]

Enter one or more numbers, or an empty line to skip updates: 1
igraph (1.3.0 -> 1.3.5) [CRAN]
Installing 1 packages: igraph
Warning: package ‘igraph’ is in use and will not be installed
v  checking for file 'C:\Users\n10677313\AppData\Local\Temp\Rtmpyerfzn\remotes5b1c7bd07847\nimble-dev-nimbleHMC-54453dc\nimbleHMC/DESCRIPTION' ...
-  preparing 'nimbleHMC':
v  checking DESCRIPTION meta-information ...
-  checking for LF line-endings in source and make files and shell scripts
-  checking for empty or unneeded directories
-  building 'nimbleHMC_0.1.0.tar.gz'
   
Installing package into ‘C:/Users/n10677313/R/R-4.0.3/library’
(as ‘lib’ is unspecified)
* installing *source* package 'nimbleHMC' ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
  converting help for package 'nimbleHMC'
    finding HTML links ... done
    addHMC                                  html  
    finding level-2 HTML links ... done

    buildHMC                                html  
    configureHMC                            html  
    nimbleHMC                               html  
    sampler_HMC                             html  
    sampler_langevin                        html  
** building package indices
** testing if installed package can be loaded from temporary location
*** arch - i386
Error : package 'nimble' is not installed for 'arch = i386'
Error: loading failed
Execution halted
*** arch - x64
ERROR: loading failed for 'i386'
* removing 'C:/Users/n10677313/R/R-4.0.3/library/nimbleHMC'
Warning message:
In i.p(...) :
  installation of package ‘C:/Users/N10677~1/AppData/Local/Temp/Rtmpyerfzn/file5b1c75daba/nimbleHMC_0.1.0.tar.gz’ had non-zero exit status.



Could you please advise in this regard?


Wala Draidi Areed

unread,
Nov 2, 2022, 1:55:11 AM11/2/22
to nimble-users
I have fixed it using the code:

devtools::install_github("nimble-dev/nimbleHMC", subdir = "nimbleHMC", INSTALL_opts=c("--no-multiarch"))

--
You received this message because you are subscribed to a topic in the Google Groups "nimble-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nimble-users/MEbgZr3i4II/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/f70dcd26-480f-471a-8d36-098820c0d48dn%40googlegroups.com.

Wala Draidi Areed

unread,
Nov 2, 2022, 2:39:23 AM11/2/22
to nimble-users
Hi agian,

I would like to see how nimbleHMC packageworks for different examples, so I ran the package in an example attached below:

library(nimbleHMC)
set.seed(1)
p <- 15    # number of explanatory variables
n <- 100   # number of observations
X <- matrix(rnorm(p*n), nrow = n, ncol = p) # explanatory variables
true_betas <- c(c(0.1, 0.2, 0.3, 0.4, 0.5), rep(0, p-5)) # coefficients
sigma <- 1
y <- rnorm(n, X %*% true_betas, sigma)

library(nimble, warn.conflicts = FALSE)
dnorm_vec <- nimbleFunction( ## Define the distribution
  run = function(x = double(1), mean = double(1), sd = double(0), log = integer(0, default = 0)) {
    returnType(double(0))
    logProb <- sum(dnorm(x, mean, sd, log = TRUE))
    if(log) return(logProb)
    else return(exp(logProb))
  })

rnorm_vec <- nimbleFunction( ## Define a simulation function, optionally.
  run = function(n = integer(0), mean = double(1), sd = double(0)) {
    returnType(double(1))
    if(n != 1) print("rnorm_vec only allows n = 1; using n = 1.")
    smp <- rnorm(n, mean, sd)
    return(smp)
  })

code <- nimbleCode({
  beta0 ~ dnorm(0, sd = 100)
  beta[1:p] ~ dmnorm(zeros[1:p], omega[1:p, 1:p])
  sigma ~ dunif(0, 100)  # prior for variance components based on Gelman (2006)
  linpred[1:n] <- beta0 + x[1:n, 1:p] %*% beta[1:p]
  y[1:n] ~ dnorm_vec(linpred[1:n], sigma)
})

constants <- list(n = n, p = p, x = X, zeros = rep(0, p), omega = 0.0001 * diag(p))
data <- list(y = y)
inits <- list(beta0 = mean(y), beta = rep(0, p), sigma = 0.5)
model <- nimbleModel(code, constants = constants, data = data, inits = inits,buildDerivs = TRUE)

Defining model
   [Note] Distribution dnorm_vec does not appear to support derivatives. Set buildDerivs = TRUE (or to a list) in its nimbleFunction to turn on derivative support.
Building model
Setting data and initial values
Running calculate on model
  [Note] Any error reports that follow may simply reflect missing values in model variables.
Checking model sizes and dimensions


HMC <- buildHMC(model)
Error: Failed to create the shared library. Run 'printErrors()' to see the compilation errors.
Cmodel <- compileNimble(model)
Error: Compiled C++ model not available; please include the model in your compilation call (or compile it in advance).
CHMC <- compileNimble(HMC, project = model)


samples <- runMCMC(CHMC, niter = 1000, nburnin = 500)

Could you please advice in this regards?

Perry de Valpine

unread,
Nov 2, 2022, 11:21:41 AM11/2/22
to Wala Draidi Areed, nimble-users
Dear Wala,

Thanks for these posts.

The report about installation problem on Windows involving the multi-architecture issue, and your solution, is helpful.  Thanks you.

To get your code to run, please add "buildDerivs = TRUE" as an argument to dnorm_vec, so it looks like:

dnorm_vec <- nimbleFunction( ## Define the distribution
  run = function(x = double(1), mean = double(1), sd = double(0), log = integer(0, default = 0)) {
    returnType(double(0))
    logProb <- sum(dnorm(x, mean, sd, log = TRUE))
    if(log) return(logProb)
    else return(exp(logProb))
  },
  buildDerivs = TRUE)

I assume you've found our page here (https://r-nimble.org/ad-beta) and the draft user manual sections (https://r-nimble.org/ADuserManual_draft/chapter_AD.html), but it does appear to me that we need to add clearer information about how to do this.  The section "Using AD with multiple nimbleFunctions" shows the argument, but not specifically in this kind of case.  Thanks again.

Your code ran and the MCMC mixed on my machine with this small change.

-Perry


You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/CAFg7vyYzQT%2BNNkxTjZJfXFRbtVQ4x7eyD9W5CsUo-Ua02W9KAg%40mail.gmail.com.

Richard Chandler

unread,
Nov 2, 2022, 11:54:38 AM11/2/22
to Perry de Valpine, Wala Draidi Areed, nimble-users
Congrats to Perry and all the Nimble developers for adding gradient-based samplers. It's a tremendous achievement and I look forward to trying it out. I especially like that you can mix MH and HMC samplers. Great stuff!

Richard


Wala Draidi Areed

unread,
Nov 2, 2022, 6:22:12 PM11/2/22
to Richard Chandler, Perry de Valpine, nimble-users
Hi Perry,

Thank you for your help. it's work now.

Aslo, Thank you for developing this package, I was planning to move to stan. You save my time.



Reply all
Reply to author
Forward
0 new messages