NS.java [src/csip/cosu] Revision:   Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip.cosu;

/**
 *
 * Calculates the efficiency between a test data set and a verification data set
 * after Nash & Sutcliffe (1970). The efficiency is described as the proportion
 * of the cumulated cubic deviation between both data sets and the cumulated
 * cubic deviation between the verification data set and its mean value.
 */
class NS extends ObjFunc {

  @Override
  public String name() {
    return "Nash Sutcliffe";
  }


  @Override
  public double eval(double[] obs, double[] sim, double missing) {
    checkArrays(sim, obs);
    int pre_size = sim.length;
    int steps = pre_size;
    double sum_vd = 0;
    int size = 0;
    for (int i = 0; i < obs.length; i++) {
      if (obs[i] > missing) {
        sum_vd += obs[i];
        size++;
      }
    }
    if (size == 0)
      throw new RuntimeException("No valid observed values.");
    double mean_vd = sum_vd / size;
    /*
     * calculating mean pow deviations
     */
    double td_vd = 0;
    double vd_mean = 0;
    for (int i = 0; i < steps; i++) {
      if (obs[i] > missing) {
        td_vd += (Math.pow(Math.abs(obs[i] - sim[i]), 2.0));
        vd_mean += (Math.pow(Math.abs(obs[i] - mean_vd), 2.0));
      }
    }
    if (Double.compare(vd_mean, 0.0) == 0)
      throw new RuntimeException("No valid mean values.");
    return 1 - (td_vd / vd_mean);
  }


  @Override
  public int direction() {
    return 1;
  }


  @Override
  public int optimum() {
    return 1;
  }

}