Example usage for org.apache.commons.math3.distribution LogNormalDistribution LogNormalDistribution

List of usage examples for org.apache.commons.math3.distribution LogNormalDistribution LogNormalDistribution

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution LogNormalDistribution LogNormalDistribution.

Prototype

public LogNormalDistribution(double scale, double shape) throws NotStrictlyPositiveException 

Source Link

Document

Create a log-normal distribution using the specified scale and shape.

Usage

From source file:fr.assoba.open.perf.GenDist.java

public static void main(String[] args) {
    // Magic number !
    LogNormalDistribution distribution = new LogNormalDistribution(6.73, 2.12);
    ArrayList<Long> list = new ArrayList<Long>();
    for (int i = 0; i < 1023; i++) {
        double d = (1.0 * i) / 1023.0;
        list.add((long) (100 * distribution.inverseCumulativeProbability(d)));
    }/*w  w  w  .  ja  v  a2  s .  c o  m*/
    System.out.print(Joiner.on(",").join(list));

}

From source file:fr.assoba.open.perf.FindDist.java

/**
 * @param args/*from  w  w w  .j  av a  2s. co  m*/
 */
public static void main(String[] args) {
    for (double s = 5; s < 9; s = s + 0.01) {
        for (double c = 0.5; c < 5; c = c + 0.01) {
            LogNormalDistribution distribution = new LogNormalDistribution(s, c);
            if ((distribution.cumulativeProbability(200) > .248)
                    && (distribution.cumulativeProbability(200) < .252)) {
                System.out.println(s + ":" + c);
                System.out.println("\t" + distribution.cumulativeProbability(200));
                System.out.println("\t" + distribution.cumulativeProbability(600));
                System.out.println("\t" + distribution.cumulativeProbability(1000 * 1000));
            }
        }
    }
}

From source file:controller.DistributionController.java

public static void logNormal(double mean, double stand, double size, double extra) {
    numerosGerados.clear();//from w  w w. j a  v  a  2s. c om
    double mu = Math.log(mean * mean / Math.sqrt(mean * mean + stand * stand));
    double sigma = Math.sqrt(Math.log((mean * mean + stand * stand) / (mean * mean)));
    LogNormalDistribution lg = new LogNormalDistribution(mu, sigma);
    Double n;
    for (int i = 0; i < size; i++) {
        n = extra + lg.sample();
        numerosGerados.add(n);
    }
}

From source file:jasima.core.random.continuous.DblLogNormal.java

public DblLogNormal(double scale, double shape) {
    super();
    setDistribution(new LogNormalDistribution(scale, shape));
}

From source file:jasima.core.random.continuous.DblLogNormal.java

/**
 * Sets the scale parameter to use.//from  w ww . j av  a2s.  c  o m
 * 
 * @param scale
 *            The scale value to use.
 */
public void setScale(double scale) {
    setDistribution(new LogNormalDistribution(scale, getShape()));
}

From source file:jasima.core.random.continuous.DblLogNormal.java

/**
 * Sets the shape parameter of the distribution.
 * //from   www. jav  a2  s.co  m
 * @param shape
 *            The shape parameter value to use.
 * @throws NotStrictlyPositiveException
 *             If shape was {@code <=0}.
 */
public void setShape(double shape) throws NotStrictlyPositiveException {
    setDistribution(new LogNormalDistribution(getScale(), shape));
}

From source file:com.ibm.og.util.Distributions.java

/**
 * Creates a lognormal distribution./*from   w ww.  jav  a 2s . c o m*/
 * 
 * @param average the average
 * @param spread the spread
 * @return a lognormal distribution instance
 * @throws IllegalArgumentException if average or spread are negative, or if average - 3*spread is
 *         negative
 */
public static Distribution lognormal(final double average, final double spread) {
    // FIXME configure lognormal distribution range correctly
    checkArgument(average >= 0.0, "average must be >= 0.0 [%s]", average);
    checkArgument(spread >= 0.0, "spread must be >= 0.0 [%s]", spread);

    if (DoubleMath.fuzzyEquals(spread, 0.0, Distributions.ERR)) {
        return constant(average);
    }

    final double min = average - (3 * spread);
    checkArgument(min >= 0.0, "three standard deviations must be >= 0.0 [%s]", min);
    final String s = String.format("LogNormalDistribution [average=%s, spread=%s]", average, spread);
    return new RealDistributionAdapter(new LogNormalDistribution(average, spread), s);
}

