Skip to content

openavmkit.model_runner

Model-run orchestration (the high-level coordinator that drives modeling).

Runs the full configured model menu (MRA, GWR, XGBoost, LightGBM, CatBoost, NGBoost, layered comps, kernel regression, ensembles, and several "naive" baselines) across each model group, with optional variable-importance experiments, then compares model outputs (the "benchmark" comparison) and produces ensemble predictions.

This module is the high-level coordinator that drives :mod:openavmkit.modeling. The main entry points (run_models, try_variables, try_models, finalize_models, ensemble runners) are exposed as wrappers in :mod:openavmkit.pipeline.

History

Formerly named openavmkit.benchmark; renamed to openavmkit.model_runner because the module orchestrates the whole model run, not only the benchmark comparison (and to avoid confusion with the research benchmark/ harness). A deprecating compatibility shim remains at openavmkit.benchmark and is slated for removal in the 0.8.0 release.

Notes

The list of models to run for each main/vacant stage is configured in settings.json under modeling.instructions.<stage>.run. Per-model-group skip lists live under modeling.instructions.<stage>.skip.<model_group>.

BenchmarkResults

BenchmarkResults(df_time, df_stats_test, df_stats_test_post_val, df_stats_full, assessor_in_test=True)

Container for benchmark results.

Attributes:

Name Type Description
df_time DataFrame

DataFrame containing timing information.

df_stats_test DataFrame

DataFrame with statistics for the test set.

df_stats_test_post_val DataFrame

DataFrame with statistics for the test set (post-valuation-date only).

df_stats_full DataFrame

DataFrame with statistics for the full universe.

test_empty bool

Whether df_stats_test contains no records

full_empty bool

Whether df_stats_full contains no records

test_post_val_empty bool

Whether df_stats_test_post_val contains no records

Initialize a BenchmarkResults instance.

Parameters:

Name Type Description Default
df_time DataFrame

DataFrame containing timing data.

required
df_stats_test DataFrame

DataFrame with test set statistics.

required
df_stats_test_post_val DataFrame

DataFrame with test set (post-valuation-date only) statistics.

required
df_stats_full DataFrame

DataFrame with full universe statistics.

required
Source code in openavmkit/model_runner.py
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def __init__(
    self,
    df_time: pd.DataFrame,
    df_stats_test: pd.DataFrame,
    df_stats_test_post_val: pd.DataFrame,
    df_stats_full: pd.DataFrame,
    assessor_in_test: bool = True,
):
    """
    Initialize a BenchmarkResults instance.

    Parameters
    ----------
    df_time : pandas.DataFrame
        DataFrame containing timing data.
    df_stats_test : pandas.DataFrame
        DataFrame with test set statistics.
    df_stats_test_post_val : pandas.DataFrame
        DataFrame with test set (post-valuation-date only) statistics.
    df_stats_full : pandas.DataFrame
        DataFrame with full universe statistics.
    """
    self.df_time = df_time
    self.df_stats_test = df_stats_test
    self.df_stats_test_post_val = df_stats_test_post_val
    self.df_stats_full = df_stats_full

    test_empty = False == (df_stats_test["count_sales"].sum() > 0)
    full_empty = False == (df_stats_full["count_sales"].sum() > 0)

    if df_stats_test_post_val is not None:
        test_post_val_empty = False == (df_stats_test_post_val["count_sales"].sum() > 0)
    else:
        test_post_val_empty = True

    self.test_empty = test_empty
    self.full_empty = full_empty
    self.test_post_val_empty = test_post_val_empty
    self.assessor_in_test = assessor_in_test

print

print()

Return a formatted string summarizing the benchmark results.

Returns:

Type Description
str

A string that includes timings, test set stats, and universe set stats.

Source code in openavmkit/model_runner.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def print(self) -> str:
    """
    Return a formatted string summarizing the benchmark results.

    Returns
    -------
    str
        A string that includes timings, test set stats, and universe set stats.
    """
    result = "Timings:\n"
    result += _format_benchmark_df(self.df_time)
    result += "\n\n"
    if (
        self.df_stats_test_post_val is not None
        and not self.test_post_val_empty
    ):
        result += "Holdout set (post-valuation-date only):\n"
        result += (
            "  (Like-for-like vs. the assessor: these sales postdate the valuation date,\n"
            "   so they are out-of-sample for both -- as long as valuation_date is aligned\n"
            "   with the assessor's roll-close date.)\n"
        )
        result += _format_benchmark_df(self.df_stats_test_post_val)
        result += "\n\n"
    result += "Holdout set:\n"
    if self.assessor_in_test:
        result += (
            "  (Assessor shown here because you've declared its values honor this same\n"
            "   holdout (analysis.ratio_study.assessor_holdout: shared). Otherwise it is\n"
            "   left off, since the holdout status of values we didn't generate is unknown.)\n"
        )
    else:
        result += (
            "  (Assessor not shown here: this is a random pre-valuation holdout we draw\n"
            "   ourselves. Our figures are out-of-sample, but we can't know whether values\n"
            "   we didn't generate were held out the same way, so the comparison wouldn't be\n"
            "   like-for-like. If you are the assessor and know the holdout status, see\n"
            "   analysis.ratio_study.assessor_holdout.)\n"
        )
    result += _format_benchmark_df(self.df_stats_test)
    result += "\n\n"
    result += "Study set:\n"
    result += (
        "  (Assessor shown as an audit of the finished roll over all sales -- the standard\n"
        "   IAAO frame, not a predictive holdout. See the sales-chasing check in the ratio\n"
        "   study report for context on interpreting a very tight assessor result.)\n"
    )
    result += _format_benchmark_df(self.df_stats_full)
    result += "\n\n"
    return result

MultiModelResults

MultiModelResults(model_results, benchmark, df_univ, df_sales, drop_assessor_from_test=False, outside=())

Container for results from multiple models along with a benchmark.

Attributes: model_results (dict[str, SingleModelResults]): Dictionary mapping model names to their results. benchmark (BenchmarkResults): Benchmark results computed from the model results.

Initialize a MultiModelResults instance.

Parameters:

Name Type Description Default
model_results dict[str, SingleModelResults]

Dictionary of individual model results.

required
benchmark BenchmarkResults

Benchmark results.

required
drop_assessor_from_test bool

Whether the assessor should be left off the pre-valuation "Test set". Stored so that add_model (which recomputes the benchmark, e.g. when the ensemble is added) preserves the same choice as the initial _calc_benchmark call.

False
outside tuple

Models whose values were made outside openavmkit: left off the "Test set" with the assessor.

()
Source code in openavmkit/model_runner.py
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
def __init__(
    self, model_results: dict[str, SingleModelResults], benchmark: BenchmarkResults, df_univ: pd.DataFrame, df_sales: pd.DataFrame, drop_assessor_from_test: bool = False, outside: tuple = ()
):
    """Initialize a MultiModelResults instance.

    Parameters
    ----------
    model_results: dict[str, SingleModelResults]
        Dictionary of individual model results.
    benchmark: BenchmarkResults
        Benchmark results.
    drop_assessor_from_test: bool
        Whether the assessor should be left off the pre-valuation "Test set". Stored so
        that ``add_model`` (which recomputes the benchmark, e.g. when the ensemble is
        added) preserves the same choice as the initial ``_calc_benchmark`` call.
    outside: tuple
        Models whose values were made outside openavmkit: left off the "Test set" with the assessor.
    """
    self.model_results = model_results
    self.benchmark = benchmark
    self.df_univ_orig = df_univ
    self.df_sales_orig = df_sales
    self.drop_assessor_from_test = drop_assessor_from_test
    self.outside = tuple(outside)

add_model

add_model(model, results)

Add a new model's results and update the benchmark.

Parameters:

Name Type Description Default
model str

The model name.

required
results SingleModelResults

The results for the given model.

required
Source code in openavmkit/model_runner.py
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
def add_model(self, model: str, results: SingleModelResults):
    """Add a new model's results and update the benchmark.

    Parameters
    ----------
    model: str
        The model name.
    results: SingleModelResults
        The results for the given model.
    """
    self.model_results[model] = results
    # Recalculate the benchmark based on updated model results. Preserve the assessor
    # drop choice -- otherwise adding the ensemble model would silently re-introduce the
    # assessor into the Test-set comparison.
    self.benchmark = _calc_benchmark(
        self.model_results, drop_assessor_from_test=self.drop_assessor_from_test, outside=self.outside
    )

academic_metrics

academic_metrics(all_results, max_trim)

The "academic" figures for each model on its test set: untrimmed, then trimmed.

Each frame has a row per model (indexed by model name) and the columns count, MAPE, MSE, RMSE, m.ratio (median ratio), avg.ratio (mean ratio), VEI, VEI_sig and Slope (of prediction on price, through the origin), as numbers. The benchmark printout formats them as text.

Parameters:

Name Type Description Default
all_results MultiModelResults

One model group's results.

required
max_trim float

The ratio study's trim, as get_max_ratio_study_trim gives it.

