SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BrownianMotionPCA.java
1/*
2 * Class: BrownianMotionPCA
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.*;
30import cern.colt.matrix.DoubleMatrix2D;
31import cern.colt.matrix.impl.DenseDoubleMatrix2D;
32import cern.colt.matrix.linalg.*;
33
42public class BrownianMotionPCA extends BrownianMotion {
43
44 protected double[][] sigmaCov; // Matrice de covariance du vecteur des observ.
45 // sigmaCov [i][j] = Cov[X(t_{i+1}),X(t_{j+1})].
46 protected double[][] A; // sigmaCov = AA' (PCA decomposition).
47 protected double[] z; // vector of standard normals.
48 protected double[] sortedEigenvalues;
49 protected boolean isDecompPCA;
50
57 public BrownianMotionPCA(double x0, double mu, double sigma, RandomStream stream) {
58 super(x0, mu, sigma, stream);
59 isDecompPCA = false;
60 }
61
67 public BrownianMotionPCA(double x0, double mu, double sigma, NormalGen gen) {
68 super(x0, mu, sigma, gen);
69 isDecompPCA = false;
70 }
71
72 public double nextObservation() {
73 throw new UnsupportedOperationException("nextObservation() is not defined in BrownianMotionPCA");
74 }
75
76 public void setParams(double x0, double mu, double sigma) {
77 super.setParams(x0, mu, sigma);
78 isDecompPCA = true; // the setParams method defined in BrownianMotion calls init() which does
79 // the PCA decompostion
80 }
81
82 public double[] generatePath() {
83 if (!isDecompPCA) {
84 init();
85 } // if the decomposition is not done, do it...
86 for (int j = 0; j < d; j++)
87 z[j] = gen.nextDouble();
88 for (int j = 0; j < d; j++) {
89 double sum = 0.0;
90 for (int k = 0; k < d; k++)
91 sum += A[j][k] * z[k];
92 path[j + 1] = x0 + mu * t[j + 1] + sum;
93 }
94 observationIndex = d;
95 observationCounter = d;
96 return path;
97 }
98
99 public double[] generatePath(double[] uniform01) {
100 if (!isDecompPCA) {
101 init();
102 } // if the decomposition is not done, do it...
103 for (int j = 0; j < d; j++)
104 z[j] = NormalDist.inverseF01(uniform01[j]);
105 for (int j = 0; j < d; j++) {
106 double sum = 0.0;
107 for (int k = 0; k < d; k++)
108 sum += A[j][k] * z[k];
109 path[j + 1] = x0 + mu * t[j + 1] + sum;
110 }
111 observationIndex = d;
112 observationCounter = d;
113 return path;
114 }
115
116 public double[][] decompPCA(double[][] sigma) {
117 // L'objet SingularValueDecomposition permet de recuperer la matrice
118 // des valeurs propres en ordre decroissant et celle des vecteurs propres de
119 // sigma (pour une matrice symetrique et definie-positive seulement).
120 SingularValueDecomposition sv = new SingularValueDecomposition(new DenseDoubleMatrix2D(sigma));
121 DoubleMatrix2D D = sv.getS(); // diagonal
122 // Calculer la racine carree des valeurs propres
123 for (int i = 0; i < D.rows(); i++) {
124 sortedEigenvalues[i] = D.getQuick(i, i);
125 D.setQuick(i, i, Math.sqrt(D.getQuick(i, i)));
126 }
127 DoubleMatrix2D P = sv.getV(); // right factor matrix
128 return P.zMult(D, null).toArray();
129 }
130
134 public double[] getSortedEigenvalues() {
135 return sortedEigenvalues;
136 }
137
138 protected void init() {
139 super.init();
140 z = new double[d];
141 sortedEigenvalues = new double[d];
142
143 // Initialize sigmaCov, based on the observation times.
144 sigmaCov = new double[d][d];
145 for (int i = 0; i < d; i++) {
146 for (int j = i; j < d; j++) {
147 sigmaCov[i][j] = sigma * sigma * t[i + 1];
148 sigmaCov[j][i] = sigmaCov[i][j];
149 }
150 }
151 A = decompPCA(sigmaCov);
152 isDecompPCA = true;
153 }
154}
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 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.
BrownianMotionPCA(double x0, double mu, double sigma, RandomStream stream)
Constructs a new BrownianMotionBridge with parameters , and initial value .
double nextObservation()
Generates and returns the next observation of the stochastic process.
double[] generatePath()
Generates, returns, and saves the sample path .
BrownianMotionPCA(double x0, double mu, double sigma, NormalGen gen)
Constructs a new BrownianMotionBridge with parameters , and initial value .
double[] getSortedEigenvalues()
Returns the sorted eigenvalues obtained in the PCA decomposition.
BrownianMotion(double x0, double mu, double sigma, RandomStream stream)
Constructs a new BrownianMotion with parameters mu,.
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...