From source file:jeplus.data.ParameterItem.java

/**
 * Sampling function for probabilistic distributions
 * @param funcstr Function string//  w  w  w . j a  v a2  s . c  o  m
 * @return List of sampled values in a string array
 */
private String[] sampleDistribution(String funcstr) {
    ArrayList<String> list = new ArrayList<>();
    String[] params = funcstr.split("\\s*[,;]\\s*");
    // For integer/double types, returns randomized N samples conforming
    // a specified distribution, currently 'gaussian'/'normal'/'n', 
    // 'uniform'/'u', 'triangular'/'tr', or 'discrete'/'d'
    // for examples: @sample(gaussian, 0, 1.5, 20), with mean, sd and N
    //           or  @sample(uniform, -10, 10, 20), with lb, ub and N
    //           of  @sample(triangular, -1.0, 0.3, 1.0, 20), with lb, mode, ub and N
    //           of  @sample(discrete, option_A, 0.3, option_B, 0.5, option_C, 0.2, 20), with op1, weight1, op2, weight2..., and N
    String distribution = params[0].toLowerCase();
    switch (distribution) {
    case "uniform":
    case "u": {
        // requires lb, ub, n
        double lb = Double.parseDouble(params[1]);
        double ub = Double.parseDouble(params[2]);
        int n = Integer.parseInt(params[3]);
        if (this.Type == DISCRETE) {
            // list.add(params[1]);
            // list.add(params[2]);
            list = null;
        } else {
            for (int i = 0; i < n; i++) {
                if (this.Type == DOUBLE) {
                    list.add(Double.toString(RandomSource.getRandomGenerator().nextDouble() * (ub - lb) + lb));
                } else if (this.Type == INTEGER) {
                    list.add(Long.toString(
                            Math.round(RandomSource.getRandomGenerator().nextDouble() * (ub - lb) + lb)));
                }
            }
        }
        break;
    }
    case "gaussian":
    case "normal":
    case "n": {
        // requires mean, sd, n
        double mean = Double.parseDouble(params[1]);
        double sd = Double.parseDouble(params[2]);
        int n = Integer.parseInt(params[3]);
        if (this.Type == DISCRETE) {
            // list.add(params[1]);
            list = null;
        } else {
            for (int i = 0; i < n; i++) {
                if (this.Type == DOUBLE) {
                    list.add(Double.toString(RandomSource.getRandomGenerator().nextGaussian() * sd + mean));
                } else if (this.Type == INTEGER) {
                    list.add(Long.toString(
                            Math.round(RandomSource.getRandomGenerator().nextGaussian() * sd + mean)));
                }
            }
        }
        break;
    }
    case "lognormal":
    case "ln": {
        // requires mean, sd, n
        double mean = Double.parseDouble(params[1]);
        double sd = Double.parseDouble(params[2]);
        int n = Integer.parseInt(params[3]);
        if (this.Type == DISCRETE) {
            // list.add(params[1]);
            list = null;
        } else {
            LogNormalDistribution lndist = new LogNormalDistribution(mean, sd);
            // Which RNG is used??
            double[] sample = lndist.sample(n);
            for (int i = 0; i < n; i++) {
                if (this.Type == DOUBLE) {
                    list.add(Double.toString(sample[i]));
                } else if (this.Type == INTEGER) {
                    list.add(Long.toString(Math.round(sample[i])));
                }
            }
        }
        break;
    }
    case "exponential":
    case "e": {
        // requires mean, n
        double mean = Double.parseDouble(params[1]);
        int n = Integer.parseInt(params[2]);
        if (this.Type == DISCRETE) {
            // list.add(params[1]);
            list = null;
        } else {
            ExponentialDistribution lndist = new ExponentialDistribution(mean);
            // Which RNG is used??
            double[] sample = lndist.sample(n);
            for (int i = 0; i < n; i++) {
                if (this.Type == DOUBLE) {
                    list.add(Double.toString(sample[i]));
                } else if (this.Type == INTEGER) {
                    list.add(Long.toString(Math.round(sample[i])));
                }
            }
        }
        break;
    }
    case "triangular":
    case "tr": {
        // requires a(lb), c(tip), b(ub), n
        double a = Double.parseDouble(params[1]);
        double c = Double.parseDouble(params[2]);
        double b = Double.parseDouble(params[3]);
        // sort a, b, c, so a < c < b
        // ...
        int n = Integer.parseInt(params[4]);
        if (this.Type == DISCRETE || !(a < c && c < b)) {
            // list.add(params[1]);
            // list.add(params[2]);
            // list.add(params[3]);
            list = null;
        } else {
            for (int i = 0; i < n; i++) {
                double x = RandomSource.getRandomGenerator().nextDouble();
                double y = 0;
                if (x <= (c - a) / (b - a)) {
                    y = Math.sqrt((c - a) * (b - a) * x) + a;
                } else {
                    y = b - Math.sqrt((1 - x) * (b - a) * (b - c));
                }
                if (this.Type == DOUBLE) {
                    list.add(Double.toString(y));
                } else if (this.Type == INTEGER) {
                    list.add(Long.toString(Math.round(y)));
                }
            }
        }
        break;
    }
    case "discrete":
    case "d": {
        // requires op1, prob1, op2, prob2, ..., n
        int nOptions = params.length / 2 - 1;
        String[] options = new String[nOptions];
        double[] probabilities = new double[nOptions];
        double[] accProb = new double[nOptions];
        for (int i = 0; i < nOptions; i++) {
            options[i] = params[2 * i + 1];
            try {
                probabilities[i] = Double.parseDouble(params[2 * i + 2]);
            } catch (NumberFormatException nfe) {
                probabilities[i] = 0.1;
            }
            if (i == 0)
                accProb[i] = probabilities[i];
            else
                accProb[i] = accProb[i - 1] + probabilities[i];
        }
        int n = Integer.parseInt(params[params.length - 1]);
        for (int i = 0; i < n; i++) {
            double x = RandomSource.getRandomGenerator().nextDouble() * accProb[nOptions - 1];
            int sel = 0;
            for (int j = 0; j < nOptions; j++) {
                if (x < accProb[j]) {
                    sel = j;
                    break;
                }
            }
            list.add(options[sel]);
        }
        break;
    }
    case "custom":
        // to be implemented
        list = null;
        break;
    }
    return (list == null) ? null : list.toArray(new String[0]);
}

