Simulating rnorm() using runif()
$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?
r normalization
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$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?
r normalization
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
4 hours ago
$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♦
1 hour ago
add a comment |
$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?
r normalization
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$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
r normalization
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 3 hours ago
masoud
74
74
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 6 hours ago
Chicago1988Chicago1988
162
162
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Chicago1988 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
4 hours ago
$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♦
1 hour ago
add a comment |
$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
4 hours ago
$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♦
1 hour ago
$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
4 hours ago
$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
4 hours ago
$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♦
1 hour ago
$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♦
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
$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).
$endgroup$
add a comment |
$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}$
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
$endgroup$
add a comment |
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
});
}
});
Chicago1988 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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).
$endgroup$
add a comment |
$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).
$endgroup$
add a comment |
$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).
$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).
edited 1 hour ago
answered 5 hours ago
Cliff ABCliff AB
13.4k12567
13.4k12567
add a comment |
add a comment |
$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}$
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
$endgroup$
add a comment |
$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}$
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
$endgroup$
add a comment |
$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}$
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
$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}$
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
edited 3 hours ago
answered 3 hours ago
StatsStats
44117
44117
add a comment |
add a comment |
Chicago1988 is a new contributor. Be nice, and check out our Code of Conduct.
Chicago1988 is a new contributor. Be nice, and check out our Code of Conduct.
Chicago1988 is a new contributor. Be nice, and check out our Code of Conduct.
Chicago1988 is a new contributor. Be nice, and check out our Code of Conduct.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$begingroup$
You will want to investigate the Probability Integral Transform.
$endgroup$
– StatsStudent
4 hours ago
$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♦
1 hour ago