# Welcome to ddop


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

`ddop` is a Python library for data-driven operations management. The
goal of `ddop` is to provide well-established data-driven operations
management tools within a programming environment that is accessible and
easy to use even for non-experts. At the current state `ddop` contains
well known data-driven newsvendor models, a set of performance metrics
that can be used for model evaluation and selection, as well as datasets
that are useful to quickly illustrate the behavior of the various
algorithms implemented in `ddop` or as benchmark for testing new models.
Through its consistent and easy-to-use interface one can run and compare
provided models with only a few lines of code.

## Install

`ddop` is available via PyPI using:

``` sh
pip install ddop2
```

## Quickstart

`ddop` provides a varity of newsvendor models. The following example
shows how to use one of these models for decision making. It assumes a
very basic knowledge of data-driven operations management practices.

As first step we initialize the model we want to use. In this example
`RandomForestWeightedNewsvendor`.

``` python
from ddop2.newsvendor import RandomForestWeightedNewsvendor
rf_nv = RandomForestWeightedNewsvendor(cu=2, co=1)
```

    2023-08-09 22:10:47.225307: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
    To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
    2023-08-09 22:10:48.239997: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

A model can take a set of parameters, each describing the model or the
optimization problem it tries to solve. Here we set the underage costs
cu to 2 and the overage costs co to 1.

As next step we load the Yaz Dataset and split it into train and test
set.

``` python
from ddop2.datasets import load_yaz
from sklearn.model_selection import train_test_split
X, y = load_yaz(one_hot_encoding=True, return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=False, random_state=0)
```

After the model is initialized, the `fit` method can be used to learn a
decision model from the training data `X_train`, `y_train`.

``` python
rf_nv.fit(X_train, y_train)
```

<style>#sk-container-id-1 {color: black;background-color: white;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: "▸";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: "▾";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: "";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: "";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id="sk-container-id-1" class="sk-top-container"><div class="sk-text-repr-fallback"><pre>RandomForestWeightedNewsvendor(co=1, cu=2)</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class="sk-container" hidden><div class="sk-item"><div class="sk-estimator sk-toggleable"><input class="sk-toggleable__control sk-hidden--visually" id="sk-estimator-id-1" type="checkbox" checked><label for="sk-estimator-id-1" class="sk-toggleable__label sk-toggleable__label-arrow">RandomForestWeightedNewsvendor</label><div class="sk-toggleable__content"><pre>RandomForestWeightedNewsvendor(co=1, cu=2)</pre></div></div></div></div></div>

We can then use the predict method to make a decision for new data
samples.

``` python
rf_nv.predict(X_test)
```

    array([[ 5,  4, 14, ..., 23, 35, 22],
           [ 6,  6, 11, ..., 26, 37, 23],
           [ 8,  8, 16, ..., 35, 55, 40],
           ...,
           [ 5,  6, 12, ..., 23, 41, 25],
           [ 6,  6, 13, ..., 24, 41, 32],
           [ 8,  9, 15, ..., 34, 57, 42]])

To get a representation of the model’s decision quality we can use the
`score` function, which takes as input `X_test` and `y_test`. The
`score` function makes a decision for each sample in `X_test` and
calculates the negated average costs with respect to the true values
`y_test` and the overage and underage costs.

``` python
rf_nv.score(X_test, y_test)
```

    -6.859375000000001
