<?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: Easiest Way to Sum over Product on 2D Data in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531088#M4534</link>
    <description>&lt;P&gt;If you provide data and the expect results, I and other experts will think about it.&lt;/P&gt;</description>
    <pubDate>Tue, 29 Jan 2019 17:38:20 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2019-01-29T17:38:20Z</dc:date>
    <item>
      <title>Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/530940#M4527</link>
      <description>&lt;P&gt;I need to find the most efficient and fastest way to compute :&lt;/P&gt;&lt;P&gt;{sum over t} ({product over j} (X[j-1])*Y[t])&lt;/P&gt;&lt;P&gt;For a high enough j and t and with a couple of more variables in the equation this needs a lot of variables(columns) to be created which seem inefficient. I was thinking that I can define array with macro variables for X(j), Y(t) and such vars, but if it's fastest or is there a better way with PROC IML or any other trick. Any suggestions?&lt;/P&gt;&lt;P&gt;Thanks a lot in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;P.S.-X, Y are numerical variables between (0,1). Also 't' varies from 1 to n and 'j' varies from 1 to 't'.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 16:03:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/530940#M4527</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2019-01-29T16:03:00Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531017#M4528</link>
      <description>&lt;P&gt;What does X and Y look like here?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 15:34:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531017#M4528</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-01-29T15:34:11Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531047#M4529</link>
      <description>X, Y are numerical variables between (0,1). Also 't' varies from 1 to n and 'j' varies from 1 to 't'.</description>
      <pubDate>Tue, 29 Jan 2019 16:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531047#M4529</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2019-01-29T16:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531056#M4531</link>
      <description>&lt;P&gt;It would be best to provide example data and the result you expect. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When t=1, do you skip the product?&amp;nbsp; If so, then t ranges from 2 to N.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 16:12:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531056#M4531</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-29T16:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531073#M4532</link>
      <description>&lt;P&gt;I'm assuming that the outer summation will go from t=2..N. Otherwise, you can modify the code accordingly. The key to an efficient implementation is to recognize that the formula is separable and factors into a cumulative product and the powers of the elements of Y:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
x = (1:4)`/5;
y = (2:5)`/5;

N = nrow(y);

/* naive loop: sum of product.
   For testing purposes only! */
