SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
NI2a.java
1/*
2 * Class: NI2a
3 * Description:
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Université de Montréal
7 * Organization: DIRO, Université de Montréal
8 * @author Nabil Channouf
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.probdistmulti.norta;
26
27import umontreal.ssj.util.*;
28import umontreal.ssj.probdist.*;
29
62public class NI2a extends NortaInitDisc {
63 private double h; /*
64 * Predefined step size for the integration-grid spacing (also named h in the
65 * paper, paragraph "Method NI2" of section 3).
66 */
67 private double delta; /*
68 * Small positive parameter to make sure that rho_m is not too close to 1 or -1;
69 * (also named delta in the paper, paragraph " Method NI2" of section 3).
70 */
71
78 public NI2a(double rX, DiscreteDistributionInt dist1, DiscreteDistributionInt dist2, double tr, double h,
79 double delta) {
80 super(rX, dist1, dist2, tr);
81 this.h = h;
82 this.delta = delta;
84 }
85
89 public double computeCorr() {
90 // x, y and c coefficients used for quadratic interpolation.
91 double[] x = new double[3];
92 double[] y = new double[3];
93 double[] c = new double[3];
94 double xtemp = 0.0, temp = 0.0; /*
95 * Values of rho and the recursive quantity I_k, given in paragraph "Method NI2"
96 * of section 3 in the paper, 2 iterations before the last one.
97 */
98 double xold, iold; /*
99 * Values of rho and I_k at one iteration before the last one.
100 */
101 double xnew, inew; // Values of rho and I_k at the last iteration.
102 double dold, dmid, dnew; /*
103 * Values of the derivative function g' at points xold, xmid (xold+h) and xnew.
104 * They correspond to g'(rho_0+2kh-2h), g'(rho_0+2kh-h) and g'(rho_0+2kh) in the
105 * formula of I_k in the paper (paragraph "Method NI2" of section 3).
106 */
107
108 double d = 0.0; // Integration distance.
109 int m; // Number of iterations needed.
114 double b = 0.0; // The returned solution.
115 double h2 = 0.0, hd3 = 0.0; // Precompute constants.
116 // double lrx = (integ ( -1) - mu1 * mu2) / sd1 * sd2; // Min.correlation.
117 // double urx = (integ (1) - mu1 * mu2) / sd1 * sd2; // Max.correlation.
118 double rho1 = 2 * Math.sin(Math.PI * rX / 6); // The initial guess.
119 double intg1 = integ(rho1); // Computes g_r(rho1).
120 double gr = rX * sd1 * sd2 + mu1 * mu2; /*
121 * Target value; integ(\rho) = gr is equivalent to \rho = the solution.
122 */
123 if (intg1 == gr)
124 return rho1;
125
126 if (intg1 < gr) { // Orient the search from left to right
127 if (0 < rX && rX < 1) // Do search between rh_0 and rho_m=1 - delta
128 d = 1 - delta - rho1;
129 if (-1 < rX && rX < 0) // Do search between rh_0 and 0
130 d = -rho1;
131 m = (int) Math.ceil(d / (2 * h));
132 h = d / (2 * m); // Readjust h
133 hd3 = h / 3;
134 h2 = 2 * h;
135 xold = rho1;
136 dold = deriv(xold);
137 iold = intg1;
138
139 for (int i = 1; i <= m; i++) { // Begin the search
140 dmid = deriv(xold + h);
141 xnew = xold + h2;
142 dnew = deriv(xnew);
143 inew = iold + hd3 * (dold + 4 * dmid + dnew);
144
145 if (inew >= gr) { // The root is in current bracketing interval
146 // Compute the parameters of quadratic interpolation
147 x[0] = xtemp;
148 x[1] = xold;
149 x[2] = xnew;
150 y[0] = temp;
151 y[1] = iold;
152 y[2] = inew;
153 Misc.interpol(2, x, y, c);
154 b = (c[2] * (xtemp + xold) - c[1]
155 + Math.sqrt((c[1] - c[2] * (xtemp + xold)) * (c[1] - c[2] * (xtemp + xold))
156 - 4 * c[2] * (c[0] - c[1] * xtemp + c[2] * xtemp * xold - gr)))
157 / (2 * c[2]);
158 return b;
159 }
160 xtemp = xold;
161 temp = iold;
162 xold = xnew;
163 dold = dnew;
164 iold = inew;
165 }
166 b = 1.0 - delta / 2; // The root is at the right of rho_m
167 }
168
169 if (intg1 > gr) { // Orient the search from right to left
170 if (0 < rX && rX < 1) // Do search between 0 and rh_0
171 d = rho1;
172 if (-1 < rX && rX < 0) // Do search between rho_m=-1+delta and rho_0
173 d = rho1 + 1 - delta;
174 m = (int) Math.ceil(d / (2 * h));
175 h = d / (2 * m); // Readjust h
176 hd3 = h / 3; // Pre-compute constant
177 h2 = 2 * h; // Pre-compute constant
178 xold = rho1;
179 dold = deriv(xold);
180 iold = intg1;
181 for (int i = 1; i <= m; i++) { // Begin the search
182 dmid = deriv(xold - h);
183 xnew = xold - h2;
184 dnew = deriv(xnew);
185 inew = iold - hd3 * (dold + 4 * dmid + dnew);
186 if (inew <= gr) { // The root is in current bracketing interval
187 // Compute the parameters of quadratic interpolation
188 x[0] = xnew;
189 x[1] = xold;
190 x[2] = xtemp;
191 y[0] = inew;
192 y[1] = iold;
193 y[2] = temp;
194 Misc.interpol(2, x, y, c);
195 b = (c[2] * (xnew + xold) - c[1]
196 + Math.sqrt((c[1] - c[2] * (xnew + xold)) * (c[1] - c[2] * (xnew + xold))
197 - 4 * c[2] * (c[0] - c[1] * xnew + c[2] * xnew * xold - gr)))
198 / (2 * c[2]);
199 return b;
200 }
201 xtemp = xold;
202 temp = iold;
203 xold = xnew;
204 dold = dnew;
205 iold = inew;
206 }
207 b = -1 + delta / 2; // The root is at the left of rho_m
208 }
209 return b;
210 }
211
212 public String toString() {
213 String desc = super.toString();
214 desc += "h : " + h + "\n";
215 desc += " delta : " + delta + "\n";
216 return desc;
217 }
218
219}
Classes implementing discrete distributions over the integers should inherit from this class.
NI2a(double rX, DiscreteDistributionInt dist1, DiscreteDistributionInt dist2, double tr, double h, double delta)
Constructor with the target rank correlation rX, the two discrete marginals dist1 and dist2,...
Definition NI2a.java:78
double computeCorr()
Computes and returns the correlation using the algorithm NI2a.
Definition NI2a.java:89
NortaInitDisc(double rX, DiscreteDistributionInt dist1, DiscreteDistributionInt dist2, double tr)
Constructor with the target rank correlation rX, the two discrete marginals dist1 and dist2 and the p...
double deriv(double r)
Computes the derivative of , given by.
void computeParams()
Computes the following inputs of each marginal distribution:
double integ(double r)
Computes the function.
This class provides miscellaneous functions that are hard to classify.
Definition Misc.java:33
static void interpol(int n, double[] X, double[] Y, double[] C)
Computes the Newton interpolating polynomial.
Definition Misc.java:221