> For the complete documentation index, see [llms.txt](https://doc.verteego.com/verteego-doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.verteego.com/verteego-doc/pipelines/forecasting-pipelines/calculators/mathematic/binary_operation.md).

# binary\_operation

Performs a binary arithmetic operation.

## Usage

{% hint style="info" %}
This calculator allows the user to do arithmetical operation between 2 columns like an addition, multiplication, max or other. Constant value can be used instead of column name in `input_columns`.
{% endhint %}

This calculator can be used with the following method:

<mark style="color:red;">**`binary_operation`**</mark>

Examples:

* Apply a discount rate to a product price
* Round quantity to be above 0

***

## Main Parameters

{% hint style="success" %}
**The bold options** represent the default values when the parameters are optional.
{% endhint %}

* *<mark style="color:blue;">input\_columns</mark>* \
  list of columns used as input of the calculators: The list of columns that will be used to fill the output column.
* *<mark style="color:blue;">output\_columns</mark>* \
  list of columns added by the calculators : Name of the filled column added to the dataset.
* *<mark style="color:blue;">global</mark>* *(true, **false)*** \
  Should this calculator be performed before data splitting during training for cross-validation
* *<mark style="color:blue;">steps</mark>* \[optionnal] *(**training, prediction**, postprocessing*) \
  List of steps in a pipeline where columns from this calculator are added to the data. Note that when the training option is listed, the calculator is actually added during preprocessing.
* *<mark style="color:blue;">store\_in\_model</mark>* \[optionnal] *(true, **false)*** \
  Please indicate whether the "calculated" columns by the calculator should be stored in the model or not to avoid recalculating them during prediction. This is only relevant if the calculated columns are added to both training and prediction. Without this parameter, the values will not be stored in the model. The following parameters only make sense if this parameter is set to *true*.
* *<mark style="color:blue;">stored\_columns</mark>* \[required if *<mark style="color:blue;">store\_in\_model</mark> is true*] \
  List indicating the columns to be stored among the *<mark style="color:blue;">output\_columns</mark>*.
* *<mark style="color:blue;">stored\_keys</mark>* \[required if *<mark style="color:blue;">store\_in\_model</mark> is true*] \
  List indicating the columns to use for identifying the correct values to join on the data for prediction among the stored values (logically, they are to be chosen from the *<mark style="color:blue;">input\_columns</mark>*).

***

## Specific Parameters

* <mark style="color:blue;">operation</mark> \
  (add, eq, ge, gt, le, lt, max, min, mod, mul, ne, sub, truediv) \
  operation you can choose from.

***

## Examples

1. After doing the prediction some of the quantity predicted (`qty_pred`) are negative which is not logic. To solve this issue the user wants to round the quantity predicted below 0 to 0 in post processing.

   ```yaml
   calculated_cols:
     round_negative_quantity:
         method: binary_operation
         input_columns:
         - qty_pred
         - 0
         output_columns:
         - qty_pred_wo_neg
         params:
           operation: max
         steps:
         - postprocessing
   ```

   **Result :**

   | qty\_pred | qty\_pred\_wo\_neg |
   | --------- | ------------------ |
   | 10        | 10                 |
   | 15        | 15                 |
   | -7        | 0                  |
   | 0         | 0                  |
   | 2         | 2                  |
   | 9         | 9                  |
2. The user wants to apply a discount rate (`discount_rate`) to a product price (`item_price`).

   <pre class="language-yaml"><code class="lang-yaml"><strong>calculated_cols:
   </strong>  apply_discount:
         method: binary_operation
         input_columns:
         - discount_rate
         - item_price
         output_columns:
         - discounted_item_price
         params:
           operation: mul
   </code></pre>

   **Result:**

   | discount\_rate | item\_price | discounted\_item\_price |
   | -------------- | ----------- | ----------------------- |
   | 0.9            | 5           | 4.5                     |
   | 0.9            | 7           | 6.3                     |
   | 0.9            | 9           | 8.1                     |
   | 0.5            | 5           | 2.5                     |
   | 0.5            | 7           | 3.5                     |
   | 0.5            | 9           | 4.5                     |
