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  package org.apache.commons.math4.legacy.distribution;
18  
19  import java.util.List;
20  
21  import org.apache.commons.math4.legacy.exception.MathArithmeticException;
22  import org.apache.commons.math4.legacy.exception.NotPositiveException;
23  import org.apache.commons.rng.simple.RandomSource;
24  import org.apache.commons.math4.legacy.core.Pair;
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.junit.Ignore;
28  
29  /**
30   * Test case {@link MixtureMultivariateNormalDistribution}.
31   */
32  public class MixtureMultivariateNormalDistributionTest {
33  
34      @Test
35      public void testNonUnitWeightSum() {
36          final double[] weights = { 1, 2 };
37          final double[][] means = { { -1.5, 2.0 },
38                                     { 4.0, 8.2 } };
39          final double[][][] covariances = { { { 2.0, -1.1 },
40                                               { -1.1, 2.0 } },
41                                             { { 3.5, 1.5 },
42                                               { 1.5, 3.5 } } };
43          final MixtureMultivariateNormalDistribution d
44              = new MixtureMultivariateNormalDistribution(weights, means, covariances);
45  
46          final List<Pair<Double, MultivariateNormalDistribution>> comp = d.getComponents();
47  
48          Assert.assertEquals(1d / 3, comp.get(0).getFirst().doubleValue(), Math.ulp(1d));
49          Assert.assertEquals(2d / 3, comp.get(1).getFirst().doubleValue(), Math.ulp(1d));
50      }
51  
52      @Test(expected=MathArithmeticException.class)
53      public void testWeightSumOverFlow() {
54          final double[] weights = { 0.5 * Double.MAX_VALUE, 0.51 * Double.MAX_VALUE };
55          final double[][] means = { { -1.5, 2.0 },
56                                     { 4.0, 8.2 } };
57          final double[][][] covariances = { { { 2.0, -1.1 },
58                                               { -1.1, 2.0 } },
59                                             { { 3.5, 1.5 },
60                                               { 1.5, 3.5 } } };
61          new MixtureMultivariateNormalDistribution(weights, means, covariances);
62      }
63  
64      @Test(expected=NotPositiveException.class)
65      public void testPreconditionPositiveWeights() {
66          final double[] negativeWeights = { -0.5, 1.5 };
67          final double[][] means = { { -1.5, 2.0 },
68                                     { 4.0, 8.2 } };
69          final double[][][] covariances = { { { 2.0, -1.1 },
70                                               { -1.1, 2.0 } },
71                                             { { 3.5, 1.5 },
72                                               { 1.5, 3.5 } } };
73          new MixtureMultivariateNormalDistribution(negativeWeights, means, covariances);
74      }
75  
76      /**
77       * Test the accuracy of the density calculation.
78       */
79      @Test
80      public void testDensities() {
81          final double[] weights = { 0.3, 0.7 };
82          final double[][] means = { { -1.5, 2.0 },
83                                     { 4.0, 8.2 } };
84          final double[][][] covariances = { { { 2.0, -1.1 },
85                                               { -1.1, 2.0 } },
86                                             { { 3.5, 1.5 },
87                                               { 1.5, 3.5 } } };
88          final MixtureMultivariateNormalDistribution d
89              = new MixtureMultivariateNormalDistribution(weights, means, covariances);
90  
91          // Test vectors
92          final double[][] testValues = { { -1.5, 2 },
93                                          { 4, 8.2 },
94                                          { 1.5, -2 },
95                                          { 0, 0 } };
96  
97          // Densities that we should get back.
98          // Calculated by assigning weights to multivariate normal distribution
99          // and summing
100         // values from dmvnorm function in R 2.15 CRAN package Mixtools v0.4.
101         // Like: .3*dmvnorm(val,mu1,sigma1)+.7*dmvnorm(val,mu2,sigma2)
102         final double[] correctDensities = { 0.02862037278930575,
103                                             0.03523044847314091,
104                                             0.000416241365629767,
105                                             0.009932042831700297 };
106 
107         for (int i = 0; i < testValues.length; i++) {
108             Assert.assertEquals(correctDensities[i], d.density(testValues[i]), Math.ulp(1d));
109         }
110     }
111 
112     /**
113      * Test the accuracy of sampling from the distribution.
114      */
115     @Ignore@Test
116     public void testSampling() {
117         final double[] weights = { 0.3, 0.7 };
118         final double[][] means = { { -1.5, 2.0 },
119                                    { 4.0, 8.2 } };
120         final double[][][] covariances = { { { 2.0, -1.1 },
121                                              { -1.1, 2.0 } },
122                                            { { 3.5, 1.5 },
123                                              { 1.5, 3.5 } } };
124         final MixtureMultivariateNormalDistribution d =
125             new MixtureMultivariateNormalDistribution(weights, means, covariances);
126         final MultivariateRealDistribution.Sampler sampler =
127             d.createSampler(RandomSource.WELL_19937_C.create(50));
128 
129         final double[][] correctSamples = getCorrectSamples();
130         final int n = correctSamples.length;
131         final double[][] samples = AbstractMultivariateRealDistribution.sample(n, sampler);
132 
133         for (int i = 0; i < n; i++) {
134             for (int j = 0; j < samples[i].length; j++) {
135                 Assert.assertEquals("sample[" + j + "]",
136                                     correctSamples[i][j], samples[i][j], 1e-16);
137             }
138         }
139     }
140 
141     /**
142      * Values used in {@link #testSampling()}.
143      */
144     private double[][] getCorrectSamples() {
145         // These were sampled from the MultivariateNormalMixtureModelDistribution class
146         // with seed 50.
147         //
148         // They were then fit to a MVN mixture model in R using mixtools.
149         //
150         // The optimal parameters were:
151         // - component weights: {0.3595186, 0.6404814}
152         // - mean vectors: {-1.645879, 1.989797}, {3.474328, 7.782232}
153         // - covariance matrices:
154         //     { 1.397738 -1.167732
155         //       -1.167732 1.801782 }
156         //   and
157         //     { 3.934593 2.354787
158         //       2.354787 4.428024 }
159         //
160         // It is considered fairly close to the actual test parameters,
161         // considering that the sample size is only 100.
162         return new double[][] {
163             { 6.259990922080121, 11.972954175355897 },
164             { -2.5296544304801847, 1.0031292519854365 },
165             { 0.49037886081440396, 0.9758251727325711 },
166             { 5.022970993312015, 9.289348879616787 },
167             { -1.686183146603914, 2.007244382745706 },
168             { -1.4729253946002685, 2.762166644212484 },
169             { 4.329788143963888, 11.514016497132253 },
170             { 3.008674596114442, 4.960246550446107 },
171             { 3.342379304090846, 5.937630105198625 },
172             { 2.6993068328674754, 7.42190871572571 },
173             { -2.446569340219571, 1.9687117791378763 },
174             { 1.922417883170056, 4.917616702617099 },
175             { -1.1969741543898518, 2.4576126277884387 },
176             { 2.4216948702967196, 8.227710158117134 },
177             { 6.701424725804463, 9.098666475042428 },
178             { 2.9890253545698964, 9.643807939324331 },
179             { 0.7162632354907799, 8.978811120287553 },
180             { -2.7548699149775877, 4.1354812280794215 },
181             { 8.304528180745018, 11.602319388898287 },
182             { -2.7633253389165926, 2.786173883989795 },
183             { 1.3322228389460813, 5.447481218602913 },
184             { -1.8120096092851508, 1.605624499560037 },
185             { 3.6546253437206504, 8.195304526564376 },
186             { -2.312349539658588, 1.868941220444169 },
187             { -1.882322136356522, 2.033795570464242 },
188             { 4.562770714939441, 7.414967958885031 },
189             { 4.731882017875329, 8.890676665580747 },
190             { 3.492186010427425, 8.9005225241848 },
191             { -1.619700190174894, 3.314060142479045 },
192             { 3.5466090064003315, 7.75182101001913 },
193             { 5.455682472787392, 8.143119287755635 },
194             { -2.3859602945473197, 1.8826732217294837 },
195             { 3.9095306088680015, 9.258129209626317 },
196             { 7.443020189508173, 7.837840713329312 },
197             { 2.136004873917428, 6.917636475958297 },
198             { -1.7203379410395119, 2.3212878757611524 },
199             { 4.618991257611526, 12.095065976419436 },
200             { -0.4837044029854387, 0.8255970441255125 },
201             { -4.438938966557163, 4.948666297280241 },
202             { -0.4539625134045906, 4.700922454655341 },
203             { 2.1285488271265356, 8.457941480487563 },
204             { 3.4873561871454393, 11.99809827845933 },
205             { 4.723049431412658, 7.813095742563365 },
206             { 1.1245583037967455, 5.20587873556688 },
207             { 1.3411933634409197, 6.069796875785409 },
208             { 4.585119332463686, 7.967669543767418 },
209             { 1.3076522817963823, -0.647431033653445 },
210             { -1.4449446442803178, 1.9400424267464862 },
211             { -2.069794456383682, 3.5824162107496544 },
212             { -0.15959481421417276, 1.5466782303315405 },
213             { -2.0823081278810136, 3.0914366458581437 },
214             { 3.521944615248141, 10.276112932926408 },
215             { 1.0164326704884257, 4.342329556442856 },
216             { 5.3718868590295275, 8.374761158360922 },
217             { 0.3673656866959396, 8.75168581694866 },
218             { -2.250268955954753, 1.4610850300996527 },
219             { -2.312739727403522, 1.5921126297576362 },
220             { 3.138993360831055, 6.7338392374947365 },
221             { 2.6978650950790115, 7.941857288979095 },
222             { 4.387985088655384, 8.253499976968 },
223             { -1.8928961721456705, 0.23631082388724223 },
224             { 4.43509029544109, 8.565290285488782 },
225             { 4.904728034106502, 5.79936660133754 },
226             { -1.7640371853739507, 2.7343727594167433 },
227             { 2.4553674733053463, 7.875871017408807 },
228             { -2.6478965122565006, 4.465127753193949 },
229             { 3.493873671142299, 10.443093773532448 },
230             { 1.1321916197409103, 7.127108479263268 },
231             { -1.7335075535240392, 2.550629648463023 },
232             { -0.9772679734368084, 4.377196298969238 },
233             { 3.6388366973980357, 6.947299283206256 },
234             { 0.27043799318823325, 6.587978599614367 },
235             { 5.356782352010253, 7.388957912116327 },
236             { -0.09187745751354681, 0.23612399246659743 },
237             { 2.903203580353435, 3.8076727621794415 },
238             { 5.297014824937293, 8.650985262326508 },
239             { 4.934508602170976, 9.164571423190052 },
240             { -1.0004911869654256, 4.797064194444461 },
241             { 6.782491700298046, 11.852373338280497 },
242             { 2.8983678524536014, 8.303837362117521 },
243             { 4.805003269830865, 6.790462904325329 },
244             { -0.8815799740744226, 1.3015810062131394 },
245             { 5.115138859802104, 6.376895810201089 },
246             { 4.301239328205988, 8.60546337560793 },
247             { 3.276423626317666, 9.889429652591947 },
248             { -4.001924973153122, 4.3353864592328515 },
249             { 3.9571892554119517, 4.500569057308562 },
250             { 4.783067027436208, 7.451125480601317 },
251             { 4.79065438272821, 9.614122776979698 },
252             { 2.677655270279617, 6.8875223698210135 },
253             { -1.3714746289327362, 2.3992153193382437 },
254             { 3.240136859745249, 7.748339397522042 },
255             { 5.107885374416291, 8.508324480583724 },
256             { -1.5830830226666048, 0.9139127045208315 },
257             { -1.1596156791652918, -0.04502759384531929 },
258             { -0.4670021307952068, 3.6193633227841624 },
259             { -0.7026065228267798, 0.4811423031997131 },
260             { -2.719979836732917, 2.5165041618080104 },
261             { 1.0336754331123372, -0.34966029029320644 },
262             { 4.743217291882213, 5.750060115251131 }
263         };
264     }
265 }