Simulating rnorm() using runif()












3












$begingroup$


I am trying to 'simulate' rnorm() using only runif().



I don't know if I should do:



sqrt(-2*log(U1))*cos(U2)


or



sqrt(-2*log(U1))*sin(U2)


Where U1 is a runif(0,1) and U2 runif(0,6.28)



I do not know if I should do it using cos or sin, or is it that I need to sample from one and the other consecutively? What is the mathematical logic behind it?










share|cite|improve this question











$endgroup$












  • $begingroup$
    You will want to investigate the Probability Integral Transform.
    $endgroup$
    – StatsStudent
    Mar 14 at 15:28






  • 1




    $begingroup$
    Because $cos(U_2)$ is the horizontal coordinate of a point uniformly distributed on the unit circle and $sin(U_2)$ is its vertical coordinate, notice that the symmetry of the circle under a switch of coordinates implies $cos(U_2)$ and $sin(U_2)$ are identically distributed. Thus, your two methods are equivalent (assuming, anyway, that you replace "6.28" by $2pi$).
    $endgroup$
    – whuber
    Mar 14 at 18:44
















3












$begingroup$


I am trying to 'simulate' rnorm() using only runif().



I don't know if I should do:



sqrt(-2*log(U1))*cos(U2)


or



sqrt(-2*log(U1))*sin(U2)


Where U1 is a runif(0,1) and U2 runif(0,6.28)



I do not know if I should do it using cos or sin, or is it that I need to sample from one and the other consecutively? What is the mathematical logic behind it?










share|cite|improve this question











$endgroup$












  • $begingroup$
    You will want to investigate the Probability Integral Transform.
    $endgroup$
    – StatsStudent
    Mar 14 at 15:28






  • 1




    $begingroup$
    Because $cos(U_2)$ is the horizontal coordinate of a point uniformly distributed on the unit circle and $sin(U_2)$ is its vertical coordinate, notice that the symmetry of the circle under a switch of coordinates implies $cos(U_2)$ and $sin(U_2)$ are identically distributed. Thus, your two methods are equivalent (assuming, anyway, that you replace "6.28" by $2pi$).
    $endgroup$
    – whuber
    Mar 14 at 18:44














3












3








3





$begingroup$


I am trying to 'simulate' rnorm() using only runif().



I don't know if I should do:



sqrt(-2*log(U1))*cos(U2)


or



sqrt(-2*log(U1))*sin(U2)


Where U1 is a runif(0,1) and U2 runif(0,6.28)



I do not know if I should do it using cos or sin, or is it that I need to sample from one and the other consecutively? What is the mathematical logic behind it?










share|cite|improve this question











$endgroup$




I am trying to 'simulate' rnorm() using only runif().



I don't know if I should do:



sqrt(-2*log(U1))*cos(U2)


or



sqrt(-2*log(U1))*sin(U2)


Where U1 is a runif(0,1) and U2 runif(0,6.28)



I do not know if I should do it using cos or sin, or is it that I need to sample from one and the other consecutively? What is the mathematical logic behind it?







r normalization






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Mar 14 at 16:51









masoud

675




675










asked Mar 14 at 14:13









Chicago1988Chicago1988

162




162












  • $begingroup$
    You will want to investigate the Probability Integral Transform.
    $endgroup$
    – StatsStudent
    Mar 14 at 15:28






  • 1




    $begingroup$
    Because $cos(U_2)$ is the horizontal coordinate of a point uniformly distributed on the unit circle and $sin(U_2)$ is its vertical coordinate, notice that the symmetry of the circle under a switch of coordinates implies $cos(U_2)$ and $sin(U_2)$ are identically distributed. Thus, your two methods are equivalent (assuming, anyway, that you replace "6.28" by $2pi$).
    $endgroup$
    – whuber
    Mar 14 at 18:44


















  • $begingroup$
    You will want to investigate the Probability Integral Transform.
    $endgroup$
    – StatsStudent
    Mar 14 at 15:28






  • 1




    $begingroup$
    Because $cos(U_2)$ is the horizontal coordinate of a point uniformly distributed on the unit circle and $sin(U_2)$ is its vertical coordinate, notice that the symmetry of the circle under a switch of coordinates implies $cos(U_2)$ and $sin(U_2)$ are identically distributed. Thus, your two methods are equivalent (assuming, anyway, that you replace "6.28" by $2pi$).
    $endgroup$
    – whuber
    Mar 14 at 18:44
















