g@9646: /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde g@9646: * Software engineering by g@9646: * Björnsen Beratende Ingenieure GmbH g@9646: * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt g@9646: * g@9646: * This file is Free Software under the GNU AGPL (>=v3) g@9646: * and comes with ABSOLUTELY NO WARRANTY! Check out the g@9646: * documentation coming with Dive4Elements River for details. g@9646: */ g@9646: package org.dive4elements.river.artifacts.model.fixings.fitting; g@9646: g@9646: import java.util.Arrays; g@9646: g@9646: import org.apache.commons.math3.exception.MathIllegalArgumentException; g@9646: import org.apache.commons.math3.stat.descriptive.AbstractUnivariateStatistic; g@9646: import org.apache.commons.math3.stat.descriptive.UnivariateStatistic; g@9646: g@9646: /** g@9646: * @author Gernot Belger g@9646: */ g@9646: final class ChiSquare extends AbstractUnivariateStatistic { g@9646: g@9646: private final double[] observation; g@9646: g@9646: public ChiSquare(final double[] observation) { g@9646: this.observation = observation; g@9646: } g@9646: g@9646: @Override g@9646: public UnivariateStatistic copy() { g@9646: /* stateless, hence we can return this */ g@9646: return this; g@9646: } g@9646: g@9646: @Override g@9646: public double evaluate(final double[] values, final int begin, final int length) throws MathIllegalArgumentException { g@9646: test(values, begin, length, true); g@9646: g@9646: final double[] residualsWeights = new double[length]; g@9646: Arrays.fill(residualsWeights, 0, length - 1, 1.0); g@9646: g@9646: double cost = 0; g@9646: g@9646: for (int i = begin; i < begin + length; i++) { g@9646: final double residual = values[i + begin] - this.observation[i + begin]; g@9646: // final double wresiduals = residual * FastMath.sqrt(residualsWeights[i]); g@9646: cost += residualsWeights[i] * residual * residual; g@9646: } g@9646: g@9646: return cost; g@9646: } g@9646: }