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