correlation among random numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg41103] correlation among random numbers
- From: Andrea Romanino <andrea.romanino at cern.ch>
- Date: Sat, 3 May 2003 03:27:08 -0400 (EDT)
- Organization: CERN
- Sender: owner-wri-mathgroup at wolfram.com
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: info at 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--