View Javadoc
1   /*
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
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
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.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.distribution.continuous;
23  
24  import org.hipparchus.exception.LocalizedCoreFormats;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.util.FastMath;
27  import org.hipparchus.util.MathUtils;
28  
29  /**
30   * This class implements the Logistic distribution.
31   *
32   * @see <a href="http://en.wikipedia.org/wiki/Logistic_distribution">Logistic Distribution (Wikipedia)</a>
33   * @see <a href="http://mathworld.wolfram.com/LogisticDistribution.html">Logistic Distribution (Mathworld)</a>
34   */
35  public class LogisticDistribution extends AbstractRealDistribution {
36  
37      /** Serializable version identifier. */
38      private static final long serialVersionUID = 20141003L;
39  
40      /** The location parameter. */
41      private final double mu;
42      /** The scale parameter. */
43      private final double s;
44  
45      /**
46       * Build a new instance.
47       *
48       * @param mu location parameter
49       * @param s scale parameter (must be positive)
50       * @throws MathIllegalArgumentException if {@code beta <= 0}
51       */
52      public LogisticDistribution(double mu, double s)
53          throws MathIllegalArgumentException {
54          if (s <= 0.0) {
55              throw new MathIllegalArgumentException(LocalizedCoreFormats.NOT_POSITIVE_SCALE,
56                                                     s);
57          }
58  
59          this.mu = mu;
60          this.s = s;
61      }
62  
63      /**
64       * Access the location parameter, {@code mu}.
65       *
66       * @return the location parameter.
67       */
68      public double getLocation() {
69          return mu;
70      }
71  
72      /**
73       * Access the scale parameter, {@code s}.
74       *
75       * @return the scale parameter.
76       */
77      public double getScale() {
78          return s;
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public double density(double x) {
84          double z = (x - mu) / s;
85          double v = FastMath.exp(-z);
86          return 1 / s * v / ((1.0 + v) * (1.0 + v));
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public double cumulativeProbability(double x) {
92          double z = 1 / s * (x - mu);
93          return 1.0 / (1.0 + FastMath.exp(-z));
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
99          MathUtils.checkRangeInclusive(p, 0, 1);
100 
101         if (p == 0) {
102             return 0.0;
103         } else if (p == 1) {
104             return Double.POSITIVE_INFINITY;
105         }
106         return s * Math.log(p / (1.0 - p)) + mu;
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public double getNumericalMean() {
112         return mu;
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public double getNumericalVariance() {
118         return (MathUtils.PI_SQUARED / 3.0) * (1.0 / (s * s));
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public double getSupportLowerBound() {
124         return Double.NEGATIVE_INFINITY;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public double getSupportUpperBound() {
130         return Double.POSITIVE_INFINITY;
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public boolean isSupportConnected() {
136         return true;
137     }
138 
139 }