<?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: How do I vectorize a DO loop - static markov chain (SAS/IML) in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281137#M2885</link>
    <description>&lt;P&gt;This is an iterative computation, so you can't avoid the DO loop. If you are only interested in the asymptotic steady-state solution, there are ways to find that directly by using eigenvalues, but if you want the forecast time series then you have to compute each step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a way to make your program more efficient. Currently you are computing a matrix power (trans**i), which is an expensive computation at each stepo of the loop. At the i_th step you are repeating all the computations from the previous step, plus one more matrix multiplication.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you can reduce this to N matrix-vector computations, which is much faster. &amp;nbsp;Inside the loop, merely update the previous computation, like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;v = input1;
do i = 1 to ForecastMonths ;
   v = v * trans;
   results[i, ] = v;
end ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It isn't clear whether you want the first row to be the 0th iteration or the result after the first iteration. I've done the latter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not only is this method faster, but it is numerically more stable because you never form large powers of matrices.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jun 2016 15:24:24 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2016-06-29T15:24:24Z</dc:date>
    <item>
      <title>How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281111#M2884</link>
      <description>&lt;P&gt;I am new to vectorizing generally and SAS/IML though have quickly realised the potential&amp;nbsp;program and computational&amp;nbsp;savings. &amp;nbsp;I have a specific problem I have set out below and have searched the internet, to no avail. &amp;nbsp;I have been inspired by the solution to another problem&amp;nbsp;in&amp;nbsp;&lt;A href="http://analyticstraining.com/2013/using-vectorization-in-sas-iml-to-improve-code-performance/" target="_blank"&gt;this link&lt;/A&gt;&amp;nbsp;-&amp;nbsp;in which a series calculation is vectorized from a DO loop and read a lot of Rick Wicklin's blog which have helped massively.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Problem:&amp;nbsp;&lt;/STRONG&gt;I have a transition matrix (e.g.&amp;nbsp;4x4) and I want to forecast the volume movement between the 4&amp;nbsp;states over&amp;nbsp;N months. The final result will&amp;nbsp;be a Nx4 matrix with the forecasted volumes in each state. &amp;nbsp;I can achieve this using a DO loop but I am hopeful that someone can help me find a vectorized solution, as I may have to run similar vectorize this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The attached files contains an example of my current approach (using a DO loop).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 14:14:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281111#M2884</guid>
      <dc:creator>Andrew_</dc:creator>
      <dc:date>2016-06-29T14:14:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281137#M2885</link>
      <description>&lt;P&gt;This is an iterative computation, so you can't avoid the DO loop. If you are only interested in the asymptotic steady-state solution, there are ways to find that directly by using eigenvalues, but if you want the forecast time series then you have to compute each step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is a way to make your program more efficient. Currently you are computing a matrix power (trans**i), which is an expensive computation at each stepo of the loop. At the i_th step you are repeating all the computations from the previous step, plus one more matrix multiplication.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, you can reduce this to N matrix-vector computations, which is much faster. &amp;nbsp;Inside the loop, merely update the previous computation, like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;v = input1;
do i = 1 to ForecastMonths ;
   v = v * trans;
   results[i, ] = v;
end ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It isn't clear whether you want the first row to be the 0th iteration or the result after the first iteration. I've done the latter.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not only is this method faster, but it is numerically more stable because you never form large powers of matrices.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 15:24:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281137#M2885</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-06-29T15:24:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281141#M2886</link>
      <description>&lt;P&gt;Thank you very much for the&amp;nbsp;prompt response. &amp;nbsp;Reading your (and other) other blog entries I was perhaps being overly hopeful there was a way to vectorize everything in my life, so it is helpful to get confirmation where a loop is required.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, thank you for the advice regarding the vector/matrix multiplication, as opposed to the matrix-power operation.&amp;nbsp;&lt;img id="manhappy" class="emoticon emoticon-manhappy" src="https://communities.sas.com/i/smilies/16x16_man-happy.png" alt="Man Happy" title="Man Happy" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 15:30:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281141#M2886</guid>
      <dc:creator>Andrew_</dc:creator>
      <dc:date>2016-06-29T15:30:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281169#M2887</link>
      <description>&lt;P&gt;A good rule of thumb is to look for a vectorized approach when your loop is over the rows or columns of a matrix or vector.&lt;/P&gt;
&lt;P&gt;In this case, the loop is an independent of the size of the matrix.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another possible opportunity for vectorization is when you are performin an operation on a VECTOR&amp;nbsp;within a loop, and the (i+1)st computation is INDEPENDENT&amp;nbsp;of the result of the i_th computation. &amp;nbsp;Then you might be able to pack all those vectors into a matrix and do one columnwise or rowwise operation on the matrix. &amp;nbsp;Alas, that is not the case here: the&amp;nbsp;&lt;SPAN&gt;(i+1)st computation depends on &lt;/SPAN&gt;&lt;SPAN&gt;the &lt;/SPAN&gt;&lt;SPAN&gt;result of the i_th computation. &lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2016 17:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281169#M2887</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-06-29T17:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281378#M2888</link>
      <description>&lt;P&gt;Thanks Rick. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understood from your other blog posts that the dependency of the (i+1)th observation on the ith means that vectorization wouldn't be possible.&amp;nbsp;However, it was when someone showed me that I can get to the ith iteration by raising the transition matrix to the ith&amp;nbsp;power that I thought there might be a way to do this in one step as I only require&amp;nbsp;the initial vector and&amp;nbsp;the transition matrix**i. &amp;nbsp;However, I note your point about matrix power being more expensive than matrix multiplication.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is disappointing. &amp;nbsp;However, I'm on a mission as I'm working with some heavily looped code at the moment and there are some definite wins for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2016 09:06:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/281378#M2888</guid>
      <dc:creator>Andrew_</dc:creator>
      <dc:date>2016-06-30T09:06:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/282893#M2890</link>
      <description>&lt;P&gt;I wrote down some of my thoughts on how to iterate&amp;nbsp;a Markov transition matrix efficiently in SAS/IML. See&lt;/P&gt;
&lt;P&gt;&lt;A title="Permalink to Markov transition matrices in SAS/IML" href="http://blogs.sas.com/content/iml/2016/07/07/markov-transition-matrices-sasiml.html" rel="bookmark" target="_blank"&gt;Markov transition matrices in SAS/IML&lt;/A&gt;&lt;/P&gt;
&lt;DIV id="wrapper" class="hfeed"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Fri, 08 Jul 2016 09:57:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/282893#M2890</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-07-08T09:57:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/285095#M2907</link>
      <description>&lt;P&gt;For future reference, I wrote up some of these ideas and related concepts for analyzing Markov chains:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2016/07/07/markov-transition-matrices-sasiml.html" target="_self"&gt;Markov Transition Matrices in SAS/IML&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.sas.com/content/iml/2016/07/13/absorbing-markov-chains-in-sas.html" target="_self"&gt;Absorbing Markov Chains in SAS&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 17 Jul 2016 10:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/285095#M2907</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2016-07-17T10:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I vectorize a DO loop - static markov chain (SAS/IML)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/285232#M2908</link>
      <description>Thanks for sharing these Rick. I have been following your blog with interest!</description>
      <pubDate>Mon, 18 Jul 2016 13:54:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/How-do-I-vectorize-a-DO-loop-static-markov-chain-SAS-IML/m-p/285232#M2908</guid>
      <dc:creator>Andrew_</dc:creator>
      <dc:date>2016-07-18T13:54:39Z</dc:date>
    </item>
  </channel>
</rss>