From source file:jeplus.JEPlusProject.java

private String[] defaultLHSdistributionSample(int n, String funcstr, int type, Random randomsrc) {
    // Trim off brackets
    int start = funcstr.indexOf("(") + 1;
    int end = funcstr.indexOf(")");
    funcstr = funcstr.substring(start, end).trim();

    ArrayList<String> list = new ArrayList<>();
    String[] params = funcstr.split("\\s*,\\s*");
    // For integer/double types, returns randomized N samples conforming
    // a specified distribution, currently 'gaussian'/'normal'/'n', 
    // 'uniform'/'u', 'triangular'/'tr', or 'discrete'/'d'
    // for examples: @sample(gaussian, 0, 1.5, 20), with mean, sd and N
    //           or  @sample(uniform, -10, 10, 20), with lb, ub and N
    //           of  @sample(triangular, -1.0, 0.3, 1.0, 20), with lb, mode, ub and N
    //           of  @sample(discrete, option_A, 0.3, option_B, 0.5, option_C, 0.2, 20), with lb, mode, ub and N
    String distribution = params[0].toLowerCase();
    switch (distribution) {
    case "uniform":
    case "u":
        // requires lb, ub, n
        double lb = Double.parseDouble(params[1]);
        double ub = Double.parseDouble(params[2]);
        for (int i = 0; i < n; i++) {
            if (type == ParameterItem.DOUBLE) {
                double bin = (ub - lb) / n;
                double v = randomsrc.nextDouble() * bin + lb + i * bin;
                list.add(Double.toString(v));
            } else if (type == ParameterItem.INTEGER) {
                double bin = (ub + 1. - lb) / n;
                double v = randomsrc.nextDouble() * bin + lb + i * bin;
                list.add(Integer.toString((int) Math.floor(v)));
            }//from   w ww  .ja v  a 2 s .  c  o  m
        }
        break;
    case "gaussian":
    case "normal":
    case "n": {
        // requires mean, sd, n
        double mean = Double.parseDouble(params[1]);
        double sd = Double.parseDouble(params[2]);
        NormalDistribution Dist = new NormalDistribution(mean, sd);
        double bin = 1.0 / n;
        for (int i = 0; i < n; i++) {
            double a = Dist.inverseCumulativeProbability((i == 0) ? bin / 10 : i * bin); // lb of each bin
            double b = Dist.inverseCumulativeProbability((i == n - 1) ? 1. - bin / n : (i + 1) * bin); // ub of each bin
            double v = randomsrc.nextDouble() * (b - a) + a;
            if (type == ParameterItem.DOUBLE) {
                list.add(Double.toString(v));
            } else if (type == ParameterItem.INTEGER) {
                // Warning: for integer, binomial distribution should be used.
                // the following function is provided just for convenience
                list.add(Long.toString(Math.round(v)));
            }
        }
        break;
    }
    case "lognormal":
    case "ln": {
        // requires mean, sd, n
        double mean = Double.parseDouble(params[1]);
        double sd = Double.parseDouble(params[2]);
        LogNormalDistribution Dist = new LogNormalDistribution(mean, sd);
        double bin = 1.0 / n;
        for (int i = 0; i < n; i++) {
            double a = Dist.inverseCumulativeProbability((i == 0) ? bin / 10 : i * bin); // lb of each bin
            double b = Dist.inverseCumulativeProbability((i == n - 1) ? 1. - bin / n : (i + 1) * bin); // ub of each bin
            double v = randomsrc.nextDouble() * (b - a) + a;
            if (type == ParameterItem.DOUBLE) {
                list.add(Double.toString(v));
            } else if (type == ParameterItem.INTEGER) {
                // Warning: for integer, binomial distribution should be used.
                // the following function is provided just for convenience
                list.add(Long.toString(Math.round(v)));
            }
        }
        break;
    }
    case "exponential":
    case "e": {
        // requires mean, sd, n
        double mean = Double.parseDouble(params[1]);
        ExponentialDistribution Dist = new ExponentialDistribution(mean);
        double bin = 1.0 / n;
        for (int i = 0; i < n; i++) {
            double a = Dist.inverseCumulativeProbability((i == 0) ? bin / 10 : i * bin); // lb of each bin
            double b = Dist.inverseCumulativeProbability((i == n - 1) ? 1. - bin / n : (i + 1) * bin); // ub of each bin
            double v = randomsrc.nextDouble() * (b - a) + a;
            if (type == ParameterItem.DOUBLE) {
                list.add(Double.toString(v));
            } else if (type == ParameterItem.INTEGER) {
                // Warning: for integer, binomial distribution should be used.
                // the following function is provided just for convenience
                list.add(Long.toString(Math.round(v)));
            }
        }
        break;
    }
    case "triangular":
    case "tr": {
        // requires a(lb), c(mode), b(ub), n
        double a = Double.parseDouble(params[1]);
        double c = Double.parseDouble(params[2]);
        double b = Double.parseDouble(params[3]);
        TriangularDistribution Dist = new TriangularDistribution(a, c, b);
        double bin = 1.0 / n;
        for (int i = 0; i < n; i++) {
            a = Dist.inverseCumulativeProbability(i * bin); // lb of each bin
            b = Dist.inverseCumulativeProbability((i + 1) * bin); // ub of each bin
            double v = randomsrc.nextDouble() * (b - a) + a;
            if (type == ParameterItem.DOUBLE) {
                list.add(Double.toString(v));
            } else if (type == ParameterItem.INTEGER) {
                // Warning: for integer, user defined discrete distribution should be used.
                // the following function is provided just for convenience
                list.add(Long.toString(Math.round(v)));
            }
        }
        break;
    }
    case "discrete":
    case "d": {
        // requires op1, prob1, op2, prob2, ..., n
        int nOptions = params.length / 2 - 1;
        String[] options = new String[nOptions];
        double[] probabilities = new double[nOptions];
        double sum = 0;
        for (int i = 0; i < nOptions; i++) {
            options[i] = params[2 * i + 1];
            try {
                probabilities[i] = Double.parseDouble(params[2 * i + 2]);
            } catch (NumberFormatException nfe) {
                probabilities[i] = 0.1;
            }
            sum += probabilities[i];
        }
        RouletteWheel Wheel = new RouletteWheel(probabilities, randomsrc);
        double bin = sum / n;
        for (int i = 0; i < n; i++) {
            double a = i * bin; // lb of each bin
            double b = (i + 1) * bin; // ub of each bin
            int sel = Wheel.spin(a, b);
            list.add(options[sel]);
        }
        break;
    }
    case "custom":
        break;
    }
    return list.toArray(new String[0]);
}

From source file:org.apache.solr.client.solrj.io.eval.LogNormalDistributionEvaluator.java

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }//from w w  w  .j ava  2  s .  c om
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }

    Number shape = (Number) first;
    Number scale = (Number) second;

    return new LogNormalDistribution(scale.doubleValue(), shape.doubleValue());
}