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.math4.legacy.distribution;
19  
20  import org.apache.commons.statistics.distribution.NormalDistribution;
21  import org.apache.commons.math4.legacy.linear.RealMatrix;
22  import org.apache.commons.rng.simple.RandomSource;
23  import org.apache.commons.math4.legacy.stat.correlation.Covariance;
24  
25  import java.util.Random;
26  
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Test cases for {@link MultivariateNormalDistribution}.
32   */
33  public class MultivariateNormalDistributionTest {
34      /**
35       * Test the ability of the distribution to report its mean value parameter.
36       */
37      @Test
38      public void testGetMean() {
39          final double[] mu = { -1.5, 2 };
40          final double[][] sigma = { { 2, -1.1 },
41                                     { -1.1, 2 } };
42          final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma);
43  
44          final double[] m = d.getMeans();
45          for (int i = 0; i < m.length; i++) {
46              Assert.assertEquals(mu[i], m[i], 0);
47          }
48      }
49  
50      /**
51       * Test the ability of the distribution to report its covariance matrix parameter.
52       */
53      @Test
54      public void testGetCovarianceMatrix() {
55          final double[] mu = { -1.5, 2 };
56          final double[][] sigma = { { 2, -1.1 },
57                                     { -1.1, 2 } };
58          final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma);
59  
60          final RealMatrix s = d.getCovariances();
61          final int dim = d.getDimension();
62          for (int i = 0; i < dim; i++) {
63              for (int j = 0; j < dim; j++) {
64                  Assert.assertEquals(sigma[i][j], s.getEntry(i, j), 0);
65              }
66          }
67      }
68  
69      /**
70       * Test the accuracy of sampling from the distribution.
71       */
72      @Test
73      public void testSampling() {
74          final double[] mu = { -1.5, 2 };
75          final double[][] sigma = { { 2, -1.1 },
76                                     { -1.1, 2 } };
77          final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma);
78          final MultivariateRealDistribution.Sampler sampler =
79              d.createSampler(RandomSource.WELL_19937_C.create(50));
80  
81          final int n = 500000;
82          final double[][] samples = AbstractMultivariateRealDistribution.sample(n, sampler);
83  
84          final int dim = d.getDimension();
85          final double[] sampleMeans = new double[dim];
86  
87          for (int i = 0; i < samples.length; i++) {
88              for (int j = 0; j < dim; j++) {
89                  sampleMeans[j] += samples[i][j];
90              }
91          }
92  
93          final double sampledValueTolerance = 1e-2;
94          for (int j = 0; j < dim; j++) {
95              sampleMeans[j] /= samples.length;
96              Assert.assertEquals(mu[j], sampleMeans[j], sampledValueTolerance);
97          }
98  
99          final double[][] sampleSigma = new Covariance(samples).getCovarianceMatrix().getData();
100         for (int i = 0; i < dim; i++) {
101             for (int j = 0; j < dim; j++) {
102                 Assert.assertEquals(sigma[i][j], sampleSigma[i][j], sampledValueTolerance);
103             }
104         }
105     }
106 
107     /**
108      * Test the accuracy of the distribution when calculating densities.
109      */
110     @Test
111     public void testDensities() {
112         final double[] mu = { -1.5, 2 };
113         final double[][] sigma = { { 2, -1.1 },
114                                    { -1.1, 2 } };
115         final MultivariateNormalDistribution d = new MultivariateNormalDistribution(mu, sigma);
116 
117         final double[][] testValues = { { -1.5, 2 },
118                                         { 4, 4 },
119                                         { 1.5, -2 },
120                                         { 0, 0 } };
121         final double[] densities = new double[testValues.length];
122         for (int i = 0; i < densities.length; i++) {
123             densities[i] = d.density(testValues[i]);
124         }
125 
126         // From dmvnorm function in R 2.15 CRAN package Mixtools v0.4.5
127         final double[] correctDensities = { 0.09528357207691344,
128                                             5.80932710124009e-09,
129                                             0.001387448895173267,
130                                             0.03309922090210541 };
131 
132         for (int i = 0; i < testValues.length; i++) {
133             Assert.assertEquals(correctDensities[i], densities[i], 1e-16);
134         }
135     }
136 
137     /**
138      * Test the accuracy of the distribution when calculating densities.
139      */
140     @Test
141     public void testUnivariateDistribution() {
142         final double[] mu = { -1.5 };
143         final double[][] sigma = { { 1 } };
144 
145         final MultivariateNormalDistribution multi = new MultivariateNormalDistribution(mu, sigma);
146 
147         final NormalDistribution uni = NormalDistribution.of(mu[0], sigma[0][0]);
148         final Random rng = new Random();
149         final int numCases = 100;
150         final double tol = Math.ulp(1d);
151         for (int i = 0; i < numCases; i++) {
152             final double v = rng.nextDouble() * 10 - 5;
153             Assert.assertEquals(uni.density(v), multi.density(new double[] { v }), tol);
154         }
155     }
156 }