$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
Mar 14 at 15:28




$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
Mar 14 at 15:28




1




1




$begingroup$
Because $cos(U_2)$ is the horizontal coordinate of a point uniformly distributed on the unit circle and $sin(U_2)$ is its vertical coordinate, notice that the symmetry of the circle under a switch of coordinates implies $cos(U_2)$ and $sin(U_2)$ are identically distributed. Thus, your two methods are equivalent (assuming, anyway, that you replace "6.28" by $2pi$).
$endgroup$
– whuber
Mar 14 at 18:44




$begingroup$
Because $cos(U_2)$ is the horizontal coordinate of a point uniformly distributed on the unit circle and $sin(U_2)$ is its vertical coordinate, notice that the symmetry of the circle under a switch of coordinates implies $cos(U_2)$ and $sin(U_2)$ are identically distributed. Thus, your two methods are equivalent (assuming, anyway, that you replace "6.28" by $2pi$).
$endgroup$
– whuber
Mar 14 at 18:44










2 Answers
2






active

oldest

votes


















5












$begingroup$

It looks like you are trying to use the Box-Muller transform.



The method is to use $U_1, U_2$ which are independently distribution Uniform(0,1). Then



$Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2) $



and



$Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2) $



are a pair of independent $N(0,1)$.



The derivation comes from using polar coordinates, but to be honest the exact steps escape me at the moment (see link if you are interested).






share|cite|improve this answer











