<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Code for multivariate GMM error in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879492#M5994</link>
    <description>&lt;P&gt;I have the following code for a multivariate GMM. When I run it, it gives an error. Any advice or guidance would be appreciated.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;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 &amp;lt; 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;&lt;/PRE&gt;</description>
    <pubDate>Wed, 07 Jun 2023 08:26:35 GMT</pubDate>
    <dc:creator>Hen07</dc:creator>
    <dc:date>2023-06-07T08:26:35Z</dc:date>
    <item>
      <title>Code for multivariate GMM error</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879492#M5994</link>
      <description>&lt;P&gt;I have the following code for a multivariate GMM. When I run it, it gives an error. Any advice or guidance would be appreciated.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;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 &amp;lt; 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;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2023 08:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879492#M5994</guid>
      <dc:creator>Hen07</dc:creator>
      <dc:date>2023-06-07T08:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: Code for multivariate GMM error</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879500#M5995</link>
      <description>Please have a look the blogpost from  &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; : &lt;A href="https://blogs.sas.com/content/iml/2013/12/09/matrices-do-not-conform-to-the-operation.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/12/09/matrices-do-not-conform-to-the-operation.html&lt;/A&gt;</description>
      <pubDate>Wed, 07 Jun 2023 09:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879500#M5995</guid>
      <dc:creator>JosvanderVelden</dc:creator>
      <dc:date>2023-06-07T09:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: Code for multivariate GMM error</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879511#M5996</link>
      <description>Why not post it at IML forum ? &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; is there.&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/bd-p/sas_iml" target="_blank"&gt;https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/bd-p/sas_iml&lt;/A&gt;</description>
      <pubDate>Wed, 07 Jun 2023 11:30:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879511#M5996</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-06-07T11:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: Code for multivariate GMM error</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879525#M5997</link>
      <description>&lt;P&gt;Are you trying to fit a Gaussian mixture model to data by using the EM algorithm? If so, try&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;nClusters = NCOL(mu);&lt;/PRE&gt;
&lt;P&gt;also, see the IML program and discussion at&lt;/P&gt;
&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2020/07/23/fit-multivariate-gaussian-mixture-em-algorithm.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2020/07/23/fit-multivariate-gaussian-mixture-em-algorithm.html&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2023 12:30:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Code-for-multivariate-GMM-error/m-p/879525#M5997</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-06-07T12:30:37Z</dc:date>
    </item>
  </channel>
</rss>

