001    package org.expasy.jpl.commons.collection.stat;
002    
003    
004    public final class ProbDistLGUtils {
005            
006            private static final double[] errorFunction;
007            
008            static {
009                    errorFunction = new double[6];
010                    errorFunction[0] = 0.682689492137; // stdev [-1, 1[
011                    errorFunction[1] = 0.271810243967; // stdev [-2, 2[ - [-1, 1[
012                    errorFunction[2] = 0.042800467833; // stdev [-3, 3[ - [-2, 2[
013                    errorFunction[3] = 0.002636453579; // stdev [-4, 4[ - [-3, 3[
014                    errorFunction[4] = 0.000062769181; // stdev [-5, 5[ - [-4, 4[
015                    errorFunction[5] = 0.000000571330; // stdev [-6, 6[ - [-5, 5[
016            }
017            
018            public static double getDensityAtStdDev(int stdDev) {
019                    if (stdDev > 5) {
020                            return 0;
021                    }
022                    return errorFunction[stdDev - 1];
023            }
024            
025            /**
026             * Get the density over the curve in the interval [stdev-stdev+1[
027             * 
028             * @param stdev the standard deviation interval to compute the density in.
029             * @return a density value [0, 1[.
030             */
031            public static double getDensityAtStdDevBin(int stdev) {
032                    int stdevAbs = stdev;
033                    boolean isNeg = false;
034                    
035                    if (stdev < 0) {
036                            stdevAbs = -stdev;
037                            isNeg = true;
038                    }
039                    
040                    if (stdevAbs > 6) {
041                            return 0;
042                    }
043                    
044                    if (isNeg) {
045                            return errorFunction[stdevAbs - 1] / 2;
046                    } else {
047                            return errorFunction[stdevAbs] / 2;
048                    }
049                    
050            }
051            
052            /**
053             * Return a list of areas for 12 sigma intervals from [-6, -5[ to [5, 6[.
054             * 
055             * @param area the area of the data.
056             * @return a list of areas where there sum is equals to area.
057             */
058            public static double[] getIntervalAreas(double area) {
059                    double[] stdDevAreas = new double[12];
060                    
061                    for (int i = -6; i < 6; i++) {
062                            stdDevAreas[i + 6] = area * getDensityAtStdDevBin(i);
063                    }
064                    
065                    return stdDevAreas;
066            }
067            
068            public static double getIntervalAreaAtStdev(double area, int stdev) {
069                    double[] stdDevAreas = getIntervalAreas(area);
070                    
071                    return stdDevAreas[stdev + 6];
072            }
073            
074            public static double[] getValues(LaplaceGaussRandomVar var) {
075                    double[] values = new double[12];
076                    
077                    for (int i = -6; i < 6; i++) {
078                            values[i + 6] = getValuesAtStdev(var, i);
079                    }
080                    
081                    return values;
082            }
083            
084            public static final double getValuesAtStdev(LaplaceGaussRandomVar var,
085                int numOfStdev) {
086                    return var.getMean() + (numOfStdev * var.getStdDev());
087            }
088            
089            public static HistogramDataSet toHistogram(LaplaceGaussRandomVar var) {
090                    
091                    double[] values = getValues(var);
092                    
093                    double[] stdDevAreas = getIntervalAreas(var.getFrequency());
094                    
095                    HistogramDataSet histo =
096                        new HistogramDataSet.Builder().addValues(values).addWeights(
097                            stdDevAreas).binWidth(var.getStdDev()).build();
098                    
099                    return histo;
100            }
101    }