SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BiStudentDist.java
1/*
2 * Class: BiStudentDist
3 * Description: standard bivariate Student t-distribution
4 * Environment: Java
5 * Software: SSJ
6 * Organization: DIRO, Universite de Montreal
7 * @author
8 * @since
9 */
10package umontreal.ssj.probdistmulti;
11
12import umontreal.ssj.probdist.StudentDist;
13
36 protected int nu; // Number of degrees of freedom
37 protected double rho;
38 protected double facRho; // sqrt(1 - rho^2)
39
45 public BiStudentDist(int nu, double rho) {
46 setParams(nu, rho);
47 }
48
49 public double density(double x, double y) {
50 if (Math.abs(rho) == 1.)
51 throw new IllegalArgumentException("|rho| = 1");
52 double T = 1.0 + (x * x - 2.0 * rho * x * y + y * y) / (nu * facRho * facRho);
53 return 1.0 / (Math.pow(T, (nu + 2) / 2.0) * (2.0 * Math.PI * facRho));
54 }
55
56 public double cdf(double x, double y) {
57 return cdf(nu, x, y, rho);
58 }
59
60 public double barF(double x, double y) {
61 return barF(nu, x, y, rho);
62 }
63
69 public static double density(int nu, double x, double y, double rho) {
70 if (nu < 1)
71 throw new IllegalArgumentException("nu < 1");
72 if (Math.abs(rho) >= 1.)
73 throw new IllegalArgumentException("|rho| >= 1");
74 double fRho = (1.0 - rho) * (1.0 + rho);
75 double T = 1.0 + (x * x - 2.0 * rho * x * y + y * y) / (nu * fRho);
76 return 1.0 / (Math.pow(T, (nu + 2) / 2.0) * (2.0 * Math.PI * Math.sqrt(fRho)));
77 }
78
92 public static double cdf(int nu, double x, double y, double rho) {
93 if (nu < 1)
94 throw new IllegalArgumentException("nu < 1");
95 if (Math.abs(rho) > 1.)
96 throw new IllegalArgumentException("|rho| > 1");
97
98// This function is translated from Alan Genz's Matlab code.
99 /*
100 * % function p = bvtl( nu, dh, dk, r ) % % A function for computing bivariate t
101 * probabilities. % bvtl calculates the probability that x < dh and y < dk; %
102 * parameters % nu number of degrees of freedom % dh 1st upper integration limit
103 * % dk 2nd upper integration limit % r correlation coefficient % % This
104 * function is based on the method described by % Dunnett, C.W. and M. Sobel,
105 * (1954), % A bivariate generalization of Student's t-distribution % with
106 * tables for certain special cases, % Biometrika 41, pp. 153-169, % % Alan Genz
107 * % Department of Mathematics % Washington State University % Pullman, Wa
108 * 99164-3113 % Email : alangenz@wsu.edu %
109 */
110 final double dh = x;
111 final double dk = y;
112 final double eps = 1.0e-15;
113 final double tpi = 2. * Math.PI;
114 double hrk, krh, bvt, snu;
115 double gmph, gmpk, xnkh, xnhk, qhrk, hkn, hpk, hkrn;
116 double btnckh, btnchk, btpdkh, btpdhk;
117
118 if (1. - rho <= eps) {
119 x = Math.min(dh, dk);
120 return StudentDist.cdf(nu, x);
121 }
122 if (rho + 1. <= eps) {
123 if (dh > -dk)
124 return StudentDist.cdf(nu, dh) - StudentDist.cdf(nu, -dk);
125 else
126 return 0.;
127 }
128 final double ors = (1. - rho) * (1. + rho);
129 hrk = dh - rho * dk;
130 krh = dk - rho * dh;
131 if (Math.abs(hrk) + ors > 0.) {
132 xnhk = hrk * hrk / (hrk * hrk + ors * (nu + dk * dk));
133 xnkh = krh * krh / (krh * krh + ors * (nu + dh * dh));
134 } else {
135 xnhk = 0.;
136 xnkh = 0.;
137 }
138 int hs, ks, j;
139 if (dh - rho * dk > 0.)
140 hs = 1;
141 else if (dh - rho * dk < 0.)
142 hs = -1;
143 else
144 hs = 0;
145 if (dk - rho * dh > 0.)
146 ks = 1;
147 else if (dk - rho * dh < 0.)
148 ks = -1;
149 else
150 ks = 0;
151 if (nu % 2 == 0) {
152 bvt = Math.atan2(Math.sqrt(ors), -rho) / tpi;
153 gmph = dh / Math.sqrt(16. * (nu + dh * dh));
154 gmpk = dk / Math.sqrt(16. * (nu + dk * dk));
155 btnckh = 2. * Math.atan2(Math.sqrt(xnkh), Math.sqrt(1. - xnkh)) / Math.PI;
156 btpdkh = 2. * Math.sqrt(xnkh * (1. - xnkh)) / Math.PI;
157 btnchk = 2. * Math.atan2(Math.sqrt(xnhk), Math.sqrt(1. - xnhk)) / Math.PI;
158 btpdhk = 2. * Math.sqrt(xnhk * (1. - xnhk)) / Math.PI;
159 for (j = 1; j <= nu / 2; ++j) {
160 bvt = bvt + gmph * (1. + ks * btnckh);
161 bvt = bvt + gmpk * (1. + hs * btnchk);
162 btnckh = btnckh + btpdkh;
163 btpdkh = 2 * j * btpdkh * (1. - xnkh) / (2 * j + 1);
164 btnchk = btnchk + btpdhk;
165 btpdhk = 2 * j * btpdhk * (1. - xnhk) / (2 * j + 1);
166 gmph = gmph * (j - 0.5) / (j * (1. + dh * dh / nu));
167 gmpk = gmpk * (j - 0.5) / (j * (1. + dk * dk / nu));
168 }
169
170 } else {
171 qhrk = Math.sqrt(dh * dh + dk * dk - 2. * rho * dh * dk + nu * ors);
172 hkrn = dh * dk + rho * nu;
173 hkn = dh * dk - nu;
174 hpk = dh + dk;
175 bvt = Math.atan2(-Math.sqrt(nu) * (hkn * qhrk + hpk * hkrn), hkn * hkrn - nu * hpk * qhrk) / tpi;
176 if (bvt < -10. * eps)
177 bvt = bvt + 1;
178 gmph = dh / (tpi * Math.sqrt(nu) * (1. + dh * dh / nu));
179 gmpk = dk / (tpi * Math.sqrt(nu) * (1. + dk * dk / nu));
180 btnckh = Math.sqrt(xnkh);
181 btpdkh = btnckh;
182 btnchk = Math.sqrt(xnhk);
183 btpdhk = btnchk;
184 for (j = 1; j <= (nu - 1) / 2; ++j) {
185 bvt = bvt + gmph * (1. + ks * btnckh);
186 bvt = bvt + gmpk * (1. + hs * btnchk);
187 btpdkh = (2 * j - 1) * btpdkh * (1. - xnkh) / (2 * j);
188 btnckh = btnckh + btpdkh;
189 btpdhk = (2 * j - 1) * btpdhk * (1. - xnhk) / (2 * j);
190 btnchk = btnchk + btpdhk;
191 gmph = gmph * j / ((j + 0.5) * (1. + dh * dh / nu));
192 gmpk = gmpk * j / ((j + 0.5) * (1. + dk * dk / nu));
193 }
194 }
195 return bvt;
196 }
197
202 public static double barF(int nu, double x, double y, double rho) {
203 double u = 1.0 + cdf(nu, x, y, rho) - cdf(nu, XBIG, y, rho) - cdf(nu, x, XBIG, rho);
204 final double eps = 1.0e-15;
205 if (u < eps)
206 return 0.0;
207 if (u <= 1.0)
208 return u;
209 return 1.0;
210 }
211
212 public double[] getMean() {
213 return getMean(nu, rho);
214 }
215
221 public static double[] getMean(int nu, double rho) {
222 if (nu < 1)
223 throw new IllegalArgumentException("nu < 1");
224 if (Math.abs(rho) > 1.)
225 throw new IllegalArgumentException("|rho| > 1");
226
227 double mean[] = new double[2];
228
229 mean[0] = 0;
230 mean[1] = 0;
231
232 return mean;
233 }
234
235 public double[][] getCovariance() {
236 return getCovariance(nu, rho);
237 }
238
243 public static double[][] getCovariance(int nu, double rho) {
244 if (nu < 1)
245 throw new IllegalArgumentException("nu < 1");
246 if (Math.abs(rho) > 1.)
247 throw new IllegalArgumentException("|rho| > 1");
248
249 double cov[][] = new double[2][2];
250
251 double coeff = (double) nu / ((double) nu - 2.0);
252
253 cov[0][0] = coeff;
254 cov[0][1] = coeff * rho;
255 cov[1][0] = coeff * rho;
256 cov[1][1] = coeff;
257
258 return cov;
259 }
260
261 public double[][] getCorrelation() {
262 return getCovariance(nu, rho);
263 }
264
269 public static double[][] getCorrelation(int nu, double rho) {
270 if (nu < 1)
271 throw new IllegalArgumentException("nu < 1");
272 if (Math.abs(rho) > 1.)
273 throw new IllegalArgumentException("|rho| > 1");
274
275 double corr[][] = new double[2][2];
276
277 corr[0][0] = 1.0;
278 corr[0][1] = rho;
279 corr[1][0] = rho;
280 corr[1][1] = 1.0;
281
282 return corr;
283 }
284
288 protected void setParams(int nu, double rho) {
289 if (nu < 1)
290 throw new IllegalArgumentException("nu < 1");
291 if (Math.abs(rho) > 1.)
292 throw new IllegalArgumentException("|rho| > 1");
293 this.dimension = 2;
294 this.nu = nu;
295 this.rho = rho;
296 facRho = Math.sqrt((1.0 - rho) * (1.0 + rho));
297 }
298
299}
Extends the class ContinuousDistribution for the Student.
double cdf(double x)
Returns the distribution function .
double density(double x, double y)
Returns , the density of evaluated at .
static double barF(int nu, double x, double y, double rho)
Computes the standard upper bivariate Student’s distribution ( cdf2bit ).
static double[][] getCorrelation(int nu, double rho)
Returns the correlation matrix of the bivariate Student’s distribution.
static double cdf(int nu, double x, double y, double rho)
Computes the standard bivariate Student’s distribution ( cdf1bit ) using the method described in tge...
double[][] getCovariance()
Returns the variance-covariance matrix of the distribution, defined as .
double cdf(double x, double y)
Computes the distribution function :
static double[] getMean(int nu, double rho)
Returns the mean vector of the bivariate Student’s.
static double density(int nu, double x, double y, double rho)
Computes the standard bivariate Student’s density function ( pdf1bit ) with correlation = rho and ...
static double[][] getCovariance(int nu, double rho)
Returns the covariance matrix of the bivariate Student’s distribution.
double barF(double x, double y)
Computes the upper cumulative distribution function.
double[] getMean()
Returns the mean vector of the distribution, defined as .
BiStudentDist(int nu, double rho)
Constructs a BiStudentDist object with correlation rho and = nu degrees of freedom.
void setParams(int nu, double rho)
Sets the parameters = nu and = rho of this object.
double[][] getCorrelation()
Returns the correlation matrix of the distribution, defined as.
Classes implementing 2-dimensional continuous distributions should inherit from this class.