Skip to content

Add optional method for exposing stan functions to R #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 12, 2022
Merged

Add optional method for exposing stan functions to R #702

merged 7 commits into from
Oct 12, 2022

Conversation

andrjohns
Copy link
Collaborator

Submission Checklist

  • Run unit tests
  • Declare copyright holder and agree to license (see below)

Summary

This PR adds the functionality to compile and expose user-defined functions to R. After fitting a model, the user simply needs to call the expose_functions() method from the fit object, and the functions will be compiled and made available. By default, these functions are not exposed to the global environment, and instead available under the functions member of the fit object:

library(cmdstanr)
#> This is cmdstanr version 0.5.3
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /Users/andrew/.cmdstan/cmdstan-2.30.1
#> - CmdStan version: 2.30.1
model <- "functions { real rtnx(real x) { return x; } } data { real y_mean; } parameters { real y; } model { y ~normal(y_mean, 1); }"
mod <- cmdstan_model(write_stan_file(model))
fit <- mod$sample(data = list(y_mean = 0), refresh = 0)
#> Running MCMC with 4 sequential chains...
#> 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#> 
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.5 seconds.

# Compile and expose functions
fit$expose_functions()
#> Compiling standalone functions...
fit$functions$rtnx(5)
#> [1] 5

Created on 2022-10-10 with reprex v2.0.2

Alternatively, if they use the global = TRUE argument then the functions will be made available in the global environment instead:

library(cmdstanr)
#> This is cmdstanr version 0.5.3
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /Users/andrew/.cmdstan/cmdstan-2.30.1
#> - CmdStan version: 2.30.1
model <- "functions { real rtnx(real x) { return x; } } data { real y_mean; } parameters { real y; } model { y ~normal(y_mean, 1); }"
mod <- cmdstan_model(write_stan_file(model))
fit <- mod$sample(data = list(y_mean = 0), refresh = 0)
#> Running MCMC with 4 sequential chains...
#> 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#> 
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.5 seconds.

# Compile and expose functions
fit$expose_functions(global = TRUE)
#> Compiling standalone functions...
rtnx(5)
#> [1] 5

Created on 2022-10-10 with reprex v2.0.2

Additionally, the user can also request that the methods are compiled with the call to cmdstan_model:

library(cmdstanr)
#> This is cmdstanr version 0.5.3
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /Users/andrew/.cmdstan/cmdstan-2.30.1
#> - CmdStan version: 2.30.1
model <- "functions { real rtnx(real x) { return x; } } data { real y_mean; } parameters { real y; } model { y ~normal(y_mean, 1); }"
mod <- cmdstan_model(write_stan_file(model), compile_standalone = TRUE)
fit <- mod$sample(data = list(y_mean = 0), refresh = 0)
#> Running MCMC with 4 sequential chains...
#> 
#> Chain 1 finished in 0.0 seconds.
#> Chain 2 finished in 0.0 seconds.
#> Chain 3 finished in 0.0 seconds.
#> Chain 4 finished in 0.0 seconds.
#> 
#> All 4 chains finished successfully.
#> Mean chain execution time: 0.0 seconds.
#> Total execution time: 0.6 seconds.

# Functions already compiled and available
fit$functions$rtnx(5)
#> [1] 5

Created on 2022-10-10 with reprex v2.0.2

Copyright and Licensing

Please list the copyright holder for the work you are submitting
(this will be you or your assignee, such as a university or company):
Andrew Johnson

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the following licenses:

@rok-cesnovar
Copy link
Member

Fantastic as always @andrjohns! Will take a look soon.

Copy link
Member

@rok-cesnovar rok-cesnovar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

Looks great, no comments necessary :)

@rok-cesnovar rok-cesnovar merged commit cdd62a0 into stan-dev:master Oct 12, 2022
@jgabry
Copy link
Member

jgabry commented Oct 13, 2022

This is awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants