SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CIRProcess.java
1/*
2 * Class: CIRProcess
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
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.stochprocess;
26
27import umontreal.ssj.rng.*;
28import umontreal.ssj.probdist.*;
29import umontreal.ssj.randvar.*;
30
56public class CIRProcess extends StochasticProcess {
57 private RandomStream stream;
58 protected ChiSquareNoncentralGen gen;
59 protected double alpha, beta, sigma, nu; // number of degrees of freedom
60 // Precomputed values
61 protected double[] parc, parlam;
62
70 public CIRProcess(double x0, double alpha, double b, double sigma, RandomStream stream) {
71 this(x0, alpha, b, sigma, new ChiSquareNoncentralGen(stream, null));
72 }
73
79 public CIRProcess(double x0, double alpha, double b, double sigma, ChiSquareNoncentralGen gen) {
80 this.alpha = alpha;
81 this.beta = b;
82 this.sigma = sigma;
83 this.x0 = x0;
84 nu = 4.0 * b * alpha / (sigma * sigma);
85 this.gen = gen;
86 stream = gen.getStream();
87 }
88
89 public double nextObservation() {
90 double xOld = path[observationIndex];
91 double lambda = xOld * parlam[observationIndex];
92 double x;
93 if (gen.getClass() == ChiSquareNoncentralPoisGen.class)
94 x = parc[observationIndex] * ChiSquareNoncentralPoisGen.nextDouble(stream, nu, lambda);
95 else if (gen.getClass() == ChiSquareNoncentralGamGen.class)
96 x = parc[observationIndex] * ChiSquareNoncentralGamGen.nextDouble(stream, nu, lambda);
97 else
98 x = parc[observationIndex] * ChiSquareNoncentralGen.nextDouble(stream, nu, lambda);
99 observationIndex++;
100 path[observationIndex] = x;
101 return x;
102 }
103
113 public double nextObservation(double nextTime) {
114 double previousTime = t[observationIndex];
115 double xOld = path[observationIndex];
116 observationIndex++;
117 t[observationIndex] = nextTime;
118 double dt = nextTime - previousTime;
119 double x = nextObservation(xOld, dt);
120 path[observationIndex] = x;
121 return x;
122 }
123
130 public double nextObservation(double x, double dt) {
131 double c = -Math.expm1(-alpha * dt) * sigma * sigma / (4.0 * alpha);
132 double lambda = x * Math.exp(-alpha * dt) / c;
133 if (gen.getClass() == ChiSquareNoncentralPoisGen.class)
134 x = c * ChiSquareNoncentralPoisGen.nextDouble(stream, nu, lambda);
135 else if (gen.getClass() == ChiSquareNoncentralGamGen.class)
136 x = c * ChiSquareNoncentralGamGen.nextDouble(stream, nu, lambda);
137 else
138 x = c * ChiSquareNoncentralGen.nextDouble(stream, nu, lambda);
139 return x;
140 }
141
142 public double[] generatePath() {
143 double xOld = x0;
144 double x, lambda;
145 int j;
146
147 if (gen.getClass() == ChiSquareNoncentralPoisGen.class) {
148 for (j = 0; j < d; j++) {
149 lambda = xOld * parlam[j];
150 x = parc[j] * ChiSquareNoncentralPoisGen.nextDouble(stream, nu, lambda);
151 path[j + 1] = x;
152 xOld = x;
153 }
154
155 } else if (gen.getClass() == ChiSquareNoncentralGamGen.class) {
156 for (j = 0; j < d; j++) {
157 lambda = xOld * parlam[j];
158 x = parc[j] * ChiSquareNoncentralGamGen.nextDouble(stream, nu, lambda);
159 path[j + 1] = x;
160 xOld = x;
161 }
162
163 } else {
164 for (j = 0; j < d; j++) {
165 lambda = xOld * parlam[j];
166 x = parc[j] * ChiSquareNoncentralGen.nextDouble(stream, nu, lambda);
167 path[j + 1] = x;
168 xOld = x;
169 }
170 }
171
172 observationIndex = d;
173 return path;
174 }
175
183 public double[] generatePath(RandomStream stream) {
184 gen.setStream(stream);
185 return generatePath();
186 }
187
194 public void setParams(double x0, double alpha, double b, double sigma) {
195 this.alpha = alpha;
196 this.beta = b;
197 this.sigma = sigma;
198 this.x0 = x0;
199 nu = 4.0 * b * alpha / (sigma * sigma);
200 if (observationTimesSet)
201 init(); // Otherwise not needed.
202 }
203
207 public void setStream(RandomStream stream) {
208 gen.setStream(stream);
209 }
210
215 return gen.getStream();
216 }
217
221 public double getAlpha() {
222 return alpha;
223 }
224
228 public double getB() {
229 return beta;
230 }
231
235 public double getSigma() {
236 return sigma;
237 }
238
245 return gen;
246 }
247
248 protected void initArrays(int d) {
249 double dt, c;
250 for (int j = 0; j < d; j++) {
251 dt = t[j + 1] - t[j];
252 c = -Math.expm1(-alpha * dt) * sigma * sigma / (4.0 * alpha);
253 parc[j] = c;
254 parlam[j] = Math.exp(-alpha * dt) / c;
255 }
256 }
257
258 // This is called by setObservationTimes to precompute constants
259 // in order to speed up the path generation.
260 protected void init() {
261 super.init();
262 parc = new double[d];
263 parlam = new double[d];
264 initArrays(d);
265 }
266
267}
This class implements noncentral chi square random variate generators using the additive property of ...
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
This class implements random variate generators for the noncentral chi square distribution with degr...
static double nextDouble(RandomStream s, double nu, double lambda)
Generates a new variate from the noncentral chi square distribution with nu = degrees of freedom and...
This class implements noncentral chi square random variate generators using Poisson and central chi s...
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
ChiSquareNoncentralGen getGen()
Returns the noncentral chi-square random variate generator used.
CIRProcess(double x0, double alpha, double b, double sigma, ChiSquareNoncentralGen gen)
The noncentral chi-square variate generator gen is specified directly instead of specifying the strea...
void setParams(double x0, double alpha, double b, double sigma)
Resets the parameters , , and of the process.
CIRProcess(double x0, double alpha, double b, double sigma, RandomStream stream)
Constructs a new CIRProcess with parameters , , and initial value .
double[] generatePath()
Generates, returns, and saves the sample path .
double getB()
Returns the value of .
double[] generatePath(RandomStream stream)
Generates a sample path of the process at all observation times, which are provided in array t.
double nextObservation(double x, double dt)
Generates an observation of the process in dt time units, assuming that the process has value at the...
double nextObservation()
Generates and returns the next observation of the stochastic process.
RandomStream getStream()
Returns the random stream of the noncentral chi-square generator.
double getAlpha()
Returns the value of .
double getSigma()
Returns the value of .
double nextObservation(double nextTime)
Generates and returns the next observation at time , using the previous observation time defined ea...
void setStream(RandomStream stream)
Resets the random stream of the noncentral chi-square generator to stream.
Abstract base class for a stochastic process sampled (or observed) at a finite number of time points...
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...