UncorrelatedRandomVectorGenerator.java

  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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */

  21. package org.hipparchus.random;

  22. import java.util.Arrays;

  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;

  25. /**
  26.  * A {@link RandomVectorGenerator} that generates vectors with uncorrelated components.
  27.  * <p>
  28.  * Components of generated vectors follow (independent) Gaussian distributions,
  29.  * with parameters supplied in the constructor.
  30.  */
  31. public class UncorrelatedRandomVectorGenerator
  32.     implements RandomVectorGenerator {

  33.     /** Underlying scalar generator. */
  34.     private final NormalizedRandomGenerator generator;

  35.     /** Mean vector. */
  36.     private final double[] mean;

  37.     /** Standard deviation vector. */
  38.     private final double[] standardDeviation;

  39.     /**
  40.      * Simple constructor.
  41.      * <p>
  42.      * Build an uncorrelated random vector generator from its mean and standard deviation vectors.
  43.      * </p>
  44.      *
  45.      * @param mean expected mean values for each component
  46.      * @param standardDeviation standard deviation for each component
  47.      * @param generator underlying generator for uncorrelated normalized components
  48.      */
  49.     public UncorrelatedRandomVectorGenerator(double[] mean, double[] standardDeviation,
  50.                                              NormalizedRandomGenerator generator) {
  51.         if (mean.length != standardDeviation.length) {
  52.             throw new MathIllegalArgumentException(LocalizedCoreFormats.DIMENSIONS_MISMATCH, mean.length,
  53.                                                    standardDeviation.length);
  54.         }
  55.         this.mean = mean.clone();
  56.         this.standardDeviation = standardDeviation.clone();
  57.         this.generator = generator;
  58.     }

  59.     /**
  60.      * Simple constructor.
  61.      * <p>
  62.      * Build a null mean random and unit standard deviation uncorrelated
  63.      * vector generator.
  64.      *
  65.      * @param dimension dimension of the vectors to generate
  66.      * @param generator underlying generator for uncorrelated normalized components
  67.      */
  68.     public UncorrelatedRandomVectorGenerator(int dimension, NormalizedRandomGenerator generator) {
  69.         mean = new double[dimension];
  70.         standardDeviation = new double[dimension];
  71.         Arrays.fill(standardDeviation, 1.0);
  72.         this.generator = generator;
  73.     }

  74.     /**
  75.      * Generate an uncorrelated random vector.
  76.      *
  77.      * @return a random vector as a newly built array of double
  78.      */
  79.     @Override
  80.     public double[] nextVector() {

  81.         double[] random = new double[mean.length];
  82.         for (int i = 0; i < random.length; ++i) {
  83.             random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble();
  84.         }

  85.         return random;
  86.     }

  87. }