Pow - 1 year ago 86
Java Question

# Calculate Normal Distrubution using Java

EDIT:

Actually I realized that what I need is the value of X. Let me make it clearer. Suppose, I know the probability P = 0.95 since I want to use two standard deviations. I know the ranges P(-500 < x <500) that means I know y and z , I know the mean and standard deviation as well. If I want to know what will be the value of x which method should I use. I have found one calculator doing something like this but could not understand which formula to use.

Original Question:

I want to calculate normal distribution probability of random variables using Java. Was not sure which formula to use to code to solve a problem like this. If I know the value of mean and Standard deviation and want to find the probability of being x's value between 2 certain values y and z (P(-500

You can use the error function, available in `org.apache.commons.math.special.Erf`, as discussed here and here.

Addendum: The methods proposed in @Brent Worden's answer considerably simplify the solution of such problems. As a concrete example, the code below shows how to solve the examples to which you refer. In addition, I found it helpful to compare the definition here to the implementation of `cumulativeProbability()` using `Erf.erf`. Note also how the implementation of `inverseCumulativeProbability()` generalizes the required iterative approach.

``````import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.NormalDistribution;
import org.apache.commons.math.distribution.NormalDistributionImpl;

/**
* @see http://stattrek.com/Tables/Normal.aspx#examples
* @see http://stackoverflow.com/questions/6353678
*/
public class CumulativeProbability {

private static NormalDistribution d;

public static void main(String[] args) throws MathException {
// Problem 1; µ = 1000; σ = 100
d = new NormalDistributionImpl(1000, 100);
System.out.println(d.cumulativeProbability(1200));
// Problem 2; µ = 50; σ = 10
d = new NormalDistributionImpl(50, 10);
System.out.println(d.inverseCumulativeProbability(0.9));
}
}
``````

Console:

```0.9772498680518208
62.81551565546365
```

Discussion:

Problem 1. Among devices having a normally distributed lifespan that lasts an average of 1000 hours with a standard deviation of 100 hours, ~97.7% will fail in 1200 hours.

Problem 2. Among people having a normally distributed skill that enables an average of 50 repetitions with a standard deviation of 10 repetitions, an individual can surpass 90% of the population with 63 repetitions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download