package presenter.evaluation;

import model.Line;
import presenter.algorithms.util.FastElementSelector;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
 *
 * @Author: Armin Wolf
 * @Email: a_wolf28@uni-muenster.de
 * @Date: 07.09.2017.
 */
public class PercentageErrorBasedMeasure {


    private ArrayList<Double> percentageError;

    public PercentageErrorBasedMeasure(final LinkedList<Line> lines, Double m, Double b) {

        ArrayList<Double> sampson = new ArrayList<>();

        for (Line line : lines) {
            Double e = Math.pow(m * line.getM() - line.getB() + b, 2) / (Math.pow(m, 2) + 1);
            sampson.add(e);
        }

        percentageError = new ArrayList<>();

        for (int j = 0; j < sampson.size(); j++) {
            percentageError.add(100 * sampson.get(j) / lines.get(j).getB());
        }
    }

    /* Percentege Error Approximation Measures */
    //verschiedene Eingaben für einen Alg.
    public Double mape() {
        double error = 0;

        for (Double d : percentageError) {
            error += Math.abs(d);
        }

        error /= percentageError.size();

        return error;
    }

    public Double mdape() {

        ArrayList<Double> abs = new ArrayList<>();

        for (Double d : percentageError) {
            abs.add(Math.abs(d));
        }

        return FastElementSelector.randomizedSelect(abs, abs.size() * 0.5);
    }

    public Double rmspe() {
        double error = 0;

        for (Double d : percentageError) {
            error += Math.pow(d, 2);
        }

        error /= percentageError.size();

        return Math.sqrt(error);
    }

    public Double rmdspe() {
        ArrayList squares = new ArrayList();
        for (Double d : percentageError) {
            squares.add(Math.pow(d, 2));
        }

        return Math.sqrt(FastElementSelector.randomizedSelect(squares, squares.size() * 0.5));
    }
}
