<?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 Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528615#M5463</link>
    <description>&lt;P&gt;Why not post it at IML forum&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;is there .also Rick wrote several blogs about it before . you could search MLE at his blog.&lt;/P&gt;</description>
    <pubDate>Sun, 20 Jan 2019 11:10:36 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-01-20T11:10:36Z</dc:date>
    <item>
      <title>SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528602#M5461</link>
      <description>&lt;P&gt;Hi, I need to solve the maximum likelihood estimation problem. Here is the equation&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="equation.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/26382i7836530EC67C8310/image-size/large?v=v2&amp;amp;px=999" role="button" title="equation.JPG" alt="equation.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;all the matrix and scalar are given except the highlighted gamma*_hat (1x1 matrix), Beta*_hat (6x1 matrix) and Sigma*_hat (6x6 matrix).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's my part of proc iml&amp;nbsp;codes for the above equations. (N = 6, T =60)&lt;/P&gt;&lt;P&gt;I already have the initial guess number for beta and sigma. I need to plug my initial guess beta and sigma to gamma equation and get a new gamma, and then use this new gamma to calculate a new beta and sigma, and repeat this process until the results convergence.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	/*gamma*/
		gamma = ((I - beta)`*inv(sigma)*(mu - beta*mu_m))/ ((I - beta)`*inv(sigma)*(I - beta));
	/*beta*/
			/**Numerator*/
		Nu = j(N,1,0);	D = 0;
		do i = 1 to T by 1;
			Nu = Nu + (R[i, ]` - gamma*ID)*(Rm[i,1] - gamma);
		end;
			/*Denominator*/
		do i = 1 to T by 1;
			D = D + (Rm[i,1] - gamma)##2;
		end;

		beta = Nu/D;
	
	/*sigma*/
	sigma = j(6,6,0);
		do i = 1 to T by 1;
			sigma = sigma + (Rt[i, ]`-gamma*(I-beta) - beta*Rm_b[i,1])*(Rt[i, ]`-gamma*(I-beta) - beta*Rm[i,1])`;
		end;
	sigma_b = sigma_b/T;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Does anyone know how can I repeat these codes? should I use do loop? is there a repeat code function in iml?&amp;nbsp; I really appreciate any help!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jan 2019 04:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528602#M5461</guid>
      <dc:creator>LZHAO006</dc:creator>
      <dc:date>2019-01-20T04:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528603#M5462</link>
      <description>&lt;P&gt;This article might be relevant to your question:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.sas.com/content/iml/2013/06/19/macros-and-loops.html" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/06/19/macros-and-loops.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jan 2019 04:13:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528603#M5462</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2019-01-20T04:13:38Z</dc:date>
    </item>
    <item>
      <title>Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528615#M5463</link>
      <description>&lt;P&gt;Why not post it at IML forum&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;is there .also Rick wrote several blogs about it before . you could search MLE at his blog.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jan 2019 11:10:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528615#M5463</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-01-20T11:10:36Z</dc:date>
    </item>
    <item>
      <title>Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528618#M5465</link>
      <description>&lt;P&gt;1. What are you trying to accomplish? Are you estimating parameters in a mixed model?&amp;nbsp;There might be better ways to solve this problem in SAS, so let us know what your ultimate goal is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Could you post&amp;nbsp;values for&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I, mu, mu_m, Rt and Rm?&lt;/P&gt;
&lt;P&gt;Also, in your SAS code you refer to Rm_b, which doesn't seem to appear in the equations.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jan 2019 12:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528618#M5465</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-20T12:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528626#M5467</link>
      <description>&lt;P&gt;Here's&amp;nbsp;the post value of&amp;nbsp; Rt&lt;/P&gt;</description>
      <pubDate>Sun, 20 Jan 2019 17:12:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528626#M5467</guid>
      <dc:creator>LZHAO006</dc:creator>
      <dc:date>2019-01-20T17:12:16Z</dc:date>
    </item>
    <item>
      <title>Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528627#M5468</link>
      <description>&lt;P&gt;HI Rick, my ultimate goal is to estimate the gamma*, beta* and sigma* using the provided equation. Right now I already have the initial estimate value for beta and sigma. and I would like to run a loop (maybe 1000times?) for my equation codes to get the gamma*, beta* and sigma*.&lt;/P&gt;&lt;P&gt;This is part of the Black version of CAPM calculation. so I add all value name with "_b" to remind me this is the black version in my original code. Sorry I didn't delete when I posted my questions.&lt;/P&gt;&lt;P&gt;Please see the attachments of my post values and initial guest for beta and sigma. I is the 6x1 identity matrix. I added in my code. Thank you very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;I = j(6,1,1);
T = 60;

/*gamma*/
		gamma = ((I - beta)`*inv(sigma)*(mu - beta*mu_m))/ ((I - beta)`*inv(sigma)*(I - beta));

/*beta*/
			/**Numerator*/
		Nu = j(N,1,0);	D = 0;
		do i = 1 to T by 1;
			Nu = Nu + (Rt[i, ]` - gamma*ID)*(Rm[i,1] - gamma);
		end;
			/*Denominator*/
		do i = 1 to T by 1;
			D = D + (Rm[i,1] - gamma)##2;
		end;

		beta = Nu/D;
	
/*sigma*/
	sigma = j(6,6,0);
		do i = 1 to T by 1;
			sigma = sigma + (Rt[i, ]`-gamma*(I-beta) - beta*Rm[i,1])*(Rt[i, ]`-gamma*(I-beta) - beta*Rm[i,1])`;
		end;
	sigma = sigma/T;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Jan 2019 17:21:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528627#M5468</guid>
      <dc:creator>LZHAO006</dc:creator>
      <dc:date>2019-01-20T17:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528637#M5469</link>
      <description>&lt;P&gt;SAS is not a case-sensitive language. Thus your matrix I and the iteration variable i are the same variable. As soon as you execute the first&amp;nbsp;loop, the I matrix is overwritten/replaced.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do&amp;nbsp;know from theory that the iterative&amp;nbsp;computation of those equations will converge to a solution? If so,&lt;/P&gt;
&lt;P&gt;write a loop such as the following (untested, but hopefully you can see the main idea):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;beta = GUESS1; gamma = GUESS2; SIGMA = GUESS3;
convergence = 0; /* false */
do k = 1 to 500 until (convergence);
&amp;nbsp; beta_old = beta; gamma_old = gamma; Sigma_old = Sigma;
&amp;nbsp; &amp;nbsp;/* compute/update&amp;nbsp;beta, gamma, and Sigma according to formulas */
&amp;nbsp; beta_diff = norm(abs(beta - beta_old));&amp;nbsp; &amp;nbsp;/* or use max(abs( ... )) */
&amp;nbsp; gamma_diff = norm(abs(gamma - gamma_old));
&amp;nbsp;&amp;nbsp;Sigma_diff =&amp;nbsp;&amp;nbsp;norm(abs(Sigma - Sigma_old));
&amp;nbsp; if beta_diff &amp;lt;= 1e-3 &amp;amp;&amp;nbsp;gamma_diff &amp;lt;= 1e-3 &amp;amp; Sigma_diff &amp;lt;= 1e-3 then
      convergence=1;
&amp;nbsp; if mod(k, 25)=0 then
&amp;nbsp; &amp;nbsp; &amp;nbsp; print k beta_diff gamma_diff Sigma_diff;
end;

print converged k beta_diff gamma_diff Sigma_diff;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Jan 2019 19:16:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528637#M5469</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-20T19:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: SAS/IML Matrix Maximum Likelihood Estimation - Do loop?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528676#M5480</link>
      <description>It works! thank you very much!!!</description>
      <pubDate>Mon, 21 Jan 2019 03:44:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-IML-Matrix-Maximum-Likelihood-Estimation-Do-loop/m-p/528676#M5480</guid>
      <dc:creator>LZHAO006</dc:creator>
      <dc:date>2019-01-21T03:44:13Z</dc:date>
    </item>
  </channel>
</rss>

