SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CIRProcessEuler.java
1/*
2 * Class: CIRProcessEuler
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
43public class CIRProcessEuler extends StochasticProcess {
44 protected NormalGen gen;
45 protected double alpha, beta, sigma;
46 // Precomputed values
47 protected double[] alphadt, sigmasqrdt;
48
55 public CIRProcessEuler(double x0, double alpha, double b, double sigma, RandomStream stream) {
56 this(x0, alpha, b, sigma, new NormalGen(stream, new NormalDist()));
57 }
58
63 public CIRProcessEuler(double x0, double alpha, double b, double sigma, NormalGen gen) {
64 this.alpha = alpha;
65 this.beta = b;
66 this.sigma = sigma;
67 this.x0 = x0;
68 this.gen = gen;
69 }
70
71 public double nextObservation() {
72 double xOld = path[observationIndex];
73 double x;
74 x = xOld + (beta - xOld) * alphadt[observationIndex]
75 + sigmasqrdt[observationIndex] * Math.sqrt(xOld) * gen.nextDouble();
76 observationIndex++;
77 if (x >= 0.0)
78 path[observationIndex] = x;
79 else
80 path[observationIndex] = 0.;
81 return x;
82 }
83
94 public double nextObservation(double nextTime) {
95 double previousTime = t[observationIndex];
96 double xOld = path[observationIndex];
97 observationIndex++;
98 t[observationIndex] = nextTime;
99 double dt = nextTime - previousTime;
100 double x = xOld + alpha * (beta - xOld) * dt + sigma * Math.sqrt(dt * xOld) * gen.nextDouble();
101 if (x >= 0.0)
102 path[observationIndex] = x;
103 else
104 path[observationIndex] = 0.;
105 return x;
106 }
107
114 public double nextObservation(double x, double dt) {
115 x = x + alpha * (beta - x) * dt + sigma * Math.sqrt(dt * x) * gen.nextDouble();
116 if (x >= 0.0)
117 return x;
118 return 0.0;
119 }
120
121 public double[] generatePath() {
122 double x;
123 double xOld = x0;
124 for (int j = 0; j < d; j++) {
125 x = xOld + (beta - xOld) * alphadt[j] + sigmasqrdt[j] * Math.sqrt(xOld) * gen.nextDouble();
126 if (x < 0.0)
127 x = 0.0;
128 path[j + 1] = x;
129 xOld = x;
130 }
131 observationIndex = d;
132 return path;
133 }
134
142 public double[] generatePath(RandomStream stream) {
143 gen.setStream(stream);
144 return generatePath();
145 }
146
154 public void setParams(double x0, double alpha, double b, double sigma) {
155 this.alpha = alpha;
156 this.beta = b;
157 this.sigma = sigma;
158 this.x0 = x0;
159 if (observationTimesSet)
160 init(); // Otherwise not needed.
161 }
162
166 public void setStream(RandomStream stream) {
167 gen.setStream(stream);
168 }
169
174 return gen.getStream();
175 }
176
180 public double getAlpha() {
181 return alpha;
182 }
183
187 public double getB() {
188 return beta;
189 }
190
194 public double getSigma() {
195 return sigma;
196 }
197
202 public NormalGen getGen() {
203 return gen;
204 }
205
206 // This is called by setObservationTimes to precompute constants
207 // in order to speed up the path generation.
208 protected void init() {
209 super.init();
210 alphadt = new double[d];
211 sigmasqrdt = new double[d];
212 double dt;
213 for (int j = 0; j < d; j++) {
214 dt = t[j + 1] - t[j];
215 alphadt[j] = alpha * (dt);
216 sigmasqrdt[j] = sigma * Math.sqrt(dt);
217 }
218 }
219
220}
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
This class implements methods for generating random variates from the normal distribution .
void setStream(RandomStream stream)
Resets the random stream of the normal generator to stream.
double getSigma()
Returns the value of .
void setParams(double x0, double alpha, double b, double sigma)
Resets the parameters x0, alpha,.
double[] generatePath()
Generates, returns, and saves the sample path .
double nextObservation(double x, double dt)
Generates an observation of the process in dt time units, assuming that the process has value at the...
RandomStream getStream()
Returns the random stream of the normal generator.
double[] generatePath(RandomStream stream)
Generates a sample path of the process at all observation times, which are provided in array t.
CIRProcessEuler(double x0, double alpha, double b, double sigma, RandomStream stream)
Constructs a new CIRProcessEuler with parameters alpha, , sigma and initial value x0.
NormalGen getGen()
Returns the normal random variate generator used.
double nextObservation()
Generates and returns the next observation of the stochastic process.
double getAlpha()
Returns the value of .
double nextObservation(double nextTime)
Generates and returns the next observation at time nextTime, using the previous observation time de...
CIRProcessEuler(double x0, double alpha, double b, double sigma, NormalGen gen)
The normal variate generator gen is specified directly instead of specifying the 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...