Skip to content

Commit

Permalink
Fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
enweg committed Dec 10, 2023
1 parent 60a4398 commit d5226db
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 47 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
.Rhistory
.RData
.Ruserdata
**/.DS_Store
Manifest.toml
Project.toml
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 0.1.1
Date: 2023-03-06 15:28:50 UTC
SHA: 099a9eccb094b8e5fdfdf2e6cf302c1e5fea9ad8
Date: 2023-03-08 19:09:58 UTC
SHA: dcfcb820cd540fef83bd9b3baa471b640e88fc84
12 changes: 12 additions & 0 deletions R/BayesFluxR.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
#' How precise should values be communicated to Julia?
#'
#' The higher the number of zeros (digits) the more precise.
.get_num_zeros <- function() {
zeros <- Sys.getenv("BFLUXR_NUM_ZEROS")
if (zeros == "") {
zeros <- 20
} else {
zeros <- as.numeric(zeros)
}
return(zeros)
}

#' Installs Julia packages if needed
#'
Expand Down
6 changes: 3 additions & 3 deletions R/adapter-mass.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ madapter.DiagCov <- function(adapt_steps, windowlength,
kappa = 0.5, epsilon = 1e-6){

juliavar <- get_random_symbol()
juliacode <- sprintf("DiagCovMassAdapter(%i, %i; kappa = %ff0, epsilon = %ff0)",
juliacode <- sprintf("DiagCovMassAdapter(%i, %i; kappa = Float32(%e), epsilon = Float32(%e))",
adapt_steps, windowlength, kappa, epsilon)
JuliaCall::julia_command(sprintf("%s = %s",
juliavar, juliacode))
Expand Down Expand Up @@ -122,7 +122,7 @@ madapter.FullCov <- function(adapt_steps, windowlength,
kappa = 0.5, epsilon = 1e-6){

juliavar <- get_random_symbol()
juliacode <- sprintf("FullCovMassAdapter(%i, %i; kappa = %ff0, epsilon = %ff0)",
juliacode <- sprintf("FullCovMassAdapter(%i, %i; kappa = Float32(%e), epsilon = Float32(%e))",
adapt_steps, windowlength, kappa, epsilon)
JuliaCall::julia_command(sprintf("%s = %s",
juliavar, juliacode))
Expand Down Expand Up @@ -172,7 +172,7 @@ madapter.RMSProp <- function(adapt_steps, lambda = 1e-5, alpha = 0.99){
JuliaCall::julia_source(system.file("Julia/ascii-translate.jl", package = "BayesFluxR"))

juliavar <- get_random_symbol()
juliacode <- sprintf("ascii_RMSPropMassAdapter(%i; lambda = %ff0, alpha = %ff0)",
juliacode <- sprintf("ascii_RMSPropMassAdapter(%i; lambda = Float32(%e), alpha = Float32(%e))",
adapt_steps, lambda, alpha)

JuliaCall::julia_command(sprintf("%s = %s",
Expand Down
6 changes: 3 additions & 3 deletions R/adapter-stepsize.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' @export
sadapter.Const <- function(l){
juliavar <- get_random_symbol()
juliacode <- sprintf("ConstantStepsize(%ff0)", l)
juliacode <- sprintf("ConstantStepsize(Float32(%e))", l)
JuliaCall::julia_command(sprintf("%s = %s",
juliavar, juliacode))

Expand Down Expand Up @@ -66,8 +66,8 @@ sadapter.DualAverage <- function(adapt_steps, initial_stepsize=1.0,
gamma = 0.05, t0 = 10, kappa = 0.75) {

juliavar <- get_random_symbol()
juliacode <- sprintf("DualAveragingStepSize(%ff0; target_accept = %ff0,
gamma = %ff0, t0 = %i, kappa = %ff0, adapt_steps = %i)",
juliacode <- sprintf("DualAveragingStepSize(Float32(%e); target_accept = Float32(%e),
gamma = Float32(%e), t0 = %i, kappa = Float32(%e), adapt_steps = %i)",
initial_stepsize, target_accept, gamma, t0,
kappa, adapt_steps)

Expand Down
8 changes: 4 additions & 4 deletions R/distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#'
#' @export
Gamma <- function(shape=2.0, scale=2.0){
juliacode <- sprintf("Gamma(%f, %f)", shape, scale)
juliacode <- sprintf("Gamma(%e, %e)", shape, scale)
symbol <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;", symbol, juliacode))
out <- list(juliavar = symbol, juliacode = juliacode)
Expand Down Expand Up @@ -50,7 +50,7 @@ Gamma <- function(shape=2.0, scale=2.0){
#' }
#' @export
InverseGamma <- function(shape=2.0, scale=2.0){
juliacode <- sprintf("InverseGamma(%f, %f)", shape, scale)
juliacode <- sprintf("InverseGamma(%e, %e)", shape, scale)
symbol <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;", symbol, juliacode))
out <- list(juliavar = symbol, juliacode = juliacode)
Expand Down Expand Up @@ -78,7 +78,7 @@ InverseGamma <- function(shape=2.0, scale=2.0){
#'
#' @export
Normal <- function(mu=0, sigma=1){
juliacode <- sprintf("Normal(%f, %f)", mu, sigma)
juliacode <- sprintf("Normal(%e, %e)", mu, sigma)
symbol <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;", symbol, juliacode))
out <- list(juliavar = symbol, juliacode = juliacode)
Expand Down Expand Up @@ -106,7 +106,7 @@ Normal <- function(mu=0, sigma=1){
#'
#' @export
Truncated <- function(dist, lower, upper){
juliacode <- sprintf("Truncated(%s, %f, %f)", dist$juliacode, lower, upper)
juliacode <- sprintf("Truncated(%s, %e, %e)", dist$juliacode, lower, upper)
symbol <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;", symbol, juliacode))
out <- list(juliavar = symbol, juliacode = juliacode)
Expand Down
4 changes: 2 additions & 2 deletions R/likelihoods.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ likelihood.feedforward_normal <- function(chain, sig_prior){
#'
#' @export
likelihood.feedforward_tdist <- function(chain, sig_prior, nu=30){
juliacode <- sprintf("BayesFlux.FeedforwardTDist(%s, %s, %ff0)",
juliacode <- sprintf("BayesFlux.FeedforwardTDist(%s, %s, Float32(%e))",
chain$nc, sig_prior$juliavar, nu)
symbol <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;", symbol, juliacode))
Expand Down Expand Up @@ -146,7 +146,7 @@ likelihood.seqtoone_normal <- function(chain, sig_prior){
#'
#' @export
likelihood.seqtoone_tdist <- function(chain, sig_prior, nu = 30){
juliacode <- sprintf("BayesFlux.SeqToOneTDist(%s, %s, %ff0)",
juliacode <- sprintf("BayesFlux.SeqToOneTDist(%s, %s, Float32(%e))",
chain$nc, sig_prior$juliavar, nu)
sym <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;", sym, juliacode))
Expand Down
10 changes: 5 additions & 5 deletions R/mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sampler.SGLD <- function(stepsize_a = 0.1, stepsize_b = 0,
JuliaCall::julia_source(system.file("Julia/ascii-translate.jl", package = "BayesFluxR"))

juliavar <- get_random_symbol()
juliacode <- sprintf("ascii_SGLD(; stepsize_a = %ff0, stepsize_b = %ff0, stepsize_gamma = %ff0, min_stepsize = Float32(%s))",
juliacode <- sprintf("ascii_SGLD(; stepsize_a = Float32(%e), stepsize_b = Float32(%e), stepsize_gamma = Float32(%e), min_stepsize = Float32(%s))",
stepsize_a, stepsize_b, stepsize_gamma, min_stepsize)
JuliaCall::julia_command(sprintf("%s = %s;",
juliavar, juliacode))
Expand Down Expand Up @@ -82,7 +82,7 @@ sampler.AdaptiveMH <- function(bnn, t0, sd, eps=1e-6){
sym.C0, sym.C0))

juliavar <- get_random_symbol()
juliacode <- sprintf("AdaptiveMH(%s, %i, %ff0, %ff0)",
juliacode <- sprintf("AdaptiveMH(%s, %i, Float32(%e), Float32(%e))",
sym.C0, t0, sd, eps)
JuliaCall::julia_command(sprintf("%s = %s;",
juliavar, juliacode))
Expand Down Expand Up @@ -130,7 +130,7 @@ sampler.GGMC <- function(beta = 0.1, l = 1.0,
JuliaCall::julia_source(system.file("Julia/ascii-translate.jl", package = "BayesFluxR"))

juliavar <- get_random_symbol()
juliacode <- sprintf("ascii_GGMC(; beta = %ff0, l = %ff0, sadapter = %s, madapter = %s, steps = %i)",
juliacode <- sprintf("ascii_GGMC(; beta = Float32(%e), l = Float32(%e), sadapter = %s, madapter = %s, steps = %i)",
beta, l, sadapter$juliavar, madapter$juliavar, steps)

JuliaCall::julia_command(sprintf("%s = %s;",
Expand Down Expand Up @@ -183,7 +183,7 @@ sampler.HMC <- function(l, path_len,
sadapter = sadapter.DualAverage(1000),
madapter = madapter.FixedMassMatrix()){
juliavar <- get_random_symbol()
juliacode <- sprintf("HMC(%ff0, %i; sadapter = %s, madapter = %s)",
juliacode <- sprintf("HMC(Float32(%e), %i; sadapter = %s, madapter = %s)",
l, path_len, sadapter$juliavar, madapter$juliavar)
JuliaCall::julia_command(sprintf("%s = %s;",
juliavar, juliacode))
Expand Down Expand Up @@ -238,7 +238,7 @@ sampler.SGNHTS <- function(l, sigmaA = 1, xi = 1, mu = 1,
JuliaCall::julia_source(system.file("Julia/ascii-translate.jl", package = "BayesFluxR"))

juliavar <- get_random_symbol()
juliacode <- sprintf("ascii_SGNHTS(%ff0, %ff0; xi = %ff0, mu = %ff0, madapter = %s)",
juliacode <- sprintf("ascii_SGNHTS(Float32(%e), Float32(%e); xi = Float32(%e), mu = Float32(%e), madapter = %s)",
l, sigmaA, xi, mu, madapter$juliavar)

JuliaCall::julia_command(sprintf("%s = %s;",
Expand Down
6 changes: 3 additions & 3 deletions R/mode-finding.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#' @export
opt.Descent <- function(eta = 0.1){
juliavar <- get_random_symbol()
juliacode <- sprintf("Flux.Descent(%f)",
juliacode <- sprintf("Flux.Descent(%e)",
eta)

JuliaCall::julia_command(sprintf("%s = %s;",
Expand Down Expand Up @@ -63,7 +63,7 @@ opt.Descent <- function(eta = 0.1){
#' @export
opt.ADAM <- function(eta = 0.001, beta = c(0.9, 0.999), eps = 1e-8){
juliavar <- get_random_symbol()
juliacode <- sprintf("Flux.ADAM(%f, (%f, %f), %f)",
juliacode <- sprintf("Flux.ADAM(%e, (%e, %e), %e)",
eta, beta[1], beta[2], eps)
JuliaCall::julia_command(sprintf("%s = %s;",
juliavar, juliacode))
Expand Down Expand Up @@ -96,7 +96,7 @@ opt.ADAM <- function(eta = 0.001, beta = c(0.9, 0.999), eps = 1e-8){
#' @export
opt.RMSProp <- function(eta = 0.001, rho = 0.9, eps = 1e-8){
juliavar <- get_random_symbol()
juliacode <- sprintf("Flux.RMSProp(%f, %f, %f)",
juliacode <- sprintf("Flux.RMSProp(%e, %e, %e)",
eta, rho, eps)
JuliaCall::julia_command(sprintf("%s = %s;",
juliavar, juliacode))
Expand Down
4 changes: 2 additions & 2 deletions R/priors.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#'
#' @export
prior.gaussian <- function(chain, sigma){
juliacode <- sprintf("GaussianPrior(%s, %ff0)",
juliacode <- sprintf("GaussianPrior(%s, Float32(%e))",
chain$nc, sigma)
juliavar <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;",
Expand Down Expand Up @@ -74,7 +74,7 @@ prior.gaussian <- function(chain, sigma){
#'
#' @export
prior.mixturescale <- function(chain, sigma1, sigma2, pi1){
juliacode <- sprintf("MixtureScalePrior(%s, %ff0, %ff0, %ff0)",
juliacode <- sprintf("MixtureScalePrior(%s, Float32(%e), Float32(%e), Float32(%e))",
chain$nc, sigma1, sigma2, pi1)
juliavar <- get_random_symbol()
JuliaCall::julia_command(sprintf("%s = %s;",
Expand Down
Loading

0 comments on commit d5226db

Please sign in to comment.