Java Code Examples for org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution#getComponents()

The following examples show how to use org.apache.commons.math3.distribution.MixtureMultivariateNormalDistribution#getComponents() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Ignore@Test
public void testInitialMixture() {
    // Testing initial mixture estimated from data
    double[] correctWeights = new double[] { 0.5, 0.5 };

    MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2];

    correctMVNs[0] = new MultivariateNormalDistribution(new double[] {
                    -0.0021722935000328823, 3.5432892936887908 },
                    new double[][] {
                            { 4.537422569229048, 3.5266152281729304 },
                            { 3.5266152281729304, 6.175448814169779 } });
    correctMVNs[1] = new MultivariateNormalDistribution(new double[] {
                    5.090902706507635, 8.68540656355283 }, new double[][] {
                    { 2.886778573963039, 1.5257474543463154 },
                    { 1.5257474543463154, 3.3794567673616918 } });

    final MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(getTestSamples(), 2);

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : initialMix
            .getComponents()) {
        Assert.assertEquals(correctWeights[i], component.getFirst(),
                Math.ulp(1d));
        Assert.assertEquals(correctMVNs[i], component.getSecond());
        i++;
    }
}
 
Example 2
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testInitialMixture() {
    // Testing initial mixture estimated from data
    final double[] correctWeights = new double[] { 0.5, 0.5 };

    final double[][] correctMeans = new double[][] {
        {-0.0021722935000328823, 3.5432892936887908},
        {5.090902706507635, 8.68540656355283},
    };

    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];

    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
            { 4.537422569229048, 3.5266152281729304 },
            { 3.5266152281729304, 6.175448814169779 } });

    correctCovMats[1] = new Array2DRowRealMatrix( new double[][] {
            { 2.886778573963039, 1.5257474543463154 },
            { 1.5257474543463154, 3.3794567673616918 } });

    final MultivariateNormalDistribution[] correctMVNs = new
            MultivariateNormalDistribution[2];

    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0],
            correctCovMats[0].getData());

    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1],
            correctCovMats[1].getData());

    final MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(getTestSamples(), 2);

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : initialMix
            .getComponents()) {
        Assert.assertEquals(correctWeights[i], component.getFirst(),
                Math.ulp(1d));
        
        final double[] means = component.getValue().getMeans();
        Assert.assertTrue(Arrays.equals(correctMeans[i], means));
        
        final RealMatrix covMat = component.getValue().getCovariances();
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}
 
Example 3
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testFit() {
    // Test that the loglikelihood, weights, and models are determined and
    // fitted correctly
    final double[][] data = getTestSamples();
    final double correctLogLikelihood = -4.292431006791994;
    final double[] correctWeights = new double[] { 0.2962324189652912, 0.7037675810347089 };
    
    final double[][] correctMeans = new double[][]{
        {-1.4213112715121132, 1.6924690505757753},
        {4.213612224374709, 7.975621325853645}
    };
    
    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];
    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
        { 1.739356907285747, -0.5867644251487614 },
        { -0.5867644251487614, 1.0232932029324642 } }
            );
    correctCovMats[1] = new Array2DRowRealMatrix(new double[][] {
        { 4.245384898007161, 2.5797798966382155 },
        { 2.5797798966382155, 3.9200272522448367 } });
    
    final MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2];
    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0], correctCovMats[0].getData());
    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1], correctCovMats[1].getData());

    MultivariateNormalMixtureExpectationMaximization fitter
        = new MultivariateNormalMixtureExpectationMaximization(data);

    MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2);
    fitter.fit(initialMix);
    MixtureMultivariateNormalDistribution fittedMix = fitter.getFittedModel();
    List<Pair<Double, MultivariateNormalDistribution>> components = fittedMix.getComponents();

    Assert.assertEquals(correctLogLikelihood,
                        fitter.getLogLikelihood(),
                        Math.ulp(1d));

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : components) {
        final double weight = component.getFirst();
        final MultivariateNormalDistribution mvn = component.getSecond();
        final double[] mean = mvn.getMeans();
        final RealMatrix covMat = mvn.getCovariances();
        Assert.assertEquals(correctWeights[i], weight, Math.ulp(1d));
        Assert.assertTrue(Arrays.equals(correctMeans[i], mean));
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}
 
