SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
MultinormalGen.java
1/*
2 * Class: MultinormalGen
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 umontreal.ssj.probdist.NormalDist;
28import umontreal.ssj.randvar.NormalGen;
29import umontreal.ssj.rng.RandomStream;
30import cern.colt.matrix.DoubleMatrix2D;
31import cern.colt.matrix.linalg.CholeskyDecomposition;
32import cern.colt.matrix.impl.DenseDoubleMatrix2D;
33
71 protected double[] mu;
72 protected DoubleMatrix2D sigma;
73 protected DoubleMatrix2D sqrtSigma;
74 protected double[] temp;
75 protected static final double MYINF = 37.54;
76
77 private void initMN(NormalGen gen1, double[] mu, int d) {
78 if (gen1 == null)
79 throw new NullPointerException("gen1 is null");
80
81 if (gen1.getMu() != 0.0)
82 throw new IllegalArgumentException("mu != 0");
83 if (gen1.getSigma() != 1.0)
84 throw new IllegalArgumentException("sigma != 1");
85 /*
86 * NormalDist dist = (NormalDist) gen1.getDistribution(); if (dist != null) { if
87 * (dist.getMu() != 0.0) throw new IllegalArgumentException ("mu != 0"); if
88 * (dist.getSigma() != 1.0) throw new IllegalArgumentException ("sigma != 1"); }
89 * dist = null;
90 */
91 this.gen1 = gen1;
92
93 if (mu == null) { // d is the dimension
94 dimension = d;
95 this.mu = new double[d];
96 } else { // d is unused
97 dimension = mu.length;
98 this.mu = (double[]) mu.clone();
99 }
100 temp = new double[dimension];
101 }
102
120 public MultinormalGen(NormalGen gen1, int d) {
121 initMN(gen1, null, d);
122 sigma = new DenseDoubleMatrix2D(d, d);
123 sqrtSigma = new DenseDoubleMatrix2D(d, d);
124 for (int i = 0; i < d; i++) {
125 sigma.setQuick(i, i, 1.0);
126 sqrtSigma.setQuick(i, i, 1.0);
127 }
128 }
129
146 protected MultinormalGen(NormalGen gen1, double[] mu, DoubleMatrix2D sigma) {
147 initMN(gen1, mu, -1);
148 this.sigma = sigma.copy();
149 }
150
155 protected MultinormalGen(NormalGen gen1, double[] mu, double[][] sigma) {
156 initMN(gen1, mu, -1);
157 this.sigma = new DenseDoubleMatrix2D(sigma);
158 }
159
165 public double[] getMu() {
166 return mu;
167 }
168
178 public double getMu(int i) {
179 return mu[i];
180 }
181
190 public void setMu(double[] mu) {
191 if (mu.length != this.mu.length)
192 throw new IllegalArgumentException("Incompatible length of mean vector");
193 this.mu = mu;
194 }
195
204 public void setMu(int i, double mui) {
205 mu[i] = mui;
206 }
207
214 public DoubleMatrix2D getSigma() {
215 return sigma.copy();
216 }
217
223 public void nextPoint(double[] p) {
224 int n = dimension;
225 for (int i = 0; i < n; i++) {
226 temp[i] = gen1.nextDouble();
227 if (temp[i] == Double.NEGATIVE_INFINITY)
228 temp[i] = -MYINF;
229 if (temp[i] == Double.POSITIVE_INFINITY)
230 temp[i] = MYINF;
231 }
232 for (int i = 0; i < n; i++) {
233 p[i] = 0;
234 for (int c = 0; c < n; c++)
235 p[i] += sqrtSigma.getQuick(i, c) * temp[c];
236 p[i] += mu[i];
237 }
238 }
239
240}
This class implements methods for generating random variates from the normal distribution .
MultinormalGen(NormalGen gen1, double[] mu, DoubleMatrix2D sigma)
Constructs a multinormal generator with mean vector mu and covariance matrix sigma.
void nextPoint(double[] p)
Generates a point from this multinormal distribution.
MultinormalGen(NormalGen gen1, int d)
Constructs a generator with the standard multinormal distribution (with and.
MultinormalGen(NormalGen gen1, double[] mu, double[][] sigma)
Equivalent to MultinormalGen(gen1, mu, new DenseDoubleMatrix2D (sigma)).
double getMu(int i)
Returns the -th component of the mean vector for this generator.
void setMu(int i, double mui)
Sets the -th component of the mean vector to mui.
DoubleMatrix2D getSigma()
Returns the covariance matrix used by this generator.
void setMu(double[] mu)
Sets the mean vector to mu.
double[] getMu()
Returns the mean vector used by this generator.
This class is the multivariate counterpart of.