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