Skip to content

How do I calibrate a model to data?

In short

  • You build: a script that tunes the service rate of a one-server queue until the model's mean wait matches a measured 3.2 minutes.
  • You need: Python 3.11 or later, pip install nagasim, and the replications tutorial.
  • Time: about ten minutes.

A model is credible when it reproduces what the real system did. By the end you give a calibration one calibration target and one parameter to tune. Then you read how well the tuned model matches, and ask ABC which values the data allow.

This page has three parts: Step by step, Check yourself and If stuck. The last section lists the reference pages for the elements used.

Step by step

  1. Save this script as goal-calibrate.py. The model starts with a wrong guess of 1.0 for the service rate. The real value, which the script does not know, is 1.25 per minute, so a mean wait of 3.2 minutes is what the real system would show.
from nagasim import build as nb, calibrate, optimize

# The model starts with a guess for the service rate. Calibration will correct it.
m = nb.Model(time_limit=5000, warmup=500)
m.add_resource(name="Server")
arrive = m.create(distribution=nb.Exponential(rate=1.0))         # one customer a minute
serve = m.process(resource_name="Server", distribution=nb.Exponential(rate=1.0))
arrive >> serve >> m.dispose()

# What you measured on the real system: customers waited 3.2 minutes on average. Accept an answer
# within 0.6 minutes of that.
targets = [{"kpi": "resource/Server/Time in Queue Avg", "observed": 3.2, "tolerance": 0.6}]
# What may move: the service rate, anywhere from 0.9 to 2.0 customers a minute.
rate = optimize.Factor(path=f"blocks.{serve.name}.params.dist_params.rate", kind="continuous",
                       low=0.9, high=2.0, name="service rate")

fit = calibrate.calibrate(m.to_dict(), targets, [rate], budget=80, replications_per_point=5, seed=1)
print(f"service rate: {fit.best['service rate']:.2f} per minute, {fit.budget_used} replications used")
row = fit.table.iloc[0]
print(f"simulated wait {row['Simulated']:.2f} minutes against {row['Observed']:.1f} observed, pass: {fit.passed}")

# Keep the tuned value as a scenario of the model.
tuned = fit.as_scenario()["overrides"][0]
print(f"scenario: {tuned['path']} = {tuned['value']:.2f}")

# Which values are consistent with the data? Rejection ABC returns an interval for each factor.
abc = calibrate.calibrate(m.to_dict(), targets, [rate], method="abc", budget=200,
                          replications_per_point=1, seed=1)
low, high = abc.posterior.iloc[0][["P5", "P95"]]
print(f"90% interval for the service rate: {low:.2f} to {high:.2f}")
  1. Run it.
python goal-calibrate.py

You should see this. The script seeds itself, so the numbers are the same on your machine.

service rate: 1.26 per minute, 80 replications used
simulated wait 3.41 minutes against 3.2 observed, pass: True
scenario: blocks.process_1.params.dist_params.rate = 1.26
90% interval for the service rate: 1.19 to 1.36
  1. Read the first line. calibrate searches the service rate between 0.9 and 2.0 and spends at most 80 replications. It found 1.26, close to the real 1.25.

  2. Read the second line. It compares the tuned model with your target. A target with a tolerance passes when the simulated mean is within that distance of the observed value. This is a verdict on the mean, so check the confidence interval in fit.table too, because a wide interval makes a pass weak.

  3. Read the third line. fit.as_scenario() turns the tuned value into a scenario with an override path. Add it to the model to keep the result.

  4. Read the last line. method="abc" draws many parameter values and keeps the ones whose results lie nearest the target. The result is an interval, not one number. Here the data allow a service rate from 1.19 to 1.36, so the single best value hides how loosely one mean wait pins the rate.

Check yourself

  • Predict, then run: if you add a second target, observed utilisation of 80 per cent for resource/Server/Usage % (snapshot), will the ABC interval get wider or narrower? Try it.
  • Explain it back: why does the tuned wait differ a little from 3.2 even though the rate is almost exact?

Done when

  • [ ] The script prints a tuned rate, a pass or fail line, a scenario and an interval.
  • [ ] You can say what the interval means that the best value alone does not.
  • [ ] You can say why one target rarely pins down more than one parameter.

If stuck

What happens Likely cause Fix
pass: False although the rate is close The simulated mean is noisy at 5 replications, so it can land outside the tolerance Raise replications_per_point or run_length, or widen tolerance to what you can measure.
The best value sits at 0.9 or 2.0 The true value is outside the range, or the target cannot be reached Widen the factor range, and check that the observed number comes from the same unit and period as the KPI.
ValueError naming a KPI The KPI key is misspelt The message lists close matches. The KPI reference has every key.
The ABC interval fills the whole range One target says little about this parameter Add targets of other kinds, such as utilisation, or a sample to match as a distribution.

Elements this page uses

Next: Connect live data end to end.