SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
GammaProcessBridge.java
1/*
2 * Class: GammaProcessBridge
3 * Description:
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 Pierre Tremblay and Jean-Sébastien Parent
9 * @since July 2003
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.stochprocess;
26
27import umontreal.ssj.rng.*;
28import umontreal.ssj.probdist.*;
29import umontreal.ssj.randvar.*;
30import umontreal.ssj.util.Num;
31
51public class GammaProcessBridge extends GammaProcess {
52 protected BetaGen Bgen;
53 protected double mu2OverNu, mu2dTOverNu;
54 protected double[] bMu2dtOverNuL, // For precomputations for G Bridge
55 bMu2dtOverNuR;
56 protected int[] wIndexList;
57 protected int bridgeCounter = -1; // Before 1st observ
58
65 public GammaProcessBridge(double s0, double mu, double nu, RandomStream stream) {
66 this(s0, mu, nu, new GammaGen(stream, new GammaDist(1.0)), new BetaGen(stream, new BetaDist(1.0, 1.0)));
67 }
68
80 public GammaProcessBridge(double s0, double mu, double nu, GammaGen Ggen, BetaGen Bgen) {
81 super(s0, mu, nu, Ggen);
82 this.Bgen = Bgen;
83 this.Bgen.setStream(Ggen.getStream()); // to avoid confusion in streams
84 this.stream = Ggen.getStream();
85 }
86
87 public double nextObservation() {
88 double s;
89 if (bridgeCounter == -1) {
90 s = x0 + Ggen.nextDouble(stream, mu2dTOverNu, muOverNu);
91 if (s <= x0)
92 s = setLarger(x0);
93 bridgeCounter = 0;
94 observationIndex = d;
95 } else {
96 int j = bridgeCounter * 3;
97 int oldIndexL = wIndexList[j];
98 int newIndex = wIndexList[j + 1];
99 int oldIndexR = wIndexList[j + 2];
100
101 double y = Bgen.nextDouble(stream, bMu2dtOverNuL[newIndex], bMu2dtOverNuR[newIndex], 0.0, 1.0);
102 s = path[oldIndexL] + (path[oldIndexR] - path[oldIndexL]) * y;
103 // make sure the process is strictly increasing
104 if (s <= path[oldIndexL])
105 s = setLarger(path, oldIndexL, oldIndexR);
106 bridgeCounter++;
107 observationIndex = newIndex;
108 }
109 observationCounter = bridgeCounter + 1;
110 path[observationIndex] = s;
111 return s;
112 }
113
114 public double nextObservation(double nextT) {
115 double s;
116 if (bridgeCounter == -1) {
117 t[d] = nextT;
118 mu2dTOverNu = mu2OverNu * (t[d] - t[0]);
119 s = x0 + Ggen.nextDouble(stream, mu2dTOverNu, muOverNu);
120 if (s <= x0)
121 s = setLarger(x0);
122 bridgeCounter = 0;
123 observationIndex = d;
124 } else {
125 int j = bridgeCounter * 3;
126 int oldIndexL = wIndexList[j];
127 int newIndex = wIndexList[j + 1];
128 int oldIndexR = wIndexList[j + 2];
129
130 t[newIndex] = nextT;
131 bMu2dtOverNuL[newIndex] = mu2OverNu * (t[newIndex] - t[oldIndexL]);
132 bMu2dtOverNuR[newIndex] = mu2OverNu * (t[oldIndexR] - t[newIndex]);
133
134 double y = Bgen.nextDouble(stream, bMu2dtOverNuL[newIndex], bMu2dtOverNuR[newIndex], 0.0, 1.0);
135
136 s = path[oldIndexL] + (path[oldIndexR] - path[oldIndexL]) * y;
137 // make sure the process is strictly increasing
138 if (s <= path[oldIndexL])
139 s = setLarger(path, oldIndexL, oldIndexR);
140 bridgeCounter++;
141 observationIndex = newIndex;
142 }
143 observationCounter = bridgeCounter + 1;
144 path[observationIndex] = s;
145 return s;
146 }
147
148 public double[] generatePath(double[] uniform01) {
149 int oldIndexL, oldIndexR, newIndex;
150 double y;
151
152 path[d] = x0 + GammaDist.inverseF(mu2dTOverNu, muOverNu, 10, uniform01[0]);
153 for (int j = 0; j < 3 * (d - 1); j += 3) {
154 oldIndexL = wIndexList[j];
155 newIndex = wIndexList[j + 1];
156 oldIndexR = wIndexList[j + 2];
157
158 y = BetaDist.inverseF(bMu2dtOverNuL[newIndex], bMu2dtOverNuR[newIndex], 8, uniform01[1 + j / 3]);
159
160 path[newIndex] = path[oldIndexL] + (path[oldIndexR] - path[oldIndexL]) * y;
161 // make sure the process is strictly increasing
162 if (path[newIndex] <= path[oldIndexL])
163 setLarger(path, oldIndexL, newIndex, oldIndexR);
164 }
165 // resetStartProcess();
166 observationIndex = d;
167 observationCounter = d;
168 return path;
169 }
170
171 public double[] generatePath() {
172 int oldIndexL, oldIndexR, newIndex;
173 double y;
174
175 path[d] = x0 + Ggen.nextDouble(stream, mu2dTOverNu, muOverNu);
176 for (int j = 0; j < 3 * (d - 1); j += 3) {
177 oldIndexL = wIndexList[j];
178 newIndex = wIndexList[j + 1];
179 oldIndexR = wIndexList[j + 2];
180
181 y = Bgen.nextDouble(stream, bMu2dtOverNuL[newIndex], bMu2dtOverNuR[newIndex], 0.0, 1.0);
182 path[newIndex] = path[oldIndexL] + (path[oldIndexR] - path[oldIndexL]) * y;
183 // make sure the process is strictly increasing
184 if (path[newIndex] <= path[oldIndexL])
185 setLarger(path, oldIndexL, newIndex, oldIndexR);
186 }
187 // resetStartProcess();
188 observationIndex = d;
189 observationCounter = d;
190 return path;
191 }
192
193 public void resetStartProcess() {
194 observationIndex = 0;
195 observationCounter = 0;
196 bridgeCounter = -1;
197 }
198
199 protected void init() {
200 super.init();
201 if (observationTimesSet) {
202
203 // Quantities for gamma bridge process
204 bMu2dtOverNuL = new double[d + 1];
205 bMu2dtOverNuR = new double[d + 1];
206 wIndexList = new int[3 * d];
207
208 int[] ptIndex = new int[d + 1];
209 int indexCounter = 0;
210 int newIndex, oldLeft, oldRight;
211
212 ptIndex[0] = 0;
213 ptIndex[1] = d;
214
215 mu2OverNu = mu * mu / nu;
216 mu2dTOverNu = mu2OverNu * (t[d] - t[0]);
217
218 for (int powOfTwo = 1; powOfTwo <= d / 2; powOfTwo *= 2) {
219 /* Make room in the indexing array "ptIndex" */
220 for (int j = powOfTwo; j >= 1; j--) {
221 ptIndex[2 * j] = ptIndex[j];
222 }
223
224 /* Insert new indices and Calculate constants */
225 for (int j = 1; j <= powOfTwo; j++) {
226 oldLeft = 2 * j - 2;
227 oldRight = 2 * j;
228 newIndex = (int) (0.5 * (ptIndex[oldLeft] + ptIndex[oldRight]));
229
230 bMu2dtOverNuL[newIndex] = mu * mu * (t[newIndex] - t[ptIndex[oldLeft]]) / nu;
231 bMu2dtOverNuR[newIndex] = mu * mu * (t[ptIndex[oldRight]] - t[newIndex]) / nu;
232
233 ptIndex[oldLeft + 1] = newIndex;
234 wIndexList[indexCounter] = ptIndex[oldLeft];
235 wIndexList[indexCounter + 1] = newIndex;
236 wIndexList[indexCounter + 2] = ptIndex[oldRight];
237
238 indexCounter += 3;
239 }
240 }
241 /* Check if there are holes remaining and fill them */
242 for (int k = 1; k < d; k++) {
243 if (ptIndex[k - 1] + 1 < ptIndex[k]) {
244 // there is a hole between (k-1) and k.
245
246 bMu2dtOverNuL[ptIndex[k - 1] + 1] = mu * mu * (t[ptIndex[k - 1] + 1] - t[ptIndex[k - 1]]) / nu;
247 bMu2dtOverNuR[ptIndex[k - 1] + 1] = mu * mu * (t[ptIndex[k]] - t[ptIndex[k - 1] + 1]) / nu;
248
249 wIndexList[indexCounter] = ptIndex[k] - 2;
250 wIndexList[indexCounter + 1] = ptIndex[k] - 1;
251 wIndexList[indexCounter + 2] = ptIndex[k];
252 indexCounter += 3;
253 }
254 }
255 }
256 }
257
264 public void setStream(RandomStream stream) {
265 super.setStream(stream);
266 this.Bgen.setStream(stream);
267 this.stream = stream;
268 }
269
270}
Extends the class ContinuousDistribution for the beta distribution.
Definition BetaDist.java:54
double inverseF(double u)
Returns the inverse distribution function .
Extends the class ContinuousDistribution for the gamma distribution tjoh95a  (page 337) with shape pa...
double inverseF(double u)
Returns the inverse distribution function .
This class implements random variate generators with the beta distribution with shape parameters and...
Definition BetaGen.java:49
This class implements random variate generators for the gamma distribution.
Definition GammaGen.java:47
void setStream(RandomStream stream)
Sets the umontreal.ssj.rng.RandomStream used by this generator to stream.
double nextObservation()
Generates and returns the next observation of the stochastic process.
GammaProcessBridge(double s0, double mu, double nu, RandomStream stream)
Constructs a new GammaProcessBridge with parameters , and initial value .
double nextObservation(double nextT)
Generates and returns the next observation at time , using the previous observation time defined ea...
void resetStartProcess()
Resets the observation counter to its initial value , so that the current observation becomes .
double[] generatePath(double[] uniform01)
Generates, returns and saves the path .
double[] generatePath()
Generates, returns and saves the path .
GammaProcessBridge(double s0, double mu, double nu, GammaGen Ggen, BetaGen Bgen)
Constructs a new GammaProcessBridge.
void setStream(RandomStream stream)
Resets the umontreal.ssj.rng.RandomStream of the.
GammaProcess(double s0, double mu, double nu, RandomStream stream)
Constructs a new GammaProcess with parameters , and initial value .
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...