001: /*
002: * Copyright 2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.distribution;
018:
019: import java.io.Serializable;
020:
021: /**
022: * Default implementation of
023: * {@link org.apache.commons.math.distribution.WeibullDistribution}.
024: *
025: * @since 1.1
026: * @version $Revision: 1.13 $ $Date: 2004-07-24 16:41:37 -0500 (Sat, 24 Jul 2004) $
027: */
028: public class WeibullDistributionImpl extends
029: AbstractContinuousDistribution implements WeibullDistribution,
030: Serializable {
031:
032: /** Serializable version identifier */
033: private static final long serialVersionUID = 8589540077390120676L;
034:
035: /** The shape parameter. */
036: private double alpha;
037:
038: /** The scale parameter. */
039: private double beta;
040:
041: /**
042: * Creates weibull distribution with the given shape and scale and a
043: * location equal to zero.
044: * @param alpha the shape parameter.
045: * @param beta the scale parameter.
046: */
047: public WeibullDistributionImpl(double alpha, double beta) {
048: super ();
049: setShape(alpha);
050: setScale(beta);
051: }
052:
053: /**
054: * For this disbution, X, this method returns P(X < <code>x</code>).
055: * @param x the value at which the CDF is evaluated.
056: * @return CDF evaluted at <code>x</code>.
057: */
058: public double cumulativeProbability(double x) {
059: double ret;
060: if (x <= 0.0) {
061: ret = 0.0;
062: } else {
063: ret = 1.0 - Math.exp(-Math.pow(x / getScale(), getShape()));
064: }
065: return ret;
066: }
067:
068: /**
069: * Access the shape parameter.
070: * @return the shape parameter.
071: */
072: public double getShape() {
073: return alpha;
074: }
075:
076: /**
077: * Access the scale parameter.
078: * @return the scale parameter.
079: */
080: public double getScale() {
081: return beta;
082: }
083:
084: /**
085: * For this distribution, X, this method returns the critical point x, such
086: * that P(X < x) = <code>p</code>.
087: * <p>
088: * Returns <code>Double.NEGATIVE_INFINITY</code> for p=0 and
089: * <code>Double.POSITIVE_INFINITY</code> for p=1.
090: *
091: * @param p the desired probability
092: * @return x, such that P(X < x) = <code>p</code>
093: * @throws IllegalArgumentException if <code>p</code> is not a valid
094: * probability.
095: */
096: public double inverseCumulativeProbability(double p) {
097: double ret;
098: if (p < 0.0 || p > 1.0) {
099: throw new IllegalArgumentException(
100: "probability argument must be between 0 and 1 (inclusive)");
101: } else if (p == 0) {
102: ret = 0.0;
103: } else if (p == 1) {
104: ret = Double.POSITIVE_INFINITY;
105: } else {
106: ret = getScale()
107: * Math.pow(-Math.log(1.0 - p), 1.0 / getShape());
108: }
109: return ret;
110: }
111:
112: /**
113: * Modify the shape parameter.
114: * @param alpha the new shape parameter value.
115: */
116: public void setShape(double alpha) {
117: if (alpha <= 0.0) {
118: throw new IllegalArgumentException(
119: "Shape must be positive.");
120: }
121: this .alpha = alpha;
122: }
123:
124: /**
125: * Modify the scale parameter.
126: * @param beta the new scale parameter value.
127: */
128: public void setScale(double beta) {
129: if (beta <= 0.0) {
130: throw new IllegalArgumentException(
131: "Scale must be positive.");
132: }
133: this .beta = beta;
134: }
135:
136: /**
137: * Access the domain value lower bound, based on <code>p</code>, used to
138: * bracket a CDF root. This method is used by
139: * {@link #inverseCumulativeProbability(double)} to find critical values.
140: *
141: * @param p the desired probability for the critical value
142: * @return domain value lower bound, i.e.
143: * P(X < <i>lower bound</i>) < <code>p</code>
144: */
145: protected double getDomainLowerBound(double p) {
146: return 0.0;
147: }
148:
149: /**
150: * Access the domain value upper bound, based on <code>p</code>, used to
151: * bracket a CDF root. This method is used by
152: * {@link #inverseCumulativeProbability(double)} to find critical values.
153: *
154: * @param p the desired probability for the critical value
155: * @return domain value upper bound, i.e.
156: * P(X < <i>upper bound</i>) > <code>p</code>
157: */
158: protected double getDomainUpperBound(double p) {
159: return Double.MAX_VALUE;
160: }
161:
162: /**
163: * Access the initial domain value, based on <code>p</code>, used to
164: * bracket a CDF root. This method is used by
165: * {@link #inverseCumulativeProbability(double)} to find critical values.
166: *
167: * @param p the desired probability for the critical value
168: * @return initial domain value
169: */
170: protected double getInitialDomain(double p) {
171: // use median
172: return Math.pow(getScale() * Math.log(2.0), 1.0 / getShape());
173: }
174: }
|