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    *      http://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  package org.apache.commons.math.random;
19  
20  import java.util.Arrays;
21  
22  /** 
23   * A {@link RandomVectorGenerator} that generates vectors with uncorrelated
24   * components. Components of generated vectors follow (independent) Gaussian
25   * distributions, with parameters supplied in the constructor.
26   * 
27   * @version $Revision: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $
28   * @since 1.2
29   */
30  
31  public class UncorrelatedRandomVectorGenerator
32    implements RandomVectorGenerator {
33  
34    /** Simple constructor.
35     * <p>Build an uncorrelated random vector generator from
36     * its mean and standard deviation vectors.</p>
37     * @param mean expected mean values for each component
38     * @param standardDeviation standard deviation for each component
39     * @param generator underlying generator for uncorrelated normalized
40     * components
41     */
42    public UncorrelatedRandomVectorGenerator(double[] mean,
43                                             double[] standardDeviation,
44                                             NormalizedRandomGenerator generator) {
45      if (mean.length != standardDeviation.length) {
46        throw new IllegalArgumentException("dimension mismatch");
47      }
48      this.mean              = (double[]) mean.clone();
49      this.standardDeviation = (double[]) standardDeviation.clone();
50      this.generator = generator;
51    }
52  
53    /** Simple constructor.
54     * <p>Build a null mean random and unit standard deviation
55     * uncorrelated vector generator</p>
56     * @param dimension dimension of the vectors to generate
57     * @param generator underlying generator for uncorrelated normalized
58     * components
59     */
60    public UncorrelatedRandomVectorGenerator(int dimension,
61                                             NormalizedRandomGenerator generator) {
62      mean              = new double[dimension];
63      standardDeviation = new double[dimension];
64      Arrays.fill(standardDeviation, 1.0);
65      this.generator = generator;
66    }
67  
68    /** Generate an uncorrelated random vector.
69     * @return a random vector as a newly built array of double
70     */
71    public double[] nextVector() {
72  
73      double[] random = new double[mean.length]; 
74      for (int i = 0; i < random.length; ++i) {
75        random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble();
76      }
77  
78      return random;
79  
80    }
81  
82    /** Mean vector. */
83    private double[] mean;
84  
85    /** Standard deviation vector. */
86    private double[] standardDeviation;
87  
88    /** Underlying scalar generator. */
89    private NormalizedRandomGenerator generator;
90  
91  }