BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hen07
Calcite | Level 5

I have the following code for a multivariate GMM. When I run it, it gives an error. Any advice or guidance would be appreciated.


proc iml;
 NOTE: IML Ready
 80         use WORK.IMPORT4;
 81         read all var {X1 X2} into x;
 81       !                               /* Read multiple variables x1 and x2 */
 82         
 83         mu = {120 150};
 83       !                   /* Mean vector */
 84         si = {20 20};
 84       !                   /* Standard deviation vector */
 85         pi = {0.5 0.5};
 85       !                   /* Mixing weights */
 86         
 87         nVars = ncol(x);
 87       !                   /* Number of variables */
 88         nClusters = nrow(mu);
 88       !                        /* Number of clusters */
 89         
 90         /* Estep */
 91         stop = 0;
 92         results = j(1, 1 + nClusters + nClusters*nVars + nClusters*nVars, .);
 93         idx = 1;
 94         do i = 1 to 200 while (stop = 0);
 95             print "Iteration" i;
 96         
 97             /* Compute gamma */
 98             gamma = pi[1] * pdf('normal', x, mu[1], si[1]) +
 99                     pi[2] * pdf('normal', x, mu[2], si[2]);
 100            gamma = gamma / (gamma[,1] + gamma[,2]);
 100      !                                               /* Normalize gamma */
 101        
 102            /* Check for convergence */
 103            if i = 1 then print gamma;
 104        
 105            nk = gamma[,1] + gamma[,2];
 105      !                                  /* Calculate nk */
 106        
 107            npi = nk / sum(nk);
 107      !                          /* Calculate npi */
 108        
 109            nmu = j(1, nVars, 0);
 110            do k = 1 to nClusters;
 111                nmu = nmu + (gamma[,k] # x[,1:nVars]);
 112            end;
 113            nmu = nmu / nk;
 113      !                      /* Calculate nmu */
 114        
 115            nsi2 = j(nVars, nVars, 0);
 116        
 116      !  do k = 1 to nClusters;
 117            
 117      !      diff = x[,1:nVars] - nmu;
 118            
 118      !      nsi2 = nsi2 + (gamma[,k] * (diff` * diff));
 119        
 119      !  end;
 120        
 121            nsi = sqrt(nsi2);
 121      !                        /* Calculate nsi */
 122        
 123            diff = max(abs(mu - nmu));
 123      !                                 /* Calculate diff */
 124        
 125            if diff < 0.005 then stop = 1;
 126        
 127            mu = nmu;
 127      !                /* Update mu */
 128            si = nsi;
 128      !                /* Update si */
 129            pi = npi;
 129      !                /* Update pi */
 130        
 131            results[idx, 1] = i;
 132            results[idx, 2:(1+nClusters)] = npi;
 133            results[idx, (2+nClusters):(1+nClusters+nVars)] = nmu;
 134            results[idx, (2+nClusters+nVars):(1+nClusters+2*nVars)] = nsi;
 135            idx = idx + 1;
 136        end;
 ERROR: (execution) Matrices do not conform to the operation.
 
  operation : * at line 118 column 31
  operands  : _TEM1001, _TEM1003
 _TEM1001   1500 rows      1 col     (numeric)
 
 _TEM1003      2 rows      2 cols    (numeric)
 
  5054876.6 5443147.7
  5443147.7 6657773.5
 
  statement : ASSIGN at line 118 column 6
 137        
 138        create results from results;
 139        append from results;
 140        close results;
 NOTE: The data set WORK.RESULTS has 1 observations and 6 variables.
 141        
 142        quit;
1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Are you trying to fit a Gaussian mixture model to data by using the EM algorithm? If so, try 

nClusters = NCOL(mu);

also, see the IML program and discussion at

https://blogs.sas.com/content/iml/2020/07/23/fit-multivariate-gaussian-mixture-em-algorithm.html 

View solution in original post

3 REPLIES 3
Rick_SAS
SAS Super FREQ

Are you trying to fit a Gaussian mixture model to data by using the EM algorithm? If so, try 

nClusters = NCOL(mu);

also, see the IML program and discussion at

https://blogs.sas.com/content/iml/2020/07/23/fit-multivariate-gaussian-mixture-em-algorithm.html 

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1525 views
  • 2 likes
  • 4 in conversation