Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

correlation among random numbers

5 views
Skip to first unread message

Andrea Romanino

unread,
May 3, 2003, 3:41:02 AM5/3/03
to

A Mathematica program I wrote that uses a Montecarlo method to generate
a probability distribution gives incorrect results.

The problem turns out to be due to an unexpected correlation among
independent random variables. In its simplest form, the problem is
illustrated in the notebook you can find attached.

I am using Mathematica 4.2.0 for Linux. The problem also arises with the
version 4.1.

While I can easily get rid of that correlation with "empirical"
modifications to the program, I am afraid that other correlations, not
leading to obviously incorrect results, may be hidden in the results. I
would therefore be extremely grateful if anyone could help me
understanding the origin of the problem and how to avoid it.

Thank you very much,
Andrea Romanino

(************** Content-type: application/mathematica **************
CreatedBy='Mathematica 4.2'

Mathematica-Compatible Notebook

This notebook can be used with any Mathematica-compatible
application, such as Mathematica, MathReader or Publicon. The data
for the notebook starts with the line containing stars above.

To get the notebook into a Mathematica-compatible application, do
one of the following:

* Save the data starting with the line of stars above into a file
with a name ending in .nb, then open the file inside the
application;

* Copy the data starting with the line of stars above to the
clipboard, then use the Paste menu command inside the application.

Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode. Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).

NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing
the word CacheID, otherwise Mathematica-compatible applications may
try to use invalid cache data.

For more information on notebooks and Mathematica-compatible
applications, contact Wolfram Research:
web: http://www.wolfram.com
email: in...@wolfram.com
phone: +1-217-398-0700 (U.S.)

Notebook reader applications are available free of charge from
Wolfram Research.
*******************************************************************)

(*CacheID: 232*)


(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[ 6256, 154]*)
(*NotebookOutlinePosition[ 6942, 178]*)
(* CellTagsIndexPosition[ 6898, 174]*)
(*WindowFrame->Normal*)

Notebook[{
Cell[TextData[{
"Correlation between independent random numbers (",
StyleBox["Mathematica",
FontSlant->"Italic"],
" 4.2.0 for Linux)"
}], "Section"],

Cell["\<\
Omitting one of the random number generations between r and x or \
between x and y destroys the correlation.
Same if Random[NormalDistribution[0,1]] is replaced by Random[]\
\>", \
"Subsection"],

Cell["\<\
To see that the correlation indeed arises evaluate the cells below
The correlation arises between the variables r, x and y in the form Mod[x-y, \
1] - r = 0
Omitting one of the random number generations between r and x (or between x \
and y) eliminates the correlation
Idem if one of the \"Random[NormalDistribution[0,1]]\" is replaced by \
\"Random[]\" or viceversa\
\>", "Subsection"],

Cell[BoxData[{
\(Off[General::"\<spell\>",
General::"\<spell1\>"]\), "\[IndentingNewLine]",
\(\(<< "\<Statistics`NormalDistribution`\>";\)\)}], "Input",
AspectRatioFixed->True],

Cell[BoxData[{
\(\(nrnd = 10;\)\), "\n",
\(tab = {};
Do[\[IndentingNewLine]r = Random[]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0,
1]]; \[IndentingNewLine]Random[]; \[IndentingNewLine]Random[]; \
\[IndentingNewLine]Random[]; \[IndentingNewLine]x =
Random[]; \[IndentingNewLine]Random[]; \[IndentingNewLine]Random[]; \
\[IndentingNewLine]Random[]; \[IndentingNewLine]y =
Random[]; \[IndentingNewLine]tab =
Append[tab, {StringJoin["\<[x-y] = \>", ToString[Mod[x - y, 1]]],
StringJoin["\<r = \>", ToString[r]],
StringJoin["\<difference = \>",
ToString[Chop[Mod[x - y, 1] - r]]]}], {w,
nrnd}];\), "\[IndentingNewLine]",
\(TableForm[tab]\)}], "Input"],

Cell["A graphical representation of the correlation:", "Subsection"],

Cell[BoxData[{
\(\(nrnd = 1000;\)\), "\n",
\(tab = {PointSize[0.02]};
Do[\[IndentingNewLine]r = Random[]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0,
1]]; \[IndentingNewLine]Random[]; \[IndentingNewLine]Random[]; \
\[IndentingNewLine]Random[]; \[IndentingNewLine]x =
Random[]; \[IndentingNewLine]Random[]; \[IndentingNewLine]Random[]; \
\[IndentingNewLine]Random[]; \[IndentingNewLine]y =
Random[]; \[IndentingNewLine]tab =
Append[tab, Point[{Mod[x - y, 1], r}]], {w,
nrnd}];\), "\[IndentingNewLine]",
\(\(Show[Graphics[tab], Frame -> True, AspectRatio -> 1];\)\)}], "Input"],

