1 16 17 package org.apache.commons.math.distribution; 18 19 import java.io.Serializable ; 20 21 import org.apache.commons.math.MathException; 22 import org.apache.commons.math.special.Erf; 23 24 30 public class NormalDistributionImpl extends AbstractContinuousDistribution 31 implements NormalDistribution, Serializable { 32 33 34 static final long serialVersionUID = 8589540077390120676L; 35 36 37 private double mean = 0; 38 39 40 private double standardDeviation = 1; 41 42 47 public NormalDistributionImpl(double mean, double sd){ 48 super(); 49 setMean(mean); 50 setStandardDeviation(sd); 51 } 52 53 57 public NormalDistributionImpl(){ 58 this(0.0, 1.0); 59 } 60 61 65 public double getMean() { 66 return mean; 67 } 68 69 73 public void setMean(double mean) { 74 this.mean = mean; 75 } 76 77 81 public double getStandardDeviation() { 82 return standardDeviation; 83 } 84 85 90 public void setStandardDeviation(double sd) { 91 if (sd <= 0.0) { 92 throw new IllegalArgumentException ( 93 "Standard deviation must be positive."); 94 } 95 standardDeviation = sd; 96 } 97 98 104 public double cumulativeProbability(double x) throws MathException { 105 return 0.5 * (1.0 + Erf.erf((x - mean) / 106 (standardDeviation * Math.sqrt(2.0)))); 107 } 108 109 123 public double inverseCumulativeProbability(final double p) 124 throws MathException { 125 if (p == 0) { 126 return Double.NEGATIVE_INFINITY; 127 } 128 if (p == 1) { 129 return Double.POSITIVE_INFINITY; 130 } 131 return super.inverseCumulativeProbability(p); 132 } 133 134 143 protected double getDomainLowerBound(double p) { 144 double ret; 145 146 if (p < .5) { 147 ret = -Double.MAX_VALUE; 148 } else { 149 ret = getMean(); 150 } 151 152 return ret; 153 } 154 155 164 protected double getDomainUpperBound(double p) { 165 double ret; 166 167 if (p < .5) { 168 ret = getMean(); 169 } else { 170 ret = Double.MAX_VALUE; 171 } 172 173 return ret; 174 } 175 176 184 protected double getInitialDomain(double p) { 185 double ret; 186 187 if (p < .5) { 188 ret = getMean() - getStandardDeviation(); 189 } else if (p > .5) { 190 ret = getMean() + getStandardDeviation(); 191 } else { 192 ret = getMean(); 193 } 194 195 return ret; 196 } 197 } 198 | Popular Tags |