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