Example 4
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testInitialMixture() {
    // Testing initial mixture estimated from data
    final double[] correctWeights = new double[] { 0.5, 0.5 };

    final double[][] correctMeans = new double[][] {
        {-0.0021722935000328823, 3.5432892936887908},
        {5.090902706507635, 8.68540656355283},
    };

    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];

    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
            { 4.537422569229048, 3.5266152281729304 },
            { 3.5266152281729304, 6.175448814169779 } });

    correctCovMats[1] = new Array2DRowRealMatrix( new double[][] {
            { 2.886778573963039, 1.5257474543463154 },
            { 1.5257474543463154, 3.3794567673616918 } });

    final MultivariateNormalDistribution[] correctMVNs = new
            MultivariateNormalDistribution[2];

    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0],
            correctCovMats[0].getData());

    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1],
            correctCovMats[1].getData());

    final MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(getTestSamples(), 2);

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : initialMix
            .getComponents()) {
        Assert.assertEquals(correctWeights[i], component.getFirst(),
                Math.ulp(1d));
        
        final double[] means = component.getValue().getMeans();
        Assert.assertTrue(Arrays.equals(correctMeans[i], means));
        
        final RealMatrix covMat = component.getValue().getCovariances();
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}
 
Example 5
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testFit() {
    // Test that the loglikelihood, weights, and models are determined and
    // fitted correctly
    final double[][] data = getTestSamples();
    final double correctLogLikelihood = -4.292431006791994;
    final double[] correctWeights = new double[] { 0.2962324189652912, 0.7037675810347089 };
    
    final double[][] correctMeans = new double[][]{
        {-1.4213112715121132, 1.6924690505757753},
        {4.213612224374709, 7.975621325853645}
    };
    
    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];
    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
        { 1.739356907285747, -0.5867644251487614 },
        { -0.5867644251487614, 1.0232932029324642 } }
            );
    correctCovMats[1] = new Array2DRowRealMatrix(new double[][] {
        { 4.245384898007161, 2.5797798966382155 },
        { 2.5797798966382155, 3.9200272522448367 } });
    
    final MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2];
    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0], correctCovMats[0].getData());
    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1], correctCovMats[1].getData());

    MultivariateNormalMixtureExpectationMaximization fitter
        = new MultivariateNormalMixtureExpectationMaximization(data);

    MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2);
    fitter.fit(initialMix);
    MixtureMultivariateNormalDistribution fittedMix = fitter.getFittedModel();
    List<Pair<Double, MultivariateNormalDistribution>> components = fittedMix.getComponents();

    Assert.assertEquals(correctLogLikelihood,
                        fitter.getLogLikelihood(),
                        Math.ulp(1d));

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : components) {
        final double weight = component.getFirst();
        final MultivariateNormalDistribution mvn = component.getSecond();
        final double[] mean = mvn.getMeans();
        final RealMatrix covMat = mvn.getCovariances();
        Assert.assertEquals(correctWeights[i], weight, Math.ulp(1d));
        Assert.assertTrue(Arrays.equals(correctMeans[i], mean));
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}
 
