1 16 package org.apache.commons.math.special; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.math.MathException; 21 import org.apache.commons.math.util.ContinuedFraction; 22 23 29 public class Beta implements Serializable { 30 31 private static final double DEFAULT_EPSILON = 10e-9; 32 33 36 private Beta() { 37 super(); 38 } 39 40 51 public static double regularizedBeta(double x, double a, double b) 52 throws MathException 53 { 54 return regularizedBeta(x, a, b, DEFAULT_EPSILON, Integer.MAX_VALUE); 55 } 56 57 71 public static double regularizedBeta(double x, double a, double b, 72 double epsilon) throws MathException 73 { 74 return regularizedBeta(x, a, b, epsilon, Integer.MAX_VALUE); 75 } 76 77 87 public static double regularizedBeta(double x, double a, double b, 88 int maxIterations) throws MathException 89 { 90 return regularizedBeta(x, a, b, DEFAULT_EPSILON, maxIterations); 91 } 92 93 116 public static double regularizedBeta(double x, final double a, 117 final double b, double epsilon, int maxIterations) throws MathException 118 { 119 double ret; 120 121 if (Double.isNaN(x) || Double.isNaN(a) || Double.isNaN(b) || (x < 0) || 122 (x > 1) || (a <= 0.0) || (b <= 0.0)) 123 { 124 ret = Double.NaN; 125 } else if (x > (a + 1.0) / (a + b + 2.0)) { 126 ret = 1.0 - regularizedBeta(1.0 - x, b, a, epsilon, maxIterations); 127 } else { 128 ContinuedFraction fraction = new ContinuedFraction() { 129 protected double getB(int n, double x) { 130 double ret; 131 double m; 132 if (n % 2 == 0) { m = n / 2.0; 134 ret = (m * (b - m) * x) / 135 ((a + (2 * m) - 1) * (a + (2 * m))); 136 } else { 137 m = (n - 1.0) / 2.0; 138 ret = -((a + m) * (a + b + m) * x) / 139 ((a + (2 * m)) * (a + (2 * m) + 1.0)); 140 } 141 return ret; 142 } 143 144 protected double getA(int n, double x) { 145 return 1.0; 146 } 147 }; 148 ret = Math.exp((a * Math.log(x)) + (b * Math.log(1.0 - x)) - 149 Math.log(a) - logBeta(a, b, epsilon, maxIterations)) * 150 1.0 / fraction.evaluate(x, epsilon, maxIterations); 151 } 152 153 return ret; 154 } 155 156 163 public static double logBeta(double a, double b) { 164 return logBeta(a, b, DEFAULT_EPSILON, Integer.MAX_VALUE); 165 } 166 167 184 public static double logBeta(double a, double b, double epsilon, 185 int maxIterations) { 186 187 double ret; 188 189 if (Double.isNaN(a) || Double.isNaN(b) || (a <= 0.0) || (b <= 0.0)) { 190 ret = Double.NaN; 191 } else { 192 ret = Gamma.logGamma(a) + Gamma.logGamma(b) - 193 Gamma.logGamma(a + b); 194 } 195 196 return ret; 197 } 198 } 199 | Popular Tags |