SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MultinormalCholeskyGen.java
1/*
2 * Class: MultinormalCholeskyGen
3 * Description: multivariate normal random variable generator
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.randvarmulti;
26
27import cern.colt.matrix.DoubleMatrix2D;
28import cern.colt.matrix.impl.DenseDoubleMatrix2D;
29import cern.colt.matrix.linalg.CholeskyDecomposition;
30import umontreal.ssj.probdist.NormalDist;
31import umontreal.ssj.randvar.NormalGen;
32import umontreal.ssj.rng.RandomStream;
33
55
56 private void initL() {
57 if (mu.length != sigma.rows() || mu.length != sigma.columns())
58 throw new IllegalArgumentException("Incompatible mean vector and covariance matrix");
59 CholeskyDecomposition decomp = new CholeskyDecomposition(sigma);
60 // if (!decomp.isSymmetricPositiveDefinite())
61 // throw new IllegalArgumentException
62 // ("The covariance matrix must be symmetric and positive-definite");
63 sqrtSigma = decomp.getL();
64 }
65
79 public MultinormalCholeskyGen(NormalGen gen1, double[] mu, double[][] sigma) {
80 super(gen1, mu, sigma);
81 initL();
82 }
83
100 public MultinormalCholeskyGen(NormalGen gen1, double[] mu, DoubleMatrix2D sigma) {
101 super(gen1, mu, sigma);
102 initL();
103 }
104
111 public DoubleMatrix2D getCholeskyDecompSigma() {
112 return sqrtSigma.copy();
113 }
114
122 public void setSigma(DoubleMatrix2D sigma) {
123 if (sigma.rows() != mu.length || sigma.columns() != mu.length)
124 throw new IllegalArgumentException("Invalid dimensions of covariance matrix");
125 CholeskyDecomposition decomp = new CholeskyDecomposition(sigma);
126 // if (!decomp.isSymmetricPositiveDefinite())
127 // throw new IllegalArgumentException
128 // ("The new covariance matrix must be symmetric and positive-definite");
129 this.sigma.assign(sigma);
130 this.sqrtSigma = decomp.getL();
131 }
132
137 public static void nextPoint(NormalGen gen1, double[] mu, double[][] sigma, double[] p) {
138 nextPoint(gen1, mu, new DenseDoubleMatrix2D(sigma), p);
139 }
140
163 public static void nextPoint(NormalGen gen1, double[] mu, DoubleMatrix2D sigma, double[] p) {
164 if (gen1 == null)
165 throw new NullPointerException("gen1 is null");
166
167 NormalDist dist = (NormalDist) gen1.getDistribution();
168 if (dist.getMu() != 0.0)
169 throw new IllegalArgumentException("mu != 0");
170 if (dist.getSigma() != 1.0)
171 throw new IllegalArgumentException("sigma != 1");
172
173 if (mu.length != sigma.rows() || mu.length != sigma.columns())
174 throw new IllegalArgumentException("Incompatible mean vector and covariance matrix dimensions");
175 CholeskyDecomposition decomp = new CholeskyDecomposition(sigma);
176 double[] temp = new double[mu.length];
177 DoubleMatrix2D sqrtSigma = decomp.getL();
178 for (int i = 0; i < temp.length; i++) {
179 temp[i] = gen1.nextDouble();
180 if (temp[i] == Double.NEGATIVE_INFINITY)
181 temp[i] = -MYINF;
182 if (temp[i] == Double.POSITIVE_INFINITY)
183 temp[i] = MYINF;
184 }
185 for (int i = 0; i < temp.length; i++) {
186 p[i] = 0;
187 for (int c = 0; c <= i; c++)
188 p[i] += sqrtSigma.getQuick(i, c) * temp[c];
189 p[i] += mu[i];
190 }
191 }
192
200 public void nextPoint(double[] p) {
201 int n = mu.length;
202 for (int i = 0; i < n; i++) {
203 temp[i] = gen1.nextDouble();
204 if (temp[i] == Double.NEGATIVE_INFINITY)
205 temp[i] = -MYINF;
206 if (temp[i] == Double.POSITIVE_INFINITY)
207 temp[i] = MYINF;
208 }
209 for (int i = 0; i < n; i++) {
210 p[i] = 0;
211 // Matrix is lower-triangular
212 for (int c = 0; c <= i; c++)
213 p[i] += sqrtSigma.getQuick(i, c) * temp[c];
214 p[i] += mu[i];
215 }
216 }
217}
Extends the class ContinuousDistribution for the normal distribution (e.g., tjoh95a  (page 80)).
double getMu()
Returns the parameter .
double getSigma()
Returns the parameter .
This class implements methods for generating random variates from the normal distribution .
void setSigma(DoubleMatrix2D sigma)
Sets the covariance matrix of this multinormal generator to sigma (and recomputes ).
MultinormalCholeskyGen(NormalGen gen1, double[] mu, DoubleMatrix2D sigma)
Constructs a multinormal generator with mean vector mu and covariance matrix sigma.
DoubleMatrix2D getCholeskyDecompSigma()
Returns the lower-triangular matrix in the Cholesky decomposition of .
void nextPoint(double[] p)
Generates a point from this multinormal distribution.
static void nextPoint(NormalGen gen1, double[] mu, DoubleMatrix2D sigma, double[] p)
Generates a -dimensional vector from the multinormal distribution with mean vector mu and covariance ...
static void nextPoint(NormalGen gen1, double[] mu, double[][] sigma, double[] p)
Equivalent to nextPoint(gen1, mu, new DenseDoubleMatrix2D(sigma), p).
MultinormalCholeskyGen(NormalGen gen1, double[] mu, double[][] sigma)
Equivalent to MultinormalCholeskyGen(gen1, mu, new DenseDoubleMatrix2D(sigma)).
MultinormalGen(NormalGen gen1, int d)
Constructs a generator with the standard multinormal distribution (with and.