Example 6
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Ignore@Test
public void testFit() {
    // Test that the loglikelihood, weights, and models are determined and
    // fitted correctly
    double[][] data = getTestSamples();
    double correctLogLikelihood = -4.292431006791994;
    double[] correctWeights = new double[] { 0.2962324189652912, 0.7037675810347089 };
    MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2];
    correctMVNs[0] = new MultivariateNormalDistribution(new double[] {
                    -1.4213112715121132, 1.6924690505757753 },
                    new double[][] {
                            { 1.739356907285747, -0.5867644251487614 },
                            { -0.5867644251487614, 1.0232932029324642 } });

    correctMVNs[1] = new MultivariateNormalDistribution(new double[] {
                    4.213612224374709, 7.975621325853645 },
                    new double[][] {
                            { 4.245384898007161, 2.5797798966382155 },
                            { 2.5797798966382155, 3.9200272522448367 } });

    MultivariateNormalMixtureExpectationMaximization fitter
        = new MultivariateNormalMixtureExpectationMaximization(data);

    MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2);
    fitter.fit(initialMix);
    MixtureMultivariateNormalDistribution fittedMix = fitter.getFittedModel();
    List<Pair<Double, MultivariateNormalDistribution>> components = fittedMix.getComponents();

    Assert.assertEquals(correctLogLikelihood,
                        fitter.getLogLikelihood(),
                        Math.ulp(1d));

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : components) {
        double weight = component.getFirst();
        MultivariateNormalDistribution mvn = component.getSecond();
        Assert.assertEquals(correctWeights[i], weight, Math.ulp(1d));
        Assert.assertEquals(correctMVNs[i], mvn);
        i++;
    }
}
 
Example 7
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testInitialMixture() {
    // Testing initial mixture estimated from data
    final double[] correctWeights = new double[] { 0.5, 0.5 };

    final double[][] correctMeans = new double[][] {
        {-0.0021722935000328823, 3.5432892936887908},
        {5.090902706507635, 8.68540656355283},
    };

    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];

    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
            { 4.537422569229048, 3.5266152281729304 },
            { 3.5266152281729304, 6.175448814169779 } });

    correctCovMats[1] = new Array2DRowRealMatrix( new double[][] {
            { 2.886778573963039, 1.5257474543463154 },
            { 1.5257474543463154, 3.3794567673616918 } });

    final MultivariateNormalDistribution[] correctMVNs = new
            MultivariateNormalDistribution[2];

    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0],
            correctCovMats[0].getData());

    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1],
            correctCovMats[1].getData());

    final MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(getTestSamples(), 2);

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : initialMix
            .getComponents()) {
        Assert.assertEquals(correctWeights[i], component.getFirst(),
                Math.ulp(1d));
        
        final double[] means = component.getValue().getMeans();
        Assert.assertTrue(Arrays.equals(correctMeans[i], means));
        
        final RealMatrix covMat = component.getValue().getCovariances();
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}
 
Example 8
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testFit() {
    // Test that the loglikelihood, weights, and models are determined and
    // fitted correctly
    final double[][] data = getTestSamples();
    final double correctLogLikelihood = -4.292431006791994;
    final double[] correctWeights = new double[] { 0.2962324189652912, 0.7037675810347089 };
    
    final double[][] correctMeans = new double[][]{
        {-1.4213112715121132, 1.6924690505757753},
        {4.213612224374709, 7.975621325853645}
    };
    
    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];
    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
        { 1.739356907285747, -0.5867644251487614 },
        { -0.5867644251487614, 1.0232932029324642 } }
            );
    correctCovMats[1] = new Array2DRowRealMatrix(new double[][] {
        { 4.245384898007161, 2.5797798966382155 },
        { 2.5797798966382155, 3.9200272522448367 } });
    
    final MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2];
    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0], correctCovMats[0].getData());
    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1], correctCovMats[1].getData());

    MultivariateNormalMixtureExpectationMaximization fitter
        = new MultivariateNormalMixtureExpectationMaximization(data);

    MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2);
    fitter.fit(initialMix);
    MixtureMultivariateNormalDistribution fittedMix = fitter.getFittedModel();
    List<Pair<Double, MultivariateNormalDistribution>> components = fittedMix.getComponents();

    Assert.assertEquals(correctLogLikelihood,
                        fitter.getLogLikelihood(),
                        Math.ulp(1d));

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : components) {
        final double weight = component.getFirst();
        final MultivariateNormalDistribution mvn = component.getSecond();
        final double[] mean = mvn.getMeans();
        final RealMatrix covMat = mvn.getCovariances();
        Assert.assertEquals(correctWeights[i], weight, Math.ulp(1d));
        Assert.assertTrue(Arrays.equals(correctMeans[i], mean));
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}
 