required
Source code in openavmkit/model_runner.py
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
def academic_metrics(
    all_results: MultiModelResults, max_trim: float
) -> tuple[pd.DataFrame, pd.DataFrame]:
    """
    The "academic" figures for each model on its test set: untrimmed, then trimmed.

    Each frame has a row per model (indexed by model name) and the columns ``count``,
    ``MAPE``, ``MSE``, ``RMSE``, ``m.ratio`` (median ratio), ``avg.ratio`` (mean ratio),
    ``VEI``, ``VEI_sig`` and ``Slope`` (of prediction on price, through the origin), as
    numbers. The benchmark printout formats them as text.

    Parameters
    ----------
    all_results : MultiModelResults
        One model group's results.
    max_trim : float
        The ratio study's trim, as ``get_max_ratio_study_trim`` gives it.
    """
    metrics_data = {
        "Model": [],
        "count": [],
        "RMSE": [],
        "MSE": [],
        "MAPE": [],
        "m.ratio": [],
        "avg.ratio": [],
        "VEI": [],
        "VEI_sig": [],
        "Slope": []
    }
    trimmed_data = {
        "Model": [],
        "count": [],
        "RMSE": [],
        "MSE": [],
        "MAPE": [],
        "Slope": [],
        "m.ratio": [],
        "avg.ratio": [],
        "VEI": [],
        "VEI_sig": [],
    }

    for model_name, model_result in all_results.model_results.items():

        df_test = model_result.df_test.copy()

        df_test["y_pred"] = model_result.pred_test.y_pred
        df_test["y_true"] = model_result.pred_test.y

        # Note any NA predictions:
        if df_test["y_pred"].isna().any():
            mask_na = df_test["y_pred"].isna()
            count_na = mask_na.count()
            print(f"WARNING: y_pred has {count_na} NaN values!")
            df_test = df_test[~mask_na]

        # Get test set predictions and actual values
        y_pred = df_test["y_pred"].to_numpy()
        y_true = df_test["y_true"].to_numpy()

        y_true = y_true.astype(np.float64)
        y_pred = y_pred.astype(np.float64)

        y_ratio = y_pred / y_true
        mask = trim_outliers_mask(y_ratio, max_trim)

        if len(mask) == 0:
            y_true_trim = y_true
            y_pred_trim = y_pred
        else:
            y_true_trim = y_true[mask]
            y_pred_trim = y_pred[mask]

        if len(y_true) > 1 and len(y_pred) > 1:
            # MAPE calculation
            mape = mean_absolute_percentage_error(y_true, y_pred)

            # OLS R² calculation
            reg = _simple_ols(df_test, "y_true", "y_pred", intercept=False)
            slope, r2_0 = reg["slope"], reg["r2"]

            # MSE 
            mse = calc_mse(y_pred, y_true)
            rmse = np.sqrt(mse)
        else:
            slope = np.nan
            mse = np.nan
            rmse = np.nan
            mape = np.nan

        if len(y_true_trim) > 1 and len(y_pred_trim) > 1:
            # MAPE calculation
            mape_trim = mean_absolute_percentage_error(y_true_trim, y_pred_trim)

            # OLS R² calculation
            df_trim = pd.DataFrame(data={"y_true":y_true_trim,"y_pred":y_pred_trim})
            reg = _simple_ols(df_trim, "y_true", "y_pred", intercept=False)
            slope_trim, r2_trim = reg["slope"], reg["r2"]

            mse_trim = calc_mse(y_pred_trim, y_true_trim)
            # Keep this in rmse_trim. Writing it to `rmse` clobbered the untrimmed value
            # computed above, so the untrimmed table then reported the TRIMMED RMSE
            # alongside the untrimmed MSE -- neither table's RMSE was sqrt(its own MSE).
            rmse_trim = np.sqrt(mse_trim)
        else:
            slope_trim = np.nan
            mape_trim = np.nan
            mse_trim = np.nan
            rmse_trim = np.nan

        count = len(y_true)
        count_trim = len(y_true_trim)

        metrics_data["Model"].append(model_name)
        metrics_data["count"].append(count)
        metrics_data["MAPE"].append(mape)
        metrics_data["MSE"].append(mse)
        metrics_data["RMSE"].append(rmse)
        metrics_data["m.ratio"].append(model_result.pred_test.ratio_study.median_ratio)
        metrics_data["avg.ratio"].append(model_result.pred_test.ratio_study.mean_ratio)
        metrics_data["VEI"].append(model_result.ve_test["vei"])
        metrics_data["VEI_sig"].append(model_result.ve_test["vei_significance"])
        metrics_data["Slope"].append(slope)

        trimmed_data["Model"].append(model_name)
        trimmed_data["count"].append(count_trim)
        trimmed_data["MAPE"].append(mape_trim)
        trimmed_data["MSE"].append(mse_trim)
        trimmed_data["RMSE"].append(rmse_trim)
        trimmed_data["m.ratio"].append(model_result.pred_test.ratio_study.median_ratio_trim)
        trimmed_data["avg.ratio"].append(model_result.pred_test.ratio_study.mean_ratio_trim)

        # The trimmed vertical-equity score, computed on the same rows the other
        # trimmed statistics in this table use.
        ve_trim = getattr(model_result, "ve_test_trim", None) or model_result.ve_test
        trimmed_data["VEI"].append(ve_trim["vei"])
        trimmed_data["VEI_sig"].append(ve_trim["vei_significance"])
        trimmed_data["Slope"].append(slope_trim)

    columns = ["count", "MAPE", "MSE", "RMSE", "m.ratio", "avg.ratio", "VEI", "VEI_sig", "Slope"]
    metrics_df = pd.DataFrame(metrics_data).set_index("Model")[columns]
    trimmed_df = pd.DataFrame(trimmed_data).set_index("Model")[columns]
    return metrics_df, trimmed_df

calc_df_mape

calc_df_mape(df, field_prediction, settings, dep_var, is_land_predictions=False)

Calculate MAPE

Parameters:

Name Type Description Default
df DataFrame

Dataframe you want to calculate MAPE for

required
field_prediction str

The field name for predictions.

required
settings dict

Settings dictionary

required
dep_var str

The field you're trying to predict

required
is_land_predictions bool

Are you predicting land values or not. If true, uses the valid_for_land_ratio_study validity flag.

False
Source code in openavmkit/model_runner.py
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
def calc_df_mape(
    df: pd.DataFrame,
    field_prediction: str,
    settings: dict,
    dep_var: str,
    is_land_predictions: bool = False
):
    """
    Calculate MAPE 

    Parameters
    ----------
    df : pd.DataFrame
        Dataframe you want to calculate MAPE for
    field_prediction : str
        The field name for predictions.
    settings : dict
        Settings dictionary
    dep_var: str
        The field you're trying to predict
    is_land_predictions: bool
        Are you predicting land values or not. If true, uses the `valid_for_land_ratio_study` validity flag.
    """

    # Clean arrays
    y = df[dep_var].to_numpy()
    df[dep_var] = pd.to_numeric(df[dep_var], errors="coerce")
    df[field_prediction] = pd.to_numeric(df[field_prediction], errors="coerce")

    # Get validity field
    valid_field = "valid_for_ratio_study"
    if is_land_predictions:
        valid_field = "valid_for_land_ratio_study"

    # select only values that are not NaN in either and are valid for ratio study:
    df_clean = df[
        df[valid_field] & 
        ~pd.isna(df[dep_var]) & 
        ~pd.isna(df[field_prediction])
    ]

    # Get y & y_pred
    y = df_clean[dep_var].to_numpy()
    y_pred = df_clean[field_prediction].to_numpy()

    # Calculate MAPE
    if len(y) > 0 and len(y_pred) > 0:
        return mean_absolute_percentage_error(y, y_pred)

    return float("nan")

generate_variable_report

generate_variable_report(report, settings, model_group, best_variables)

Generate a variable selection report.

This function updates the MarkdownReport with various threshold values, weights, and summary tables based on the best variables.

Parameters:

Name Type Description Default
report MarkdownReport

The markdown report object.

required
settings dict

The settings dictionary.

required
model_group str

The model group identifier.

required
best_variables list[str]

List of selected best variables.

required

Returns:

Type Description
MarkdownReport

The updated markdown report.

Source code in openavmkit/model_runner.py
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
def generate_variable_report(
    report: MarkdownReport, settings: dict, model_group: str, best_variables: list[str]
):
    """
    Generate a variable selection report.

    This function updates the MarkdownReport with various threshold values, weights, and
    summary tables based on the best variables.

    Parameters
    ----------
    report : MarkdownReport
        The markdown report object.
    settings : dict
        The settings dictionary.
    model_group : str
        The model group identifier.
    best_variables : list[str]
        List of selected best variables.

    Returns
    -------
    MarkdownReport
        The updated markdown report.
    """
    locality = settings.get("locality", {})
    report.set_var("locality", locality.get("name", "...LOCALITY..."))

    mg = get_model_group(settings, model_group)
    report.set_var("val_date", get_valuation_date(settings).strftime("%Y-%m-%d"))
    report.set_var("model_group", mg.get("name", mg))

    instructions = settings.get("modeling", {}).get("instructions", {})
    feature_selection = instructions.get("feature_selection", {})
    thresh = feature_selection.get("thresholds", {})

    report.set_var("thresh_correlation", thresh.get("correlation", ".2f"))
    report.set_var("thresh_enr_coef", thresh.get("enr_coef", ".2f"))
    report.set_var("thresh_vif", thresh.get("vif", ".2f"))
    report.set_var("thresh_p_value", thresh.get("p_value", ".2f"))
    report.set_var("thresh_t_value", thresh.get("t_value", ".2f"))
    report.set_var("thresh_adj_r2", thresh.get("adj_r2", ".2f"))

    weights = feature_selection.get("weights", {})
    df_weights = pd.DataFrame(weights.items(), columns=["Statistic", "Weight"])
    df_weights["Statistic"] = df_weights["Statistic"].map(
        {
            "vif": "VIF",
            "p_value": "P-value",
            "t_value": "T-value",
            "corr_score": "Correlation",
            "enr_coef": "ENR",
            "coef_sign": "Coef. sign",
            "adj_r2": "R-squared",
        }
    )
    df_weights.set_index("Statistic", inplace=True)
    report.set_var("pre_model_weights", df_weights.to_markdown())

    # TODO: Construct summary and post-model tables as needed.
    post_model_table = "...POST MODEL TABLE..."
    report.set_var("post_model_table", post_model_table)

    return report

get_data_split_for

get_data_split_for(model_name, model_engine, model_entry, model_group, location_fields, ind_vars, df_sales, df_universe, settings, dep_var, dep_var_test, fields_cat, interactions, test_keys, train_keys, vacant_only)

Prepare a DataSplit object for a given model.

Parameters:

Name Type Description Default
model_name str

Model unique identifier

required
model_engine str

Model engine ("xgboost", "mra", etc.)

required
model_entry dict

Model parameters

required
model_group str

The model group identifier.

required
location_fields list[str] or None

List of location fields.

required
ind_vars list[str]

List of independent variables.

required
df_sales DataFrame

Sales DataFrame.

required
df_universe DataFrame

Universe DataFrame.

required
settings dict

The settings dictionary.

required
dep_var str

Dependent variable for training.

required
dep_var_test str

Dependent variable for testing.

required
fields_cat list[str]

List of categorical fields.

required
interactions dict

Dictionary of variable interactions.

required
test_keys list[str]

Keys for test split.

required
train_keys list[str]

Keys for training split.

required
vacant_only bool

Whether to consider only vacant sales.

required

Returns:

Type Description
DataSplit

A DataSplit object.

