Theil Fit Diagnostics
The script reports R squared, MAPE, and the split of the mean square
error1 (Sterman) into bias, variance and covariance. It
does this for each variable that is in both a saved run (the observed series)
and a job (a simulated series) of an Experimenter Output.csv. The script
adds a second view of the same fit to the RMSPE/R2/Theil-U report of
Calibration Fit. Its first argument is the output folder, not the saved run.
See “Where it runs” below.
The app bundles this script as Theil_Diagnostics.R and Theil_Diagnostics.py. Type the bare file name as a Script’s File, or pick it with Bundled scripts… on a Script row. A copy beside the Experiment file runs in place of the bundled one. See Run your own R or Python. Back to the Scripts.
| File | Needs |
|---|---|
Theil_Diagnostics.R | Rscript. Base R only, no package |
Theil_Diagnostics.py | python3, pandas and numpy. Install them with pip install pandas numpy |
The two agree to 1e-9 on the same input. The test suite checks this on every fixture.
Requirements
| Language | Needs |
|---|---|
| Python | Python 3.11 or later, with pandas and numpy. Install them with pip install pandas numpy. |
| R | R 4.3 or later. No package. |
The test suite runs on these versions in GitHub Actions. Older versions are not tested.
What It Computes
For each variable that is in both series, over the times the two share:
R squared against the observed series. 1 is a perfect fit. The value goes negative when the model tracks the data worse than the mean of the data does.
MAPE, the mean absolute percentage error, in percent. A time where the observed value is zero or missing does not contribute.
The split of the mean square error into three proportions that sum to 11, 2 (Sterman):
| Column | The Error Is | What It Indicates |
|---|---|---|
Bias_Proportion | The two means differ | Systematic. The model runs high or low |
Variance_Proportion | The two amplitudes differ | Systematic. The trend or the spread is wrong |
Covariance_Proportion | The point by point timing differs | Unsystematic. Tolerable when the model tracks the trend |
These are the same quantities that the Theil_Um, Theil_Us and Theil_Uc
columns of Calibration Fit carry. The names differ, so a row from
Calibration_Fit_<dataset>.csv and a row from
Theil_Diagnostics_<dataset>.csv sit side by side with no column-name
collision. This script does not repeat Theil_U of Calibration Fit, the
composite inequality coefficient.
The results go to Theil_Diagnostics_<dataset>.csv beside Output.csv, one
row per variable. There is no combined file across datasets and no summary
row. Each row stands on its own.
Naming the saved run
The source_kind column says which rows are which, and a name never does.
Saved-run rows hold the observed series. Job rows hold the simulated series.
Among several jobs the script keeps the longest series per variable. Job
name breaks a tie.
EXPERIMENTER_COMPARISON_RUN is optional. Experimenter sets it on every
Script run. The value names a saved run only when the scope has exactly one
comparison candidate. Otherwise it stays empty, inside a Sensitivity scope
for example.
- One saved run in the file and no name set: the script uses that run. It assumes no name and prints no fallback note.
- Several saved runs: the variable selects one.
- Several saved runs and no name set: the script stops.
- A name that is not a saved run stops the script, even when the file holds one saved run. A job name is one example.
Unlike Calibration Fit, a command-line argument never names the saved run here. The first argument is the output folder, the convention every other script in this library uses.
Where it runs
With no argument, the output folder comes from EXPERIMENTER_OUTPUT_DIR and
the time column from EXPERIMENTER_TIME_COLUMN. This is the shape that an
Experimenter Script step runs in. You can also give the folder of a finished
batch by hand as the first command-line argument. Then the script finds the
time column by elimination against the column set that Experimenter
publishes. The argument wins when you give both.
Rscript Theil_Diagnostics.R /path/to/Outputs/MyBatchWhat It Prints
One summary line. It holds the dataset, the variable count, and the mean R2, MAPE and three proportions across the variables. Each of these cases stops the script with a message that names the problem:
- A missing output folder, a folder that does not exist, or a missing
Output.csv. - More than one dataset with none selected.
- A
samplecolumn (a Sensitivity batch, not one run against one observed series). - A file that lacks
source,source_kind,variableorvalue. A file with aruncolumn and nosourceis an older layout, and the script does not read it. - An unknown column, or a
source_kindother thanjoborsaved_run. - No
saved_runrow, or nojobrow. - Several saved runs with no name selected, or a named run that is not a saved run in the file.
- No variable shared between the two series.
In a Chain
The script reads Output.csv and writes Theil_Diagnostics_<dataset>.csv
into the same folder. To get one CSV for every dataset of a sweep, run
Collect_Datasets --stem Theil_Diagnostics after it. See the chaining
rules.
Version Policy
The library README sets the format policy. The header of this file names the Experimenter version that it targets. The script checks every column before it computes anything. An unknown column stops the script with a message that names it. A version difference prints one line and does not stop the run.
References
- Sterman, J.D. (1984). Appropriate summary statistics for evaluating the historical fit of system dynamics models. Dynamica, 10 (Winter), 51-66. No DOI. Listed on the MIT publication page of Sterman. Defines the split of the mean square error into bias, variance and covariance that this script implements.
- Theil, H. (1966). Applied Economic Forecasting. North-Holland. No DOI. WorldCat OCLC 170823. Sterman (1984) calls the three proportions “the Theil inequality statistics” and credits Theil for them.