| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (

This example Commons Math source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

dimensionmismatchexception, dimensionmismatchexception, gaussianrandomgenerator, jdkrandomgenerator, override, override, realmatrix, testcase, uncorrelatedrandomvectorgenerator, uncorrelatedrandomvectorgenerator, uncorrelatedrandomvectorgeneratortest, uncorrelatedrandomvectorgeneratortest, vectorialcovariance, vectorialmean

The Commons Math source code

//Licensed to the Apache Software Foundation (ASF) under one
//or more contributor license agreements.  See the NOTICE file
//distributed with this work for additional information
//regarding copyright ownership.  The ASF licenses this file
//to you under the Apache License, Version 2.0 (the
//"License"); you may not use this file except in compliance
//with the License.  You may obtain a copy of the License at


//Unless required by applicable law or agreed to in writing,
//software distributed under the License is distributed on an
//KIND, either express or implied.  See the License for the
//specific language governing permissions and limitations
//under the License.

package org.apache.commons.math.random;

import org.apache.commons.math.DimensionMismatchException;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.stat.descriptive.moment.VectorialCovariance;
import org.apache.commons.math.stat.descriptive.moment.VectorialMean;

import junit.framework.*;

public class UncorrelatedRandomVectorGeneratorTest
extends TestCase {

    public UncorrelatedRandomVectorGeneratorTest(String name) {
        mean = null;
        standardDeviation = null;
        generator = null;

    public void testMeanAndCorrelation() throws DimensionMismatchException {

        VectorialMean meanStat = new VectorialMean(mean.length);
        VectorialCovariance covStat = new VectorialCovariance(mean.length, true);
        for (int i = 0; i < 10000; ++i) {
            double[] v = generator.nextVector();

        double[] estimatedMean = meanStat.getResult();
        double scale;
        RealMatrix estimatedCorrelation = covStat.getResult();
        for (int i = 0; i < estimatedMean.length; ++i) {
            assertEquals(mean[i], estimatedMean[i], 0.07);
            for (int j = 0; j < i; ++j) {
                scale = standardDeviation[i] * standardDeviation[j];
                assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03);
            scale = standardDeviation[i] * standardDeviation[i];
            assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025);


    public void setUp() {
        mean              = new double[] {0.0, 1.0, -3.0, 2.3};
        standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
        RandomGenerator rg = new JDKRandomGenerator();
        generator =
            new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
                    new GaussianRandomGenerator(rg));

    public void tearDown() {
        mean = null;
        standardDeviation = null;
        generator = null;

    private double[] mean;
    private double[] standardDeviation;
    private UncorrelatedRandomVectorGenerator generator;


Other Commons Math examples (source code examples)

Here is a short list of links related to this Commons Math source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.