s = 0;
do t = 2 to N;
   p = 1;
   do j = 2 to t;
      p = p* (x[j-1]#y[t]);
   end;
   s = s + p;
end;
print s;

/* efficient computation: problem factors into the product of X
   and sum of powers of Y */
xT = cuprod(x);              /* cumulative product */
yT = y##(0:N-1)`;           /* powers of Y */
v = xT[1:N-1] # yT[2:N]; /* elementwise product */
s2 = sum(v);
print s2;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Jan 2019 16:49:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531073#M4532</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-29T16:49:20Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531081#M4533</link>
      <description>Thanks Rick. I have read so many papers of yours (still haven't learn). Thank you so much. I have one additional request though. Once CUPROD reaches end of a row, I want it to stop and start over from the next row instead of carrying the product from previous row. Any quick tip?</description>
      <pubDate>Tue, 29 Jan 2019 17:12:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531081#M4533</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2019-01-29T17:12:24Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531088#M4534</link>
      <description>&lt;P&gt;If you provide data and the expect results, I and other experts will think about it.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 17:38:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531088#M4534</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-29T17:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531145#M4535</link>
      <description>&lt;P&gt;Suppose a person can belong to any of the RACEs with the probability of PROB_RACE. And marginal probability of dying in years 1 to 3 is given(marginal survival will be 1-PROB_DYING):&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;RACE&lt;/TD&gt;&lt;TD&gt;PROB_RACE&lt;/TD&gt;&lt;TD&gt;PROB_DYING_YR1&lt;/TD&gt;&lt;TD&gt;PROB_DYING_YR2&lt;/TD&gt;&lt;TD&gt;PROB_DYING_YR3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;0.26&lt;/TD&gt;&lt;TD&gt;0.1&lt;/TD&gt;&lt;TD&gt;0.2&lt;/TD&gt;&lt;TD&gt;0.3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;0.35&lt;/TD&gt;&lt;TD&gt;0.16&lt;/TD&gt;&lt;TD&gt;0.25&lt;/TD&gt;&lt;TD&gt;0.45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;TD&gt;0.23&lt;/TD&gt;&lt;TD&gt;0.23&lt;/TD&gt;&lt;TD&gt;0.34&lt;/TD&gt;&lt;TD&gt;0.17&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;D&lt;/TD&gt;&lt;TD&gt;0.16&lt;/TD&gt;&lt;TD&gt;0.18&lt;/TD&gt;&lt;TD&gt;0.17&lt;/TD&gt;&lt;TD&gt;0.14&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the person belonging to RACE A will have a probability of survival after 3 years, call it SURV3_A = (1-0.1)*(1-0.2)*(1-0.3).&lt;/P&gt;&lt;P&gt;Similarly, SURV3_B=(1-0.16)*(1-0.25)*(1-0.45) and so on...&lt;/P&gt;&lt;P&gt;And a random person will have a probability of survival after 3 years as : PROB_A*SURV3_A+PROB_B*SURV3_B+PROB_C*SURV3_C+PROB_D*SURV3_D.&lt;/P&gt;&lt;P&gt;I was thinking of a generalization to calculate probability of survival of any random person after 'T' years when there can be 'N' possible RACEs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can think of taking one row at a time,using CUPROD, then summing across columns which seem inefficient, so wondering if any easy way?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 19:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531145#M4535</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2019-01-29T19:45:10Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531157#M4537</link>
      <description>&lt;P&gt;Aha! Now we see what you are trying to do! Much clearer and much simpler. I think the "trick" you are looking for is to &lt;A href="https://blogs.sas.com/content/iml/2011/06/06/use-subscript-reduction-operators.html" target="_self"&gt;use a subscript reduction operator for each row and across the columns:&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
p_Race = {0.26, 0.35, 0.23, 0.16};
p_Die = {
0.1	0.2	0.3,
0.16	0.25	0.45,
0.23	0.34	0.17,
0.18	0.17	0.14 };

p_surv = p_Race # (1 - p_Die)[, #];
print p_surv;&lt;/CODE&gt;&lt;/PRE&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Jan 2019 20:17:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531157#M4537</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-29T20:17:40Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531160#M4538</link>
      <description>Great thanks! Wish I could think like you! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Elegant!!</description>
      <pubDate>Tue, 29 Jan 2019 20:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531160#M4538</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2019-01-29T20:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531302#M4545</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;, sorry for being superxcited yesterday and not verifying everything. The trick you suggested gives only final column:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0.504&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.3465&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.421806&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.585316&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Is there a way to get all the columns in the range, like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;0.9&lt;/TD&gt;&lt;TD&gt;0.72&lt;/TD&gt;&lt;TD&gt;0.504&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.84&lt;/TD&gt;&lt;TD&gt;0.63&lt;/TD&gt;&lt;TD&gt;0.3465&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.77&lt;/TD&gt;&lt;TD&gt;0.5082&lt;/TD&gt;&lt;TD&gt;0.421806&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;0.82&lt;/TD&gt;&lt;TD&gt;0.6806&lt;/TD&gt;&lt;TD&gt;0.585316&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Thanks a lot in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 09:49:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531302#M4545</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2019-01-30T09:49:02Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531315#M4546</link>
      <description>&lt;P&gt;Where are you getting those numbers? Those are not the results&amp;nbsp;of the program I sent.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, to answer your question, it sounds like you want to use the matrix whose columns&amp;nbsp;contain the cumulative probabilities.of&amp;nbsp;1 - p_Die.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
p_Race = {0.26, 0.35, 0.23, 0.16};
p_Die = {
0.1	0.2	0.3,
0.16	0.25	0.45,
0.23	0.34	0.17,
0.18	0.17	0.14 };

/* form matrix of cumulative products of columns */
p_cumul = 1 - p_Die;
do j = 2 to ncol(p_cumul);
   p_cumul[,j] = p_cumul[,j] # p_cumul[,j-1]; 
end;

M = p_Race # p_cumul;
print M;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 10:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531315#M4546</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-30T10:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531318#M4548</link>
      <description>&lt;P&gt;This same &lt;A href="https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Efficient-Cumulative-Sum-over-Cumulative-Product/m-p/531301/highlight/false#M4544" target="_self"&gt;question is cross=posted.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 10:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531318#M4548</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2019-01-30T10:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Easiest Way to Sum over Product on 2D Data</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531320#M4549</link>
      <description>&lt;P&gt;Sorry, I only gave calcs for (1-X) part. Other parts were working properly, so I guessed if I get the solution to this one, it'll be solved. My apologies.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 10:39:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Easiest-Way-to-Sum-over-Product-on-2D-Data/m-p/531320#M4549</guid>
      <dc:creator>thepushkarsingh</dc:creator>
      <dc:date>2019-01-30T10:39:09Z</dc:date>
    </item>
  </channel>
</rss>

