view artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/fitting/ChiSquare.java @ 9646:0380717105ba

Implemented alternative fitting strategy for Log-Linear function.
author Gernot Belger <g.belger@bjoernsen.de>
date Mon, 02 Dec 2019 17:56:15 +0100
parents
children
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.model.fixings.fitting;

import java.util.Arrays;

import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic;
import org.apache.commons.math3.stat.descriptive.UnivariateStatistic;

/**
 * @author Gernot Belger
 */
final class ChiSquare extends AbstractUnivariateStatistic {

    private final double[] observation;

    public ChiSquare(final double[] observation) {
        this.observation = observation;
    }

    @Override
    public UnivariateStatistic copy() {
        /* stateless, hence we can return this */
        return this;
    }

    @Override
    public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException {
        test(values, begin, length, true);

        final double[] residualsWeights = new double[length];
        Arrays.fill(residualsWeights, 0, length - 1, 1.0);

        double cost = 0;

        for (int i = begin; i < begin + length; i++) {
            final double residual = values[i + begin] - this.observation[i + begin];
            // final double wresiduals = residual * FastMath.sqrt(residualsWeights[i]);
            cost += residualsWeights[i] * residual * residual;
        }

        return cost;
    }
}

http://dive4elements.wald.intevation.org