SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BrownianMotion.java
1/*
2 * Class: BrownianMotion
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
54public class BrownianMotion extends StochasticProcess {
55 protected NormalGen gen;
56 protected double mu, sigma;
57 // Precomputed values for standard BM
58 protected double[] mudt, sigmasqrdt;
59
69 public BrownianMotion(double x0, double mu, double sigma, RandomStream stream) {
70 this(x0, mu, sigma, new NormalGen(stream));
71 }
72
82 public BrownianMotion(double x0, double mu, double sigma, NormalGen gen) {
83 this.mu = mu;
84 this.sigma = sigma;
85 this.x0 = x0;
86 this.gen = gen;
87 }
88
89 public double nextObservation() {
90 double x = path[observationIndex];
91 x += mudt[observationIndex] + sigmasqrdt[observationIndex] * gen.nextDouble();
92 observationIndex++;
93 path[observationIndex] = x;
94 return x;
95 }
96
105 public double nextObservation(double nextTime) {
106 // This method is useful for generating variance gamma processes
107 double x = path[observationIndex];
108 double previousTime = t[observationIndex];
109 observationIndex++;
110 t[observationIndex] = nextTime;
111 double dt = nextTime - previousTime;
112 x += mu * dt + sigma * Math.sqrt(dt) * gen.nextDouble();
113 path[observationIndex] = x;
114 return x;
115 }
116
123 public double nextObservation(double x, double dt) {
124 x += mu * dt + sigma * Math.sqrt(dt) * gen.nextDouble();
125 return x;
126 }
127
128 public double[] generatePath() {
129 double x = x0;
130 for (int j = 0; j < d; j++) {
131 x += mudt[j] + sigmasqrdt[j] * gen.nextDouble();
132 path[j + 1] = x;
133 }
134 observationIndex = d;
135 observationCounter = d;
136 return path;
137 }
138
144 public double[] generatePath(double[] uniform01) {
145 double x = x0;
146 for (int j = 0; j < d; j++) {
147 x += mudt[j] + sigmasqrdt[j] * NormalDist.inverseF01(uniform01[j]);
148 path[j + 1] = x;
149 }
150 observationIndex = d;
151 observationCounter = d;
152 return path;
153 }
154
155 public double[] generatePath(RandomStream stream) {
156 gen.setStream(stream);
157 return generatePath();
158 }
159
166 public void setParams(double x0, double mu, double sigma) {
167 this.x0 = x0;
168 this.mu = mu;
169 if (sigma <= 0)
170 throw new IllegalArgumentException("sigma <= 0");
171 this.sigma = sigma;
172 if (observationTimesSet)
173 init(); // Otherwise not needed.
174 }
175
179 public void setStream(RandomStream stream) {
180 gen.setStream(stream);
181 }
182
187 return gen.getStream();
188 }
189
193 public double getMu() {
194 return mu;
195 }
196
200 public double getSigma() {
201 return sigma;
202 }
203
210 public NormalGen getGen() {
211 return gen;
212 }
213
214 // This is called by setObservationTimes to precompute constants
215 // in order to speed up the path generation.
216 protected void init() {
217 super.init();
218 mudt = new double[d];
219 sigmasqrdt = new double[d];
220 for (int j = 0; j < d; j++) {
221 double dt = t[j + 1] - t[j];
222 mudt[j] = mu * dt;
223 sigmasqrdt[j] = sigma * Math.sqrt(dt);
224 }
225 }
226
227}
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
static double inverseF01(double u)
Same as inverseF(0, 1, u).
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 getMu()
Returns the value of .
double nextObservation(double nextTime)
Generates and returns the next observation at time nextTime.
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()
Generates, returns, and saves the sample path .
NormalGen getGen()
Returns the normal random variate generator used.
double[] generatePath(RandomStream stream)
Same as generatePath(), but first resets the stream to stream.
double getSigma()
Returns the value of .
double nextObservation()
Generates and returns the next observation of the stochastic process.
BrownianMotion(double x0, double mu, double sigma, NormalGen gen)
Constructs a new BrownianMotion with parameters mu,.
void setParams(double x0, double mu, double sigma)
Resets the parameters , and of the process.
double[] generatePath(double[] uniform01)
Same as generatePath(), but a vector of uniform random numbers must be provided to the method.
BrownianMotion(double x0, double mu, double sigma, RandomStream stream)
Constructs a new BrownianMotion with parameters mu,.
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...