SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CauchyDist.java
1/*
2 * Class: CauchyDist
3 * Description: Cauchy distribution
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 March 2009
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.util.Misc;
28import optimization.*;
29
47public class CauchyDist extends ContinuousDistribution {
48 private double alpha;
49 private double beta;
50
51 private static class Optim implements Uncmin_methods {
52 private int n;
53 private double[] xi;
54
55 public Optim(double[] x, int n) {
56 this.n = n;
57 this.xi = new double[n];
58 System.arraycopy(x, 0, this.xi, 0, n);
59 }
60
61 public double f_to_minimize(double[] p) {
62 double sum = 0.0;
63
64 if (p[2] <= 0.0) // barrier at 0
65 return 1.0e200;
66
67 for (int i = 0; i < n; i++)
68 sum -= Math.log(density(p[1], p[2], xi[i]));
69
70 return sum;
71 }
72
73 public void gradient(double[] x, double[] g) {
74 }
75
76 public void hessian(double[] x, double[][] h) {
77 }
78 }
79
84 public CauchyDist() {
85 setParams(0.0, 1.0);
86 }
87
92 public CauchyDist(double alpha, double beta) {
93 setParams(alpha, beta);
94 }
95
96 public double density(double x) {
97 return density(alpha, beta, x);
98 }
99
100 public double cdf(double x) {
101 return cdf(alpha, beta, x);
102 }
103
104 public double barF(double x) {
105 return barF(alpha, beta, x);
106 }
107
108 public double inverseF(double u) {
109 return inverseF(alpha, beta, u);
110 }
111
112 public double getMean() {
113 return CauchyDist.getMean(alpha, beta);
114 }
115
116 public double getVariance() {
117 return CauchyDist.getVariance(alpha, beta);
118 }
119
120 public double getStandardDeviation() {
121 return CauchyDist.getStandardDeviation(alpha, beta);
122 }
123
127 public static double density(double alpha, double beta, double x) {
128 if (beta <= 0.0)
129 throw new IllegalArgumentException("beta <= 0");
130 double t = (x - alpha) / beta;
131 return 1.0 / (beta * Math.PI * (1 + t * t));
132 }
133
137 public static double cdf(double alpha, double beta, double x) {
138 if (beta <= 0.0)
139 throw new IllegalArgumentException("beta <= 0");
140 double z = (x - alpha) / beta;
141 if (z < -0.5)
142 return Math.atan(-1. / z) / Math.PI;
143 return Math.atan(z) / Math.PI + 0.5;
144 }
145
149 public static double barF(double alpha, double beta, double x) {
150 if (beta <= 0.0)
151 throw new IllegalArgumentException("beta <= 0");
152 double z = (x - alpha) / beta;
153 if (z > 0.5)
154 return Math.atan(1. / z) / Math.PI;
155 return 0.5 - Math.atan(z) / Math.PI;
156 }
157
161 public static double inverseF(double alpha, double beta, double u) {
162 if (beta <= 0.0)
163 throw new IllegalArgumentException("beta <= 0");
164 if (u < 0.0 || u > 1.0)
165 throw new IllegalArgumentException("u must be in [0,1]");
166 if (u <= 0.0)
167 return Double.NEGATIVE_INFINITY;
168 if (u >= 1.0)
169 return Double.POSITIVE_INFINITY;
170 if (u < 0.5)
171 return alpha - 1.0 / Math.tan(Math.PI * u) * beta;
172 return alpha + Math.tan(Math.PI * (u - 0.5)) * beta;
173 }
174
189 public static double[] getMLE(double[] x, int n) {
190 double sum = 0.0;
191
192 if (n <= 0)
193 throw new IllegalArgumentException("n <= 0");
194
195 Optim system = new Optim(x, n);
196
197 double[] parameters = new double[2];
198 double[] xpls = new double[3];
199 double[] param = new double[3];
200 double[] fpls = new double[3];
201 double[] gpls = new double[3];
202 int[] itrcmd = new int[2];
203 double[][] a = new double[3][3];
204 double[] udiag = new double[3];
205
206 param[1] = EmpiricalDist.getMedian(x, n);
207
208 int m = Math.round((float) n / 4.0f);
209 double q3 = Misc.quickSelect(x, n, 3 * m);
210 double q1 = Misc.quickSelect(x, n, m);
211 param[2] = (q3 - q1) / 2.0;
212
213 Uncmin_f77.optif0_f77(2, param, system, xpls, fpls, gpls, itrcmd, a, udiag);
214
215 for (int i = 0; i < 2; i++)
216 parameters[i] = xpls[i + 1];
217
218 return parameters;
219 }
220
230 public static CauchyDist getInstanceFromMLE(double[] x, int n) {
231 double parameters[] = getMLE(x, n);
232 return new CauchyDist(parameters[0], parameters[1]);
233 }
234
241 public static double getMean(double alpha, double beta) {
242 if (beta <= 0.0)
243 throw new IllegalArgumentException("beta <= 0");
244
245 throw new UnsupportedOperationException("Undefined mean");
246 }
247
253 public static double getVariance(double alpha, double beta) {
254 if (beta <= 0.0)
255 throw new IllegalArgumentException("beta <= 0");
256
257 return Double.POSITIVE_INFINITY;
258 }
259
265 public static double getStandardDeviation(double alpha, double beta) {
266 return Double.POSITIVE_INFINITY;
267 }
268
272 public double getAlpha() {
273 return alpha;
274 }
275
279 public double getBeta() {
280 return beta;
281 }
282
287 public void setParams(double alpha, double beta) {
288 if (beta <= 0.0)
289 throw new IllegalArgumentException("beta <= 0");
290 this.alpha = alpha;
291 this.beta = beta;
292 }
293
298 public double[] getParams() {
299 double[] retour = { alpha, beta };
300 return retour;
301 }
302
306 public String toString() {
307 return getClass().getSimpleName() + " : alpha = " + alpha + ", beta = " + beta;
308 }
309
310}
static double density(double alpha, double beta, double x)
Computes the density function.
double density(double x)
Returns , the density evaluated at .
double getMean()
Returns the mean.
double getStandardDeviation()
Returns the standard deviation.
static double getMean(double alpha, double beta)
Throws an exception since the mean does not exist.
static double getStandardDeviation(double alpha, double beta)
Returns since the standard deviation does not exist.
String toString()
Returns a String containing information about the current distribution.
static double getVariance(double alpha, double beta)
Returns since the variance does not exist.
double inverseF(double u)
Returns the inverse distribution function .
static double cdf(double alpha, double beta, double x)
Computes the distribution function.
CauchyDist(double alpha, double beta)
Constructs a CauchyDist object with parameters alpha and beta.
static double[] getMLE(double[] x, int n)
Estimates the parameters of the Cauchy distribution using the maximum likelihood method,...
double getAlpha()
Returns the value of for this object.
double cdf(double x)
Returns the distribution function .
double[] getParams()
Return a table containing parameters of the current distribution.
double getBeta()
Returns the value of for this object.
CauchyDist()
Constructs a CauchyDist object with parameters and .
double getVariance()
Returns the variance.
static CauchyDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a Cauchy distribution with parameters.
static double barF(double alpha, double beta, double x)
Computes the complementary distribution.
double barF(double x)
Returns the complementary distribution function.
void setParams(double alpha, double beta)
Sets the value of the parameters and for this object.
static double inverseF(double alpha, double beta, double u)
Computes the inverse of the distribution.
Classes implementing continuous distributions should inherit from this base class.
Extends DiscreteDistribution to an empirical distribution function, based on the observations (sorte...
double getMedian()
Returns the median.
This class provides miscellaneous functions that are hard to classify.
Definition Misc.java:33
static double quickSelect(double[] A, int n, int k)
Returns the smallest item of the array of size.
Definition Misc.java:47