blob: d365f9b5a08c85923d558d9fe72b26b5bb8881c8 [file] [log] [blame]
Raymonddee08492015-04-02 10:43:13 -07001/*
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
18package org.apache.commons.math.random;
19
20import java.util.Arrays;
21
22import org.apache.commons.math.exception.DimensionMismatchException;
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: 962515 $ $Date: 2010-07-09 15:15:28 +0200 (ven. 09 juil. 2010) $
30 * @since 1.2
31 */
32
33public class UncorrelatedRandomVectorGenerator
34 implements RandomVectorGenerator {
35
36 /** Underlying scalar generator. */
37 private final NormalizedRandomGenerator generator;
38
39 /** Mean vector. */
40 private final double[] mean;
41
42 /** Standard deviation vector. */
43 private final double[] standardDeviation;
44
45 /** Simple constructor.
46 * <p>Build an uncorrelated random vector generator from
47 * its mean and standard deviation vectors.</p>
48 * @param mean expected mean values for each component
49 * @param standardDeviation standard deviation for each component
50 * @param generator underlying generator for uncorrelated normalized
51 * components
52 */
53 public UncorrelatedRandomVectorGenerator(double[] mean,
54 double[] standardDeviation,
55 NormalizedRandomGenerator generator) {
56 if (mean.length != standardDeviation.length) {
57 throw new DimensionMismatchException(mean.length, standardDeviation.length);
58 }
59 this.mean = mean.clone();
60 this.standardDeviation = standardDeviation.clone();
61 this.generator = generator;
62 }
63
64 /** Simple constructor.
65 * <p>Build a null mean random and unit standard deviation
66 * uncorrelated vector generator</p>
67 * @param dimension dimension of the vectors to generate
68 * @param generator underlying generator for uncorrelated normalized
69 * components
70 */
71 public UncorrelatedRandomVectorGenerator(int dimension,
72 NormalizedRandomGenerator generator) {
73 mean = new double[dimension];
74 standardDeviation = new double[dimension];
75 Arrays.fill(standardDeviation, 1.0);
76 this.generator = generator;
77 }
78
79 /** Generate an uncorrelated random vector.
80 * @return a random vector as a newly built array of double
81 */
82 public double[] nextVector() {
83
84 double[] random = new double[mean.length];
85 for (int i = 0; i < random.length; ++i) {
86 random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble();
87 }
88
89 return random;
90
91 }
92
93}