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

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

Introduction

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

Prototype

public TriangularDistribution(double a, double c, double b)
        throws NumberIsTooLargeException, NumberIsTooSmallException 

Source Link

Document

Creates a triangular real distribution using the given lower limit, upper limit, and mode.

Usage

From source file:adams.data.distribution.Triangular.java

/**
 * Returns the configured distribution./*from   www . j a  v  a  2  s  .c o  m*/
 *
 * @return      the distribution
 */
@Override
public RealDistribution getRealDistribution() {
    return new TriangularDistribution(m_A, m_C, m_B);
}

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 ww  w. j  a  v a2 s  .c om
        }
        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.TriangularDistributionEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {

    if (values.length != 3) {
        throw new IOException("Triangular distribution requires three numeric parameters low, mode, high");
    }/*from   w w w.j a v  a  2s .com*/

    double low = ((Number) values[0]).doubleValue();
    double mode = ((Number) values[1]).doubleValue();
    double high = ((Number) values[2]).doubleValue();

    return new TriangularDistribution(low, mode, high);
}

From source file:org.lightjason.trafficsimulation.math.EDistributionFactory.java

/**
 * generate the distribution/*from   w  w  w .j a v  a2  s  .c om*/
 *
 * @param p_args distribution arguments
 * @return the distribution
 */
public final AbstractRealDistribution generate(final double... p_args) {
    switch (this) {
    case BETA:
        return new BetaDistribution(p_args[0], p_args[1]);
    case CAUCHY:
        return new CauchyDistribution(p_args[0], p_args[1]);
    case CHI_SQUARED:
        return new ChiSquaredDistribution(p_args[0]);
    case EXPONENTIAL:
        return new ExponentialDistribution(p_args[0]);
    case F:
        return new FDistribution(p_args[0], p_args[1]);
    case GAMMA:
        return new GammaDistribution(p_args[0], p_args[1]);
    case GUMBEL:
        return new GumbelDistribution(p_args[0], p_args[1]);
    case LAPLACE:
        return new LaplaceDistribution(p_args[0], p_args[1]);
    case LEVY:
        return new LevyDistribution(p_args[0], p_args[1]);
    case LOGISTIC:
        return new LogisticDistribution(p_args[0], p_args[1]);
    case LOG_NORMAL:
        return new LogNormalDistribution(p_args[0], p_args[1]);
    case NAKAGAMI:
        return new NakagamiDistribution(p_args[0], p_args[1]);
    case NORMAL:
        return new NormalDistribution(p_args[0], p_args[1]);
    case PARETO:
        return new ParetoDistribution(p_args[0], p_args[1]);
    case T:
        return new TDistribution(p_args[0]);
    case TRIANGULAR:
        return new TriangularDistribution(p_args[0], p_args[1], p_args[2]);
    case UNIFORM:
        return new UniformRealDistribution(p_args[0], p_args[1]);
    case WEIBULL:
        return new WeibullDistribution(p_args[0], p_args[1]);

    default:
        throw new RuntimeException(MessageFormat.format("not set {0}", this));
    }
}