Example usage for org.apache.commons.math.distribution NormalDistributionImpl NormalDistributionImpl

List of usage examples for org.apache.commons.math.distribution NormalDistributionImpl NormalDistributionImpl

Introduction

In this page you can find the example usage for org.apache.commons.math.distribution NormalDistributionImpl NormalDistributionImpl.

Prototype

public NormalDistributionImpl(double mean, double sd) 

Source Link

Document

Create a normal distribution using the given mean and standard deviation.

Usage

From source file:org.peerfact.impl.util.stats.distributions.LimitedNormalDistribution.java

/**
 * Returns a random value that is distributed as a Normal Distribution with
 * an upper and lower limit./*from  ww  w  .jav a 2s.  c om*/
 * 
 * @param _mu
 *            average
 * @param _sigma
 *            standard deviation
 * @param _min
 *            lower limit, set to "null", if no limit
 * @param _max
 *            upper limit, set to "null", if no limit
 * @return as double
 */
public static double returnValue(double _mu, double _sigma, Double _min, Double _max) {
    double lmax;
    double lmin;
    double lpmax = 1;
    double lpmin = 0;
    double lpfactor;

    NormalDistributionImpl llimitedNormal = new NormalDistributionImpl(_mu, _sigma);
    if (_min == null) {
        if (_max != null) {
            // only max is limted
            lmax = _max.doubleValue();
            try {
                lpmax = llimitedNormal.cumulativeProbability(lmax);
            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    } else {
        if (_max == null) {
            // only min is limited.
            lmin = _min.doubleValue();
            try {
                lpmin = llimitedNormal.cumulativeProbability(lmin);
            } catch (MathException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            // both sides limited.

            // make sure min is really smaller than max.
            if (_max.doubleValue() > _min.doubleValue()) {
                lmin = _min.doubleValue();
                lmax = _max.doubleValue();
            } else {
                lmax = _min.doubleValue();
                lmin = _max.doubleValue();
            }

            // get min and max probabilites that are possible
            try {
                lpmin = llimitedNormal.cumulativeProbability(lmin);
                lpmax = llimitedNormal.cumulativeProbability(lmax);

                lpfactor = lpmax - lpmin;

            } catch (MathException e) {
                e.printStackTrace();
            }
        }
    }
    lpfactor = lpmax - lpmin;

    double lrandom = lpmin + Simulator.getRandom().nextDouble() * lpfactor;
    double lresult;

    try {
        lresult = llimitedNormal.inverseCumulativeProbability(lrandom);
    } catch (MathException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        lresult = 0;
    }

    return lresult;

}

From source file:org.renjin.Distributions.java

public static double dnorm(final double x, final double mean, final double sd, boolean log) {
    return d(new NormalDistributionImpl(mean, sd), x, log);
}

From source file:org.renjin.Distributions.java

public static double pnorm(final double q, final double mean, final double sd, boolean lowerTail,
        boolean logP) {
    return p(new NormalDistributionImpl(mean, sd), q, lowerTail, logP);
}

From source file:org.renjin.Distributions.java

public static double plnorm(final double q, final double logmean, final double logsd, boolean lowerTail,
        boolean logP) {
    return p(new NormalDistributionImpl(logmean, logsd), Math.log(q), lowerTail, logP);
}

From source file:org.renjin.Distributions.java

public static double qnorm(final double p, final double mean, final double sd, boolean lowerTail,
        boolean logP) {
    return q(new NormalDistributionImpl(mean, sd), p, lowerTail, logP);
}

From source file:org.renjin.Distributions.java

public static double qlnorm(final double p, final double meanlog, final double sdlog, boolean lowerTail,
        boolean logP) {
    return Math.exp(q(new NormalDistributionImpl(meanlog, sdlog), p, lowerTail, logP));
}

From source file:org.renjin.primitives.random.Distributions.java

public static double dnorm(@Recycle double x, @Recycle double mean, @Recycle double sd, boolean log) {
    return d(new NormalDistributionImpl(mean, sd), x, log);
}

From source file:org.renjin.primitives.random.Distributions.java

public static double pnorm(@Recycle double q, @Recycle double mean, @Recycle double sd, boolean lowerTail,
        boolean logP) {
    return p(new NormalDistributionImpl(mean, sd), q, lowerTail, logP);
}

From source file:org.renjin.primitives.random.Distributions.java

public static double plnorm(@Recycle double q, @Recycle double logmean, @Recycle double logsd,
        boolean lowerTail, boolean logP) {
    return p(new NormalDistributionImpl(logmean, logsd), Math.log(q), lowerTail, logP);
}

From source file:org.renjin.primitives.random.Distributions.java

public static double qnorm(@Recycle double p, @Recycle double mean, @Recycle double sd, boolean lowerTail,
        boolean logP) {
    return q(new NormalDistributionImpl(mean, sd), p, lowerTail, logP);
}