1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
31
32
33
34 public class LaplaceDistribution extends AbstractRealDistribution {
35
36
37 private static final long serialVersionUID = 20141003L;
38
39
40 private final double mu;
41
42 private final double beta;
43
44
45
46
47
48
49
50
51 public LaplaceDistribution(double mu, double beta)
52 throws MathIllegalArgumentException {
53 if (beta <= 0.0) {
54 throw new MathIllegalArgumentException(LocalizedCoreFormats.NOT_POSITIVE_SCALE, beta);
55 }
56
57 this.mu = mu;
58 this.beta = beta;
59 }
60
61
62
63
64
65
66 public double getLocation() {
67 return mu;
68 }
69
70
71
72
73
74
75 public double getScale() {
76 return beta;
77 }
78
79
80 @Override
81 public double density(double x) {
82 return FastMath.exp(-FastMath.abs(x - mu) / beta) / (2.0 * beta);
83 }
84
85
86 @Override
87 public double cumulativeProbability(double x) {
88 if (x <= mu) {
89 return FastMath.exp((x - mu) / beta) / 2.0;
90 } else {
91 return 1.0 - FastMath.exp((mu - x) / beta) / 2.0;
92 }
93 }
94
95
96 @Override
97 public double inverseCumulativeProbability(double p) throws MathIllegalArgumentException {
98 MathUtils.checkRangeInclusive(p, 0, 1);
99
100 if (p == 0) {
101 return Double.NEGATIVE_INFINITY;
102 } else if (p == 1) {
103 return Double.POSITIVE_INFINITY;
104 }
105 double x = (p > 0.5) ? -Math.log(2.0 - 2.0 * p) : Math.log(2.0 * p);
106 return mu + beta * x;
107 }
108
109
110 @Override
111 public double getNumericalMean() {
112 return mu;
113 }
114
115
116 @Override
117 public double getNumericalVariance() {
118 return 2.0 * beta * beta;
119 }
120
121
122 @Override
123 public double getSupportLowerBound() {
124 return Double.NEGATIVE_INFINITY;
125 }
126
127
128 @Override
129 public double getSupportUpperBound() {
130 return Double.POSITIVE_INFINITY;
131 }
132
133
134 @Override
135 public boolean isSupportConnected() {
136 return true;
137 }
138
139 }