<?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: Vectorize Do Loop to increase performance in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165185#M1596</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can get rid of the inner loop. I haven't figured out whether the outer loop can be simplified. The WEIGHTS matrix and the data[,i]-data[,j] computation make each computation dependent on a loop variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So is this is some sort of finite difference computation, and that's why we have data[,i]-data[,j]?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can we assume that the WEIGHTS matrix is positive? Your example fills it with random normal values, but that isn't usually the case for a matrix of that name.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 06 Feb 2014 17:57:10 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2014-02-06T17:57:10Z</dc:date>
    <item>
      <title>Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165182#M1593</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="margin: 0 0 1em; font-size: 13.63636302947998px; background-color: #ffffff; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"&gt;I am trying to vectorize the code below.&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 13.63636302947998px; background-color: #ffffff; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"&gt;&lt;SPAN style="font-size: 13.63636302947998px;"&gt;&lt;STRONG&gt;Can somebody help me to avoid the two do loops?&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 13.63636302947998px; background-color: #ffffff; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"&gt;You can ignore the code before the do loops, that is simply to generate the data.&lt;/P&gt;&lt;P style="margin: 0 0 1em; font-size: 13.63636302947998px; background-color: #ffffff; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif;"&gt;&lt;CODE style="font-size: 13.63636302947998px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"&gt;proc iml;&lt;BR /&gt;/*Generate Data*/&lt;BR /&gt;data=J(5,9,0);&lt;BR /&gt;call randseed(1);&lt;BR /&gt;call randgen(data, "Normal");&lt;BR /&gt;&lt;BR /&gt;dim=nrow(data);&lt;BR /&gt;obs=ncol(data);&lt;BR /&gt;weights=J(obs,obs,0);&lt;BR /&gt;call randgen(weights, "Normal");&lt;BR /&gt;&lt;BR /&gt;mat=j(dim,dim,0);&lt;BR /&gt;/*I would like to vectorize this loop*/&lt;BR /&gt;do j=1 to obs;&lt;BR /&gt;&amp;nbsp; do i=1 to obs;&lt;BR /&gt;&amp;nbsp; x=data[,i]-data[,j];&lt;BR /&gt;&amp;nbsp; mat=mat+weights[i,j]*x*t(x);&lt;BR /&gt;&amp;nbsp; end;&amp;nbsp; &lt;BR /&gt;end;&lt;BR /&gt;quit;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: bernhard&amp;nbsp; I (rather Rick Wicklin actually) noticed an error in the code. In fact I need to go through the full inner loop up to obs.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Feb 2014 12:31:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165182#M1593</guid>
      <dc:creator>bernhard</dc:creator>
      <dc:date>2014-02-06T12:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165183#M1594</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;1) What are you trying to accomplish statistically? For example, is this a weighted OLS regression or some other standard statistical analysis?&lt;/P&gt;&lt;P&gt;2) You have obs=ncol(data) and dim=nrow(data).&amp;nbsp; Usually the rows of the data matrix contain the observations and the columns contain the variables, but you seem to have reversed that convention.&lt;/P&gt;&lt;P&gt;3) The program contains the comment "of course the loop could go to obs." I think vectorizing will be easier if the inner loop is complete, but when I do that (and remove the line mat=mat+t(mat)), I don't get the same answer.&amp;nbsp; Can you write the complete inner loop?&lt;/P&gt;&lt;P&gt;4) For vectorization, there are basic principles that often help. See &lt;A href="http://blogs.sas.com/content/iml/2013/05/15/vectorize-computations/"&gt;"How to vectorize computations in a matrix language"&lt;/A&gt;&amp;nbsp; If you answer 1-3, I think it will be easier to apply these prinicples.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Feb 2014 13:15:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165183#M1594</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2014-02-06T13:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165184#M1595</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Rick&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you. I have read several very interesting posts on your Blog.&lt;/P&gt;&lt;P&gt;1. I am trying to calculate the gradient of a more complex function. I do not want to post it here though. Maybe I simply need to do some more thinking to see the simplification.&lt;/P&gt;&lt;P&gt;2. You are right about that. This has been so form the beginning in my code. I unfortunately started with a transposed matrix. Although it is only a notation. I do not want to change all my code.&lt;/P&gt;&lt;P&gt;3. In fact I had an error there. I need to do the full loop.&lt;/P&gt;&lt;P&gt;4. I have seen that post, thank you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I realize that the above answers will probably not help you to answer my original question. I need to do some more thinking or maybe post the full function which I want to differentiate. Although I believe it is possible that my program cannot be simplified.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Feb 2014 17:09:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165184#M1595</guid>
      <dc:creator>bernhard</dc:creator>
      <dc:date>2014-02-06T17:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165185#M1596</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can get rid of the inner loop. I haven't figured out whether the outer loop can be simplified. The WEIGHTS matrix and the data[,i]-data[,j] computation make each computation dependent on a loop variable.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So is this is some sort of finite difference computation, and that's why we have data[,i]-data[,j]?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can we assume that the WEIGHTS matrix is positive? Your example fills it with random normal values, but that isn't usually the case for a matrix of that name.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Feb 2014 17:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165185#M1596</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2014-02-06T17:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165186#M1597</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you please tell me how to avoid the inner loop?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am investigating a Mahalanobis distance and try to minimize an estimation error depending on the input matrix of the Mahalanobis matrix.&lt;/P&gt;&lt;P&gt;The weights matrix is not positive.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 10:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165186#M1597</guid>
      <dc:creator>bernhard</dc:creator>
      <dc:date>2014-02-11T10:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165187#M1598</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sure, I was waiting for you to post the correct code with the full inner loop (#3 above).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you are computing a Mahalanibos-type matrix, you might be interested in this article: &lt;A href="http://blogs.sas.com/content/iml/2012/02/22/how-to-compute-mahalanobis-distance-in-sas/" title="http://blogs.sas.com/content/iml/2012/02/22/how-to-compute-mahalanobis-distance-in-sas/"&gt; How to compute Mahalanobis distance in SAS - The DO Loop&lt;/A&gt; It presents several tricks for efficiently computing weighted distance matrices.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 11 Feb 2014 14:31:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165187#M1598</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2014-02-11T14:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165188#M1599</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have edited the original post (i.e. the code section) a few days ago. Here is the loop&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;do j=1 to obs;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;&amp;nbsp; do i=1 to obs;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;&amp;nbsp; x=data[,i]-data[,j];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;&amp;nbsp; mat=mat+weights[i,j]*x*t(x);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;&amp;nbsp; end;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;end;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; background-color: #ffffff;"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Thanks for the link I will have a look at that post.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Feb 2014 07:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165188#M1599</guid>
      <dc:creator>bernhard</dc:creator>
      <dc:date>2014-02-12T07:49:35Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165189#M1600</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I think you can get rid of the inner loop by doing this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; do j = 1 to obs;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = data - data[, j];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mat = mat + x * diag(weights[ ,j]) * t(x);&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I can't see any way to avoid the outer loop.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Feb 2014 09:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165189#M1600</guid>
      <dc:creator>IanWakeling</dc:creator>
      <dc:date>2014-02-12T09:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: Vectorize Do Loop to increase performance</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165190#M1601</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sorry that I didn't see that you revised your original code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes, Ian's method will work, although it is slightly more efficient to use the # operator to avoid forming the diagonal matrix:&lt;/P&gt;&lt;P&gt;do j=1 to p;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; x=data-data[,j];&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; mat=mat+x*(weights[,j]#t(x));&lt;/P&gt;&lt;P&gt;end; &lt;/P&gt;&lt;P&gt;(For an explanation, see &lt;A href="http://blogs.sas.com/content/iml/2010/12/10/converting-between-correlation-and-covariance-matrices/" title="http://blogs.sas.com/content/iml/2010/12/10/converting-between-correlation-and-covariance-matrices/"&gt; Converting between correlation and covariance matrices - The DO Loop&lt;/A&gt; )&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you determine that the weights are positive, the you can use&lt;/P&gt;&lt;P&gt;do j=1 to p;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; x=(data-data[,j])#sqrt(t(weights[,j]));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; mat=mat + x*x`;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 12 Feb 2014 15:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Vectorize-Do-Loop-to-increase-performance/m-p/165190#M1601</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2014-02-12T15:26:55Z</dc:date>
    </item>
  </channel>
</rss>

