View Javadoc
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 java.util.Arrays;
26  
27  import org.hipparchus.exception.LocalizedCoreFormats;
28  import org.hipparchus.exception.MathIllegalArgumentException;
29  
30  /**
31   * A {@link RandomVectorGenerator} that generates vectors with uncorrelated components.
32   * <p>
33   * Components of generated vectors follow (independent) Gaussian distributions,
34   * with parameters supplied in the constructor.
35   */
36  public class UncorrelatedRandomVectorGenerator
37      implements RandomVectorGenerator {
38  
39      /** Underlying scalar generator. */
40      private final NormalizedRandomGenerator generator;
41  
42      /** Mean vector. */
43      private final double[] mean;
44  
45      /** Standard deviation vector. */
46      private final double[] standardDeviation;
47  
48      /**
49       * Simple constructor.
50       * <p>
51       * Build an uncorrelated random vector generator from its mean and standard deviation vectors.
52       * </p>
53       *
54       * @param mean expected mean values for each component
55       * @param standardDeviation standard deviation for each component
56       * @param generator underlying generator for uncorrelated normalized components
57       */
58      public UncorrelatedRandomVectorGenerator(double[] mean, double[] standardDeviation,
59                                               NormalizedRandomGenerator generator) {
60          if (mean.length != standardDeviation.length) {
61              throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH, mean.length,
62                                                     standardDeviation.length);
63          }
64          this.mean = mean.clone();
65          this.standardDeviation = standardDeviation.clone();
66          this.generator = generator;
67      }
68  
69      /**
70       * Simple constructor.
71       * <p>
72       * Build a null mean random and unit standard deviation uncorrelated
73       * vector generator.
74       *
75       * @param dimension dimension of the vectors to generate
76       * @param generator underlying generator for uncorrelated normalized components
77       */
78      public UncorrelatedRandomVectorGenerator(int dimension, NormalizedRandomGenerator generator) {
79          mean = new double[dimension];
80          standardDeviation = new double[dimension];
81          Arrays.fill(standardDeviation, 1.0);
82          this.generator = generator;
83      }
84  
85      /**
86       * Generate an uncorrelated random vector.
87       *
88       * @return a random vector as a newly built array of double
89       */
90      @Override
91      public double[] nextVector() {
92  
93          double[] random = new double[mean.length];
94          for (int i = 0; i < random.length; ++i) {
95              random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble();
96          }
97  
98          return random;
99      }
100 
101 }