Functions for Statistics : Statistics « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Statistics 




Functions for Statistics
    

/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */
//package org.wiztools.commons;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 @author subWiz
 */
public final class Statistics {
    private Statistics() {}

    private static final String LOG_ARRAY_LENGTH_0 = "Length of array cannot be 0!";

    /* MEAN */

    public static int mean(final int[] listthrows IllegalArgumentException {
        if(list.length == 0) {
            throw new IllegalArgumentException(LOG_ARRAY_LENGTH_0);
        }
        int sum = 0;
        for(int n: list) {
            sum += n;
        }
        return sum / list.length;
    }

    public static long mean(final long[] listthrows IllegalArgumentException {
        if(list.length == 0) {
            throw new IllegalArgumentException(LOG_ARRAY_LENGTH_0);
        }
        long sum = 0;
        for(long n: list) {
            sum += n;
        }
        return sum / list.length;
    }

    public static float mean(final float[] listthrows IllegalArgumentException {
        if(list.length == 0) {
            throw new IllegalArgumentException(LOG_ARRAY_LENGTH_0);
        }
        float sum = 0;
        for(float n: list) {
            sum += n;
        }
        return sum / list.length;
    }

    public static double mean(final double[] listthrows IllegalArgumentException {
        if(list.length == 0) {
            throw new IllegalArgumentException(LOG_ARRAY_LENGTH_0);
        }
        float sum = 0;
        for(double n: list) {
            sum += n;
        }
        return sum / list.length;
    }

    /* MEDIAN */

    public static int median(final int[] listthrows IllegalArgumentException {
        if(list.length < 1) {
            throw new IllegalArgumentException();
        }
        Arrays.sort(list);
        if((list.length % 2== 0) { // is even
            final int idx = list.length / 2;
            return (list[idx+ list[idx-1]) 2;
        }
        else // is odd
            final int idx = list.length / 2;
            return list[idx];
        }
    }

    /* MODE */

    private static class ModeCompute <T extends Number> {
        private Map<T, Integer> map = new HashMap<T, Integer>();
        private int maxFrequency = 0;

        void add(T t) {
            final int count = (map.get(t== null)1(map.get(t1);

            // Update map:
            map.put(t, count);

            // Update maxFrequency:
            if(count > maxFrequency)
                maxFrequency = count;
        }

        List<T> getMode() {
            List<T> out = new ArrayList<T>();

            for(Map.Entry<T, Integer> entry: map.entrySet()) {
                if(entry.getValue() == maxFrequency) {
                    out.add(entry.getKey());
                }
            }

            return out;
        }
    }

    public static int[] mode(final int[] listthrows IllegalArgumentException {
        if(list.length == 0) {
            throw new IllegalArgumentException(LOG_ARRAY_LENGTH_0);
        }

        final ModeCompute<Integer> modeComp = new ModeCompute<Integer>();
        for(int i=0; i<list.length; i++) {
            modeComp.add(list[i]);
        }

        final List<Integer> l = modeComp.getMode();

        // Prepare the output array:
        final int[] out = new int[l.size()];
        Iterator<Integer> itr = l.iterator();
        for(int i=0; i<out.length; i++) {
            out[i= itr.next();
        }

        return out;
    }
}

   
    
    
    
  














Related examples in the same category
1.Statistics Functions
2.Functions for StatisticsFunctions for Statistics
3.Standard deviation is a statistical measure of spread or variability.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.