$endgroup$





















    2












    $begingroup$

    Let $N_1,N_2$ be independent $N(0,1)$ random variates. $(N_1,N_2)$ defines a point in Cartesian coordinates. We transform to polar by



    $$ N_1 = R cos theta$$
    $$ N_2 = R sin theta$$



    This transformation is one to one and has continuous derivatives. So we can derive the joint distribution $f_{R,theta}(r,theta)$ by using



    $$f_{R,theta}(r,theta) = f_{N_1,N_2}(n_1,n_2)|J^{-1}|$$



    where $J$ is the Jacobian of the transformation and



    $$ J^{-1}(r,theta )=
    {begin{bmatrix}{dfrac {partial n_1}{partial r}}{dfrac {partial n_1}{partial theta }}\{dfrac {partial n_2}{partial r}}{dfrac {partial n_2}{partial theta }}end{bmatrix}}
    = begin{bmatrix}cos theta & -rsin theta \sin theta & rcos theta end{bmatrix}$$



    Hence $$ |J^{-1}| = r cos^2 theta + r sin^2 theta = r $$



    and



    $$ f_{R,theta}(r,theta) = r frac{1}{2pi} e^{-frac{1}{2}(n_1^2+n_2^2)} = boxed{frac{r}{2 pi}e^{-frac{1}{2}(r^2)}} qquad 0 leq theta leq 2 pi, 0 leq r leq infty $$



    And we see $theta sim unif[0,2pi]$ (since $f_{Theta}(theta) = frac{1}{2pi}$ ) and $R^2 sim exp(1/2)$



    Hence, to simulate $Theta$ we simply take $2 pi U_2$ where $$U_2 sim unif[0,1]$$ and to simulate $R$ we can take $$-2ln U_1$$ where $U_1 sim unif[0,1]$. ( to see why find the density of $X=-ln U, U sim unif[0,1]$).



    So, Box and Muller simply inverted $N_1= R cos theta$, $N_2= R sin theta$ and moved from $(R,Theta)$ to $(N_1,N_2)$ by simulating $Theta$ from $2 pi U_2$, and an independent $R$ from $ sqrt{- 2 ln U_1}$



    explicitly



    $$Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2)$$



    $$Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2)$$



    And that is that is the mathematical logic behind it.





    p.s. As I don't know whether it is clear through the mathematical justification (I believe it is, but you may have been lost in details), note you need a pair of drawings from the uniform to get a pair of normals






    share|cite|improve this answer











    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "65"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f397505%2fsimulating-rnorm-using-runif%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5












      $begingroup$

      It looks like you are trying to use the Box-Muller transform.



      The method is to use $U_1, U_2$ which are independently distribution Uniform(0,1). Then



      $Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2) $



      and



      $Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2) $



      are a pair of independent $N(0,1)$.



      The derivation comes from using polar coordinates, but to be honest the exact steps escape me at the moment (see link if you are interested).






      share|cite|improve this answer











      $endgroup$


















        5












        $begingroup$

        It looks like you are trying to use the Box-Muller transform.



        The method is to use $U_1, U_2$ which are independently distribution Uniform(0,1). Then



        $Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2) $



        and



        $Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2) $



        are a pair of independent $N(0,1)$.



        The derivation comes from using polar coordinates, but to be honest the exact steps escape me at the moment (see link if you are interested).






        share|cite|improve this answer











        $endgroup$
















          5












          5








          5





          $begingroup$

          It looks like you are trying to use the Box-Muller transform.



          The method is to use $U_1, U_2$ which are independently distribution Uniform(0,1). Then



          $Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2) $



          and



          $Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2) $



          are a pair of independent $N(0,1)$.



          The derivation comes from using polar coordinates, but to be honest the exact steps escape me at the moment (see link if you are interested).






          share|cite|improve this answer











          $endgroup$



          It looks like you are trying to use the Box-Muller transform.



          The method is to use $U_1, U_2$ which are independently distribution Uniform(0,1). Then



          $Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2) $



          and



          $Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2) $



          are a pair of independent $N(0,1)$.



          The derivation comes from using polar coordinates, but to be honest the exact steps escape me at the moment (see link if you are interested).







          share|cite|improve this answer














          share|cite|improve this answer



          share|cite|improve this answer








          edited Mar 14 at 18:15

























          answered Mar 14 at 14:58









          Cliff ABCliff AB

          13.6k12567




          13.6k12567

























              2












              $begingroup$

              Let $N_1,N_2$ be independent $N(0,1)$ random variates. $(N_1,N_2)$ defines a point in Cartesian coordinates. We transform to polar by



              $$ N_1 = R cos theta$$
              $$ N_2 = R sin theta$$



              This transformation is one to one and has continuous derivatives. So we can derive the joint distribution $f_{R,theta}(r,theta)$ by using



              $$f_{R,theta}(r,theta) = f_{N_1,N_2}(n_1,n_2)|J^{-1}|$$



              where $J$ is the Jacobian of the transformation and



              $$ J^{-1}(r,theta )=
              {begin{bmatrix}{dfrac {partial n_1}{partial r}}{dfrac {partial n_1}{partial theta }}\{dfrac {partial n_2}{partial r}}{dfrac {partial n_2}{partial theta }}end{bmatrix}}
              = begin{bmatrix}cos theta & -rsin theta \sin theta & rcos theta end{bmatrix}$$



              Hence $$ |J^{-1}| = r cos^2 theta + r sin^2 theta = r $$



              and



              $$ f_{R,theta}(r,theta) = r frac{1}{2pi} e^{-frac{1}{2}(n_1^2+n_2^2)} = boxed{frac{r}{2 pi}e^{-frac{1}{2}(r^2)}} qquad 0 leq theta leq 2 pi, 0 leq r leq infty $$



              And we see $theta sim unif[0,2pi]$ (since $f_{Theta}(theta) = frac{1}{2pi}$ ) and $R^2 sim exp(1/2)$



              Hence, to simulate $Theta$ we simply take $2 pi U_2$ where $$U_2 sim unif[0,1]$$ and to simulate $R$ we can take $$-2ln U_1$$ where $U_1 sim unif[0,1]$. ( to see why find the density of $X=-ln U, U sim unif[0,1]$).



              So, Box and Muller simply inverted $N_1= R cos theta$, $N_2= R sin theta$ and moved from $(R,Theta)$ to $(N_1,N_2)$ by simulating $Theta$ from $2 pi U_2$, and an independent $R$ from $ sqrt{- 2 ln U_1}$



              explicitly



              $$Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2)$$



              $$Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2)$$



              And that is that is the mathematical logic behind it.





              p.s. As I don't know whether it is clear through the mathematical justification (I believe it is, but you may have been lost in details), note you need a pair of drawings from the uniform to get a pair of normals






              share|cite|improve this answer











              $endgroup$


















                2












                $begingroup$

                Let $N_1,N_2$ be independent $N(0,1)$ random variates. $(N_1,N_2)$ defines a point in Cartesian coordinates. We transform to polar by



                $$ N_1 = R cos theta$$
                $$ N_2 = R sin theta$$



                This transformation is one to one and has continuous derivatives. So we can derive the joint distribution $f_{R,theta}(r,theta)$ by using



                $$f_{R,theta}(r,theta) = f_{N_1,N_2}(n_1,n_2)|J^{-1}|$$



                where $J$ is the Jacobian of the transformation and



                $$ J^{-1}(r,theta )=
                {begin{bmatrix}{dfrac {partial n_1}{partial r}}{dfrac {partial n_1}{partial theta }}\{dfrac {partial n_2}{partial r}}{dfrac {partial n_2}{partial theta }}end{bmatrix}}
                = begin{bmatrix}cos theta & -rsin theta \sin theta & rcos theta end{bmatrix}$$



                Hence $$ |J^{-1}| = r cos^2 theta + r sin^2 theta = r $$



                and



                $$ f_{R,theta}(r,theta) = r frac{1}{2pi} e^{-frac{1}{2}(n_1^2+n_2^2)} = boxed{frac{r}{2 pi}e^{-frac{1}{2}(r^2)}} qquad 0 leq theta leq 2 pi, 0 leq r leq infty $$



                And we see $theta sim unif[0,2pi]$ (since $f_{Theta}(theta) = frac{1}{2pi}$ ) and $R^2 sim exp(1/2)$



                Hence, to simulate $Theta$ we simply take $2 pi U_2$ where $$U_2 sim unif[0,1]$$ and to simulate $R$ we can take $$-2ln U_1$$ where $U_1 sim unif[0,1]$. ( to see why find the density of $X=-ln U, U sim unif[0,1]$).



                So, Box and Muller simply inverted $N_1= R cos theta$, $N_2= R sin theta$ and moved from $(R,Theta)$ to $(N_1,N_2)$ by simulating $Theta$ from $2 pi U_2$, and an independent $R$ from $ sqrt{- 2 ln U_1}$



                explicitly



                $$Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2)$$



                $$Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2)$$



                And that is that is the mathematical logic behind it.





                p.s. As I don't know whether it is clear through the mathematical justification (I believe it is, but you may have been lost in details), note you need a pair of drawings from the uniform to get a pair of normals






                share|cite|improve this answer











                $endgroup$
















                  2












                  2








                  2





                  $begingroup$

                  Let $N_1,N_2$ be independent $N(0,1)$ random variates. $(N_1,N_2)$ defines a point in Cartesian coordinates. We transform to polar by



                  $$ N_1 = R cos theta$$
                  $$ N_2 = R sin theta$$



                  This transformation is one to one and has continuous derivatives. So we can derive the joint distribution $f_{R,theta}(r,theta)$ by using



                  $$f_{R,theta}(r,theta) = f_{N_1,N_2}(n_1,n_2)|J^{-1}|$$



                  where $J$ is the Jacobian of the transformation and



                  $$ J^{-1}(r,theta )=
                  {begin{bmatrix}{dfrac {partial n_1}{partial r}}{dfrac {partial n_1}{partial theta }}\{dfrac {partial n_2}{partial r}}{dfrac {partial n_2}{partial theta }}end{bmatrix}}
                  = begin{bmatrix}cos theta & -rsin theta \sin theta & rcos theta end{bmatrix}$$



                  Hence $$ |J^{-1}| = r cos^2 theta + r sin^2 theta = r $$



                  and



                  $$ f_{R,theta}(r,theta) = r frac{1}{2pi} e^{-frac{1}{2}(n_1^2+n_2^2)} = boxed{frac{r}{2 pi}e^{-frac{1}{2}(r^2)}} qquad 0 leq theta leq 2 pi, 0 leq r leq infty $$



                  And we see $theta sim unif[0,2pi]$ (since $f_{Theta}(theta) = frac{1}{2pi}$ ) and $R^2 sim exp(1/2)$



                  Hence, to simulate $Theta$ we simply take $2 pi U_2$ where $$U_2 sim unif[0,1]$$ and to simulate $R$ we can take $$-2ln U_1$$ where $U_1 sim unif[0,1]$. ( to see why find the density of $X=-ln U, U sim unif[0,1]$).



                  So, Box and Muller simply inverted $N_1= R cos theta$, $N_2= R sin theta$ and moved from $(R,Theta)$ to $(N_1,N_2)$ by simulating $Theta$ from $2 pi U_2$, and an independent $R$ from $ sqrt{- 2 ln U_1}$



                  explicitly



                  $$Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2)$$



                  $$Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2)$$



                  And that is that is the mathematical logic behind it.





                  p.s. As I don't know whether it is clear through the mathematical justification (I believe it is, but you may have been lost in details), note you need a pair of drawings from the uniform to get a pair of normals






                  share|cite|improve this answer











                  $endgroup$



                  Let $N_1,N_2$ be independent $N(0,1)$ random variates. $(N_1,N_2)$ defines a point in Cartesian coordinates. We transform to polar by



                  $$ N_1 = R cos theta$$
                  $$ N_2 = R sin theta$$



                  This transformation is one to one and has continuous derivatives. So we can derive the joint distribution $f_{R,theta}(r,theta)$ by using



                  $$f_{R,theta}(r,theta) = f_{N_1,N_2}(n_1,n_2)|J^{-1}|$$



                  where $J$ is the Jacobian of the transformation and



                  $$ J^{-1}(r,theta )=
                  {begin{bmatrix}{dfrac {partial n_1}{partial r}}{dfrac {partial n_1}{partial theta }}\{dfrac {partial n_2}{partial r}}{dfrac {partial n_2}{partial theta }}end{bmatrix}}
                  = begin{bmatrix}cos theta & -rsin theta \sin theta & rcos theta end{bmatrix}$$



                  Hence $$ |J^{-1}| = r cos^2 theta + r sin^2 theta = r $$



                  and



                  $$ f_{R,theta}(r,theta) = r frac{1}{2pi} e^{-frac{1}{2}(n_1^2+n_2^2)} = boxed{frac{r}{2 pi}e^{-frac{1}{2}(r^2)}} qquad 0 leq theta leq 2 pi, 0 leq r leq infty $$



                  And we see $theta sim unif[0,2pi]$ (since $f_{Theta}(theta) = frac{1}{2pi}$ ) and $R^2 sim exp(1/2)$



                  Hence, to simulate $Theta$ we simply take $2 pi U_2$ where $$U_2 sim unif[0,1]$$ and to simulate $R$ we can take $$-2ln U_1$$ where $U_1 sim unif[0,1]$. ( to see why find the density of $X=-ln U, U sim unif[0,1]$).



                  So, Box and Muller simply inverted $N_1= R cos theta$, $N_2= R sin theta$ and moved from $(R,Theta)$ to $(N_1,N_2)$ by simulating $Theta$ from $2 pi U_2$, and an independent $R$ from $ sqrt{- 2 ln U_1}$



                  explicitly



                  $$Z_0 = sqrt{-2 ln(U_1)}cos(2 pi U_2)$$



                  $$Z_1 = sqrt{-2 ln(U_1)} sin(2 pi U_2)$$



                  And that is that is the mathematical logic behind it.





                  p.s. As I don't know whether it is clear through the mathematical justification (I believe it is, but you may have been lost in details), note you need a pair of drawings from the uniform to get a pair of normals







                  share|cite|improve this answer














                  share|cite|improve this answer



                  share|cite|improve this answer








                  edited Mar 14 at 21:11

























                  answered Mar 14 at 16:14









                  StatsStats

                  668210




                  668210






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Cross Validated!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f397505%2fsimulating-rnorm-using-runif%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?