Java Mean Calculation calculateMeanAndStandardDeviationIgnoreNegatives( float[] data)

Here you can find the source of calculateMeanAndStandardDeviationIgnoreNegatives( float[] data)

Description

All negative values in array are ignored

License

Open Source License

Parameter

Parameter Description
data a parameter

Declaration

public static float[] calculateMeanAndStandardDeviationIgnoreNegatives(
        float[] data) 

Method Source Code

//package com.java2s;
/*/*  www  .  j a  v a2 s .c  om*/
 * Copyright (c) 2008-2013 Maksim Khadkevich and Fondazione Bruno Kessler.
 *
 * This file is part of MART.
 * MART is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2, as published
 * by the Free Software Foundation.
 *
 * MART 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 MART; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    /**
     * All negative values in array are ignored
     *
     * @param data
     * @return
     */
    public static float[] calculateMeanAndStandardDeviationIgnoreNegatives(
            float[] data) {
        float sumMean = 0;
        float sumDev = 0;
        float mean, dev;
        int count = 0;

        for (int i = 0; i < data.length; i++) {
            if (data[i] > 0) {
                sumMean += data[i];
                count++;
            }
        }

        if (count == 0) {
            return new float[2];
        }

        mean = sumMean / count;

        for (int i = 0; i < data.length; i++) {
            if (data[i] > 0) {
                sumDev += (mean - data[i]) * (mean - data[i]);
            }
        }
        dev = (float) Math.sqrt(sumDev / count);

        float[] out = new float[2];
        out[0] = mean;
        out[1] = dev;

        return out;
    }
}

Related

  1. calculateMean(double[] values)
  2. calculateMean(float[] data)
  3. calculateMeanAndStandardDeviation(float[] data)
  4. calculateMeanAndStandardDeviationVectors( float[][] vectors)
  5. calculateMeanLevel(float[] distribution, int mid, int oldMid, float difference)
  6. calculateMeanSD(double[] d)