2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.distribution;
19
import org.apache.commons.math.MathException;
20
import org.apache.commons.math.special.Gamma;
21
import org.apache.commons.math.special.Beta;
24
* Implements the Beta distribution.
28
* <li><a href="http://en.wikipedia.org/wiki/Beta_distribution">
29
* Beta distribution</a></li>
32
* @version $Revision: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
35
public class BetaDistributionImpl
36
extends AbstractContinuousDistribution implements BetaDistribution {
38
/** Serializable version identifier. */
39
private static final long serialVersionUID = -1221965979403477668L;
41
/** First shape parameter. */
44
/** Second shape parameter. */
47
/** Normalizing factor used in density computations.
48
* updated whenever alpha or beta are changed.
53
* Build a new instance.
54
* @param alpha first shape parameter (must be positive)
55
* @param beta second shape parameter (must be positive)
57
public BetaDistributionImpl(double alpha, double beta) {
64
public void setAlpha(double alpha) {
70
public double getAlpha() {
75
public void setBeta(double beta) {
81
public double getBeta() {
86
* Recompute the normalization factor.
88
private void recomputeZ() {
89
if (Double.isNaN(z)) {
90
z = Gamma.logGamma(alpha) + Gamma.logGamma(beta) - Gamma.logGamma(alpha + beta);
95
public double density(Double x) throws MathException {
101
throw new MathException("Cannot compute beta density at 0 when alpha = {0,number}", alpha);
106
throw new MathException("Cannot compute beta density at 1 when beta = %.3g", beta);
110
double logX = Math.log(x);
111
double log1mX = Math.log1p(-x);
112
return Math.exp((alpha - 1) * logX + (beta - 1) * log1mX - z);
118
public double inverseCumulativeProbability(double p) throws MathException {
124
return super.inverseCumulativeProbability(p);
130
protected double getInitialDomain(double p) {
136
protected double getDomainLowerBound(double p) {
142
protected double getDomainUpperBound(double p) {
147
public double cumulativeProbability(double x) throws MathException {
153
return Beta.regularizedBeta(x, alpha, beta);
159
public double cumulativeProbability(double x0, double x1) throws MathException {
160
return cumulativeProbability(x1) - cumulativeProbability(x0);