MixtureMultivariateNormalDistribution.java

  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.math3.distribution;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.apache.commons.math3.exception.DimensionMismatchException;
  21. import org.apache.commons.math3.exception.NotPositiveException;
  22. import org.apache.commons.math3.random.RandomGenerator;
  23. import org.apache.commons.math3.util.Pair;

  24. /**
  25.  * Multivariate normal mixture distribution.
  26.  * This class is mainly syntactic sugar.
  27.  *
  28.  * @see MixtureMultivariateRealDistribution
  29.  * @since 3.2
  30.  */
  31. public class MixtureMultivariateNormalDistribution
  32.     extends MixtureMultivariateRealDistribution<MultivariateNormalDistribution> {

  33.     /**
  34.      * Creates a multivariate normal mixture distribution.
  35.      * <p>
  36.      * <b>Note:</b> this constructor will implicitly create an instance of
  37.      * {@link org.apache.commons.math3.random.Well19937c Well19937c} as random
  38.      * generator to be used for sampling only (see {@link #sample()} and
  39.      * {@link #sample(int)}). In case no sampling is needed for the created
  40.      * distribution, it is advised to pass {@code null} as random generator via
  41.      * the appropriate constructors to avoid the additional initialisation
  42.      * overhead.
  43.      *
  44.      * @param weights Weights of each component.
  45.      * @param means Mean vector for each component.
  46.      * @param covariances Covariance matrix for each component.
  47.      */
  48.     public MixtureMultivariateNormalDistribution(double[] weights,
  49.                                                  double[][] means,
  50.                                                  double[][][] covariances) {
  51.         super(createComponents(weights, means, covariances));
  52.     }

  53.     /**
  54.      * Creates a mixture model from a list of distributions and their
  55.      * associated weights.
  56.      * <p>
  57.      * <b>Note:</b> this constructor will implicitly create an instance of
  58.      * {@link org.apache.commons.math3.random.Well19937c Well19937c} as random
  59.      * generator to be used for sampling only (see {@link #sample()} and
  60.      * {@link #sample(int)}). In case no sampling is needed for the created
  61.      * distribution, it is advised to pass {@code null} as random generator via
  62.      * the appropriate constructors to avoid the additional initialisation
  63.      * overhead.
  64.      *
  65.      * @param components List of (weight, distribution) pairs from which to sample.
  66.      */
  67.     public MixtureMultivariateNormalDistribution(List<Pair<Double, MultivariateNormalDistribution>> components) {
  68.         super(components);
  69.     }

  70.     /**
  71.      * Creates a mixture model from a list of distributions and their
  72.      * associated weights.
  73.      *
  74.      * @param rng Random number generator.
  75.      * @param components Distributions from which to sample.
  76.      * @throws NotPositiveException if any of the weights is negative.
  77.      * @throws DimensionMismatchException if not all components have the same
  78.      * number of variables.
  79.      */
  80.     public MixtureMultivariateNormalDistribution(RandomGenerator rng,
  81.                                                  List<Pair<Double, MultivariateNormalDistribution>> components)
  82.         throws NotPositiveException, DimensionMismatchException {
  83.         super(rng, components);
  84.     }

  85.     /**
  86.      * @param weights Weights of each component.
  87.      * @param means Mean vector for each component.
  88.      * @param covariances Covariance matrix for each component.
  89.      * @return the list of components.
  90.      */
  91.     private static List<Pair<Double, MultivariateNormalDistribution>> createComponents(double[] weights,
  92.                                                                                        double[][] means,
  93.                                                                                        double[][][] covariances) {
  94.         final List<Pair<Double, MultivariateNormalDistribution>> mvns
  95.             = new ArrayList<Pair<Double, MultivariateNormalDistribution>>(weights.length);

  96.         for (int i = 0; i < weights.length; i++) {
  97.             final MultivariateNormalDistribution dist
  98.                 = new MultivariateNormalDistribution(means[i], covariances[i]);

  99.             mvns.add(new Pair<Double, MultivariateNormalDistribution>(weights[i], dist));
  100.         }

  101.         return mvns;
  102.     }
  103. }