Example 9
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testInitialMixture() {
    // Testing initial mixture estimated from data
    final double[] correctWeights = new double[] { 0.5, 0.5 };

    final double[][] correctMeans = new double[][] {
        {-0.0021722935000328823, 3.5432892936887908},
        {5.090902706507635, 8.68540656355283},
    };

    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];

    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
            { 4.537422569229048, 3.5266152281729304 },
            { 3.5266152281729304, 6.175448814169779 } });

    correctCovMats[1] = new Array2DRowRealMatrix( new double[][] {
            { 2.886778573963039, 1.5257474543463154 },
            { 1.5257474543463154, 3.3794567673616918 } });

    final MultivariateNormalDistribution[] correctMVNs = new
            MultivariateNormalDistribution[2];

    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0],
            correctCovMats[0].getData());

    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1],
            correctCovMats[1].getData());

    final MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(getTestSamples(), 2);

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : initialMix
            .getComponents()) {
        Assert.assertEquals(correctWeights[i], component.getFirst(),
                Math.ulp(1d));
        
        final double[] means = component.getValue().getMeans();
        Assert.assertTrue(Arrays.equals(correctMeans[i], means));
        
        final RealMatrix covMat = component.getValue().getCovariances();
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}
 
Example 10
Source File: MultivariateNormalMixtureExpectationMaximizationTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testFit() {
    // Test that the loglikelihood, weights, and models are determined and
    // fitted correctly
    final double[][] data = getTestSamples();
    final double correctLogLikelihood = -4.292431006791994;
    final double[] correctWeights = new double[] { 0.2962324189652912, 0.7037675810347089 };
    
    final double[][] correctMeans = new double[][]{
        {-1.4213112715121132, 1.6924690505757753},
        {4.213612224374709, 7.975621325853645}
    };
    
    final RealMatrix[] correctCovMats = new Array2DRowRealMatrix[2];
    correctCovMats[0] = new Array2DRowRealMatrix(new double[][] {
        { 1.739356907285747, -0.5867644251487614 },
        { -0.5867644251487614, 1.0232932029324642 } }
            );
    correctCovMats[1] = new Array2DRowRealMatrix(new double[][] {
        { 4.245384898007161, 2.5797798966382155 },
        { 2.5797798966382155, 3.9200272522448367 } });
    
    final MultivariateNormalDistribution[] correctMVNs = new MultivariateNormalDistribution[2];
    correctMVNs[0] = new MultivariateNormalDistribution(correctMeans[0], correctCovMats[0].getData());
    correctMVNs[1] = new MultivariateNormalDistribution(correctMeans[1], correctCovMats[1].getData());

    MultivariateNormalMixtureExpectationMaximization fitter
        = new MultivariateNormalMixtureExpectationMaximization(data);

    MixtureMultivariateNormalDistribution initialMix
        = MultivariateNormalMixtureExpectationMaximization.estimate(data, 2);
    fitter.fit(initialMix);
    MixtureMultivariateNormalDistribution fittedMix = fitter.getFittedModel();
    List<Pair<Double, MultivariateNormalDistribution>> components = fittedMix.getComponents();

    Assert.assertEquals(correctLogLikelihood,
                        fitter.getLogLikelihood(),
                        Math.ulp(1d));

    int i = 0;
    for (Pair<Double, MultivariateNormalDistribution> component : components) {
        final double weight = component.getFirst();
        final MultivariateNormalDistribution mvn = component.getSecond();
        final double[] mean = mvn.getMeans();
        final RealMatrix covMat = mvn.getCovariances();
        Assert.assertEquals(correctWeights[i], weight, Math.ulp(1d));
        Assert.assertTrue(Arrays.equals(correctMeans[i], mean));
        Assert.assertEquals(correctCovMats[i], covMat);
        i++;
    }
}