knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(cramR) library(data.table) library(DT)
This vignette demonstrates the simulation capabilities included in the cramR package. The simulation code is primarily intended for reproducing experimental results from the associated theoretical papers and for validating the performance of the Cram method under controlled data-generating processes. While not intended for direct use in practical applications, these simulations allow users to benchmark and understand the empirical behavior of the method in synthetic environments.
cram_bandit_sim()
?The cram_bandit_sim()
function runs on-policy simulation for contextual bandit algorithms using the Cram method. It evaluates the statistical properties of policy value estimates such as:
This is useful for benchmarking bandit policies under controlled, simulated environments.
You need to provide:
bandit
:
A contextual bandit environment object that generates contexts (feature vectors) and rewards for each arm.
Example: ContextualLinearBandit
, or any object following the contextual package interface.
policy
:
A policy object that takes in a context and selects an arm (action) at each timestep.
Example: BatchContextualLinTSPolicy
, or any compatible contextual package policy.
horizon
:
An integer specifying the number of timesteps (rounds) per simulation.
Each simulation will run for exactly horizon
steps.
simulations
:
An integer specifying the number of independent Monte Carlo simulations to perform.
Each simulation will independently reset the environment and policy.
Optional Parameters:
alpha
:
A numeric value between 0 and 1 specifying the significance level for confidence intervals when calculating empirical coverage.
Default: 0.05
(for 95% confidence intervals).
seed
:
An optional integer to set the random seed for reproducibility.
If NULL
, no seed is set.
do_parallel
:
A logical value indicating whether to parallelize the simulations across available CPU cores.
Default: FALSE
(parallelization disabled).
We recommend keeping do_parallel = FALSE
unless necessary, as parallel execution can make it harder for the underlying contextual
package to reliably track simulation history.
In particular, parallel runs may cause missing or incomplete entries in the recorded history, which are then discarded during analysis.
# Number of time steps horizon <- 500L # Number of simulations simulations <- 100L # Number of arms k = 4 # Number of context features d= 3 # Reward beta parameters of linear model (the outcome generation models, one for each arm, are linear with arm-specific parameters betas) list_betas <- cramR::get_betas(simulations, d, k) # Define the contextual linear bandit, where sigma is the scale of the noise in the outcome linear model bandit <- cramR::ContextualLinearBandit$new(k = k, d = d, list_betas = list_betas, sigma = 0.3) # Define the policy object (choose between Contextual Epsilon Greedy, UCB Disjoint and Thompson Sampling) policy <- cramR::BatchContextualEpsilonGreedyPolicy$new(epsilon=0.1, batch_size=5) # policy <- cramR::BatchLinUCBDisjointPolicyEpsilon$new(alpha=1.0, epsilon=0.1, batch_size=1) # policy <- cramR::BatchContextualLinTSPolicy$new(v = 0.1, batch_size=1) sim <- cram_bandit_sim(horizon, simulations, bandit, policy, alpha=0.05, do_parallel = FALSE)
The output contains:
A data.table
with one row per simulation, including:
estimate
: estimated policy value variance_est
: estimated variance estimand
: true policy value (computed from held-out context data) prediction_error
: estimate - estimand
est_rel_error
: relative error on estimate variance_prediction_error
: relative error on variance ci_lower
, ci_upper
: bounds of the confidence interval std_error
: standard error Result tables (raw and interactive), reporting:
head(sim$estimates)
sim$interactive_table
list_betas
is updated internally to track the true parameters per simulation contextual
) even when do_parallel = FALSE
.This simulation builds on:
contextual
package) ```r autograph_files <- list.files(tempdir(), pattern = "^__autograph_generated_file.*\.py$", full.names = TRUE) if (length(autograph_files) > 0) { try(unlink(autograph_files, recursive = TRUE, force = TRUE), silent = TRUE) }
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.