Debian Codegraph @

src/dr/math/distributions/LaplaceDistribution.java

raw symbols SCIP
1 /*
2 * LaplaceDistribution.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 * @author Alexei Drummond
32 */
33 public class LaplaceDistribution implements Distribution {
34
35 // the mean parameter
36 double mu;
37
38 // the scale parameter
39 double beta;
40
41 // the maximum density
42 double c;
43
44 /**
45 * This general constructor creates a new laplace distribution with a
46 * specified mean and scale
47 *
48 * @param mu mean
49 * @param beta scale
50 */
51 public LaplaceDistribution(double mu, double beta) {
52 setParameters(mu, beta);
53 }
54
55 /**
56 * This default constructor creates a new laplace distribution with rate 1
57 */
58 public LaplaceDistribution() {
59 this(0, 1);
60 }
61
62 /**
63 * Set parameters of this distribution
64 *
65 * @param k mean
66 * @param b scale
67 */
68 public void setParameters(double k, double b) {
69 if (b <= 0) b = 1;
70 mu = k;
71 beta = b;
72
73 //Normalizing constant
74 c = 1 / (2 * beta);
75 }
76
77 /**
78 * @return mean parameter
79 */
80 public double getMu() {
81 return mu;
82 }
83
84 /**
85 * @return scale parameter
86 */
87 public double getBeta() {
88 return beta;
89 }
90
91 /**
92 * @return maximum value of pdf function
93 */
94 public double getMaxDensity() {
95 return c;
96 }
97
98 /**
99 * Cumulative distribution function
100 */
101 public double cdf(double x) {
102 if (x == mu) return 0.5;
103 else return (0.5) * (1 + ((x - mu) / Math.abs(x - mu))
104 * (1 - Math.exp(-Math.abs(x - mu) / beta)));
105 }
106
107 /**
108 * Density function
109 */
110 public double pdf(double x) {
111 return c * Math.exp(-Math.abs(x - mu) / beta);
112 }
113
114 public double logPdf(double x) {
115 return Math.log(c) - (Math.abs(x - mu) / beta);
116 }
117
118 public double quantile(double y) {
119 double sign = 1;
120 if (y < 0.5) sign = -1;
121
122 return mu - beta * sign * Math.log(1 - 2 * Math.abs(y - 0.5));
123 }
124
125 public double mean() {
126 return mu;
127 }
128
129 public double variance() {
130 return 2 * beta * beta;
131 }
132
133 public UnivariateFunction getProbabilityDensityFunction() {
134 return pdfFunction;
135 }
136
137 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
138 public final double evaluate(double x) {
139 return pdf(x);
140 }
141
142 public final double getLowerBound() {
143 return Double.NEGATIVE_INFINITY;
144 }
145
146 public final double getUpperBound() {
147 return Double.POSITIVE_INFINITY;
148 }
149 };
150 }