Source code in openavmkit/model_runner.py
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
def get_data_split_for(
    model_name: str,
    model_engine: str,
    model_entry: dict,
    model_group: str,
    location_fields: list[str] | None,
    ind_vars: list[str],
    df_sales: pd.DataFrame,
    df_universe: pd.DataFrame,
    settings: dict,
    dep_var: str,
    dep_var_test: str,
    fields_cat: list[str],
    interactions: dict,
    test_keys: list[str],
    train_keys: list[str],
    vacant_only: bool,
):
    """
    Prepare a DataSplit object for a given model.

    Parameters
    ----------
    model_name: str,
        Model unique identifier
    model_engine : str
        Model engine ("xgboost", "mra", etc.)
    model_entry : dict
        Model parameters
    model_group : str
        The model group identifier.
    location_fields : list[str] or None
        List of location fields.
    ind_vars : list[str]
        List of independent variables.
    df_sales : pandas.DataFrame
        Sales DataFrame.
    df_universe : pandas.DataFrame
        Universe DataFrame.
    settings : dict
        The settings dictionary.
    dep_var : str
        Dependent variable for training.
    dep_var_test : str
        Dependent variable for testing.
    fields_cat : list[str]
        List of categorical fields.
    interactions : dict
        Dictionary of variable interactions.
    test_keys : list[str]
        Keys for test split.
    train_keys : list[str]
        Keys for training split.
    vacant_only : bool
        Whether to consider only vacant sales.

    Returns
    -------
    DataSplit
        A DataSplit object.
    """

    unit = area_unit(settings)
    lenunit = length_unit(settings)

    if model_engine == "local_area":
        _ind_vars = location_fields + [f"bldg_area_finished_{unit}", f"land_area_{unit}"]
    elif model_engine == "multi_mra":
        _ind_vars = [v for v in ind_vars if v not in location_fields]
    elif model_engine == "assessor":
        _ind_vars = ["assr_market_value"]
    elif model_engine == "pass_through":
        field = model_entry.get("field")
        if field is None:
            raise ValueError("pass_through model \"{model_name}\" has no .field parameter!")
        _ind_vars = [field]
    elif model_engine == "ground_truth":
        _ind_vars = ["true_market_value"]
    elif model_engine == "spatial_lag":
        sale_field = get_sale_field(settings)
        field = f"spatial_lag_{sale_field}"
        if vacant_only:
            field = f"{field}_vacant"
        _require_spatial_lag(model_name, df_universe, {field: "price_vacant" if vacant_only else "price"})
        _ind_vars = [field]
    elif model_engine == "spatial_lag_area":
        sale_field = get_sale_field(settings)
        _require_spatial_lag(
            model_name,
            df_universe,
            {
                f"spatial_lag_{sale_field}_impr_{unit}": "price_impr",
                f"spatial_lag_{sale_field}_land_{unit}": "price_land",
            },
        )
        _ind_vars = [
            f"spatial_lag_{sale_field}_impr_{unit}",
            f"spatial_lag_{sale_field}_land_{unit}",
            f"bldg_area_finished_{unit}",
            f"land_area_{unit}",
        ]
    elif model_engine == "catboost":
        df_sales = _clean_categoricals(df_sales, fields_cat, settings)
        df_universe = _clean_categoricals(df_universe, fields_cat, settings)
        _ind_vars = ind_vars
    elif model_engine == "lcomp":
        _ind_vars = ind_vars
    else:
        _ind_vars = ind_vars
        if model_engine == "gwr" or model_engine == "kernel":
            exclude_vars = ["latitude", "longitude", "latitude_norm", "longitude_norm"]
            _ind_vars = [var for var in _ind_vars if var not in exclude_vars]

    _validate_ind_vars_across_frames(
        _ind_vars, df_sales, df_universe, fields_cat, model_name, model_group
    )

    return DataSplit(
        model_name,
        df_sales,
        df_universe,
        model_group,
        settings,
        dep_var,
        dep_var_test,
        _ind_vars,
        fields_cat,
        interactions,
        test_keys,
        train_keys,
        vacant_only=vacant_only,
    )

get_variable_recommendations

get_variable_recommendations(df_sales, df_universe, vacant_only, settings, model_group, variables_to_use=None, tests_to_run=None, do_cross=True, do_report=False, do_plots=False, verbose=False, t=None, return_tests=False)

Determine which variables are most likely to be meaningful in a model.

This function examines sales and universe data, applies feature selection via correlations, elastic net regularization, R², p-values, t-values, and VIF, and produces a set of recommended variables along with a written report.

Parameters:

Name Type Description Default
df_sales DataFrame

The sales data.

required
df_universe DataFrame

The parcel universe data.

required
vacant_only bool

Whether to consider only vacant sales.

required
settings dict

The settings dictionary.

required
model_group str

The model group to consider.

required
variables_to_use list[str] or None

A list of variables to use for feature selection. If None, variables are pulled from modeling section

None
tests_to_run list[str] or None

A list of tests to run. If None, all tests are run. Legal values are "corr", "r2", "p_value", "t_value", "enr", and "vif"

None
do_report bool

If True, generates a report of the variable selection process.

False
do_plots bool

If True, prints correlation plots

False
verbose bool

If True, prints additional debugging information.

False
t TimingData or None

TimingData object

None
return_tests bool

If True, the result also has "tests": each test's results for every variable, before and after its drops (initial / final where the test drops recursively), with the thresholds and which one-hot columns each categorical became. For a front end that shows the tests one by one. Defaults to False.

False

Returns:

Type Description
dict

