<?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 Proc IML -&amp;gt; Verifying -2 Log-Likelihood Issue; AR(1) Matrix, CS Matrix in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-gt-Verifying-2-Log-Likelihood-Issue-AR-1-Matrix-CS/m-p/961022#M6471</link>
    <description>&lt;P&gt;Hi all!&lt;/P&gt;&lt;P&gt;Forgive me in advance, I am in no way a SAS professional and am trying to learn. I'm working on version 9.4 for reference.&lt;/P&gt;&lt;P&gt;I am working on a code to compute the estimated log-likelihood for a model given parameters, particularly to verify the&amp;nbsp;maximized log-likelihood function value. The covariance is being analyzed with an AR (1) matrix and a CS matrix. The computed log-likelihood using proc mixed for the AR (1) matrix is -256.7323 and CS matrix is -204.7334, but the following code throws log-likelihoods that are extremely off. Part a.2-3 is for the AR (1) matrix and Part A.4 is for the CS Matrix. There is no missing data in the dataset. If anyone could point me to a resource or has a suggestion, please let me know!&lt;/P&gt;&lt;PRE&gt;/*Part A.2-3*/
proc iml;
   start log_normal_density(y, mu, sigma, rho);
       n = nrow(y);
       R = j(n, n, .);
       do i = 1 to n;
           R[i,i] = 1;
           do j = i+1 to n;
               R[i,j] = rho##abs(i-j);
               R[j,i] = R[i,j];
           end;
       end;
       sigmaMat = sigma##2 * R;
       eigenvalues = eigval(sigmaMat);
       logDetSigma = sum(log(eigenvalues));
       if logDetSigma &amp;lt;= 0 then logDetSigma = 1e-8;
       invSigmaMat = inv(sigmaMat);
       const = -0.5 * n * log(2 * constant("pi")) - 0.5 * logDetSigma;
       diff = y - mu;
       quad_form = sum(diff # invSigmaMat * diff);
       log_pdf = const - 0.5 * quad_form;
       return(log_pdf);
   finish;
   start mylik(y, a, b, sigma, rho);
       n = nrow(y);
       mu = (a + b * (0:n-1))`;
       lik = log_normal_density(y, mu, sigma, rho);
       return(lik);
   finish;
   use rats;
   read all var {wei} into Y;
   close rats;
   a = 4.0237;
   b = 0.2458;
   sigma = sqrt(0.01764);
   rho = 0.7643;
   logL = mylik(Y, a, b, sigma, rho);
   neg2LogL = (-2) * logL;
   print logL neg2LogL;
quit;
/*Part A.4*/
proc iml;
start log_normal_density_cs(y, mu, sigma, rho);
    n = nrow(y);

    /* Correct CS covariance matrix */
    R = j(n, n, rho);        
    do i = 1 to n;
        R[i,i] = 1;    
    end;

    sigmaMat = sigma * ((1 - rho) * I(n) + rho * J(n, n, 1)); 

    detR = det(sigmaMat);    
    invR = inv(sigmaMat);     

    const = -0.5 * n * log(2 * constant("pi")) - 0.5 * log(detR);
    log_pdf = const - 0.5 * t(y - mu) * invR * (y - mu);

    return(log_pdf);
finish;

start mylik_cs(y, a, b, sigma, rho);
    n = nrow(y);
    mu = a + b * T(0:n-1);  /* Transpose to match y's dimension */
    lik = log_normal_density_cs(y, mu, sigma, rho);
    return(lik);
finish;
use rats;
read all var {wei} into Y;
close rats;
a = 4.0615;
b = 0.2431;
sigma = 0.009249;
rho = 0.007725;
logL_cs = mylik_cs(Y, a, b, sigma, rho);
neg2LogL_cs = (-2) * logL_cs;

print logL_cs neg2LogL_cs;
quit;&lt;/PRE&gt;</description>
    <pubDate>Thu, 06 Mar 2025 01:05:25 GMT</pubDate>
    <dc:creator>mattll218</dc:creator>
    <dc:date>2025-03-06T01:05:25Z</dc:date>
    <item>
      <title>Proc IML -&gt; Verifying -2 Log-Likelihood Issue; AR(1) Matrix, CS Matrix</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-gt-Verifying-2-Log-Likelihood-Issue-AR-1-Matrix-CS/m-p/961022#M6471</link>
      <description>&lt;P&gt;Hi all!&lt;/P&gt;&lt;P&gt;Forgive me in advance, I am in no way a SAS professional and am trying to learn. I'm working on version 9.4 for reference.&lt;/P&gt;&lt;P&gt;I am working on a code to compute the estimated log-likelihood for a model given parameters, particularly to verify the&amp;nbsp;maximized log-likelihood function value. The covariance is being analyzed with an AR (1) matrix and a CS matrix. The computed log-likelihood using proc mixed for the AR (1) matrix is -256.7323 and CS matrix is -204.7334, but the following code throws log-likelihoods that are extremely off. Part a.2-3 is for the AR (1) matrix and Part A.4 is for the CS Matrix. There is no missing data in the dataset. If anyone could point me to a resource or has a suggestion, please let me know!&lt;/P&gt;&lt;PRE&gt;/*Part A.2-3*/
proc iml;
   start log_normal_density(y, mu, sigma, rho);
       n = nrow(y);
       R = j(n, n, .);
       do i = 1 to n;
           R[i,i] = 1;
           do j = i+1 to n;
               R[i,j] = rho##abs(i-j);
               R[j,i] = R[i,j];
           end;
       end;
       sigmaMat = sigma##2 * R;
       eigenvalues = eigval(sigmaMat);
       logDetSigma = sum(log(eigenvalues));
       if logDetSigma &amp;lt;= 0 then logDetSigma = 1e-8;
       invSigmaMat = inv(sigmaMat);
       const = -0.5 * n * log(2 * constant("pi")) - 0.5 * logDetSigma;
       diff = y - mu;
       quad_form = sum(diff # invSigmaMat * diff);
       log_pdf = const - 0.5 * quad_form;
       return(log_pdf);
   finish;
   start mylik(y, a, b, sigma, rho);
       n = nrow(y);
       mu = (a + b * (0:n-1))`;
       lik = log_normal_density(y, mu, sigma, rho);
       return(lik);
   finish;
   use rats;
   read all var {wei} into Y;
   close rats;
   a = 4.0237;
   b = 0.2458;
   sigma = sqrt(0.01764);
   rho = 0.7643;
   logL = mylik(Y, a, b, sigma, rho);
   neg2LogL = (-2) * logL;
   print logL neg2LogL;
quit;
/*Part A.4*/
proc iml;
start log_normal_density_cs(y, mu, sigma, rho);
    n = nrow(y);

    /* Correct CS covariance matrix */
    R = j(n, n, rho);        
    do i = 1 to n;
        R[i,i] = 1;    
    end;

    sigmaMat = sigma * ((1 - rho) * I(n) + rho * J(n, n, 1)); 

    detR = det(sigmaMat);    
    invR = inv(sigmaMat);     

    const = -0.5 * n * log(2 * constant("pi")) - 0.5 * log(detR);
    log_pdf = const - 0.5 * t(y - mu) * invR * (y - mu);

    return(log_pdf);
finish;

start mylik_cs(y, a, b, sigma, rho);
    n = nrow(y);
    mu = a + b * T(0:n-1);  /* Transpose to match y's dimension */
    lik = log_normal_density_cs(y, mu, sigma, rho);
    return(lik);
finish;
use rats;
read all var {wei} into Y;
close rats;
a = 4.0615;
b = 0.2431;
sigma = 0.009249;
rho = 0.007725;
logL_cs = mylik_cs(Y, a, b, sigma, rho);
neg2LogL_cs = (-2) * logL_cs;

print logL_cs neg2LogL_cs;
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Mar 2025 01:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-gt-Verifying-2-Log-Likelihood-Issue-AR-1-Matrix-CS/m-p/961022#M6471</guid>
      <dc:creator>mattll218</dc:creator>
      <dc:date>2025-03-06T01:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Proc IML -&gt; Verifying -2 Log-Likelihood Issue; AR(1) Matrix, CS Matrix</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-gt-Verifying-2-Log-Likelihood-Issue-AR-1-Matrix-CS/m-p/961053#M6472</link>
      <description>&lt;P&gt;I'll try to look at your code more closely later today, but here are some initial suggestions:&lt;/P&gt;
&lt;P&gt;1. You can get the AR(1) correlation matrix by using the TOEPLITZ function. See the AR1COEFF function defined in&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2017/06/12/log-likelihood-function-in-sas.html" target="_blank"&gt;Two simple ways to construct a log-likelihood function in SAS - The DO Loop&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;2. If you want to compute the log-determinant of a matrix, use the LOGABSDET function. See&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2014/10/20/log-determinant-of-a-matrix.html" target="_blank"&gt;Compute the log-determinant of an arbitrary matrix - The DO Loop&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;3. To compute the log-density of common probability distributions, use the LOGPDF function. Again, see&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2017/06/12/log-likelihood-function-in-sas.html" target="_blank"&gt;Two simple ways to construct a log-likelihood function in SAS - The DO Loop&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you post the RATS data?&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2025 11:29:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-gt-Verifying-2-Log-Likelihood-Issue-AR-1-Matrix-CS/m-p/961053#M6472</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-03-06T11:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: Proc IML -&gt; Verifying -2 Log-Likelihood Issue; AR(1) Matrix, CS Matrix</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-gt-Verifying-2-Log-Likelihood-Issue-AR-1-Matrix-CS/m-p/961240#M6475</link>
      <description>&lt;P&gt;What does your PROC MIXED code look like? Can you upload the data?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Mar 2025 15:52:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Proc-IML-gt-Verifying-2-Log-Likelihood-Issue-AR-1-Matrix-CS/m-p/961240#M6475</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-03-07T15:52:08Z</dc:date>
    </item>
  </channel>
</rss>