Cell["\<\
What one should obtain (I have just removed one line from the cell \
above)\
\>", "Subsection"],

Cell[BoxData[{
\(\(nrnd = 1000;\)\), "\n",
\(tab = {PointSize[0.02]};
Do[\[IndentingNewLine]r = Random[]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0, 1]]; \[IndentingNewLine]Random[
NormalDistribution[0,
1]]; \[IndentingNewLine]Random[]; \[IndentingNewLine]Random[]; \
\[IndentingNewLine]x =
Random[]; \[IndentingNewLine]Random[]; \[IndentingNewLine]Random[]; \
\[IndentingNewLine]Random[]; \[IndentingNewLine]y =
Random[]; \[IndentingNewLine]tab =
Append[tab, Point[{Mod[x - y, 1], r}]], {w,
nrnd}];\), "\[IndentingNewLine]",
\(\(Show[Graphics[tab], Frame -> True, AspectRatio -> 1];\)\)}], "Input"]
},
FrontEndVersion->"4.2 for X",
ScreenRectangle->{{0, 1280}, {0, 1024}},
AutoGeneratedPackage->Automatic,
CellGrouping->Manual,
WindowSize->{1072, 823},
WindowMargins->{{Automatic, 26}, {Automatic, 52}}
]

(*******************************************************************
Cached data follows. If you edit this Notebook file directly, not
using Mathematica, you must remove the line containing CacheID at
the top of the file. The cache data will then be recreated when
you save this file from within Mathematica.
*******************************************************************)

(*CellTagsOutline
CellTagsIndex->{}
*)

(*CellTagsIndex
CellTagsIndex->{}
*)

(*NotebookFileOutline
Notebook[{
Cell[1754, 51, 158, 5, 58, "Section"],
Cell[1915, 58, 204, 5, 62, "Subsection"],
Cell[2122, 65, 396, 8, 96, "Subsection"],
Cell[2521, 75, 194, 4, 43, "Input"],
Cell[2718, 81, 1202, 22, 363, "Input"],
Cell[3923, 105, 68, 0, 45, "Subsection"],
Cell[3994, 107, 1088, 19, 347, "Input"],
Cell[5085, 128, 105, 3, 45, "Subsection"],
Cell[5193, 133, 1059, 19, 331, "Input"]
}
]
*)

(*******************************************************************
End of Mathematica Notebook file.
*******************************************************************)


--------------34F4ECE46DB32F3959B214D6--

Andrzej Kozlowski

unread,
May 4, 2003, 3:53:11 AM5/4/03
to
Most likely you have encountered the problem discussed (with a
solution) in:

http://forums.wolfram.com/mathgroup/archive/2000/May/msg00088.html

Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
http://platon.c.u-tokyo.ac.jp/andrzej/

Bobby Treat

unread,
May 5, 2003, 2:56:37 AM5/5/03
to
This uses Daniel Lichtblau's suggestion to get rid of the correlation
problem, but it may slow things down.

Unprotect@NormalDistribution;
NormalDistribution /: Random[NormalDistribution[mu_:0, sigma_:1]] := \
normal[mu, sigma, myRandom, myRandom]
Protect@NormalDistribution;
myRandom := ((Random[Integer, 2^30 - 1]/2^30.) + Random[Integer,
2^30 - 1])/2^30.

It's far too tedious to do it this way for every distribution in the
inventory, so fixing Random[] itself seems preferable.

But WRI should do that.

Bobby

Bill Rowe

unread,
May 6, 2003, 6:37:41 AM5/6/03
to
On 5/5/03 at 2:44 AM, drmajorbob+M...@mailblocks.com (Bobby
Treat) wrote:

>This uses Daniel Lichtblau's suggestion to get rid of the correlation
>problem, but it may slow things down.

>Unprotect@NormalDistribution; NormalDistribution /:
>Random[NormalDistribution[mu_:0, sigma_:1]] := \ normal[mu, sigma,
>myRandom, myRandom] Protect@NormalDistribution; myRandom :=
>((Random[Integer, 2^30 - 1]/2^30.) + Random[Integer, 2^30 - 1])/2^30.

>It's far too tedious to do it this way for every distribution in the
>inventory, so fixing Random[] itself seems preferable.

An alternative would be to use the work around to generate random reals and the Quantile function to map these to the desired random number, i.e.

myRandom:=Random[Integer, 2^30 -1]/2^30. + Random[Integer, 2^30 - 1]/2^60.
r = Quantile[NormalDistribution[mu,sigma],myRandom

The advantage of this approach is it gives a generic answer independent of the distribution since the details of the distribution are built into Quantile. The disadvantage is this approach loses any customizations for specific distributions that would result in faster generation of random numbers for that distribution.

>But WRI should do that.

Agreed

0 new messages