Debian Codegraph @

src/dr/math/distributions/LogNormalDistribution.java

raw symbols SCIP
1 /*
2 * LogNormalDistribution.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math.distributions;
27
28 import dr.math.UnivariateFunction;
29
30 /**
31 * log normal distribution (pdf, cdf, quantile)
32 *
33 * @author Korbinian Strimmer
34 * @version $Id: LogNormalDistribution.java,v 1.3 2005/06/21 16:25:15 beth Exp $
35 */
36 public class LogNormalDistribution implements Distribution {
37 //
38 // Public stuff
39 //
40
41 /**
42 * @param M the mean of the random variable's natural logarithm
43 * @param S the stdev of the random variable's natural logarithm
44 */
45 public LogNormalDistribution(double M, double S) {
46 this.M = M;
47 this.S = S;
48 }
49
50 public final double getM() {
51 return M;
52 }
53
54 public final void setM(double M) {
55 this.M = M;
56 }
57
58 public final double getS() {
59 return S;
60 }
61
62 public final void setS(double S) {
63 this.S = S;
64 }
65
66 public double pdf(double x) {
67 return pdf(x, M, S);
68 }
69
70 public double logPdf(double x) {
71 return logPdf(x, M, S);
72 }
73
74 public double cdf(double x) {
75 return cdf(x, M, S);
76 }
77
78 public double quantile(double y) {
79 return quantile(y, M, S);
80 }
81
82 public double mean() {
83 return mean(M, S);
84 }
85
86 public double variance() {
87 return variance(M, S);
88 }
89
90 public double mode() {
91 return mode(M, S);
92 }
93
94 public final UnivariateFunction getProbabilityDensityFunction() {
95 return pdfFunction;
96 }
97
98 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
99 public final double evaluate(double x) {
100 return pdf(x);
101 }
102
103 public final double getLowerBound() {
104 return 0.0;
105 }
106
107 public final double getUpperBound() {
108 return Double.POSITIVE_INFINITY;
109 }
110 };
111
112 /**
113 * probability density function
114 *
115 * @param x argument
116 * @param M log mean
117 * @param S log standard deviation
118 * @return pdf at x
119 */
120 public static double pdf(double x, double M, double S) {
121 if (x <= 0) return 0; // no density for x<=0
122
123 return NormalDistribution.pdf(Math.log(x), M, S) / x;
124 }
125
126 /**
127 * the natural log of the probability density function of the distribution
128 *
129 * @param x argument
130 * @param M log mean
131 * @param S log standard deviation
132 * @return log pdf at x
133 */
134 public static double logPdf(double x, double M, double S) {
135 if (x < 0) return Double.NEGATIVE_INFINITY; // no density for x<0
136
137 return NormalDistribution.logPdf(Math.log(x), M, S) - Math.log(x);
138 }
139
140 /**
141 * cumulative density function
142 *
143 * @param x argument
144 * @param M log mean
145 * @param S log standard deviation
146 * @return cdf at x
147 */
148 public static double cdf(double x, double M, double S) {
149 if (x < 0) return 0; // no density for x<0
150
151 return NormalDistribution.cdf(Math.log(x), M, S, false);
152 //return NormalDistribution.cdf(Math.log(x), M, S);
153 }
154
155 /**
156 * quantiles (=inverse cumulative density function)
157 *
158 * @param z argument
159 * @param M log mean
160 * @param S log standard deviation
161 * @return icdf at z
162 */
163 public static double quantile(double z, double M, double S) {
164 return Math.exp(NormalDistribution.quantile(z, M, S));
165 }
166
167 /**
168 * mean
169 *
170 * @param M log mean
171 * @param S log standard deviation
172 * @return mean
173 */
174 public static double mean(double M, double S) {
175 return Math.exp(M + (S * S / 2));
176 }
177
178 /**
179 * mode
180 *
181 * @param M log mean
182 * @param S log standard deviation
183 * @return mode
184 */
185 public static double mode(double M, double S) {
186 return Math.exp(M - S * S);
187 }
188
189 /**
190 * variance
191 *
192 * @param M log mean
193 * @param S log standard deviation
194 * @return variance
195 */
196 public static double variance(double M, double S) {
197 final double S2 = S * S;
198
199 return Math.exp(S2 + 2 * M) * (Math.exp(S2) - 1);
200 }
201
202 // Private
203
204 protected double M, S;
205 }