SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
TruncatedDist.java
1/*
2 * Class: TruncatedDist
3 * Description: an arbitrary continuous distribution truncated
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Richard Simard
9 * @since
10 *
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 */
25package umontreal.ssj.probdist;
26
27import umontreal.ssj.functions.MathFunctionUtil;
28import umontreal.ssj.functions.MathFunction;
29
49 public static int NUMINTERVALS = 500;
50
51 private ContinuousDistribution dist0; // The original (non-truncated) dist.
52 private double fa; // F(a)
53 private double fb; // F(b)
54 private double barfb; // bar F(b)
55 private double fbfa; // F(b) - F(a)
56 private double a;
57 private double b;
58 private double approxMean;
59 private double approxVariance;
60 private double approxStandardDeviation;
61
66 public TruncatedDist(ContinuousDistribution dist, double a, double b) {
67 setParams(dist, a, b);
68 }
69
70 public double density(double x) {
71 if ((x < a) || (x > b))
72 return 0;
73 return dist0.density(x) / fbfa;
74 }
75
76 public double cdf(double x) {
77 if (x <= a)
78 return 0;
79 else if (x >= b)
80 return 1;
81 else
82 return (dist0.cdf(x) - fa) / fbfa;
83 }
84
85 public double barF(double x) {
86 if (x <= a)
87 return 1;
88 else if (x >= b)
89 return 0;
90 else
91 return (dist0.barF(x) - barfb) / fbfa;
92 }
93
94 public double inverseF(double u) {
95 if (u == 0)
96 return a;
97 if (u == 1)
98 return b;
99 return dist0.inverseF(fa + fbfa * u);
100 }
101
109 public double getMean() {
110 if (Double.isNaN(approxMean))
111 throw new UnsupportedOperationException("Undefined mean");
112 return approxMean;
113 }
114
122 public double getVariance() {
123 if (Double.isNaN(approxVariance))
124 throw new UnsupportedOperationException("Unknown variance");
125 return approxVariance;
126 }
127
134 public double getStandardDeviation() {
135 if (Double.isNaN(approxStandardDeviation))
136 throw new UnsupportedOperationException("Unknown standard deviation");
137 return approxStandardDeviation;
138 }
139
143 public double getA() {
144 return a;
145 }
146
150 public double getB() {
151 return b;
152 }
153
157 public double getFa() {
158 return fa;
159 }
160
164 public double getFb() {
165 return fb;
166 }
167
172 public double getArea() {
173 return fbfa;
174 }
175
180 public void setParams(ContinuousDistribution dist, double a, double b) {
181 if (a >= b)
182 throw new IllegalArgumentException("a must be smaller than b.");
183 this.dist0 = dist;
184 if (a < dist.getXinf())
185 a = dist.getXinf();
186 if (b > dist.getXsup())
187 b = dist.getXsup();
188 supportA = this.a = a;
189 supportB = this.b = b;
190 fa = dist.cdf(a);
191 fb = dist.cdf(b);
192 fbfa = fb - fa;
193 barfb = dist.barF(b);
194
195 if (((a <= dist.getXinf()) && (b >= dist.getXsup()))
196 || ((a == Double.NEGATIVE_INFINITY) && (b == Double.POSITIVE_INFINITY))) {
197 approxMean = dist.getMean();
198 approxVariance = dist.getVariance();
199 approxStandardDeviation = dist.getStandardDeviation();
200
201 } else {
202 // The mean is the integral of xf*(x) over [a, b].
203 MomentFunction func1 = new MomentFunction(dist, 1);
204 approxMean = MathFunctionUtil.simpsonIntegral(func1, a, b, NUMINTERVALS) / fbfa;
205
206 // Estimate the integral of (x-E[X])^2 f*(x) over [a, b]
207 MomentFunction func2 = new MomentFunction(dist, 2, approxMean);
208 approxVariance = MathFunctionUtil.simpsonIntegral(func2, a, b, NUMINTERVALS) / fbfa;
209
210 approxStandardDeviation = Math.sqrt(approxVariance);
211 }
212 }
213
219 public double[] getParams() {
220 double[] retour = { a, b, fa, fb, fbfa };
221 return retour;
222 }
223
227 public String toString() {
228 return getClass().getSimpleName() + " : a = " + a + ", b = " + b + ", F(a) = " + fa + ", F(b) = " + fb
229 + ", F(b)-F(a) = " + fbfa;
230 }
231
232 private static class MomentFunction implements MathFunction {
233 private ContinuousDistribution dist;
234 private int moment;
235 private double offset;
236
237 public MomentFunction(ContinuousDistribution dist, int moment) {
238 this.dist = dist;
239 this.moment = moment;
240 offset = 0;
241 }
242
243 public MomentFunction(ContinuousDistribution dist, int moment, double offset) {
244 this(dist, moment);
245 this.offset = offset;
246 }
247
248 public double evaluate(double x) {
249 double res = dist.density(x);
250 final double offsetX = x - offset;
251 for (int i = 0; i < moment; i++)
252 res *= offsetX;
253 return res;
254 }
255 }
256}
Provides utility methods for computing derivatives and integrals of functions.
static double simpsonIntegral(MathFunction func, double a, double b, int numIntervals)
Computes and returns an approximation of the integral of func over , using the Simpson’s method wit...
Classes implementing continuous distributions should inherit from this base class.
double getStandardDeviation()
Returns the standard deviation.
double getXinf()
Returns such that the probability density is 0 everywhere outside the interval .
double getXsup()
Returns such that the probability density is 0 everywhere outside the interval .
abstract double density(double x)
Returns , the density evaluated at .
double barF(double x)
Returns the complementary distribution function.
double getStandardDeviation()
Returns the square root of the approximate variance.
double density(double x)
Returns , the density evaluated at .
double getB()
Returns the value of .
double barF(double x)
Returns the complementary distribution function.
String toString()
Returns a String containing information about the current distribution.
void setParams(ContinuousDistribution dist, double a, double b)
Sets the parameters dist, and for this object.
double getMean()
Returns an approximation of the mean computed with the Simpson.
TruncatedDist(ContinuousDistribution dist, double a, double b)
Constructs a new distribution by truncating distribution dist to the interval .
double getArea()
Returns the value of , the area under the truncated density function.
double getVariance()
Returns an approximation of the variance computed with the Simpson.
double inverseF(double u)
Returns the inverse distribution function .
double[] getParams()
Return a table containing the parameters of the current distribution.
double getFa()
Returns the value of .
double cdf(double x)
Returns the distribution function .
double getA()
Returns the value of .
double getFb()
Returns the value of .
This interface should be implemented by classes which represent univariate mathematical functions.
double cdf(double x)
Returns the distribution function .