Example 2

This example computes a pooled variance-covariance matrix for the Fisher iris data. The data are not processed as a whole but consecutively in blocks of 10 observations. In each update call, the pooled variance-covariance matrix is internally updated with the new block of observations. The final matrix and the group variable means are printed.

import com.imsl.stat.PooledCovariances;
import com.imsl.math.PrintMatrix;

public class PooledCovariancesEx2 {

    static public void main(String arg[]) {
        double[][] x = {
            {5.100, 3.500, 1.400, 0.200}, {4.900, 3.000, 1.400, 0.200},
            {4.700, 3.200, 1.300, 0.200}, {4.600, 3.100, 1.500, 0.200},
            {5.000, 3.600, 1.400, 0.200}, {5.400, 3.900, 1.700, 0.400},
            {4.600, 3.400, 1.400, 0.300}, {5.000, 3.400, 1.500, 0.200},
            {4.400, 2.900, 1.400, 0.200}, {4.900, 3.100, 1.500, 0.100},
            {5.400, 3.700, 1.500, 0.200}, {4.800, 3.400, 1.600, 0.200},
            {4.800, 3.000, 1.400, 0.100}, {4.300, 3.000, 1.100, 0.100},
            {5.800, 4.000, 1.200, 0.200}, {5.700, 4.400, 1.500, 0.400},
            {5.400, 3.900, 1.300, 0.400}, {5.100, 3.500, 1.400, 0.300},
            {5.700, 3.800, 1.700, 0.300}, {5.100, 3.800, 1.500, 0.300},
            {5.400, 3.400, 1.700, 0.200}, {5.100, 3.700, 1.500, 0.400},
            {4.600, 3.600, 1.000, 0.200}, {5.100, 3.300, 1.700, 0.500},
            {4.800, 3.400, 1.900, 0.200}, {5.000, 3.000, 1.600, 0.200},
            {5.000, 3.400, 1.600, 0.400}, {5.200, 3.500, 1.500, 0.200},
            {5.200, 3.400, 1.400, 0.200}, {4.700, 3.200, 1.600, 0.200},
            {4.800, 3.100, 1.600, 0.200}, {5.400, 3.400, 1.500, 0.400},
            {5.200, 4.100, 1.500, 0.100}, {5.500, 4.200, 1.400, 0.200},
            {4.900, 3.100, 1.500, 0.200}, {5.000, 3.200, 1.200, 0.200},
            {5.500, 3.500, 1.300, 0.200}, {4.900, 3.600, 1.400, 0.100},
            {4.400, 3.000, 1.300, 0.200}, {5.100, 3.400, 1.500, 0.200},
            {5.000, 3.500, 1.300, 0.300}, {4.500, 2.300, 1.300, 0.300},
            {4.400, 3.200, 1.300, 0.200}, {5.000, 3.500, 1.600, 0.600},
            {5.100, 3.800, 1.900, 0.400}, {4.800, 3.000, 1.400, 0.300},
            {5.100, 3.800, 1.600, 0.200}, {4.600, 3.200, 1.400, 0.200},
            {5.300, 3.700, 1.500, 0.200}, {5.000, 3.300, 1.400, 0.200},
            {7.000, 3.200, 4.700, 1.400}, {6.400, 3.200, 4.500, 1.500},
            {6.900, 3.100, 4.900, 1.500}, {5.500, 2.300, 4.000, 1.300},
            {6.500, 2.800, 4.600, 1.500}, {5.700, 2.800, 4.500, 1.300},
            {6.300, 3.300, 4.700, 1.600}, {4.900, 2.400, 3.300, 1.000},
            {6.600, 2.900, 4.600, 1.300}, {5.200, 2.700, 3.900, 1.400},
            {5.000, 2.000, 3.500, 1.000}, {5.900, 3.000, 4.200, 1.500},
            {6.000, 2.200, 4.000, 1.000}, {6.100, 2.900, 4.700, 1.400},
            {5.600, 2.900, 3.600, 1.300}, {6.700, 3.100, 4.400, 1.400},
            {5.600, 3.000, 4.500, 1.500}, {5.800, 2.700, 4.100, 1.000},
            {6.200, 2.200, 4.500, 1.500}, {5.600, 2.500, 3.900, 1.100},
            {5.900, 3.200, 4.800, 1.800}, {6.100, 2.800, 4.000, 1.300},
            {6.300, 2.500, 4.900, 1.500}, {6.100, 2.800, 4.700, 1.200},
            {6.400, 2.900, 4.300, 1.300}, {6.600, 3.000, 4.400, 1.400},
            {6.800, 2.800, 4.800, 1.400}, {6.700, 3.000, 5.000, 1.700},
            {6.000, 2.900, 4.500, 1.500}, {5.700, 2.600, 3.500, 1.000},
            {5.500, 2.400, 3.800, 1.100}, {5.500, 2.400, 3.700, 1.000},
            {5.800, 2.700, 3.900, 1.200}, {6.000, 2.700, 5.100, 1.600},
            {5.400, 3.000, 4.500, 1.500}, {6.000, 3.400, 4.500, 1.600},
            {6.700, 3.100, 4.700, 1.500}, {6.300, 2.300, 4.400, 1.300},
            {5.600, 3.000, 4.100, 1.300}, {5.500, 2.500, 4.000, 1.300},
            {5.500, 2.600, 4.400, 1.200}, {6.100, 3.000, 4.600, 1.400},
            {5.800, 2.600, 4.000, 1.200}, {5.000, 2.300, 3.300, 1.000},
            {5.600, 2.700, 4.200, 1.300}, {5.700, 3.000, 4.200, 1.200},
            {5.700, 2.900, 4.200, 1.300}, {6.200, 2.900, 4.300, 1.300},
            {5.100, 2.500, 3.000, 1.100}, {5.700, 2.800, 4.100, 1.300},
            {6.300, 3.300, 6.000, 2.500}, {5.800, 2.700, 5.100, 1.900},
            {7.100, 3.000, 5.900, 2.100}, {6.300, 2.900, 5.600, 1.800},
            {6.500, 3.000, 5.800, 2.200}, {7.600, 3.000, 6.600, 2.100},
            {4.900, 2.500, 4.500, 1.700}, {7.300, 2.900, 6.300, 1.800},
            {6.700, 2.500, 5.800, 1.800}, {7.200, 3.600, 6.100, 2.500},
            {6.500, 3.200, 5.100, 2.000}, {6.400, 2.700, 5.300, 1.900},
            {6.800, 3.000, 5.500, 2.100}, {5.700, 2.500, 5.000, 2.000},
            {5.800, 2.800, 5.100, 2.400}, {6.400, 3.200, 5.300, 2.300},
            {6.500, 3.000, 5.500, 1.800}, {7.700, 3.800, 6.700, 2.200},
            {7.700, 2.600, 6.900, 2.300}, {6.000, 2.200, 5.000, 1.500},
            {6.900, 3.200, 5.700, 2.300}, {5.600, 2.800, 4.900, 2.000},
            {7.700, 2.800, 6.700, 2.000}, {6.300, 2.700, 4.900, 1.800},
            {6.700, 3.300, 5.700, 2.100}, {7.200, 3.200, 6.000, 1.800},
            {6.200, 2.800, 4.800, 1.800}, {6.100, 3.000, 4.900, 1.800},
            {6.400, 2.800, 5.600, 2.100}, {7.200, 3.000, 5.800, 1.600},
            {7.400, 2.800, 6.100, 1.900}, {7.900, 3.800, 6.400, 2.000},
            {6.400, 2.800, 5.600, 2.200}, {6.300, 2.800, 5.100, 1.500},
            {6.100, 2.600, 5.600, 1.400}, {7.700, 3.000, 6.100, 2.300},
            {6.300, 3.400, 5.600, 2.400}, {6.400, 3.100, 5.500, 1.800},
            {6.000, 3.000, 4.800, 1.800}, {6.900, 3.100, 5.400, 2.100},
            {6.700, 3.100, 5.600, 2.400}, {6.900, 3.100, 5.100, 2.300},
            {5.800, 2.700, 5.100, 1.900}, {6.800, 3.200, 5.900, 2.300},
            {6.700, 3.300, 5.700, 2.500}, {6.700, 3.000, 5.200, 2.300},
            {6.300, 2.500, 5.000, 1.900}, {6.500, 3.000, 5.200, 2.000},
            {6.200, 3.400, 5.400, 2.300}, {5.900, 3.000, 5.100, 1.800}
        };

        // Fisher Iris dataset contains three groups of 50 observations
        // Group 1: Setosa, Group 2: Versicolor, Group 3: Virginica, 50 of each
        int nGroups = 3;
        int[] groups = new int[x.length];
        for (int i = 0; i < 50; i++) {
            groups[i] = 1;
            groups[i + 50] = 2;
            groups[i + 100] = 3;
        }

        int blockGroups[] = new int[10];
        int nrows = blockGroups.length, ncols = x[0].length;
        double[][] blockObservations = new double[nrows][ncols];

        // Create PooledCovariances object
        PooledCovariances pc = new PooledCovariances(nGroups);

        // Add 10 consecutive observations to the pooled Covariance matrix at
        // a time
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < nrows; j++) {
                blockGroups[j] = groups[i * 10 + j];
                System.arraycopy(x[i * 10 + j], 0, blockObservations[j], 0, 4);
            }
            // Update pooled covariance matrix with the new observations
            pc.update(blockObservations, blockGroups);
        }

        double covar[][] = pc.getPooledCovariances();
        new PrintMatrix("Pooled Covariances").print(covar);
        new PrintMatrix("Means").print(pc.getMeans());
        System.out.println("Total number of observations: "
                + pc.getTotalNumberOfObservations());
    }
}

Output

      Pooled Covariances
     0      1      2      3    
0  0.265  0.093  0.168  0.038  
1  0.093  0.115  0.055  0.033  
2  0.168  0.055  0.185  0.043  
3  0.038  0.033  0.043  0.042  

             Means
     0      1      2      3    
0  5.006  3.428  1.462  0.246  
1  5.936  2.77   4.26   1.326  
2  6.588  2.974  5.552  2.026  

Total number of observations: 150
Link to Java source.