The $profiles()
method returns a list of data frames with
profiling data if any profiling data was written to the profile CSV files.
See save_profile_files()
to control where the files are saved.
Support for profiling Stan programs is available with CmdStan >= 2.26 and requires adding profiling statements to the Stan program.
profiles()
A list of data frames with profiling data if the profiling CSV files were created.
# \dontrun{
# first fit a model using MCMC
mcmc_program <- write_stan_file(
'data {
int<lower=0> N;
array[N] int<lower=0,upper=1> y;
}
parameters {
real<lower=0,upper=1> theta;
}
model {
profile("likelihood") {
y ~ bernoulli(theta);
}
}
generated quantities {
array[N] int y_rep;
profile("gq") {
y_rep = bernoulli_rng(rep_vector(theta, N));
}
}
'
)
mod_mcmc <- cmdstan_model(mcmc_program)
data <- list(N = 10, y = c(1,1,0,0,0,1,0,1,0,0))
fit <- mod_mcmc$sample(data = data, seed = 123, 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.
#>
fit$profiles()
#> [[1]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 gq 0x10efcde00 0.000375429 0.000375429 0.000000000 0
#> 2 likelihood 0x10efcde00 0.001165040 0.000834157 0.000330887 7169
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 0 0 1000
#> 2 7169 7169 1
#>
#> [[2]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 gq 0x10d0b0e00 0.000407951 0.000407951 0.000000000 0
#> 2 likelihood 0x10d0b0e00 0.001283690 0.000915781 0.000367905 7155
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 0 0 1000
#> 2 7155 7155 1
#>
#> [[3]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 gq 0x106bb1e00 0.000399315 0.000399315 0.000000000 0
#> 2 likelihood 0x106bb1e00 0.001242900 0.000887379 0.000355522 6879
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 0 0 1000
#> 2 6879 6879 1
#>
#> [[4]]
#> name thread_id total_time forward_time reverse_time chain_stack
#> 1 gq 0x111b21e00 0.000516567 0.000516567 0.000000000 0
#> 2 likelihood 0x111b21e00 0.001479350 0.001061390 0.000417962 6892
#> no_chain_stack autodiff_calls no_autodiff_calls
#> 1 0 0 1000
#> 2 6892 6892 1
#>
# }