<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560</link>
    <title>MATLAB Central Newsreader - Help with Random Variable Generation</title>
    <description>Feed for thread: Help with Random Variable Generation</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 16 Jan 2009 17:44:01 -0500</pubDate>
      <title>Help with Random Variable Generation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560#622138</link>
      <author>Omega69 Omega69</author>
      <description>Hi everybody,&lt;br&gt;
I'm trying to generate a random variable which probability density function is:&lt;br&gt;
f(x)=1-|x|,    -1&amp;lt;=x&amp;lt;=1&lt;br&gt;
To do that, I have calculated cumulative distribution function, which is (if I have not made any mistake...):&lt;br&gt;
F(x)= 0          if         x&amp;lt;-1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x?/2+x+1/2       if      -1&amp;lt;=x&amp;lt;=0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-x?/2+x+1/2       if       0&amp;lt;=x&amp;lt;=-1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1                       if        x&amp;gt;1&lt;br&gt;
&lt;br&gt;
Now, I declare u=F(x) (where u is a uniform variable), and I try to isolate x to get F^-1(u), and then declare them in Matlab, but here is where I have problems:&lt;br&gt;
&lt;br&gt;
1) I know how to isolate x when -1&amp;lt;=x&amp;lt;=0, and I get x=sqrt(2*u)-1, but I don't know how to isolate it when 0&amp;lt;=x&amp;lt;=-1, any idea for this?&lt;br&gt;
&lt;br&gt;
2) And the other part is that I don't know how to write this into Matlab. I have try this:&lt;br&gt;
N=1000;&lt;br&gt;
u1=rand(N,1);&lt;br&gt;
u2=rand(N,2)&lt;br&gt;
X=(sqrt(2*u1)-1)*(u2(N,1)&amp;lt;0,5)+((the thing I don't know how to isolate))*(u2&amp;gt;0.5);&lt;br&gt;
&lt;br&gt;
But the results I think that are wrong.&lt;br&gt;
&lt;br&gt;
Anyone has any idea to get this correct?&lt;br&gt;
&lt;br&gt;
Thank you very much in advance!!</description>
    </item>
    <item>
      <pubDate>Fri, 16 Jan 2009 19:00:19 -0500</pubDate>
      <title>Re: Help with Random Variable Generation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560#622149</link>
      <author>Sadik </author>
      <description>&quot;Omega69 Omega69&quot; &amp;lt;Omega69@gmail.com&amp;gt; wrote in message &amp;lt;gkqh11$osq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi everybody,&lt;br&gt;
&amp;gt; I'm trying to generate a random variable which probability density function is:&lt;br&gt;
&amp;gt; f(x)=1-|x|,    -1&amp;lt;=x&amp;lt;=1&lt;br&gt;
&amp;gt; To do that, I have calculated cumulative distribution function, which is (if I have not made any mistake...):&lt;br&gt;
&amp;gt; F(x)= 0          if         x&amp;lt;-1&lt;br&gt;
&amp;gt;           x?/2+x+1/2       if      -1&amp;lt;=x&amp;lt;=0&lt;br&gt;
&amp;gt;          -x?/2+x+1/2       if       0&amp;lt;=x&amp;lt;=-1&lt;br&gt;
&amp;gt;           1                       if        x&amp;gt;1&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now, I declare u=F(x) (where u is a uniform variable), and I try to isolate x to get F^-1(u), and then declare them in Matlab, but here is where I have problems:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 1) I know how to isolate x when -1&amp;lt;=x&amp;lt;=0, and I get x=sqrt(2*u)-1, but I don't know how to isolate it when 0&amp;lt;=x&amp;lt;=-1, any idea for this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 2) And the other part is that I don't know how to write this into Matlab. I have try this:&lt;br&gt;
&amp;gt; N=1000;&lt;br&gt;
&amp;gt; u1=rand(N,1);&lt;br&gt;
&amp;gt; u2=rand(N,2)&lt;br&gt;
&amp;gt; X=(sqrt(2*u1)-1)*(u2(N,1)&amp;lt;0,5)+((the thing I don't know how to isolate))*(u2&amp;gt;0.5);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But the results I think that are wrong.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Anyone has any idea to get this correct?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you very much in advance!!&lt;br&gt;
&lt;br&gt;
Hello Omega69,&lt;br&gt;
&lt;br&gt;
1) You have already found the function so the inverse for the positive part will be &lt;br&gt;
&lt;br&gt;
x = 1 - sqrt(2*(1-y))&lt;br&gt;
&lt;br&gt;
This is because x-1 will be negative in the range 0 &amp;lt; x &amp;lt; 1. So you choose the negative square root.&lt;br&gt;
&lt;br&gt;
2) Your inverse is one-to-one. So you don't need to generate a second uniform random variable.  Your code should be&lt;br&gt;
&lt;br&gt;
N=1000;&lt;br&gt;
u1=rand(N,1);&lt;br&gt;
&lt;br&gt;
X=(sqrt(2*u1)-1).*(u1&amp;lt;0.5)+(1 - sqrt(2*(1-u1))).*(u1&amp;gt;=0.5);&lt;br&gt;
&lt;br&gt;
It seems to work. Hope this helps.</description>
    </item>
    <item>
      <pubDate>Fri, 16 Jan 2009 20:49:02 -0500</pubDate>
      <title>Re: Help with Random Variable Generation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560#622176</link>
      <author>Roger Stafford</author>
      <description>&quot;Omega69 Omega69&quot; &amp;lt;Omega69@gmail.com&amp;gt; wrote in message &amp;lt;gkqh11$osq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I'm trying to generate a random variable which probability density function is:&lt;br&gt;
&amp;gt; f(x)=1-|x|,    -1&amp;lt;=x&amp;lt;=1&lt;br&gt;
&amp;gt; ......&lt;br&gt;
&lt;br&gt;
&amp;nbsp;x = sum(rand(1,2))-1;&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Fri, 16 Jan 2009 21:09:02 -0500</pubDate>
      <title>Re: Help with Random Variable Generation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560#622181</link>
      <author>Bruno Luong</author>
      <description>&quot;Roger Stafford&quot; &amp;lt;ellieandrogerxyzzy@mindspring.com.invalid&amp;gt; wrote in message &amp;lt;gkqrru$fht$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;  x = sum(rand(1,2))-1;&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Clever!&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 16 Jan 2009 21:12:01 -0500</pubDate>
      <title>Re: Help with Random Variable Generation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560#622183</link>
      <author>Bruno Luong</author>
      <description>Inspired by Roger!&lt;br&gt;
&lt;br&gt;
diff(rand(2,1));&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 16 Jan 2009 21:43:02 -0500</pubDate>
      <title>Re: Help with Random Variable Generation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560#622186</link>
      <author>Roger Stafford</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;gkqt71$jom$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Inspired by Roger!&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; diff(rand(2,1));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
That's even better, Bruno!&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Sat, 17 Jan 2009 13:00:04 -0500</pubDate>
      <title>Re: Help with Random Variable Generation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/242560#622247</link>
      <author>Bongo </author>
      <description>Thanks everyone for your help! I've already solved my problem!!&lt;br&gt;
Thanks!!! :)</description>
    </item>
  </channel>
</rss>

