1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22
23 package org.hipparchus.random;
24
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.linear.RealMatrix;
28 import org.hipparchus.linear.RectangularCholeskyDecomposition;
29
30 /**
31 * A {@link RandomVectorGenerator} that generates vectors with with
32 * correlated components.
33 * <p>
34 * Random vectors with correlated components are built by combining
35 * the uncorrelated components of another random vector in such a way that
36 * the resulting correlations are the ones specified by a positive
37 * definite covariance matrix.
38 * <p>
39 * The main use for correlated random vector generation is for Monte-Carlo
40 * simulation of physical problems with several variables, for example to
41 * generate error vectors to be added to a nominal vector. A particularly
42 * interesting case is when the generated vector should be drawn from a <a
43 * href="http://en.wikipedia.org/wiki/Multivariate_normal_distribution">
44 * Multivariate Normal Distribution</a>. The approach using a Cholesky
45 * decomposition is quite usual in this case. However, it can be extended
46 * to other cases as long as the underlying random generator provides
47 * {@link NormalizedRandomGenerator normalized values} like {@link
48 * GaussianRandomGenerator} or {@link UniformRandomGenerator}.
49 * <p>
50 * Sometimes, the covariance matrix for a given simulation is not
51 * strictly positive definite. This means that the correlations are
52 * not all independent from each other. In this case, however, the non
53 * strictly positive elements found during the Cholesky decomposition
54 * of the covariance matrix should not be negative either, they
55 * should be null. Another non-conventional extension handling this case
56 * is used here. Rather than computing <code>C = U<sup>T</sup>.U</code>
57 * where <code>C</code> is the covariance matrix and <code>U</code>
58 * is an upper-triangular matrix, we compute <code>C = B.B<sup>T</sup></code>
59 * where <code>B</code> is a rectangular matrix having
60 * more rows than columns. The number of columns of <code>B</code> is
61 * the rank of the covariance matrix, and it is the dimension of the
62 * uncorrelated random vector that is needed to compute the component
63 * of the correlated vector. This class handles this situation
64 * automatically.
65 */
66 public class CorrelatedRandomVectorGenerator
67 implements RandomVectorGenerator {
68 /** Mean vector. */
69 private final double[] mean;
70 /** Underlying generator. */
71 private final NormalizedRandomGenerator generator;
72 /** Storage for the normalized vector. */
73 private final double[] normalized;
74 /** Root of the covariance matrix. */
75 private final RealMatrix root;
76
77 /**
78 * Builds a correlated random vector generator from its mean
79 * vector and covariance matrix.
80 *
81 * @param mean Expected mean values for all components.
82 * @param covariance Covariance matrix.
83 * @param small Diagonal elements threshold under which column are
84 * considered to be dependent on previous ones and are discarded
85 * @param generator underlying generator for uncorrelated normalized
86 * components.
87 * @throws org.hipparchus.exception.MathIllegalArgumentException
88 * if the covariance matrix is not strictly positive definite.
89 * @throws MathIllegalArgumentException if the mean and covariance
90 * arrays dimensions do not match.
91 */
92 public CorrelatedRandomVectorGenerator(double[] mean,
93 RealMatrix covariance, double small,
94 NormalizedRandomGenerator generator) {
95 int order = covariance.getRowDimension();
96 if (mean.length != order) {
97 throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH,
98 mean.length, order);
99 }
100 this.mean = mean.clone();
101
102 final RectangularCholeskyDecomposition decomposition =
103 new RectangularCholeskyDecomposition(covariance, small);
104 root = decomposition.getRootMatrix();
105
106 this.generator = generator;
107 normalized = new double[decomposition.getRank()];
108
109 }
110
111 /**
112 * Builds a null mean random correlated vector generator from its
113 * covariance matrix.
114 *
115 * @param covariance Covariance matrix.
116 * @param small Diagonal elements threshold under which column are
117 * considered to be dependent on previous ones and are discarded.
118 * @param generator Underlying generator for uncorrelated normalized
119 * components.
120 * @throws org.hipparchus.exception.MathIllegalArgumentException
121 * if the covariance matrix is not strictly positive definite.
122 */
123 public CorrelatedRandomVectorGenerator(RealMatrix covariance, double small,
124 NormalizedRandomGenerator generator) {
125 int order = covariance.getRowDimension();
126 mean = new double[order];
127 for (int i = 0; i < order; ++i) {
128 mean[i] = 0;
129 }
130
131 final RectangularCholeskyDecomposition decomposition =
132 new RectangularCholeskyDecomposition(covariance, small);
133 root = decomposition.getRootMatrix();
134
135 this.generator = generator;
136 normalized = new double[decomposition.getRank()];
137
138 }
139
140 /** Get the underlying normalized components generator.
141 * @return underlying uncorrelated components generator
142 */
143 public NormalizedRandomGenerator getGenerator() {
144 return generator;
145 }
146
147 /** Get the rank of the covariance matrix.
148 * The rank is the number of independent rows in the covariance
149 * matrix, it is also the number of columns of the root matrix.
150 * @return rank of the square matrix.
151 * @see #getRootMatrix()
152 */
153 public int getRank() {
154 return normalized.length;
155 }
156
157 /** Get the root of the covariance matrix.
158 * The root is the rectangular matrix <code>B</code> such that
159 * the covariance matrix is equal to <code>B.B<sup>T</sup></code>
160 * @return root of the square matrix
161 * @see #getRank()
162 */
163 public RealMatrix getRootMatrix() {
164 return root;
165 }
166
167 /** Generate a correlated random vector.
168 * @return a random vector as an array of double. The returned array
169 * is created at each call, the caller can do what it wants with it.
170 */
171 @Override
172 public double[] nextVector() {
173
174 // generate uncorrelated vector
175 for (int i = 0; i < normalized.length; ++i) {
176 normalized[i] = generator.nextNormalizedDouble();
177 }
178
179 // compute correlated vector
180 double[] correlated = new double[mean.length];
181 for (int i = 0; i < correlated.length; ++i) {
182 correlated[i] = mean[i];
183 for (int j = 0; j < root.getColumnDimension(); ++j) {
184 correlated[i] += root.getEntry(i, j) * normalized[j];
185 }
186 }
187
188 return correlated;
189 }
190
191 }