001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.random;
019
020import java.util.Arrays;
021
022import org.apache.commons.math3.exception.DimensionMismatchException;
023
024/**
025 * A {@link RandomVectorGenerator} that generates vectors with uncorrelated
026 * components. Components of generated vectors follow (independent) Gaussian
027 * distributions, with parameters supplied in the constructor.
028 *
029 * @since 1.2
030 */
031
032public class UncorrelatedRandomVectorGenerator
033  implements RandomVectorGenerator {
034
035    /** Underlying scalar generator. */
036    private final NormalizedRandomGenerator generator;
037
038    /** Mean vector. */
039    private final double[] mean;
040
041    /** Standard deviation vector. */
042    private final double[] standardDeviation;
043
044  /** Simple constructor.
045   * <p>Build an uncorrelated random vector generator from
046   * its mean and standard deviation vectors.</p>
047   * @param mean expected mean values for each component
048   * @param standardDeviation standard deviation for each component
049   * @param generator underlying generator for uncorrelated normalized
050   * components
051   */
052  public UncorrelatedRandomVectorGenerator(double[] mean,
053                                           double[] standardDeviation,
054                                           NormalizedRandomGenerator generator) {
055    if (mean.length != standardDeviation.length) {
056        throw new DimensionMismatchException(mean.length, standardDeviation.length);
057    }
058    this.mean              = mean.clone();
059    this.standardDeviation = standardDeviation.clone();
060    this.generator = generator;
061  }
062
063  /** Simple constructor.
064   * <p>Build a null mean random and unit standard deviation
065   * uncorrelated vector generator</p>
066   * @param dimension dimension of the vectors to generate
067   * @param generator underlying generator for uncorrelated normalized
068   * components
069   */
070  public UncorrelatedRandomVectorGenerator(int dimension,
071                                           NormalizedRandomGenerator generator) {
072    mean              = new double[dimension];
073    standardDeviation = new double[dimension];
074    Arrays.fill(standardDeviation, 1.0);
075    this.generator = generator;
076  }
077
078  /** Generate an uncorrelated random vector.
079   * @return a random vector as a newly built array of double
080   */
081  public double[] nextVector() {
082
083    double[] random = new double[mean.length];
084    for (int i = 0; i < random.length; ++i) {
085      random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble();
086    }
087
088    return random;
089
090  }
091
092}