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.math4.legacy.distribution;

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

  20. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  21. import org.apache.commons.math4.legacy.exception.NotPositiveException;
  22. import org.apache.commons.math4.legacy.core.Pair;

  23. /**
  24.  * Multivariate normal mixture distribution.
  25.  * This class is mainly syntactic sugar.
  26.  *
  27.  * @see MixtureMultivariateRealDistribution
  28.  * @since 3.2
  29.  */
  30. public class MixtureMultivariateNormalDistribution
  31.     extends MixtureMultivariateRealDistribution<MultivariateNormalDistribution> {
  32.     /**
  33.      * Creates a mixture model from a list of distributions and their
  34.      * associated weights.
  35.      *
  36.      * @param components Distributions from which to sample.
  37.      * @throws NotPositiveException if any of the weights is negative.
  38.      * @throws DimensionMismatchException if not all components have the same
  39.      * number of variables.
  40.      */
  41.     public MixtureMultivariateNormalDistribution(List<Pair<Double, MultivariateNormalDistribution>> components)
  42.         throws NotPositiveException,
  43.                DimensionMismatchException {
  44.         super(components);
  45.     }

  46.     /**
  47.      * Creates a multivariate normal mixture distribution.
  48.      *
  49.      * @param weights Weights of each component.
  50.      * @param means Mean vector for each component.
  51.      * @param covariances Covariance matrix for each component.
  52.      * @throws NotPositiveException if any of the weights is negative.
  53.      * @throws DimensionMismatchException if not all components have the same
  54.      * number of variables.
  55.      */
  56.     public MixtureMultivariateNormalDistribution(double[] weights,
  57.                                                  double[][] means,
  58.                                                  double[][][] covariances)
  59.         throws NotPositiveException,
  60.                DimensionMismatchException {
  61.         this(createComponents(weights, means, covariances));
  62.     }

  63.     /**
  64.      * Creates components of the mixture model.
  65.      *
  66.      * @param weights Weights of each component.
  67.      * @param means Mean vector for each component.
  68.      * @param covariances Covariance matrix for each component.
  69.      * @return the list of components.
  70.      */
  71.     private static List<Pair<Double, MultivariateNormalDistribution>> createComponents(double[] weights,
  72.                                                                                        double[][] means,
  73.                                                                                        double[][][] covariances) {
  74.         final List<Pair<Double, MultivariateNormalDistribution>> mvns
  75.             = new ArrayList<>(weights.length);

  76.         for (int i = 0; i < weights.length; i++) {
  77.             final MultivariateNormalDistribution dist
  78.                 = new MultivariateNormalDistribution(means[i], covariances[i]);

  79.             mvns.add(new Pair<>(weights[i], dist));
  80.         }

  81.         return mvns;
  82.     }
  83. }