# `OPTWIN`

### *class* capymoa.drift.detectors.OPTWIN[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/drift/detectors/optwin.py#L13)

Bases: [`BaseDriftDetector`](capymoa.drift.base_detector.BaseDriftDetector.md#capymoa.drift.base_detector.BaseDriftDetector)

Optimal Window Concept Drift Detector

Drift Identification with Optimal Sub-Windows (OPTWIN) <sup>[1](#id2)</sup> is a drift detection
method.

```pycon
>>> import numpy as np
>>> from capymoa.drift.detectors import OPTWIN
>>> np.random.seed(0)
>>>
>>> detector = OPTWIN(rigor=0.1, drift_confidence=0.9)
>>>
>>> data_stream = np.random.randint(2, size=2000)
>>> for i in range(999, 2000):
...     data_stream[i] = np.random.randint(4, high=8)
>>>
>>> for i in range(2000):
...     detector.add_element(data_stream[i])
...     if detector.detected_change():
...         print('Change detected in data: ' + str(data_stream[i]) + ' - at index: ' + str(i))
Change detected in data: 6 - at index: 1164
```

* <a id='id2'>**[1]**</a> Tosi, Mauro D. L., and Martin Theobald. “OPTWIN: Drift Identification with Optimal Sub-Windows.” 2024 IEEE 40th International Conference on Data Engineering Workshops (ICDEW), 2024.

#### \_\_init_\_(rigor: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.5, drift_confidence: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.999, warning_confidence: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.9, empty_w: [bool](https://docs.python.org/3/builtins/functions.html#bool) = True, w_length_max: [int](https://docs.python.org/3/builtins/functions.html#int) = 1_000, w_length_min: [int](https://docs.python.org/3/builtins/functions.html#int) = 30, minimum_noise: [float](https://docs.python.org/3/builtins/functions.html#float) = 1e-6)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/drift/detectors/optwin.py#L84)

Initialize the OPTWIN drift detector.

* **Parameters:**
  * **rigor** – Rigorousness of drift identification
  * **drift_confidence** – Confidence value chosen by user
  * **warning_confidence** – Confidence value for warning zone
  * **empty_w** – Empty window when drift is detected
  * **w_length_max** – Maximum window size. 25000 is recommended but slows down
    initialization as it pre-computes optimal cuts for all window sizes up to
    `w_length_max`.
  * **w_length_min** – Minimum window size
  * **minimum_noise** – Noise to be added to stdev in case it is 0

#### add_element(element: [float](https://docs.python.org/3/builtins/functions.html#float)) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/drift/detectors/optwin.py#L333)

Add the new element and perform change detection

* **Parameters:**
  **element** – The new observation

#### detected_change() → [bool](https://docs.python.org/3/builtins/functions.html#bool)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/drift/base_detector.py#L52)

Is the detector currently detecting a concept drift?

#### detected_warning() → [bool](https://docs.python.org/3/builtins/functions.html#bool)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/drift/base_detector.py#L56)

Is the detector currently warning of an upcoming concept drift?

#### *classmethod* from_params(schema: [Any](https://docs.python.org/3/library/typing.html#typing.Any) = None, params: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1) → [Any](https://docs.python.org/3/library/typing.html#typing.Any)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L170)

Construct an instance from parameters produced by `get_params`.

#### get_params() → [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)][[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/drift/detectors/optwin.py#L479)

Get the hyper-parameters of the OPTWIN drift detector.

#### reset(clean_history: [bool](https://docs.python.org/3/builtins/functions.html#bool) = False) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/drift/detectors/optwin.py#L444)

Reset the detector state.

Clears the sliding window, running statistics, and per-drift bookkeeping
so that replaying the same input reproduces the as-constructed flag
trace. Hyper-parameters and the pre-computed optimal-cut tables are
preserved (they are data-independent).

* **Parameters:**
  **clean_history** – Whether to reset detection history, defaults to False

#### REQUIRES_FIT *: [bool](https://docs.python.org/3/builtins/functions.html#bool)* *= False*

If `True`, this detector needs a reference distribution before
it can detect change. Concept drift detectors leave this as
`False` and only use [`add_element()`](#capymoa.drift.detectors.OPTWIN.add_element).
