SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
OrnsteinUhlenbeckProcess.java
1/*
2 * Class: OrnsteinUhlenbeckProcess
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
54 protected NormalGen gen;
55 protected double alpha, beta, sigma;
56 // Precomputed values
57 protected double[] badt, alphadt, sigmasqrdt;
58
66 public OrnsteinUhlenbeckProcess(double x0, double alpha, double b, double sigma, RandomStream stream) {
67 this(x0, alpha, b, sigma, new NormalGen(stream));
68 }
69
75 public OrnsteinUhlenbeckProcess(double x0, double alpha, double b, double sigma, NormalGen gen) {
76 this.alpha = alpha;
77 this.beta = b;
78 this.sigma = sigma;
79 this.x0 = x0;
80 this.gen = gen;
81 }
82
83 public double nextObservation() {
84 double xOld = path[observationIndex];
85 double x = badt[observationIndex] + xOld * alphadt[observationIndex]
86 + sigmasqrdt[observationIndex] * gen.nextDouble();
87 observationIndex++;
88 path[observationIndex] = x;
89 return x;
90 }
91
102 public double nextObservation(double nextTime) {
103 double previousTime = t[observationIndex];
104 double xOld = path[observationIndex];
105 observationIndex++;
106 t[observationIndex] = nextTime;
107 double dt = nextTime - previousTime;
108 double tem = Math.exp(-alpha * dt);
109 double tem1 = -Math.expm1(-alpha * dt);
110 double x = tem * xOld + beta * tem1 + sigma * Math.sqrt(tem1 * (1.0 + tem) / (2.0 * alpha)) * gen.nextDouble();
111 path[observationIndex] = x;
112 return x;
113 }
114
121 public double nextObservation(double x, double dt) {
122 double tem = Math.exp(-alpha * dt);
123 double tem1 = -Math.expm1(-alpha * dt);
124 x = tem * x + beta * tem1 + sigma * Math.sqrt(tem1 * (1.0 + tem) / (2.0 * alpha)) * gen.nextDouble();
125 return x;
126 }
127
128 public double[] generatePath() {
129 double x;
130 double xOld = x0;
131 for (int j = 0; j < d; j++) {
132 x = badt[j] + xOld * alphadt[j] + sigmasqrdt[j] * gen.nextDouble();
133 path[j + 1] = x;
134 xOld = x;
135 }
136 observationIndex = d;
137 return path;
138 }
139
147 public double[] generatePath(RandomStream stream) {
148 gen.setStream(stream);
149 return generatePath();
150 }
151
159 public void setParams(double x0, double alpha, double b, double sigma) {
160 this.alpha = alpha;
161 this.beta = b;
162 this.sigma = sigma;
163 this.x0 = x0;
164 if (observationTimesSet)
165 init(); // Otherwise not needed.
166 }
167
171 public void setStream(RandomStream stream) {
172 gen.setStream(stream);
173 }
174
179 return gen.getStream();
180 }
181
185 public double getAlpha() {
186 return alpha;
187 }
188
192 public double getB() {
193 return beta;
194 }
195
199 public double getSigma() {
200 return sigma;
201 }
202
207 public NormalGen getGen() {
208 return gen;
209 }
210
211 protected void initArrays(int d) {
212 double dt, tem, tem1;
213 for (int j = 0; j < d; j++) {
214 dt = t[j + 1] - t[j];
215 tem = Math.exp(-alpha * dt);
216 tem1 = -Math.expm1(-alpha * dt);
217 badt[j] = beta * tem1;
218 alphadt[j] = tem;
219 sigmasqrdt[j] = sigma * Math.sqrt(tem1 * (1.0 + tem) / (2.0 * alpha));
220 }
221 }
222
223 // This is called by setObservationTimes to precompute constants
224 // in order to speed up the path generation.
225 protected void init() {
226 super.init();
227 badt = new double[d];
228 alphadt = new double[d];
229 sigmasqrdt = new double[d];
230 initArrays(d);
231 }
232
233}
This class implements methods for generating random variates from the normal distribution .
RandomStream getStream()
Returns the random stream of the normal generator.
OrnsteinUhlenbeckProcess(double x0, double alpha, double b, double sigma, NormalGen gen)
Here, the normal variate generator is specified directly instead of specifying the stream.
double[] generatePath()
Generates, returns, and saves the sample path .
double nextObservation()
Generates and returns the next observation of the stochastic process.
OrnsteinUhlenbeckProcess(double x0, double alpha, double b, double sigma, RandomStream stream)
Constructs a new OrnsteinUhlenbeckProcess with parameters.
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...
void setParams(double x0, double alpha, double b, double sigma)
Resets the parameters x0, alpha,.
NormalGen getGen()
Returns the normal random variate generator used.
double nextObservation(double nextTime)
Generates and returns the next observation at time nextTime, using the previous observation time de...
void setStream(RandomStream stream)
Resets the random stream of the normal 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...