co.turnus.common.util.CommonDataUtil.java Source code

Java tutorial

Introduction

Here is the source code for co.turnus.common.util.CommonDataUtil.java

Source

/* 
 * TURNUS, the co-exploration framework
 * 
 * Copyright (C) 2014 EPFL SCI STI MM
 *
 * This file is part of TURNUS.
 *
 * TURNUS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TURNUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TURNUS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Additional permission under GNU GPL version 3 section 7
 * 
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or 
 * an Eclipse library), containing parts covered by the terms of the 
 * Eclipse Public License (EPL), the licensors of this Program grant you 
 * additional permission to convey the resulting work.  Corresponding Source 
 * for a non-source form of such a combination shall include the source code 
 * for the parts of Eclipse libraries used as well as that of the  covered work.
 * 
 */
package co.turnus.common.util;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
import org.apache.commons.math3.util.FastMath;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.util.EcoreUtil;

import co.turnus.common.CommonFactory;
import co.turnus.common.StatisticalData;

public class CommonDataUtil {

    public static StatisticalData createFrom(SummaryStatistics summary) {
        StatisticalData data = CommonFactory.eINSTANCE.createStatisticalData();
        if (summary.getN() != 0) {
            data.setMax(summary.getMax());
            data.setMin(summary.getMin());
            data.setSamples(summary.getN());
            data.setSum(summary.getSum());
            data.setVariance(summary.getVariance());
            data.setMean(summary.getMean());
        }
        return data;
    }

    public static <T> double averageOf(EMap<T, StatisticalData> map) {
        double val = 0;
        long n = 0;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val += s.getValue().getMean();
            n += s.getValue().getSamples();
        }
        return val / n;
    }

    public static <T> double averageOf(Map<T, StatisticalData> map) {
        double val = 0;
        long n = 0;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val += s.getValue().getMean();
            n += s.getValue().getSamples();
        }
        return val / n;
    }

    public static <T> double maxOf(EMap<T, StatisticalData> map) {
        double val = Double.MIN_VALUE;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val = FastMath.max(val, s.getValue().getMin());
        }
        return val;
    }

    public static <T> double maxOf(Map<T, StatisticalData> map) {
        double val = Double.MIN_VALUE;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val = FastMath.max(val, s.getValue().getMin());
        }
        return val;
    }

    public static void merge(StatisticalData target, StatisticalData[] data, double cov) {
        for (StatisticalData d : data) {
            merge(target, d, cov);
        }
    }

    public static void merge(StatisticalData target, StatisticalData data, double cov) {

        double max = FastMath.max(target.getMax(), data.getMax());
        target.setMax(max);

        double min = FastMath.min(target.getMin(), data.getMin());
        target.setMin(min);

        double sum = target.getSum() + data.getSum();
        target.setSum(sum);

        double mean = target.getMean() + data.getMean();
        target.setMean(mean);

        double var = target.getVariance() + data.getVariance() + 2 * cov;
        target.setVariance(var);

        long samples = target.getSamples() + data.getSamples();
        target.setSamples(samples);

    }

    public static <T> double minOf(EMap<T, StatisticalData> map) {
        double val = Double.MAX_VALUE;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val = FastMath.min(val, s.getValue().getMin());
        }
        return val;
    }

    public static <T> double minOf(Map<T, StatisticalData> map) {
        double val = Double.MAX_VALUE;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val = FastMath.min(val, s.getValue().getMin());
        }
        return val;
    }

    public static StatisticalData sum(StatisticalData[] data, double cov) {
        if (data.length == 1) {
            return EcoreUtil.copy(data[0]);
        }

        StatisticalData sum = CommonFactory.eINSTANCE.createStatisticalData();
        sum.setMax(Double.MIN_VALUE);
        sum.setMin(Double.MAX_VALUE);
        sum.setVariance(-2 * cov); // initial offset for the first iteration...

        for (StatisticalData d : data) {
            sum.setMax(FastMath.max(sum.getMax(), d.getMax()));
            sum.setMin(FastMath.min(sum.getMin(), d.getMin()));
            sum.setMean(sum.getMean() + d.getMean());
            sum.setSamples(sum.getSamples() + d.getSamples());
            sum.setVariance(sum.getVariance() + d.getVariance() + 2 * cov);
            sum.setSamples(sum.getSamples() + d.getSamples());
        }

        return sum;
    }

    public static StatisticalData sum(StatisticalData o1, StatisticalData o2, double cov) {
        StatisticalData sum = CommonFactory.eINSTANCE.createStatisticalData();
        sum.setMax(FastMath.max(o1.getMax(), o2.getMax()));
        sum.setMin(FastMath.min(o1.getMin(), o2.getMin()));
        sum.setMean(o1.getMean() + o2.getMean());
        sum.setSamples(o1.getSamples() + o2.getSamples());
        sum.setVariance(o1.getVariance() + o2.getVariance() + 2 * cov);
        sum.setSamples(o1.getSamples() + o2.getSamples());
        return sum;

    }

    public static <T> double sumOf(EMap<T, StatisticalData> map) {
        double val = 0;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val += s.getValue().getMean();
        }
        return val;
    }

    public static <T> double sumOf(Map<T, StatisticalData> map) {
        double val = 0;
        for (Entry<T, StatisticalData> s : map.entrySet()) {
            val += s.getValue().getMean();
        }
        return val;
    }

}