All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.cilib.math.Stats Maven / Gradle / Ivy

/**           __  __
 *    _____ _/ /_/ /_    Computational Intelligence Library (CIlib)
 *   / ___/ / / / __ \   (c) CIRG @ UP
 *  / /__/ / / / /_/ /   http://cilib.net
 *  \___/_/_/_/_.___/
 */
package net.sourceforge.cilib.math;

import static fj.Equal.*;
import fj.F;
import fj.Function;
import static fj.Function.*;
import static fj.Ord.*;
import fj.P;
import fj.P2;
import fj.data.List;
import static fj.data.List.*;
import fj.data.Stream;
import static fj.function.Doubles.*;
import fj.function.Integers;
import net.sourceforge.cilib.util.functions.Utils;

public final class Stats {
    
    private Stats() {}
    
    public static Double mean(final Iterable a) {
        return mean.f(a);
    }
    
    public static Double median(final Iterable a) {
        return median.f(a);
    }
    
    public static Double variance(final Iterable a) {
        return variance.f(a);
    }
    
    public static Double stdDev(final Iterable a) {
        return stdDev.f(a);
    }
    
    public static Iterable rank(final Iterable a) {
        return rank.f(a);
    }
    
    public static F, Double> mean =
        new F, Double>() {
            @Override
            public Double f(Iterable a) {
                List l = iterableList(a);
                return sum(l) / l.length();
            }
        };
    
    public static F, Double> variance =
        new F, Double>() {
            @Override
            public Double f(Iterable a) {
                List l = iterableList(a);
                double m = mean.f(a);
                int len = l.length();
                return sum(l.map(flip(subtract).f(m).andThen(flip(power).f(2.0)))) / len;
            }
        };
    
    public static F, Double> stdDev =
        new F, Double>() {
            @Override
            public Double f(Iterable a) {
                return Math.sqrt(variance.f(a));
            }
        };
    
    public static F, Double> median =
        new F, Double>() {
            @Override
            public Double f(Iterable a) {
                List l = iterableList(a).sort(doubleOrd);
                int len = l.length();
                P2, List> split = l.splitAt(len / 2);
                
                if (len % 2 == 0) {
                    return (split._1().last() + split._2().head()) / 2.0;
                }
                return split._2().head();
            }
        };
    
    public static F, Iterable> rank =
        new F, Iterable>() {
            @Override
            public Iterable f(Iterable a) {
                return join(Stream.iterableStream(a).zipIndex()
                .sort(p2Ord(doubleOrd, intOrd))
                .zipIndex()
                .map(P2.split_(P2.split_(Function.identity(), Function.identity()), Integers.add.f(1)))
                .toList()
                .group(p2Equal(p2Equal(doubleEqual, Utils.alwaysEqual()), Utils.alwaysEqual()))
                .map(new F,Integer>>, List,Double>>>() {
                    @Override
                    public List,Double>> f(List,Integer>> a) {
                        final Double average = Integers.sum(a.map(P2.,Integer>__2())) / (double) a.length();
                        return a.map(new F,Integer>,P2,Double>>() {
                            @Override
                            public P2,Double> f(P2,Integer> b) {
                                return P.p(b._1(), average);
                            }
                        });
                    }
                }))
                .sort(p2Ord(p2Ord(Utils.equalOrd(), intOrd), Utils.equalOrd()))
                .map(P2.,Double>__2());
            }
        };
   
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy