Quick Examples
Basic Simulation (Manufacturing)
local
l_exp: MONTE_CARLO_EXPERIMENT
l_stats: SIMULATION_STATISTICS
do
create l_exp.make (100) -- 100 trials
l_exp.set_trial_logic (agent measure_shaft)
l_exp.run_simulation
l_stats := l_exp.statistics
print ("Mean: " + l_stats.mean.out + " mm%N")
print ("Std Dev: " + l_stats.std_dev.out + " mm%N")
print ("95% CI: [" + l_stats.ci_95.first.out + ", "
+ l_stats.ci_95.second.out + "]%N")
end
measure_shaft: TRIAL_OUTCOME
do
create Result.make
create {MEASUREMENT} m.make_real (25.0 + random_noise)
Result.add_measurement ("diameter", m)
end
MEASUREMENT - Type-Safe Values
local
m_real: MEASUREMENT
m_int: MEASUREMENT
do
create m_real.make_real (42.5)
create m_int.make_integer (100)
-- Type-safe conversion
print (m_real.as_integer) -- Truncates: 42
print (m_int.as_real) -- Promotes: 100.0
end
TRIAL_OUTCOME - Collect Results
local
outcome: TRIAL_OUTCOME
m1, m2: MEASUREMENT
do
create outcome.make
create m1.make_real (25.3)
outcome.add_measurement ("diameter", m1)
create m2.make_real (0.02)
outcome.add_measurement ("tolerance", m2)
-- Retrieve by name
if attached outcome.measurement_at ("diameter") as d then
print (d.as_real)
end
end
SIMULATION_STATISTICS - Results
local
stats: SIMULATION_STATISTICS
ci_95: TUPLE [REAL_64, REAL_64]
do
-- Typically obtained from MONTE_CARLO_EXPERIMENT
stats := l_exp.statistics
-- Access properties
print ("Mean: " + stats.mean.out + "%N")
print ("Std Dev: " + stats.std_dev.out + "%N")
print ("Min: " + stats.min.out + "%N")
print ("Max: " + stats.max.out + "%N")
print ("Sample Size: " + stats.sample_size.out + "%N")
-- Confidence intervals
ci_95 := stats.ci_95
ci_99 := stats.ci_99
-- Custom confidence level
if attached stats.confidence_interval (0.90) as ci_90 then
print ("90% CI: [" + ci_90.first.out + ", " + ci_90.second.out + "]%N")
end
end
Common Patterns
Pattern 1: Simple Single Measurement
trial_logic: TRIAL_OUTCOME
local m: MEASUREMENT
do
create Result.make
create m.make_real (random_value)
Result.add_measurement ("value", m)
end
Pattern 2: Multiple Measurements
trial_logic: TRIAL_OUTCOME
local m1, m2: MEASUREMENT
do
create Result.make
create m1.make_real (random_stock_return)
Result.add_measurement ("stock", m1)
create m2.make_real (random_bond_return)
Result.add_measurement ("bond", m2)
end
Pattern 3: Reproducible Results
l_exp.set_seed (42) -- Use same seed for reproducibility
l_exp.set_trial_logic (agent trial_logic)
l_exp.run_simulation