SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
HyperbolicSecantDist.java
1/*
2 * Class: HyperbolicSecantDist
3 * Description: hyperbolic secant 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
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 optimization.*;
28
49 protected double mu;
50 protected double sigma;
51 private static final double ZLIMB = 500.0;
52 private static final double ZLIMS = 50.0;
53
54 private static class Optim implements Uncmin_methods {
55 private int n;
56 private double[] xi;
57
58 public Optim(double[] x, int n) {
59 this.n = n;
60 this.xi = new double[n];
61 System.arraycopy(x, 0, this.xi, 0, n);
62 }
63
64 public double f_to_minimize(double[] p) {
65 double sum = 0.0;
66
67 if (p[2] <= 0.0)
68 return 1e200;
69
70 for (int i = 0; i < n; i++)
71 sum -= Math.log(density(p[1], p[2], xi[i]));
72
73 return sum;
74 }
75
76 public void gradient(double[] x, double[] g) {
77 }
78
79 public void hessian(double[] x, double[][] h) {
80 }
81 }
82
88 public HyperbolicSecantDist(double mu, double sigma) {
89 setParams(mu, sigma);
90 }
91
92 public double density(double x) {
93 return HyperbolicSecantDist.density(mu, sigma, x);
94 }
95
96 public double cdf(double x) {
97 return HyperbolicSecantDist.cdf(mu, sigma, x);
98 }
99
100 public double barF(double x) {
101 return HyperbolicSecantDist.barF(mu, sigma, x);
102 }
103
104 public double inverseF(double u) {
105 return HyperbolicSecantDist.inverseF(mu, sigma, u);
106 }
107
108 public double getMean() {
109 return HyperbolicSecantDist.getMean(mu, sigma);
110 }
111
112 public double getVariance() {
113 return HyperbolicSecantDist.getVariance(mu, sigma);
114 }
115
116 public double getStandardDeviation() {
117 return HyperbolicSecantDist.getStandardDeviation(mu, sigma);
118 }
119
127 public static double density(double mu, double sigma, double x) {
128 if (sigma <= 0.0)
129 throw new IllegalArgumentException("sigma <= 0");
130 double y = (x - mu) / sigma;
131 if (Math.abs(y) >= ZLIMB)
132 return 0.0;
133 else
134 return (1.0 / (Math.cosh(Math.PI * y / 2.0) * 2.0 * sigma));
135 }
136
141 public static double cdf(double mu, double sigma, double x) {
142 if (sigma <= 0.0)
143 throw new IllegalArgumentException("sigma <= 0");
144 double y = (x - mu) / sigma;
145 if (y >= ZLIMS)
146 return 1.0;
147 else if (y <= -ZLIMB)
148 return 0.0;
149 else
150 return (2.0 * Math.atan(Math.exp(Math.PI * y / 2.0))) / Math.PI;
151 }
152
157 public static double barF(double mu, double sigma, double x) {
158 if (sigma <= 0.0)
159 throw new IllegalArgumentException("sigma <= 0");
160
161 double y = (x - mu) / sigma;
162 if (y >= ZLIMB)
163 return 0.0;
164 else if (y <= -ZLIMS)
165 return 1.0;
166 else
167 return 2.0 / Math.PI * Math.atan(Math.exp(-Math.PI * y / 2.0));
168 }
169
174 public static double inverseF(double mu, double sigma, double u) {
175 if (sigma <= 0.0)
176 throw new IllegalArgumentException("sigma <= 0");
177 if (u < 0.0 || u > 1.0)
178 throw new IllegalArgumentException("u not in [0,1]");
179
180 if (u >= 1.0)
181 return Double.POSITIVE_INFINITY;
182 else if (u <= 0.0)
183 return Double.NEGATIVE_INFINITY;
184 else
185 return (mu + (2.0 * sigma / Math.PI * Math.log(Math.tan(Math.PI / 2.0 * u))));
186 }
187
202 public static double[] getMLE(double[] x, int n) {
203 double sum;
204
205 if (n <= 0)
206 throw new IllegalArgumentException("n <= 0");
207
208 Optim system = new Optim(x, n);
209
210 double[] parameters = new double[2];
211 double[] xpls = new double[3];
212 double[] param = new double[3];
213 double[] fpls = new double[3];
214 double[] gpls = new double[3];
215 int[] itrcmd = new int[2];
216 double[][] a = new double[3][3];
217 double[] udiag = new double[3];
218
219 sum = 0.0;
220 for (int i = 0; i < n; i++)
221 sum += x[i];
222 param[1] = sum / (double) n;
223
224 sum = 0.0;
225 for (int i = 0; i < n; i++)
226 sum += (x[i] - param[1]) * (x[i] - param[1]);
227 param[2] = Math.sqrt(sum / (double) n);
228
229 Uncmin_f77.optif0_f77(2, param, system, xpls, fpls, gpls, itrcmd, a, udiag);
230
231 for (int i = 0; i < 2; i++)
232 parameters[i] = xpls[i + 1];
233
234 return parameters;
235 }
236
246 public static HyperbolicSecantDist getInstanceFromMLE(double[] x, int n) {
247 double parameters[] = getMLE(x, n);
248 return new HyperbolicSecantDist(parameters[0], parameters[1]);
249 }
250
257 public static double getMean(double mu, double sigma) {
258 if (sigma <= 0.0)
259 throw new IllegalArgumentException("sigma <= 0");
260 return mu;
261 }
262
270 public static double getVariance(double mu, double sigma) {
271 if (sigma <= 0.0)
272 throw new IllegalArgumentException("sigma <= 0");
273
274 return (sigma * sigma);
275 }
276
283 public static double getStandardDeviation(double mu, double sigma) {
284 return Math.sqrt(HyperbolicSecantDist.getVariance(mu, sigma));
285 }
286
290 public double getMu() {
291 return mu;
292 }
293
297 public double getSigma() {
298 return sigma;
299 }
300
304 public void setParams(double mu, double sigma) {
305 if (sigma <= 0.0)
306 throw new IllegalArgumentException("sigma <= 0");
307
308 this.mu = mu;
309 this.sigma = sigma;
310 }
311
316 public double[] getParams() {
317 double[] retour = { mu, sigma };
318 return retour;
319 }
320
324 public String toString() {
325 return getClass().getSimpleName() + " : mu = " + mu + ", sigma = " + sigma;
326 }
327
328}
Classes implementing continuous distributions should inherit from this base class.
static double getMean(double mu, double sigma)
Computes and returns the mean of the hyperbolic secant distribution with parameters and .
void setParams(double mu, double sigma)
Sets the parameters and of this object.
static double cdf(double mu, double sigma, double x)
Computes the distribution function of the hyperbolic secant distribution with parameters and .
double getSigma()
Returns the parameter of this object.
static double getStandardDeviation(double mu, double sigma)
Computes and returns the standard deviation of the hyperbolic secant distribution with parameters an...
double barF(double x)
Returns the complementary distribution function.
String toString()
Returns a String containing information about the current distribution.
static HyperbolicSecantDist getInstanceFromMLE(double[] x, int n)
Creates a new instance of a hyperbolic secant distribution with parameters and estimated using the ...
double getMu()
Returns the parameter of this object.
static double[] getMLE(double[] x, int n)
Estimates the parameters of the hyperbolic secant distribution using the maximum likelihood method,...
static double barF(double mu, double sigma, double x)
Computes the complementary distribution function of the hyperbolic secant distribution with parameter...
static double density(double mu, double sigma, double x)
Computes the density function ( fHyperbolicSecant ) for a hyperbolic secant distribution with paramet...
double[] getParams()
Return a table containing the parameters of the current distribution.
HyperbolicSecantDist(double mu, double sigma)
Constructs a hyperbolic secant distribution with parameters.
double cdf(double x)
Returns the distribution function .
double density(double x)
Returns , the density evaluated at .
double inverseF(double u)
Returns the inverse distribution function .
static double inverseF(double mu, double sigma, double u)
Computes the inverse of the hyperbolic secant distribution with parameters and .
static double getVariance(double mu, double sigma)
Computes and returns the variance of the hyperbolic secant distribution with parameters and.
double getStandardDeviation()
Returns the standard deviation.