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    
018    package org.apache.commons.math.random;
019    
020    import java.util.Arrays;
021    
022    import org.apache.commons.math.MathRuntimeException;
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     * @version $Revision: 811827 $ $Date: 2009-09-06 11:32:50 -0400 (Sun, 06 Sep 2009) $
030     * @since 1.2
031     */
032    
033    public class UncorrelatedRandomVectorGenerator
034      implements RandomVectorGenerator {
035    
036        /** Underlying scalar generator. */
037        private final NormalizedRandomGenerator generator;
038    
039        /** Mean vector. */
040        private final double[] mean;
041    
042        /** Standard deviation vector. */
043        private final double[] standardDeviation;
044    
045      /** Simple constructor.
046       * <p>Build an uncorrelated random vector generator from
047       * its mean and standard deviation vectors.</p>
048       * @param mean expected mean values for each component
049       * @param standardDeviation standard deviation for each component
050       * @param generator underlying generator for uncorrelated normalized
051       * components
052       */
053      public UncorrelatedRandomVectorGenerator(double[] mean,
054                                               double[] standardDeviation,
055                                               NormalizedRandomGenerator generator) {
056        if (mean.length != standardDeviation.length) {
057          throw MathRuntimeException.createIllegalArgumentException(
058                "dimension mismatch {0} != {1}",
059                mean.length, standardDeviation.length);
060        }
061        this.mean              = mean.clone();
062        this.standardDeviation = standardDeviation.clone();
063        this.generator = generator;
064      }
065    
066      /** Simple constructor.
067       * <p>Build a null mean random and unit standard deviation
068       * uncorrelated vector generator</p>
069       * @param dimension dimension of the vectors to generate
070       * @param generator underlying generator for uncorrelated normalized
071       * components
072       */
073      public UncorrelatedRandomVectorGenerator(int dimension,
074                                               NormalizedRandomGenerator generator) {
075        mean              = new double[dimension];
076        standardDeviation = new double[dimension];
077        Arrays.fill(standardDeviation, 1.0);
078        this.generator = generator;
079      }
080    
081      /** Generate an uncorrelated random vector.
082       * @return a random vector as a newly built array of double
083       */
084      public double[] nextVector() {
085    
086        double[] random = new double[mean.length];
087        for (int i = 0; i < random.length; ++i) {
088          random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble();
089        }
090    
091        return random;
092    
093      }
094    
095    }