# Constraints (monotonic)
URL: https://docs.datarobot.com/en/docs/classic-ui/modeling/analyze-models/describe/monotonic.html

> How to force an XGBoost model to learn only monotonic (always increasing or always decreasing) relationships between chosen features and the target.

# Constraints (monotonic)

In some projects (typically insurance and banking), you may want to force the directional relationship between a feature and the target (for example, higher home values should always lead to higher home insurance rates). By training with monotonic constraints, you force certain XGBoost models to learn only monotonic (always increasing or always decreasing) relationships between specific features and the target. For example, increasing values in feature(s) that describe the value of the home have an always increasing relationship with the target, `claim losses`. You set the direction of the relationships—increasing or decreasing— by creating feature lists and applying them in [Advanced options > Feature Constraints](https://docs.datarobot.com/en/docs/classic-ui/modeling/build-models/adv-opt/feature-con.html). You can also create lists and retrain models from the Leaderboard menu after the initial model run.

## General workflow

Typically, working with monotonic constraints follows the workflow described below. Review the [modeling considerations](https://docs.datarobot.com/en/docs/classic-ui/modeling/analyze-models/describe/monotonic.html#feature-considerations) before starting, including special considerations for time series projects.

1. Create feature listsfor monotonically increasing and/or monotonically decreasing constraints.
2. Set the target and the modeling feature list.
3. From theAdvanced optionslink,configure feature constraints.
4. Start the model build using any of themodeling modes.
5. When model building finishes,investigate the results.
6. (Optional)Retrain model(s)using different monotonic constraints (or without constraints).
7. Compare training results between models using theFeature Effectspartial dependence graph to help with final model selection.

## Create feature lists

The first step in monotonic modeling is to [create feature lists](https://docs.datarobot.com/en/docs/classic-ui/modeling/build-models/build-basic/feature-lists.html#create-feature-lists) that identify monotonically constrained features. The feature(s) contained in these list(s) are those that have a forced directional relationship, increasing or decreasing, with the target. Note that use of monotonic feature lists does not replace the standard model building feature list. Instead, these lists identify features in the selected model building feature list that should be handled differently.

When creating lists, remember:

- Features must be of type numeric, percentage, length, or currency.
- There can be no feature overlap in the feature lists for monotonically increasing and monotonically decreasing constraints.
- After creating lists, be sure to change the Feature List dropdown to the list for the project model build (otherwise, it remains on the last created list).
- The target feature is automatically added to every feature list.

## Evaluate results

When model building is complete, there are several tools for investigating and evaluating the results. First, check the Leaderboard to identify the model(s) that are, or can be, trained with monotonic constraints (identified with the MONO [https://docs.datarobot.com/en/docs/images/icon-mono.png](https://docs.datarobot.com/en/docs/images/icon-mono.png) badge). To determine if a model was built with constraints, check the Leaderboard model description.

Expand a constrained model and, from the Describe > Constraints tab, review the features that were constrained:

There are cases where some, or all, of the features identified in the constraint list were not used in the final modeling feature list. This happens, for example, when DataRobot creates a reduced feature list for the recommended model. In essence, this also serves as a constraint.

Next, calculate and view the [Feature Effects](https://docs.datarobot.com/en/docs/classic-ui/modeling/analyze-models/understand/feature-effects-classic.html) partial dependence graph, checking the results for monotonically constrained features. This graph helps determine whether a monotonically constrained feature maintains the monotonic relationship with the predicted target.
Partial dependence is calculated, based on the validation set, between each predictor and predicted target values—it calculates the relationship between average target values and predictor values of each bin. Since impacts from other predictors on predicted target values tend to be averaged out, partial dependence reflects the relationship between an individual predictor and the target. If a model is trained with monotonic constraints, the relationship between monotonically constrained features and predicted target tends to be monotonic.

For example, this graph plots partial dependence on a model trained without constraints:

When monotonically decreasing constraints are applied, partial dependence looks more like this:

## Retrain models with new constraints

You may decide, after a model build, to change the monotonic feature list and rerun one or more models. To do this:

1. From the Leaderboard, select models that support constraints by checking the box to the left of the model name. Click the MONO badge to filter the Leaderboard display so that it shows only eligible models.
2. Expand the menu and selectRun Selected Model(s) with Constraints. Note that this option is only available if all selected models support constraints.
3. In the upper part of the screen, the window expands and provides a dropdown for selecting monotonic increasing and decreasing feature lists.
4. Once set, clickRun Models.

## Feature considerations

The following considerations apply to monotonic modeling.

- Blenders that contain monotonic models do not display the MONO label on the Leaderboard.
- Monotonic modeling is available in binary classification and regression projects.
- Monotonic constraints can only be applied between Numeric, Percentage, Length, or Currency type variables and the target.
- Generalized Additive Models, Frequency/Severity, and Frequency/Cost models don't support interactions with features trained with monotonic constraints.
- Only Extreme Gradient Boosted Trees, Generalized Additive Models and Frequency/Severity (both frequency and severity model based on XGBoost) and Frequency/Cost model (both frequency and cost model based on XGBoost) support training with monotonic constraints.
- Extreme Gradient Boosted Trees that includeSearch for differencestasks (i.e., DIFF3),Search for ratiotasks (i.e., RATIO3) don’t support training with monotonic constraints. (This is because these tasks tend to change the monotonic correlation between features and predicted target.)
- Constraints for models used by Autopilot or from the Repository can only be set inadvanced optionsand can't be changed after a project is created.
- New constraints can only be set for models on the Leaderboard.
- IfInclude only monotonic modelsis selected in advanced options, Autopilot only runs the average blender.
- When using monotonic constraints with time series projects:

---
