# Copyright 2021 IRT Saint Exupéry, https://www.irt-saintexupery.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# Contributors:
#    INITIAL AUTHORS - API and implementation and/or documentation
#        :author: Matthias De Lozzo
#    OTHER AUTHORS   - MACROSCOPIC CHANGES
r"""Expected improvement for the maximum.

Statistics:

.. math::

EI[x] = E[\max(Y(x)-y_{max},0)]

where :math:y_{max}=\max_{1\leq i \leq n}~y^{(i)}.

Bootstrap estimator:

.. math::

\widehat{EI}[x] = \frac{1}{B}\sum_{b=1}^B \max(Y_b(x)-f_{max},0)

"""
from __future__ import annotations

from numpy import ndarray

[docs]class MaxExpectedImprovement(MLDataAcquisitionCriterion):
"""Expected Improvement of the regression model for the maximum.

This criterion is scaled by the output range.
"""

def _get_func(self):
def func(input_data: ndarray) -> float:
"""Evaluation function.

Args:
input_data: The model input data.

Returns:
The acquisition criterion value.
"""
data = self.algo_distribution.learning_set
maximum_output = max(data.get_data_by_group(data.OUTPUT_GROUP))
expected_improvement = self.algo_distribution.compute_expected_improvement(
input_data, maximum_output, True
)
return expected_improvement / self.output_range

return func