A dictionary with keys "variables" (the best variables list), "report" (the generated report) and "df_results" (each variable's scores); and "tests" when return_tests is True.

Source code in openavmkit/model_runner.py
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
def get_variable_recommendations(
    df_sales: pd.DataFrame,
    df_universe: pd.DataFrame,
    vacant_only: bool,
    settings: dict,
    model_group: str,
    variables_to_use: list[str] | None = None,
    tests_to_run: list[str] | None = None,
    do_cross: bool = True,
    do_report: bool = False,
    do_plots: bool = False,
    verbose: bool = False,
    t: TimingData = None,
    return_tests: bool = False,
) -> dict:
    """Determine which variables are most likely to be meaningful in a model.

    This function examines sales and universe data, applies feature selection via
    correlations, elastic net regularization, R², p-values, t-values, and VIF, and
    produces a set of recommended variables along with a written report.

    Parameters
    ----------
    df_sales : pandas.DataFrame
        The sales data.
    df_universe : pandas.DataFrame
        The parcel universe data.
    vacant_only : bool
        Whether to consider only vacant sales.
    settings : dict
        The settings dictionary.
    model_group : str
        The model group to consider.
    variables_to_use : list[str] or None
        A list of variables to use for feature selection. If None, variables are pulled
        from modeling section
    tests_to_run : list[str] or None
        A list of tests to run. If None, all tests are run. Legal values are "corr",
        "r2", "p_value", "t_value", "enr", and "vif"
    do_report : bool
        If True, generates a report of the variable selection process.
    do_plots: bool, optional
        If True, prints correlation plots
    verbose : bool, optional
        If True, prints additional debugging information.
    t : TimingData or None
        TimingData object
    return_tests : bool, optional
        If True, the result also has "tests": each test's results for every variable, before
        and after its drops (``initial`` / ``final`` where the test drops recursively), with
        the thresholds and which one-hot columns each categorical became. For a front end
        that shows the tests one by one. Defaults to False.

    Returns
    -------
    dict
        A dictionary with keys "variables" (the best variables list), "report" (the
        generated report) and "df_results" (each variable's scores); and "tests" when
        ``return_tests`` is True.
    """    
    if t is None:
        t = TimingData()

    t.start("variables.markdown")
    report: MarkdownReport = MarkdownReport("variables")
    t.stop("variables.markdown")
    if tests_to_run is None:
        tests_to_run: list[str] = ["corr", "r2", "p_value", "t_value", "enr", "vif"]

    if "sale_price_time_adj" not in df_sales.columns:
        warnings.warn("Time adjustment was not found in sales data. Calculating now...")
        t.start("variables.time_adjustment")
        df_sales = enrich_time_adjustment(df_sales, settings, write=False, verbose=verbose)
        t.stop("variables.time_adjustment")

    t.start("variables.stuff")
    settings_model = settings.get("modeling", {})
    vacant_status = "vacant" if vacant_only else "main"
    model_entries = settings_model.get("models", {}).get(vacant_status, {})
    model_entries = model_entries.get(model_group, model_entries)
    entry: dict | None = model_entries.get("model", model_entries.get("default", {}))
    if variables_to_use is None:
        variables_to_use: list | None = entry.get("ind_vars", None)

    if variables_to_use is None or len(variables_to_use) == 0:
        raise ValueError("No independent variables provided! Please define some!")

    categoricals = get_fields_categorical(settings, df_sales, include_boolean=False)

    flagged = []
    categoricals_to_use = [x for x in variables_to_use if x in categoricals]
    for variable in categoricals_to_use:
        if df_sales[variable].nunique() > 50:
            warnings.warn(
                f"Variable '{variable}' has more than 50 unique values. No variable analysis will be done on it and it will not be auto-dropped. Hope you know what you're doing!"
            )
            flagged.append(variable)

    if len(flagged) > 0:
        variables_to_use = [
            variable for variable in variables_to_use if variable not in flagged
        ]

    # Check for duplicate variables in variables_to_use
    if variables_to_use is not None:
        seen_vars = set()
        duplicates = []
        deduped_vars = []

        for var in variables_to_use:
            if var in seen_vars:
                duplicates.append(var)
            else:
                seen_vars.add(var)
                deduped_vars.append(var)

        if duplicates:
            print(
                f"\n⚠️ WARNING: Found duplicate variables in variables_to_use: {duplicates}"
            )
            print(f"Using only the first occurrence of each variable for analysis.")
            variables_to_use = deduped_vars

    # Check for duplicate columns in DataFrame (could happen from merges)
    duplicate_cols = df_sales.columns[df_sales.columns.duplicated()].tolist()
    if duplicate_cols:
        print(
            f"\n⚠️ WARNING: Found duplicate columns in sales DataFrame: {duplicate_cols}"
        )
        print(
            f"This could cause errors in analysis. Keeping only first occurrence of each column."
        )
        df_sales = df_sales.loc[:, ~df_sales.columns.duplicated()]

    duplicate_cols_univ = df_universe.columns[df_universe.columns.duplicated()].tolist()
    if duplicate_cols_univ:
        print(
            f"\n⚠️ WARNING: Found duplicate columns in universe DataFrame: {duplicate_cols_univ}"
        )
        print(
            f"This could cause errors in analysis. Keeping only first occurrence of each column."
        )
        df_universe = df_universe.loc[:, ~df_universe.columns.duplicated()]

    t.stop("variables.stuff")

    t.start("variables.prepare_ds")
    ds = _prepare_ds(
        "var_recs", df_sales, df_universe, model_group, vacant_only, settings, variables_to_use
    )
    t.stop("variables.prepare_ds")
    t.start("variables.one_hot")
    ds = ds.encode_categoricals_with_one_hot()
    t.stop("variables.one_hot")
    t.start("variables.split")
    ds.split()
    t.stop("variables.split")
    feature_selection = (
        settings.get("modeling", {})
        .get("instructions", {})
        .get("feature_selection", {})
    )
    thresh = feature_selection.get("thresholds", {})

    X_sales = ds.X_sales[ds.ind_vars]
    y_sales = ds.y_sales

    X_univ = ds.X_univ[ds.ind_vars]

    t.start("variables.rep")
    rep_results = calc_representation(X_sales, X_univ, do_plots=do_plots)
    bad_vars = rep_results["bad_vars"]
    t.stop("variables.rep")

    # Remove bad variables
    ind_vars = [var for var in ds.ind_vars if var not in bad_vars]

    if "corr" in tests_to_run:
        # Correlation
        X_corr = ds.df_sales[[ds.dep_var] + ind_vars]
        t.start("variables.corr")
        corr_results = calc_correlations(
            X_corr,
            thresh.get("correlation", 0.1),
            do_plots=do_plots,
            min_strength=thresh.get("correlation_strength"),
            overlap=bool(feature_selection.get("correlation_overlap", True)),
        )

        # Remove bad variables
        dropped = set(corr_results["bad_vars"]) | set(corr_results.get("weak") or [])
        ind_vars = [var for var in ds.ind_vars if var not in dropped]
        t.stop("variables.corr")
    else:
        corr_results = None

    if "enr" in tests_to_run:
        # Elastic net regularization
        try:
            t.start("variables.enr")
            enr_coefs = calc_elastic_net_regularization(
                X_sales, y_sales, thresh.get("enr", 0.01)
            )
            t.stop("variables.enr")
        except ValueError as e:
            nulls_in_X = X_sales[X_sales.isna().any(axis=1)]
            print(f"Found {len(nulls_in_X)} rows with nulls in X:")
            # identify columns with nulls in them:
            cols_with_null = nulls_in_X.columns[nulls_in_X.isna().any()].tolist()
            print(f"Columns with nulls: {cols_with_null}")
            raise e
    else:
        enr_coefs = None

    if "r2" in tests_to_run:
        # R² values
        t.start("variables.r2")
        r2_values = calc_r2(ds.df_sales, ind_vars, y_sales)
        t.stop("variables.r2")
    else:
        r2_values = None

    if "p_value" in tests_to_run:
        # P Values
        t.start("variables.p")
        p_values = calc_p_values_recursive_drop(
            X_sales, y_sales, thresh.get("p_value", 0.05)
        )
        t.stop("variables.p")
    else:
        p_values = None

    if "t_value" in tests_to_run:
        # T Values
        t.start("variables.t")
        t_values = calc_t_values_recursive_drop(
            X_sales, y_sales, thresh.get("t_value", 2)
        )
        t.stop("variables.t")
    else:
        t_values = None

    if "vif" in tests_to_run:
        t.start("variables.vif")
        # VIF
        # Filter out boolean columns before VIF calculation
        bool_cols = []
        vif_X = X_sales.copy()

        for col in X_sales.columns:
            # Check if column is boolean or contains only 0/1 values
            if X_sales[col].dtype == bool or (
                X_sales[col].isin([0, 1, True, False]).all()
                and len(X_sales[col].unique()) <= 2
            ):
                bool_cols.append(col)

        if bool_cols:
            vif_X = vif_X.drop(columns=bool_cols)

        # Don't run VIF if we have no columns left or too few rows
        if 0 < vif_X.shape[1] < len(vif_X):
            vif = calc_vif_recursive_drop(vif_X, thresh.get("vif", 10), settings)

            # Add boolean columns back to the final VIF results with NaN VIF values
            if bool_cols and vif is not None and "final" in vif:
                for bool_col in bool_cols:
                    vif["final"] = pd.concat(
                        [
                            vif["final"],
                            pd.DataFrame(
                                {"variable": [bool_col], "vif": [float("nan")]}
                            ),
                        ],
                        ignore_index=True,
                    )
        else:
            if verbose:
                print(
                    "Skipping VIF calculation - not enough non-boolean variables or samples"
                )
            vif = {
                "initial": pd.DataFrame(columns=["variable", "vif"]),
                "final": pd.DataFrame(columns=["variable", "vif"]),
            }
        t.stop("variables.vif")
    else:
        vif = None

    t.start("variables.calc_recs")
    # Generate final results & recommendations
    df_results = _calc_variable_recommendations(
        ds=ds,
        settings=settings,
        rep_results=rep_results,
        correlation_results=corr_results,
        enr_results=enr_coefs,
        r2_values_results=r2_values,
        p_values_results=p_values,
        t_values_results=t_values,
        vif_results=vif,
        report=report
    )
    t.stop("variables.calc_recs")

    t.start("variables.final_stuff")
    curr_variables = df_results["variable"].tolist()
    best_variables = curr_variables.copy()
    best_score = float("inf")

    df_cross = df_results.copy()
    y = ds.y_sales

    t.start("variables.final_stuff.while")
    if do_cross:
        while len(curr_variables) > 0:
            X = ds.df_sales[curr_variables]
            t.start("variables.final_stuff.while.cross")
            cv_score = calc_cross_validation_score(X, y)
            t.stop("variables.final_stuff.while.cross")
            if cv_score < best_score:
                best_score = cv_score
                best_variables = curr_variables.copy()
            worst_idx = df_cross["weighted_score"].idxmin()
            worst_variable = df_cross.loc[worst_idx, "variable"]
            curr_variables.remove(worst_variable)
            # Remove the variable from the results dataframe.
            df_cross = df_cross[df_cross["variable"].ne(worst_variable)]
    t.stop("variables.final_stuff.while")

    # Create a table from the list of best variables.
    df_best = pd.DataFrame(best_variables, columns=["Variable"])
    df_best["Rank"] = range(1, len(df_best) + 1)
    df_best["Description"] = df_best["Variable"]

    t.start("variables.final_stuff.apply_dd")
    df_best = _apply_dd_to_df_rows(
        df_best, "Variable", settings, ds.one_hot_descendants, "name"
    )
    df_best = _apply_dd_to_df_rows(
        df_best, "Description", settings, ds.one_hot_descendants, "description"
    )
    t.stop("variables.final_stuff.apply_dd")
    df_best = df_best[["Rank", "Variable", "Description"]]
    df_best.loc[df_best["Variable"].eq(df_best["Description"]), "Description"] = ""
    df_best.set_index("Rank", inplace=True)

    if do_report:
        report.set_var("summary_table", df_best.to_markdown())
        report = generate_variable_report(report, settings, model_group, best_variables)
    else:
        report = None
    t.stop("variables.final_stuff")

    print(t.print())

    out = {"variables": best_variables, "report": report, "df_results": df_results}
    if return_tests:
        out["tests"] = {
            "thresholds": thresh,
            "weights": feature_selection.get("weights", {}),
            "one_hot": dict(ds.one_hot_descendants or {}),
            "representation": rep_results.get("raw"),
            "representation_bad": rep_results.get("bad_vars", []),
            "corr": corr_results,
            "enr": enr_coefs,
            "r2": r2_values,
            "p_value": p_values,
            "t_value": t_values,
            "vif": vif,
        }
    return out

run_ensemble

run_ensemble(df_sales, df_universe, model_group, vacant_only, dep_var, dep_var_test, outpath, all_results, settings, verbose=False)

Run an ensemble model based on the provided parameters.

This function optimizes the ensemble model and runs it, returning the results and the list of models used in the ensemble.

Parameters:

Name Type Description Default
df_sales DataFrame or None

Sales DataFrame. If None, it will be read from the MultiModelResults.

required
df_universe DataFrame or None

Universe DataFrame. If None, it will be read from the MultiModelResults.

required
model_group str

Model group identifier.

required
vacant_only bool

Whether to use only vacant sales.

required
dep_var str

Dependent variable for training.

required
dep_var_test str

Dependent variable for testing.

required
outpath str

Output path for saving results.

required
all_results MultiModelResults

MultiModelResults containing all model results.

required
settings dict

Settings dictionary.

required
verbose bool

If True, prints additional information. Defaults to False.

False

Returns:

Type Description
tuple[SingleModelResults, list[str]]

A tuple containing the SingleModelResults of the ensemble model and a list of models used in the ensemble.

Source code in openavmkit/model_runner.py
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
def run_ensemble(
    df_sales: pd.DataFrame | None,
    df_universe: pd.DataFrame | None,
    model_group: str,
    vacant_only: bool,
    dep_var: str,
    dep_var_test: str,
    outpath: str,
    all_results: MultiModelResults,
    settings: dict,
    verbose: bool = False,
) -> tuple[SingleModelResults, list[str]]:
    """Run an ensemble model based on the provided parameters.

    This function optimizes the ensemble model and runs it, returning the results and the list of models used in the ensemble.

    Parameters
    ----------
    df_sales : pandas.DataFrame or None
        Sales DataFrame. If None, it will be read from the MultiModelResults.
    df_universe : pandas.DataFrame or None
        Universe DataFrame. If None, it will be read from the MultiModelResults.
    model_group : str
        Model group identifier.
    vacant_only : bool
        Whether to use only vacant sales.
    dep_var : str
        Dependent variable for training.
    dep_var_test : str
        Dependent variable for testing.
    outpath : str
        Output path for saving results.
    all_results : MultiModelResults
        MultiModelResults containing all model results.
    settings : dict
        Settings dictionary.
    verbose : bool, optional
        If True, prints additional information. Defaults to False.

    Returns
    -------
    tuple[SingleModelResults, list[str]]
        A tuple containing the SingleModelResults of the ensemble model and a list of models used in the ensemble.
    """
    if verbose:
        print("Optimizing ensemble...")

    ensemble_list = _optimize_ensemble(
        df_sales,
        df_universe,
        model_group,
        vacant_only,
        dep_var,
        dep_var_test,
        all_results,
        settings,
        verbose=verbose,
        ensemble_list=None,
    )
    if verbose:
        print("Running ensemble...")
    ensemble = _run_ensemble(
        df_sales,
        df_universe,
        model_group,
        vacant_only=vacant_only,
        dep_var=dep_var,
        dep_var_test=dep_var_test,
        outpath=outpath,
        ensemble_list=ensemble_list,
        all_results=all_results,
        settings=settings,
        verbose=verbose,
    )
    if verbose:
        print("Finished ensemble!")
    return ensemble, ensemble_list

run_models

run_models(sup, settings, save_params=False, use_saved_params=True, save_results=False, verbose=False, run_main=True, run_vacant=True, run_ensemble=True, do_shaps=False, do_plots=False, return_results=False)

Runs predictive models on the given SalesUniversePair.

This function takes detailed instructions from the provided settings dictionary and handles all the internal details like splitting the data, training the models, and saving the results. It performs basic statistic analysis on each model, and optionally combines results into an ensemble model.

If "run_main" is true, it will run normal (full market value) models. If "run_vacant" is true, it will run vacant models as well -- models that only use vacant sales as evidence to generate land values.

This function iterates over model groups and runs models for both main and vacant cases.

Parameters:

Name Type Description Default
sup SalesUniversePair

Sales and universe data.

required
settings dict

The settings dictionary.

required
save_params bool

Whether to save model parameters.

False
use_saved_params bool

Whether to use saved model parameters.

True
save_results bool

Whether to save model results.

False
verbose bool

If True, prints additional information.

False
run_main bool

Whether to run main (non-vacant) models.

True
run_vacant bool

Whether to run vacant models.

True
run_ensemble bool

Whether to run ensemble models.

True
do_shaps bool

Whether to compute SHAP values.

False
do_plots bool

Whether to plot scatterplots

False
return_results bool

Whether to return the results when they aren't saved. Each model group's results are then held until the run ends, rather than freed as the next group starts. Defaults to False.

False

Returns:

Type Description
dict[str, MultiModelResults]

Each model group's results and benchmark, by model group, when they're saved or return_results is set; otherwise empty.

Source code in openavmkit/model_runner.py
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
def run_models(
    sup: SalesUniversePair,
    settings: dict,
    save_params: bool = False,
    use_saved_params: bool = True,
    save_results: bool = False,
    verbose: bool = False,
    run_main: bool = True,
    run_vacant: bool = True,
    run_ensemble: bool = True,
    do_shaps: bool = False,
    do_plots: bool = False,
    return_results: bool = False,
):
    """
    Runs predictive models on the given SalesUniversePair.

    This function takes detailed instructions from the provided settings dictionary and handles all the internal
    details like splitting the data, training the models, and saving the results. It performs basic statistic analysis
    on each model, and optionally combines results into an ensemble model.

    If "run_main" is true, it will run normal (full market value) models.
    If "run_vacant" is true, it will run vacant models as well -- models that only use vacant sales as evidence
    to generate land values.

    This function iterates over model groups and runs models for both main and vacant cases.

    Parameters
    ----------
    sup : SalesUniversePair
        Sales and universe data.
    settings : dict
        The settings dictionary.
    save_params : bool, optional
        Whether to save model parameters.
    use_saved_params : bool, optional
        Whether to use saved model parameters.
    save_results : bool, optional
        Whether to save model results.
    verbose : bool, optional
        If True, prints additional information.
    run_main : bool, optional
        Whether to run main (non-vacant) models.
    run_vacant : bool, optional
        Whether to run vacant models.
    run_ensemble : bool, optional
        Whether to run ensemble models.
    do_shaps : bool, optional
        Whether to compute SHAP values.
    do_plots : bool, optional
        Whether to plot scatterplots
    return_results : bool, optional
        Whether to return the results when they aren't saved. Each model group's results are then held until the
        run ends, rather than freed as the next group starts. Defaults to False.

    Returns
    -------
    dict[str, MultiModelResults]
        Each model group's results and benchmark, by model group, when they're saved or ``return_results`` is set;
        otherwise empty.
    """

    t = TimingData()

    t.start("setup")
    s = settings
    s_model = s.get("modeling", {})
    s_inst = s_model.get("instructions", {})
    model_groups = s_inst.get("model_groups", [])

    df_univ = sup["universe"]

    if len(model_groups) == 0:
        model_groups = get_model_group_ids(settings, df_univ)

    dict_all_results = {}
    # A missing optional engine (XGBoost, CatBoost, NGBoost) is found now, not after the models before it trained.
    require_engines(_engines_to_run(settings, model_groups, run_main, run_vacant))
    t.stop("setup")

    t.start("run model groups")
    for model_group in model_groups:
        t.start(f"model group: {model_group}")
        for main_vacant in ["main", "vacant"]:
            if main_vacant == "main" and not run_main:
                continue
            if main_vacant == "vacant" and not run_vacant:
                continue

            models_to_skip = s_inst.get(main_vacant, {}).get("skip", {}).get(model_group, [])

            if "all" in models_to_skip:
                if verbose:
                    print(
                        f"Skipping all models for model_group: {model_group}/{main_vacant}"
                    )
                continue

            if verbose:
                print("")
                print("")
                print("******************************************************")
                print(f"Running models for model_group: {model_group}")
                print("******************************************************")
                print("")
                print("")

            mg_results = _run_models(
                sup,
                model_group,
                settings,
                main_vacant,
                save_params,
                use_saved_params,
                save_results,
                verbose,
                run_ensemble,
                do_shaps=do_shaps,
                do_plots=do_plots
            )
            # Kept to be written, or when the caller asked for them back: otherwise each group's results are let go
            # as the next starts, so a try holds one group's at a time.
            if mg_results is not None and (save_results or return_results):
                dict_all_results[model_group] = mg_results
        t.stop(f"model group: {model_group}")
    t.stop("run model groups")

    if save_results:
        t.start("write")
        write_out_all_results(sup, dict_all_results, settings)
        t.stop("write")

    print("**********TIMING FOR RUN ALL MODELS***********")
    print(t.print())
    print("***********************************************")

    return dict_all_results

run_one_model

run_one_model(df_sales, df_universe, vacant_only, model_group, model_name, model_entries, settings, dep_var, dep_var_test, best_variables, fields_cat, outpath, save_params, use_saved_params, save_results, verbose=False, test_keys=None, train_keys=None)

Run a single model based on provided parameters and return its results.

Parameters:

Name Type Description Default
df_sales DataFrame

Sales DataFrame.

required
df_universe DataFrame

Universe DataFrame.

required
vacant_only bool

Whether to use only vacant sales.

required
model_group str

Model group identifier.

required
model_name str

Model's unique identifier.

required
model_entries dict

Dictionary of model configuration entries.

required
settings dict

Settings dictionary.

required
dep_var str

Dependent variable for training.

required
dep_var_test str

Dependent variable for testing.

required
best_variables list[str]

List of best variables selected.

required
fields_cat list[str]

List of categorical fields.

required
outpath str

Output path for saving results.

required
save_params bool

Whether to save parameters.

required
use_saved_params bool

Whether to use saved parameters.

required
save_results bool

Whether to save results.

required
verbose bool

If True, prints additional information.

False
test_keys list[str] or None

Optional list of test keys (will be read from disk if not provided).

None
train_keys list[str] or None

Optional list of training keys (will be read from disk if not provided).

None

Returns:

Type Description
SingleModelResults or None

SingleModelResults if successful, else None.

Source code in openavmkit/model_runner.py
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
def run_one_model(
    df_sales: pd.DataFrame,
    df_universe: pd.DataFrame,
    vacant_only: bool,
    model_group: str,
    model_name: str,
    model_entries: dict,
    settings: dict,
    dep_var: str,
    dep_var_test: str,
    best_variables: list[str],
    fields_cat: list[str],
    outpath: str,
    save_params: bool,
    use_saved_params: bool,
    save_results: bool,
    verbose: bool = False,
    test_keys: list[str] | None = None,
    train_keys: list[str] | None = None,
) -> SingleModelResults | None:
    """
    Run a single model based on provided parameters and return its results.

    Parameters
    ----------
    df_sales : pandas.DataFrame
        Sales DataFrame.
    df_universe : pandas.DataFrame
        Universe DataFrame.
    vacant_only : bool
        Whether to use only vacant sales.
    model_group : str
        Model group identifier.
    model_name : str
        Model's unique identifier.
    model_entries : dict
        Dictionary of model configuration entries.
    settings : dict
        Settings dictionary.
    dep_var : str
        Dependent variable for training.
    dep_var_test : str
        Dependent variable for testing.
    best_variables : list[str]
        List of best variables selected.
    fields_cat : list[str]
        List of categorical fields.
    outpath : str
        Output path for saving results.
    save_params : bool
        Whether to save parameters.
    use_saved_params : bool
        Whether to use saved parameters.
    save_results : bool
        Whether to save results.
    verbose : bool, optional
        If True, prints additional information.
    test_keys : list[str] or None, optional
        Optional list of test keys (will be read from disk if not provided).
    train_keys : list[str] or None, optional
        Optional list of training keys (will be read from disk if not provided).

    Returns
    -------
    SingleModelResults or None
        SingleModelResults if successful, else None.
    """

    t = TimingData()

    t.start("setup")

    entry: dict | None = model_entries.get(model_name, None)
    default_entry: dict | None = model_entries.get("default", {})
    entry_is_default = entry is None
    if entry is None:
        entry = default_entry
        if entry is None:
            raise ValueError(
                f"Model entry for {model_name} not found, and there is no default entry!"
            )
    # No "model" key means the name IS the engine (e.g. "mra", "lightgbm"). That fallback is
    # load-bearing for plain entries, but it silently turns an *alias* with no entry in this
    # model group's block (e.g. "lgbm_x") into a bogus engine name -- see the dispatch else.
    model_engine = entry.get("model", model_name)
    if model_engine == "default":
        # this isn't a real model, just a settings object to fill in for others
        return None

    if "*" in model_engine:
        sales_chase = 0.01
        model_engine = model_engine.replace("*", "")
    else:
        sales_chase = False

    if verbose:
        print(f"------------------------------------------------")
        print(f"Running model {model_name} on {len(df_sales)} rows...")

    are_ind_vars_default = entry.get("ind_vars", None) is None
    ind_vars: list | None = entry.get("ind_vars", default_entry.get("ind_vars", None))

    if ind_vars is None:
        raise ValueError(f"ind_vars not found for model {model_name}")
    # De-duplicate with a DETERMINISTIC order: sorted() is independent of PYTHONHASHSEED, so the
    # feature column order (and thus XGBoost/LightGBM column-subsampling selections) is reproducible
    # across processes and separate invocations. Plain list(set(...)) is hash-ordered and was a
    # latent source of run-to-run nondeterminism (see run_one_model_cv worker pinning).
    ind_vars = sorted(set(ind_vars))

    if are_ind_vars_default:
        if (best_variables is not None) and (set(ind_vars) != set(best_variables)):
            if verbose:
                print(
                    f"--> using default variables, auto-optimized variable list: {best_variables}"
                )
            ind_vars = best_variables

    interactions = get_variable_interactions(entry, settings, df_sales)
    location_fields = entry.get("locations", get_locations(settings, df_sales))

    if test_keys is None or train_keys is None:
        test_keys, train_keys = read_split_keys(model_group)
    t.stop("setup")

    t.start("data split")
    ds = get_data_split_for(
        model_name=model_name,
        model_engine=model_engine,
            model_entry=entry,
            model_group=model_group,
            location_fields=location_fields,
            ind_vars=ind_vars,
            df_sales=df_sales,
            df_universe=df_universe,
            settings=settings,
            dep_var=dep_var,
            dep_var_test=dep_var_test,
            fields_cat=fields_cat,
            interactions=interactions,
            test_keys=test_keys,
            train_keys=train_keys,
            vacant_only=vacant_only,
        )

    # safeguards against invalid splits
    n_sales = len(ds.df_sales) if ds.df_sales is not None else 0
    n_train = len(ds.df_train) if ds.df_train is not None else 0
    n_test  = len(ds.df_test)  if ds.df_test  is not None else 0
    p = ds.X_train.shape[1] if getattr(ds, "X_train", None) is not None else 0

    if n_train == 0 or p == 0:
        # compute some helpful diagnostics
        missing_vars = []
        if p == 0:
            # requested vars that are not present in train
            train_cols = set(ds.df_train.columns) if ds.df_train is not None else set()
            missing_vars = [v for v in ds.ind_vars if v not in train_cols]

        why = []
        if n_train == 0:
            if n_test == n_sales and n_sales > 0:
                why.append("all sales ended up in the test split (train set empty) — check split keys")
            else:
                why.append("filters/slicing removed all training rows")
        if p == 0:
            why.append(f"no usable features in X_train (missing ind_vars: {missing_vars[:20]}{'...' if len(missing_vars)>20 else ''})")

        warnings.warn(
            f"Skipping model {model_group}/{model_name} ({model_engine}): "
            f"sales={n_sales}, train={n_train}, test={n_test}, X_train_cols={p}. "
            + " ".join(why),
            RuntimeWarning
        )
        return None

    t.stop("data split")

    t.start("setup")
    if len(ds.y_sales) < 15:
        if verbose:
            print(f"--> model {model_name} has less than 15 sales. Skipping...")
        return None

    optimize_vars = entry.get("optimize_vars", False)
    intercept = entry.get("intercept", True)
    # Per-model opt-in to log-target training (mra / multi_mra only). Fitting on log(price) keeps
    # the linear models from extrapolating negative values; the model exponentiates its own
    # predictions back to price space, so this stays contained to the model (no dep_var changes).
    log = entry.get("log", False)
    n_trials = entry.get("n_trials", 50)
    use_gpu = entry.get("use_gpu", True)
    categories = entry.get("categories", "auto")  # CatBoost: how it handles category variables
    seed = get_model_seed(settings)
    t.stop("setup")

    t.start("run")
    if model_engine == "garbage":
        results = run_garbage(
            ds, normal=False, sales_chase=sales_chase, verbose=verbose
        )
    elif model_engine == "garbage_normal":
        results = run_garbage(ds, normal=True, sales_chase=sales_chase, verbose=verbose)
    elif model_engine == "mean":
        results = run_average(
            ds, average_type="mean", sales_chase=sales_chase, verbose=verbose
        )
    elif model_engine == "median":
        results = run_average(
            ds, average_type="median", sales_chase=sales_chase, verbose=verbose
        )
    elif model_engine == "naive_area":
        results = run_naive_area(ds, sales_chase=sales_chase, verbose=verbose)
    elif model_engine == "local_area":
        results = run_local_area(
            ds,
            location_fields=location_fields,
            sales_chase=sales_chase,
            verbose=verbose,
        )
    elif model_engine == "assessor" or model_engine == "pass_through":
        results = run_pass_through(ds, model_engine, verbose=verbose)
    elif model_engine == "ground_truth":
        results = run_ground_truth(ds, verbose=verbose)
    elif model_engine == "spatial_lag":
        results = run_spatial_lag(ds, per_area=False, verbose=verbose)
    elif model_engine == "spatial_lag_area":
        results = run_spatial_lag(ds, per_area=True, verbose=verbose)
    elif model_engine == "mra":
        results = run_mra(ds, intercept=intercept, verbose=verbose, log=log)
    elif model_engine == "multi_mra":
        results = run_multi_mra(ds, outpath, location_fields, optimize_vars=optimize_vars, intercept=intercept, verbose=verbose, log=log)
    elif model_engine == "kernel":
        results = run_kernel(
            ds, outpath, save_params, use_saved_params, verbose=verbose
        )
    elif model_engine == "gwr":
        results = run_gwr(ds, outpath, save_params, use_saved_params, verbose=verbose)
    elif model_engine == "xgboost":
        results = run_xgboost(
            ds, outpath, save_params, use_saved_params, n_trials=n_trials, verbose=verbose, seed=seed
        )
    elif model_engine == "lightgbm":
        results = run_lightgbm(
            ds, outpath, save_params, use_saved_params, n_trials=n_trials, verbose=verbose, seed=seed
        )
    elif model_engine == "catboost":
        results = run_catboost(
            ds,
            outpath,
            save_params,
            use_saved_params,
            n_trials=n_trials,
            verbose=verbose,
            use_gpu=use_gpu,
            seed=seed,
            categories=categories,
        )
    elif model_engine == "ngboost":
        results = run_ngboost(
            ds, outpath, save_params, use_saved_params, n_trials=n_trials, verbose=verbose, seed=seed
        )
    elif model_engine == "lcomp":
        results = run_layeredcomp(
            ds, outpath, save_params, use_saved_params, n_trials=n_trials, verbose=verbose, seed=seed
        )
    else:
        if entry_is_default:
            raise ValueError(
                f'Model "{model_name}" (model_group "{model_group}") has no entry in '
                f'modeling.models.{"vacant" if vacant_only else "main"}, so it fell back to the '
                f'"default" entry, which does not name an engine -- leaving "{model_engine}" '
                f"(the model's own name) as the engine, and that is not a known engine. If "
                f'"{model_name}" is an alias, it needs its own entry with a "model" key. Note '
                f"that a per-model-group override block REPLACES the top-level block wholesale, "
                f"so every name in instructions.run must be defined in whichever block applies "
                f"to this group."
            )
        raise ValueError(
            f'Model engine "{model_engine}" (from model "{model_name}", model_group '
            f'"{model_group}") not found!'
        )
    t.stop("run")

    if results is None:
        return None

    if ds.vacant_only:
        # If this is a vacant model, we attempt to load a corresponding "full value" model
        max_trim = get_max_ratio_study_trim(settings, results.ds.model_group)

    if save_results:
        t.start("write")
        main_vacant = "vacant" if vacant_only else "main"
        location = get_model_location(settings, main_vacant, model_name, model_group)
        _write_model_results(results, outpath, settings, location, verbose=verbose)
        t.stop("write")

    return results

run_one_model_cv

run_one_model_cv(df_sales, df_universe, vacant_only, model_group, model_name, model_entries, settings, dep_var, dep_var_test, best_variables, fields_cat, outpath, save_params, use_saved_params, save_results, verbose=False)

Nested cross-validation wrapper around :func:run_one_model.

Two phases, both driven through the ordinary single-model path so every engine, ratio study, and back-transform is reused unchanged:

  • Phase 1 (holdout): run the model once per fold — each fold trains on the other folds (parcels intact, post-val excluded) and predicts its own held-out slice. Because each fold re-tunes from scratch on its own training data (distinct outpath → distinct params.json), the held-out slice is leakage-free w.r.t. training and HP selection. The slices stitch into an out-of-fold (OOF) prediction for every trainable sale.
  • Phase 2 (study/ship): refit once on ALL trainable sales; its predictions on all sales (study) and the universe (shipped values) are kept as-is, and its held-out post-val predictions cover the post-valuation sales in the report (leakage-free — post-val never trains).

The returned result is the Phase-2 result with its test side replaced by the 100%-coverage OOF frame (see :meth:SingleModelResults.override_test_predictions), so pred_test is the honest full-coverage holdout, while pred_sales / pred_univ are the study / shipped values.

Falls back to a single split only when no folds.csv exists (legacy mode / skipped group). Log-target (log_-prefixed dep_var_test) is handled uniformly: :meth:SingleModelResults.override_test_predictions re-applies the same log back-transform the ordinary path does.

Source code in openavmkit/model_runner.py
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
def run_one_model_cv(
    df_sales: pd.DataFrame,
    df_universe: pd.DataFrame,
    vacant_only: bool,
    model_group: str,
    model_name: str,
    model_entries: dict,
    settings: dict,
    dep_var: str,
    dep_var_test: str,
    best_variables: list[str],
    fields_cat: list[str],
    outpath: str,
    save_params: bool,
    use_saved_params: bool,
    save_results: bool,
    verbose: bool = False,
) -> SingleModelResults | None:
    """Nested cross-validation wrapper around :func:`run_one_model`.

    Two phases, both driven through the ordinary single-model path so every engine, ratio
    study, and back-transform is reused unchanged:

    - **Phase 1 (holdout):** run the model once per fold — each fold trains on the other folds
      (parcels intact, post-val excluded) and predicts its own held-out slice. Because each
      fold re-tunes from scratch on its own training data (distinct ``outpath`` → distinct
      ``params.json``), the held-out slice is leakage-free w.r.t. training *and* HP selection.
      The slices stitch into an out-of-fold (OOF) prediction for every trainable sale.
    - **Phase 2 (study/ship):** refit once on ALL trainable sales; its predictions on all sales
      (study) and the universe (shipped values) are kept as-is, and its held-out post-val
      predictions cover the post-valuation sales in the report (leakage-free — post-val never
      trains).

    The returned result is the Phase-2 result with its test side replaced by the 100%-coverage
    OOF frame (see :meth:`SingleModelResults.override_test_predictions`), so ``pred_test`` is the
    honest full-coverage holdout, while ``pred_sales`` / ``pred_univ`` are the study / shipped
    values.

    Falls back to a single split only when no ``folds.csv`` exists (legacy mode / skipped group).
    Log-target (``log_``-prefixed ``dep_var_test``) is handled uniformly:
    :meth:`SingleModelResults.override_test_predictions` re-applies the same log back-transform
    the ordinary path does.
    """
    fold_data = _read_fold_keys(model_group)
    if fold_data is None or fold_data["n_folds"] < 2:
        return run_one_model(
            df_sales, df_universe, vacant_only, model_group, model_name, model_entries,
            settings, dep_var, dep_var_test, best_variables, fields_cat, outpath,
            save_params, use_saved_params, save_results, verbose=verbose,
        )

    train_all = [str(k) for k in fold_data["train_all"]]
    post_val = [str(k) for k in fold_data["post_val"]]
    cv_prod_mode = settings.get("modeling", {}).get("instructions", {}).get(
        "cv_production_params", "aggregate"
    )
    # Out-of-fold contributions. Off => the legacy behavior, where the Phase-2 model explains
    # the OOF holdout frame (attributions and predictions from different models; see
    # `_stitch_oof_contributions`).
    emit_oof_contribs = bool(
        settings.get("modeling", {}).get("instructions", {}).get("cv_oof_contributions", True)
    )

    # ---- Phase 1: per-fold out-of-fold predictions ----
    # Folds are independent and each is internally deterministic, so running them in parallel is
    # bit-identical to sequential. To avoid CPU oversubscription (each fold's tuner already runs a
    # trial-level thread pool), each worker caps its tuner threads to a fair share of the cores.
    tasks = [
        (k, [str(x) for x in hk], [str(x) for x in tk])
        for k, (hk, tk) in enumerate(fold_data["folds"])
        if len(hk) > 0 and len(tk) > 0
    ]
    cpu = os.cpu_count() or 2
    cv_max_workers = int(settings.get("modeling", {}).get("instructions", {}).get(
        "cv_max_workers", min(len(tasks), max(1, cpu - 2))
    ))
    cv_max_workers = max(1, min(cv_max_workers, len(tasks)))
    fold_args = (
        df_sales, df_universe, vacant_only, model_group, model_name, model_entries,
        settings, dep_var, dep_var_test, best_variables, fields_cat, outpath,
        use_saved_params, verbose, emit_oof_contribs and save_results,
    )

    # Parallelism comes from the fold axis, and each fold tunes its trials SERIALLY (1 thread).
    # This is deliberate: XGBoost/LightGBM threaded trial-concurrency is not numerically identical
    # across concurrency levels (tiny MAPE differences flip which trial wins), so varying the
    # trial-thread count would make results depend on cv_max_workers. Fixing trials to serial makes
    # every CV run bit-identical regardless of worker count, and fold_workers x 1 thread never
    # oversubscribes. (The single-split path is untouched — it keeps full trial parallelism.)
    if cv_max_workers > 1 and len(tasks) > 1:
        if verbose:
            print(f"Running {len(tasks)} CV folds for {model_group}/{model_name} across "
                  f"{cv_max_workers} workers (serial tuning per fold)...")
        # Pin the math libraries to a single thread in the worker processes (inherited at spawn).
        # XGBoost/LightGBM `hist` otherwise use OpenMP threads whose count varies with system load,
        # and parallel float reductions are non-associative -> non-reproducible results run-to-run.
        # Single-threaded per worker makes each fold deterministic; parallelism comes from folds.
        # PYTHONHASHSEED must be fixed too: loky workers otherwise get a randomized hash seed,
        # so hash-ordered constructs in the fit path (e.g. list(set(ind_vars))) order features
        # differently per worker/run, and XGBoost column subsampling then picks different features
        # -> non-reproducible models. Pinning it makes every worker (and re-run) deterministic.
        _thread_env = {
            "OMP_NUM_THREADS": "1", "OPENBLAS_NUM_THREADS": "1", "MKL_NUM_THREADS": "1",
            "NUMEXPR_NUM_THREADS": "1", "VECLIB_MAXIMUM_THREADS": "1", "PYTHONHASHSEED": "0",
        }
        _saved_env = {k: os.environ.get(k) for k in _thread_env}
        for k, v in _thread_env.items():
            os.environ[k] = v
        try:
            from joblib import Parallel, delayed
            # CatBoost's results are the same at any thread count, so it takes the cores the folds leave idle.
            cb_threads = max(1, cpu // cv_max_workers)
            fold_results = Parallel(n_jobs=cv_max_workers, backend="loky")(
                delayed(_run_cv_fold)(k, hk, tk, 1, *fold_args, catboost_threads=cb_threads)
                for (k, hk, tk) in tasks
            )
        finally:
            for k, v in _saved_env.items():
                if v is None:
                    os.environ.pop(k, None)
                else:
                    os.environ[k] = v
    else:
        # cv_max_workers=1 is the explicit "no fold parallelism" escape hatch (e.g. low memory);
        # let each fold keep full trial-level parallelism (tune_workers=None) so it isn't crippled
        # (LightGBM's tuner still evaluates one trial at a time: two at once crash it, tuning._tune_lightgbm).
        fold_results = [_run_cv_fold(k, hk, tk, None, *fold_args) for (k, hk, tk) in tasks]

    if any(isinstance(r, str) and r == _FOLD_SKIPPED for r in fold_results):
        raise progress.Skipped(f"model:{model_group}:{model_name}")

    oof_map: dict[str, float] = {}
    n_fold_ok = 0
    fold_param_files: list[tuple[float, str]] = []  # (holdout utility, params.json path)
    for res in fold_results:
        if res is None:
            warnings.warn(
                f"A CV fold for {model_group}/{model_name} produced no result; its holdout "
                f"sales fall back to the study prediction."
            )
            continue
        oof_pairs, util, fpath = res
        for ks, pred in oof_pairs:
            oof_map[ks] = pred
        n_fold_ok += 1
        if fpath:
            fold_param_files.append((util, fpath))

    # ---- Production hyperparameters (skip the Phase-2 tune when possible) ----
    # For tunable engines, reuse the fold HPs instead of a fresh production study:
    #   aggregate (default) = median/mode across folds; best_fold = the best-holdout fold's HPs.
    # We write them to the prod params.json with the CV-aggregate sentinel so _get_params trusts
    # them regardless of fingerprint. refit (or non-tunable / no fold params) tunes as normal.
    prod_outpath = f"{outpath}/cv_prod"
    prod_use_saved = use_saved_params
    if fold_param_files and cv_prod_mode in ("aggregate", "best_fold"):
        dicts = []
        for _util, fpath in fold_param_files:
            d = json.loads(read_text(fpath))
            d.pop("__fingerprint", None)
            dicts.append(d)
        if cv_prod_mode == "best_fold":
            best_i = min(
                range(len(fold_param_files)),
                key=lambda i: fold_param_files[i][0] if fold_param_files[i][0] == fold_param_files[i][0] else float("inf"),
            )
            chosen = dicts[best_i]
        else:
            chosen = _aggregate_fold_params(dicts)
        os.makedirs(prod_outpath, exist_ok=True)
        with open(f"{prod_outpath}/{model_name}_params.json", "w", encoding="utf-8") as fh:
            json.dump({**chosen, "__fingerprint": _CV_AGGREGATE_FINGERPRINT}, fh)
        prod_use_saved = True
        if verbose:
            print(f"--> CV production HPs for {model_group}/{model_name}: {cv_prod_mode}")

    # ---- Phase 2: refit on all trainable; study + universe + post-val holdout ----
    # test_keys = post-val (leakage-free full-model holdout). If there are no post-val sales,
    # any small non-empty slice satisfies DataSplit.split(); its stats are overwritten below.
    prod_test_keys = post_val if len(post_val) > 0 else train_all[: max(1, len(train_all) // 5)]
    prod_train_keys = [k for k in train_all if k not in set(prod_test_keys)] if len(post_val) == 0 else train_all
    phase2 = run_one_model(
        df_sales, df_universe, vacant_only, model_group, model_name, model_entries,
        settings, dep_var, dep_var_test, best_variables, fields_cat, prod_outpath,
        save_params, prod_use_saved, save_results=False, verbose=verbose,
        test_keys=prod_test_keys, train_keys=prod_train_keys,
    )
    if phase2 is None:
        return None

    main_vacant = "vacant" if vacant_only else "main"
    location = get_model_location(settings, main_vacant, model_name, model_group)

    # ---- Post-valuation slice, explained while the frames still line up ----
    # Right now `phase2.df_test` IS the post-valuation holdout (it was built from
    # prod_test_keys) and ds.X_test matches it row for row. Phase 2 never trained on post-val,
    # so its explanation of those rows is exactly as leakage-free as each fold's is of its own
    # holdout -- they belong in the same OOF file. This is the only moment we can isolate them:
    # `override_test_predictions` below swaps df_test for the full-coverage frame.
    wrote_post_val_contribs = False
    if save_results and emit_oof_contribs and n_fold_ok > 0 and len(post_val) > 0:
        try:
            pv_dir = _model_artifact_dir(prod_outpath, model_name)
            os.makedirs(pv_dir, exist_ok=True)
            write_model_parameters(
                phase2.model, phase2, location, pv_dir, verbose=verbose, subsets={"test"},
            )
            wrote_post_val_contribs = True
        except Exception as e:
            warnings.warn(
                f"{model_group}/{model_name}: could not write post-valuation contributions "
                f"({type(e).__name__}: {e}); those rows will be missing from "
                f"contributions_test.csv."
            )

    # ---- Stitch: replace the test side with the full-coverage OOF frame ----
    if n_fold_ok == 0:
        warnings.warn(
            f"No CV folds succeeded for {model_group}/{model_name}; holdout stats fall back to "
            f"the single Phase-2 holdout."
        )
    else:
        field = phase2.field_prediction
        raw_sales = phase2.ds.df_sales.copy()
        ks = raw_sales["key_sale"].astype(str)
        post_val_map = dict(
            zip(phase2.df_test["key_sale"].astype(str).values, phase2.df_test[field].values)
        )
        pred = ks.map(oof_map)
        pred = pred.where(pred.notna(), ks.map(post_val_map))
        raw_sales[field] = pred.values
        df_test_full = raw_sales[raw_sales[field].notna()].reset_index(drop=True)
        phase2.override_test_predictions(df_test_full)

    if save_results:
        # The Phase-2 model must NOT explain the OOF holdout frame: its attributions would
        # describe a different model than the predictions sitting in that frame. Hold back the
        # "test" subset and let the stitcher assemble it from the per-fold writes instead.
        write_subsets = _cv_phase2_write_subsets(emit_oof_contribs, n_fold_ok)
        _write_model_results(
            phase2, outpath, settings, location, verbose=verbose, subsets=write_subsets
        )
        if write_subsets is not None:
            _stitch_oof_contributions(
                outpath,
                model_name,
                [k for (k, _hk, _tk) in tasks],
                prod_outpath,
                include_post_val=wrote_post_val_contribs,
                verbose=verbose,
            )

    return phase2

try_variables

try_variables(sup, settings, verbose=False, plot=False, do_report=False)

Experiment with variables to determine which are most useful for modeling.

Parameters:

Name Type Description Default
sup SalesUniversePair

The SalesUniversePair containing sales and universe data.

required
settings dict

Settings dictionary

required
verbose bool

Whether to print verbose output. Default is False.

False
plot bool

Whether to generate plots. Default is False.

False
do_report bool

Whether to generate a pdf report. Default is False.

False
Source code in openavmkit/model_runner.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
def try_variables(
    sup: SalesUniversePair,
    settings: dict,
    verbose: bool = False,
    plot: bool = False,
    do_report: bool = False,
):
    """Experiment with variables to determine which are most useful for modeling.

    Parameters
    ----------
    sup: SalesUniversePair
        The SalesUniversePair containing sales and universe data.
    settings: dict
        Settings dictionary
    verbose: bool
        Whether to print verbose output. Default is False.
    plot: bool
        Whether to generate plots. Default is False.
    do_report: bool
        Whether to generate a pdf report. Default is False.

    """

    df_hydrated = get_hydrated_sales_from_sup(sup)

    idx_vacant = df_hydrated["vacant_sale"].eq(True)

    df_vacant = df_hydrated[idx_vacant].copy()

    df_vacant = _simulate_removed_buildings(df_vacant, settings, idx_vacant)

    # update df_hydrated with *all* the characteristics of df_vacant where their keys match:
    df_hydrated.loc[idx_vacant, df_vacant.columns] = df_vacant.values

    all_best_variables = {}
    all_reports = {}

    try_vars = settings.get("modeling", {}).get("try_variables", {})
    model_groups_to_skip = try_vars.get("skip", [])

    def _try_variables(
        df_in: pd.DataFrame,
        model_group: str,
        df_univ: pd.DataFrame,
        do_report: bool,
        settings: dict,
        verbose: bool,
        results: dict,
        reports: dict
    ):
        bests = {}
        local_reports = {}

        for vacant_only in [False, True]:

            if vacant_only:
                if df_in["vacant_sale"].sum() == 0:
                    if verbose:
                        print("No vacant sales found, skipping...")
                    continue
            else:
                if df_in["valid_sale"].sum() == 0:
                    if verbose:
                        print("No valid sales found, skipping...")
                    continue

            try_vars = settings.get("modeling", {}).get("try_variables", {})
            variables_to_use = (
                try_vars.get("variables", [])
            )

            if len(variables_to_use) == 0:
                raise ValueError(
                    "No variables defined. Please check settings `modeling.try_variables.variables`"
                )

            df_univ = df_univ[df_univ["model_group"].eq(model_group)].copy()

            try:
                var_recs = get_variable_recommendations(
                    df_in,
                    df_univ,
                    vacant_only,
                    settings,
                    model_group,
                    variables_to_use=variables_to_use,
                    tests_to_run=["corr", "r2"],
                    do_report=True,
                    do_cross=True,
                    do_plots=plot,
                    verbose=verbose,
                )
            except Exception as e:
                # A model group can be too small or too degenerate (e.g. all-constant /
                # all-NaN features, too few sales) for the correlation/R2 recommendation
                # step to produce a result -- it would otherwise crash the whole
                # try_variables run. Warn and skip this group instead.
                warnings.warn(
                    f"try_variables: skipping model group '{model_group}' "
                    f"({'vacant' if vacant_only else 'main'}) -- could not compute variable "
                    f"recommendations ({type(e).__name__}: {e}). This usually means the group "
                    f"has too few or too-degenerate sales for the requested variables."
                )
                continue

            best_variables = var_recs["variables"]
            df_results = var_recs["df_results"]
            report = var_recs["report"]

            if vacant_only:
                bests["vacant_only"] = df_results
                local_reports["vacant_only"] = report
            else:
                bests["main"] = df_results
                local_reports["main"] = report

        results[model_group] = bests
        reports[model_group] = local_reports

    do_per_model_group(
        df_hydrated,
        settings,
        _try_variables,
        params={
            "settings": settings,
            "df_univ": sup.universe,
            "do_report": do_report,
            "verbose": verbose,
            "results": all_best_variables,
            "reports": all_reports
        },
        key="key_sale",
        skip=model_groups_to_skip
    )

    sale_field = get_sale_field(settings)

    print("")
    print("********** BEST VARIABLES ***********")
    for model_group in all_best_variables:
        entry = all_best_variables[model_group]
        report_entry = all_reports[model_group]
        for vacant_status in entry:
            print("")
            print(f"model group: {model_group} / {vacant_status}")
            results = entry[vacant_status]
            report = report_entry[vacant_status]
            results = results[~results["corr_strength"].isna()]

            styled = results.style.format(
                {
                    "corr_strength": "{:,.2f}",
                    "corr_clarity": "{:,.2f}",
                    "corr_score": "{:,.2f}",
                    "r2": "{:,.2f}",
                    "adj_r2": "{:,.2f}",
                    "coef_sign": "{:,.0f}"
                }
            )

            pd.set_option("display.max_rows", None)
            display(styled)
            pd.set_option("display.max_rows", 15)

            file_out = f"out/try/{model_group}/{vacant_status}.csv"
            report_out = f"out/try/{model_group}/{vacant_status}_report"
            if not os.path.exists(os.path.dirname(file_out)):
                os.makedirs(os.path.dirname(file_out))
            results.to_csv(file_out, index=False)

            if do_report:
                finish_report(report, report_out, "variable", settings)

write_out_all_results

write_out_all_results(sup, all_results, settings)

Write out all model results to CSV and Parquet files.

This function collects predictions from all model groups and writes them to a single DataFrame, which is then saved to both CSV and Parquet formats. It also merges the predictions with the universe DataFrame to include all keys.

It additionally writes the openratiostudy.com export (open_ratio_study_sales.csv and open_ratio_study_test.csv) — the slim per-sale frames that website consumes — by concatenating the ensemble (production) results across all model groups.

Parameters:

Name Type Description Default
sup SalesUniversePair

The SalesUniversePair containing sales and universe data.

required
all_results dict

A dictionary where keys are model group identifiers and values are MultiModelResults containing the results for each model group.

required
settings dict

The settings dictionary, used to resolve the report-location fields for the open-ratio-study export.

required
Source code in openavmkit/model_runner.py
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
def write_out_all_results(sup: SalesUniversePair, all_results: dict, settings: dict):
    """Write out all model results to CSV and Parquet files.

    This function collects predictions from all model groups and writes them to a single
    DataFrame, which is then saved to both CSV and Parquet formats. It also merges the
    predictions with the universe DataFrame to include all keys.

    It additionally writes the openratiostudy.com export (``open_ratio_study_sales.csv``
    and ``open_ratio_study_test.csv``) — the slim per-sale frames that website consumes —
    by concatenating the ensemble (production) results across all model groups.

    Parameters
    ----------
    sup : SalesUniversePair
        The SalesUniversePair containing sales and universe data.
    all_results : dict
        A dictionary where keys are model group identifiers and values are MultiModelResults
        containing the results for each model group.
    settings : dict
        The settings dictionary, used to resolve the report-location fields for the
        open-ratio-study export.
    """
    t = TimingData()
    df_all = None
    ors_frames = {"sales": [], "test": []}

    for model_group in all_results:
        t.start(f"model group: {model_group}")
        t.start("read")
        mm_results: MultiModelResults = all_results[model_group]

        # Skip if no results for this model group
        if mm_results is None:
            t.stop("read")
            t.stop(f"model group: {model_group}")
            continue

        # Collect all ensemble types to output
        output_models = []
        if "ensemble" in mm_results.model_results:
            output_models.append("ensemble")
        if not output_models:
            t.stop("read")
            t.stop(f"model group: {model_group}")
            continue

        # Accumulate the openratiostudy.com export from the ensemble (production) model.
        ors = _assemble_open_ratio_study(mm_results.model_results["ensemble"], settings)
        for subset, df_ors in ors.items():
            df_ors["model_group"] = model_group
            ors_frames[subset].append(df_ors)

        # For each output model, extract predictions and add to df_univ_local
        df_univ_local = None
        for model_type in output_models:
            smr = mm_results.model_results[model_type]
            col_name = (
                f"market_value_{model_type}"
                if "ensemble" not in model_type
                else "market_value"
            )
            df_pred = smr.df_universe[["key", smr.field_prediction]].rename(
                columns={smr.field_prediction: col_name}
            )
            if df_univ_local is None:
                df_univ_local = df_pred
            else:
                df_univ_local = df_univ_local.merge(df_pred, on="key", how="outer")
        df_univ_local["model_group"] = model_group

        if df_all is None:
            df_all = df_univ_local
        else:
            t.start("concat")
            df_all = pd.concat([df_all, df_univ_local])
            t.stop("concat")

        t.stop(f"model group: {model_group}")

    # Only proceed with writing if we have results
    if df_all is not None:
        t.start("copy")
        df_univ = sup.universe.copy()
        t.stop("copy")
        t.start("merge")
        df_univ = df_univ.merge(df_all, on="key", how="left")
        t.stop("merge")

        outpath = "out/models/all_model_groups"
        if not os.path.exists(outpath):
            os.makedirs(outpath)

        t.start("csv")
        df_univ.to_csv(f"{outpath}/universe.csv", index=False)
        t.stop("csv")
        t.start("parquet")
        df_univ.to_parquet(f"{outpath}/universe.parquet", engine="pyarrow")
        t.stop("parquet")

    # Write the openratiostudy.com export (study + test subsets, all model groups).
    if ors_frames["sales"] or ors_frames["test"]:
        outpath = "out/models/all_model_groups"
        if not os.path.exists(outpath):
            os.makedirs(outpath)
        t.start("open_ratio_study")
        for subset, fname in (("sales", "open_ratio_study_sales.csv"),
                              ("test", "open_ratio_study_test.csv")):
            frames = ors_frames[subset]
            if not frames:
                continue
            pd.concat(frames, ignore_index=True).to_csv(f"{outpath}/{fname}", index=False)
        t.stop("open_ratio_study")