Nothing
#!/usr/bin/env Rscript
# b06a. Uniform mixture with Monte Carlo integration
#
# This example estimates a random time coefficient with a symmetric uniform
# mixing distribution. The logit kernel, Monte Carlo integration, gradients,
# Hessian, optimizer, and report are all native Biogeme operations.
library(rbiogeme)
# The shared helper contains command-line parsing and data preparation. The
# complete model specification remains in this script.
script_path <- commandArgs(trailingOnly = FALSE)
script_path <- sub("^--file=", "", script_path[startsWith(script_path, "--file=")][[1L]])
source(file.path(dirname(normalizePath(script_path)), "example_utils.R"))
build_b06a_unif_mixture_model <- function(
database,
number_of_draws = 10000L,
seed = 1223L
) {
# These parameter names and starting values match native b06a. The
# Swissmetro ASC is fixed at zero to identify the utility scale.
asc_car <- biogeme_beta("asc_car", start = 0)
asc_train <- biogeme_beta("asc_train", start = 0)
asc_sm <- biogeme_beta("asc_sm", start = 0, fixed = TRUE)
b_cost <- biogeme_beta("b_cost", start = 0)
b_time <- biogeme_beta("b_time", start = 0)
b_time_s <- biogeme_beta("b_time_s", start = 1)
# draw() creates a named native Draws node. UNIFORMSYM means a draw from
# the symmetric uniform distribution on [-1, 1], so the random coefficient
# is b_time + b_time_s * symmetric-uniform draw.
b_time_rnd <- b_time + b_time_s * draw("b_time_rnd", "UNIFORMSYM")
# Utilities and availability are symbolic R expressions. They are compiled
# once into native Biogeme before any numerical evaluation starts.
utilities <- list(
`1` = asc_train + b_time_rnd * variable("TRAIN_TT_SCALED") +
b_cost * variable("TRAIN_COST_SCALED"),
`2` = asc_sm + b_time_rnd * variable("SM_TT_SCALED") +
b_cost * variable("SM_COST_SCALED"),
`3` = asc_car + b_time_rnd * variable("CAR_TT_SCALED") +
b_cost * variable("CAR_CO_SCALED")
)
availability <- list(
`1` = variable("TRAIN_AV_SP"),
`2` = variable("SM_AV"),
`3` = variable("CAR_AV_SP")
)
# The observed-choice selector compiles to native models.logit(...,
# i=CHOICE). Monte Carlo integration is also a native expression node.
conditional_probability <- logit_probability(
utilities = utilities,
availability = availability,
alternative = variable("CHOICE")
)
log_likelihood <- log(monte_carlo(conditional_probability))
draws <- biogeme_draws(
name = "b_time_rnd",
draw_type = "UNIFORMSYM",
number_of_draws = number_of_draws,
seed = seed
)
biogeme_model(
database = database,
formula = log_likelihood,
draws = draws
)
}
# prepare_swissmetro_example() is defined in example_utils.R. It parses the
# command line, validates the data/Python paths, configures the bridge, reads
# the data, and creates a fresh output directory. The --data, --python,
# --output, --draws, and --seed options work from any current working
# directory.
prepared <- prepare_swissmetro_example(
commandArgs(trailingOnly = TRUE),
default_model = "b06a_unif_mixture"
)
number_of_draws <- if (!is.null(prepared$options$draws) && nzchar(prepared$options$draws)) {
example_integer(prepared$options$draws, "draws")
} else {
10000L
}
seed <- if (!is.null(prepared$options$seed) && nzchar(prepared$options$seed)) {
example_integer(prepared$options$seed, "seed")
} else {
1223L
}
# estimate() always performs fresh native estimation. Remove only exact b06a
# artifacts so an old YAML or iteration file cannot silently be reused.
stale_files <- c(
"b06a_unif_mixture.yaml",
"__b06a_unif_mixture.iter",
"b06a_unif_mixture.html"
)
stale_files <- file.path(prepared$output, stale_files)
stale_files <- stale_files[file.exists(stale_files)]
if (length(stale_files) > 0L) unlink(stale_files, force = TRUE)
database <- swissmetro_data(prepared$data)
model <- build_b06a_unif_mixture_model(
database,
number_of_draws = number_of_draws,
seed = seed
)
control <- biogeme_control(
output_directory = prepared$output,
model_name = "b06a_unif_mixture",
number_of_draws = number_of_draws,
seed = seed,
analytical_hessian_mode = "automatic",
generate_html = TRUE,
generate_yaml = FALSE,
save_iterations = FALSE
)
cat(sprintf("Number of draws: %s\n", format(number_of_draws, big.mark = "_")))
# The complete uniform-mixture expression graph is compiled once. Native
# Python Biogeme performs the Monte Carlo integration and estimation.
fit <- estimate(
model,
model_name = "b06a_unif_mixture",
control = control
)
print(summary(fit))
print(coef(fit))
invisible(fit)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.