<?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 generate random 6 weights range (-1,1) with sum of absolute value equals 1 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729243#M226910</link>
    <description>&lt;P&gt;I want to simulate 1000 sets comprising 6 weights (w1, w2, w3, w4, w5, w6). Their values are uniformly distributed ranging from -1 to 1 (but their &lt;STRONG&gt;absolute&lt;/STRONG&gt; values are greater than 0.01. The sum of their &lt;STRONG&gt;absolute&lt;/STRONG&gt; values is added to 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please help?&lt;/P&gt;</description>
    <pubDate>Thu, 25 Mar 2021 21:41:24 GMT</pubDate>
    <dc:creator>rosiecao2509</dc:creator>
    <dc:date>2021-03-25T21:41:24Z</dc:date>
    <item>
      <title>generate random 6 weights range (-1,1) with sum of absolute value equals 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729243#M226910</link>
      <description>&lt;P&gt;I want to simulate 1000 sets comprising 6 weights (w1, w2, w3, w4, w5, w6). Their values are uniformly distributed ranging from -1 to 1 (but their &lt;STRONG&gt;absolute&lt;/STRONG&gt; values are greater than 0.01. The sum of their &lt;STRONG&gt;absolute&lt;/STRONG&gt; values is added to 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please help?&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 21:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729243#M226910</guid>
      <dc:creator>rosiecao2509</dc:creator>
      <dc:date>2021-03-25T21:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: generate random 6 weights range (-1,1) with sum of absolute value equals 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729265#M226926</link>
      <description>&lt;P&gt;The moment you require the sum of absolute values of &lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;1&lt;/FONT&gt;..w&lt;FONT size="1 2 3 4 5 6 7"&gt;6&lt;/FONT&gt;&lt;/STRONG&gt; to be 1, you can no longer expect each weight w to be uniformly distributed w~uniform(-1,1).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;True, you could distribute &lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;~uniform(-1,+1), excluding (-0.01,+0.01).&amp;nbsp; Actually you'd distribute absolute |&lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;|~uniform(0.01,+1), and then randomly generate a positive or negative sign.&amp;nbsp; Or to prevent any of&amp;nbsp;&lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;2&lt;/FONT&gt;&lt;/STRONG&gt; through &lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;6&lt;/FONT&gt;&lt;/STRONG&gt; being less than 0.01, you would uniformly distribute |&lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;|~uniform(0.01,0.95).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's say the absolute value of &lt;STRONG&gt;|w&lt;FONT size="1 2 3 4 5 6 7"&gt;1&lt;/FONT&gt;|&lt;/STRONG&gt; is 0.4.&amp;nbsp; Then you could uniformly distribute absolute &lt;STRONG&gt;|w&lt;FONT size="1 2 3 4 5 6 7"&gt;2&lt;/FONT&gt;|&lt;/STRONG&gt; over (0.01,0.55), i.e. (0.01, 0.95-0.4), and again randomly generate a sign.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could similarly generate&amp;nbsp;&lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;3&lt;/FONT&gt;&lt;/STRONG&gt; through &lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;5&lt;/FONT&gt;&lt;/STRONG&gt;, and finally directly calculate absolute &lt;STRONG&gt;|w&lt;FONT size="1 2 3 4 5 6 7"&gt;6&lt;/FONT&gt;|&lt;/STRONG&gt; from the others, and randomly give it a sign.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But clearly&amp;nbsp;the only weight distributed uniform(-1,1) is&amp;nbsp;&lt;STRONG&gt;w&lt;FONT size="1 2 3 4 5 6 7"&gt;1&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;&lt;FONT size="4"&gt;.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="3"&gt;So could you clarify?&lt;/FONT&gt;&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 00:35:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729265#M226926</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-26T00:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: generate random 6 weights range (-1,1) with sum of absolute value equals 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729465#M226997</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328613"&gt;@rosiecao2509&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As &lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461" target="_blank" rel="noopener"&gt;mkeintz&lt;/A&gt; has already pointed out, there is no probability distribution satisfying all your requirements. Indeed, note that for |w&lt;SUB&gt;i&lt;/SUB&gt;| (i=1, ..., 6) with a uniform distribution on [0.01, 1] you would have the expectation E(|w&lt;SUB&gt;1&lt;/SUB&gt;|+...+|w&lt;SUB&gt;6&lt;/SUB&gt;|) = 6*(0.01+1)/2 =&amp;nbsp;&lt;STRONG&gt;3.03&lt;/STRONG&gt;, not &lt;STRONG&gt;1&lt;/STRONG&gt; as per your requirement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would probably start with a &lt;A href="https://en.wikipedia.org/wiki/Dirichlet_distribution" target="_blank" rel="noopener"&gt;Dirichlet distribution&lt;/A&gt;, Dir(&lt;STRONG&gt;&lt;FONT face="symbol"&gt;a&lt;/FONT&gt;&lt;/STRONG&gt;). For example, consider the special case &lt;STRONG&gt;&lt;FONT face="symbol"&gt;a&lt;/FONT&gt;&lt;/STRONG&gt;=(1,1,1,1,1,1), which yields the &lt;EM&gt;uniform&lt;/EM&gt; distribution over the set of points with positive coordinates in six-dimensional space whose coordinates sum to 1. Once you have generated a random vector (x&lt;SUB&gt;1&lt;/SUB&gt;, ..., x&lt;SUB&gt;6&lt;/SUB&gt;) from this distribution, you easily get corresponding weights w&lt;SUB&gt;i&lt;/SUB&gt;: Just apply the linear transformation x&lt;SUB&gt;i&lt;/SUB&gt;&amp;nbsp;→ 0.01+0.94*x&lt;SUB&gt;i&lt;/SUB&gt; and generate a random sign.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is SAS code implementing this approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=i j);
call streaminit(27182818);
array w[6];
array x[6] _temporary_;
do i=1 to 1000;
  do j=1 to dim(w);
    x[j]=rand('expo');
  end;
  do j=1 to dim(w);
    w[j]=(2*rand('bern',0.5)-1)*(0.01+0.94*x[j]/sum(of x[*]));
  end;
  output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(Note that the exponential distribution with default parameter 1 is the same as the Gamma(1,1) distribution mentioned in the Wikipedia article.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In particular, this approach ensures that the six weights have the same probability distribution.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 19:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729465#M226997</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-26T19:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: generate random 6 weights range (-1,1) with sum of absolute value equals 1</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729499#M227015</link>
      <description>Thanks a lot Reinhard,&lt;BR /&gt;It works perfectly well. Thank you so much for your help.&lt;BR /&gt;&lt;BR /&gt;Wish you all the best.</description>
      <pubDate>Fri, 26 Mar 2021 21:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-random-6-weights-range-1-1-with-sum-of-absolute-value/m-p/729499#M227015</guid>
      <dc:creator>rosiecao2509</dc:creator>
      <dc:date>2021-03-26T21:26:28Z</dc:date>
    </item>
  </channel>
</rss>

