simple_montecarlo

Code Examples & Recipes

Recipe 1: Manufacturing Quality Control

-- Analyze shaft diameter measurements
measurement_analyzer
    local l_exp: MONTE_CARLO_EXPERIMENT
    do
        create l_exp.make (100)
        l_exp.set_trial_logic (agent measure_shaft)
        l_exp.run_simulation
        
        if attached l_exp.statistics as stats then
            print ("Shaft Diameter Analysis%N")
            print ("Mean: " + stats.mean.out + " mm%N")
            print ("Std Dev: " + stats.std_dev.out + "%N")
            print ("95% CI: [" + stats.ci_95.first.out + ", " 
                   + stats.ci_95.second.out + "]%N")
        end
    end

measure_shaft: TRIAL_OUTCOME
    local m: MEASUREMENT
    do
        create Result.make
        create m.make_real (25.0 + random_noise)
        Result.add_measurement ("diameter", m)
    end

Recipe 2: Portfolio Risk Analysis

-- Simulate portfolio outcomes
portfolio_simulator
    local l_exp: MONTE_CARLO_EXPERIMENT
    do
        create l_exp.make (10000)
        l_exp.set_seed (42)  -- Reproducible
        l_exp.set_trial_logic (agent simulate_market)
        l_exp.run_simulation
        
        if attached l_exp.statistics as stats then
            print ("Portfolio Analysis%N")
            print ("Expected Value: $" + stats.mean.out + "%N")
            print ("Best Case (95%): $" + stats.ci_95.second.out + "%N")
            print ("Worst Case (95%): $" + stats.ci_95.first.out + "%N")
        end
    end

simulate_market: TRIAL_OUTCOME
    local m_val: MEASUREMENT
    do
        create Result.make
        -- Simulate portfolio value under this scenario
        create m_val.make_real (compute_portfolio_value)
        Result.add_measurement ("portfolio_value", m_val)
    end

Recipe 3: Multi-Outcome Trials

-- Each trial produces multiple measurements
experiment_with_multiple_outcomes
    local l_exp: MONTE_CARLO_EXPERIMENT
    do
        create l_exp.make (1000)
        l_exp.set_trial_logic (agent multi_measurement_trial)
        l_exp.run_simulation
    end

multi_measurement_trial: TRIAL_OUTCOME
    local m1, m2, m3: MEASUREMENT
    do
        create Result.make
        create m1.make_real (random_normal (100, 15))
        Result.add_measurement ("measurement_1", m1)
        
        create m2.make_real (random_normal (50, 10))
        Result.add_measurement ("measurement_2", m2)
        
        create m3.make_real (random_exponential (2.0))
        Result.add_measurement ("measurement_3", m3)
    end

Recipe 4: Custom Confidence Level

-- Get 90% confidence interval
l_exp.run_simulation
if attached l_exp.statistics as stats then
    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

Recipe 5: Reproducible Simulation

-- Same seed produces identical results
l_exp.set_seed (42)
l_exp.set_trial_logic (agent trial_logic)
l_exp.run_simulation

-- Re-run with same seed, get identical statistics
create l_exp2.make (trial_count)
l_exp2.set_seed (42)  -- Same seed
l_exp2.set_trial_logic (agent trial_logic)
l_exp2.run_simulation

-- l_exp2.statistics == l_exp.statistics (identical)