NSLOG2.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;

/**
 *
 */
class NSLOG2 extends ObjFunc {

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

    @Override
    public double eval(double[] obs, double[] sim, double missing) {
        checkArrays(sim, obs);
        double result = 0;
        double pow = 2;
        double rsme = 0;
        double var = 0;
        double avg = 0;
        int count = 0;
        for (int i = 0; i < obs.length; i++) {
            if (obs[i] > 0 && obs[i] != missing) {
                avg += Math.log(obs[i]);
                count++;
            }
        }
        if (count == 0) {
            throw new RuntimeException("No valid count.");
        }
        avg /= count;
        for (int i = 0; i < obs.length; i++) {
            if (obs[i] > 0 && sim[i] > 0 && obs[i] != missing) {
                rsme += Math.pow(Math.abs(Math.log(obs[i]) - Math.log(sim[i])), pow);
                var += Math.pow(Math.abs(Math.log(obs[i]) - avg), pow);
            }
        }
        if (Double.compare(var, 0.0) == 0) {
//            throw new RuntimeException("No valid var value.");
            result = 0;
        } else {
            result = 1.0 - (rsme / var);
            if (Double.isNaN(result)) {
                result = 0;
            }
        }
        return result;
    }

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

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

}