(************** Content-type: application/mathematica ************** 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@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[ 11124, 358]*) (*NotebookOutlinePosition[ 11760, 380]*) (* CellTagsIndexPosition[ 11716, 376]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Selecting random numbers from a specified distribution", "Section"], Cell[TextData[{ "The default setting for Random is a uniform distribution on the interval \ {0, 1}. There are a number of built-in ways to modify this, such as \ specifying the type of number you want or to specify a domain different from \ {0, 1}. You can also use the Statistics packages and specify any of the \ built-in distributions from which to select the random numbers. There are \ occasions when you need to select random numbers from a distribution which is \ not built in to ", StyleBox["Mathematica", FontSlant->"Italic"], ". This notebook illustrates how to do that." }], "Text"], Cell[CellGroupData[{ Cell["A density function", "Subsection"], Cell["\<\ Here is a density function to illustrate the process. It is based \ on a half period of the sine function with support interval {h, h+ \ \[Pi]/B}.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"f2", "[", RowBox[{"t_", ",", "B_", ",", "h_"}], "]"}], " ", ":=", " ", RowBox[{"B", " ", RowBox[{"UnitStep", "[", RowBox[{ RowBox[{"(", RowBox[{"t", "-", "h"}], ")"}], RowBox[{"(", RowBox[{"h", "+", " ", RowBox[{"\[Pi]", "/", "B"}], "-", "t"}], ")"}]}], "]"}], RowBox[{ RowBox[{"Sin", "[", RowBox[{"B", RowBox[{"(", RowBox[{"t", "-", "h"}], ")"}]}], "]"}], "/", "2"}]}]}]], "Input"], Cell[CellGroupData[{ Cell["A check using plot", "Subsubsection"], Cell[BoxData[ RowBox[{"Plot", "[", RowBox[{ RowBox[{"f2", "[", RowBox[{"t", ",", "5", ",", "2"}], "]"}], ",", RowBox[{"{", RowBox[{"t", ",", "1", ",", "4"}], "}"}]}], "]"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["A check using Integrate", "Subsubsection"], Cell[BoxData[ RowBox[{ RowBox[{"Integrate", "[", RowBox[{ RowBox[{"f2", "[", RowBox[{"t", ",", "2.5", ",", "3"}], "]"}], ",", RowBox[{"{", RowBox[{"t", ",", "1", ",", "6"}], "}"}]}], "]"}], "//", "Simplify"}]], "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["A cumulative distribution function", "Subsection"], Cell["\<\ In order to select random numbers from this distribution we need \ first to find the distribution function. You can check that the following is \ the correct distribution function for f.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"F", "[", RowBox[{"x_", ",", "B_", ",", "h_"}], "]"}], " ", ":=", RowBox[{ FractionBox["1", "2"], " ", RowBox[{"(", RowBox[{ RowBox[{ RowBox[{"-", RowBox[{"(", RowBox[{ RowBox[{"-", "1"}], "+", RowBox[{"Cos", "[", RowBox[{"B", " ", RowBox[{"(", RowBox[{"h", "-", "x"}], ")"}]}], "]"}]}], ")"}]}], " ", RowBox[{"UnitStep", "[", RowBox[{ RowBox[{"(", RowBox[{"h", "+", FractionBox["\[Pi]", "B"], "-", "x"}], ")"}], " ", RowBox[{"(", RowBox[{ RowBox[{"-", "h"}], "+", "x"}], ")"}]}], "]"}]}], "+", RowBox[{"2", " ", RowBox[{"UnitStep", "[", RowBox[{ RowBox[{"-", "h"}], "-", FractionBox["\[Pi]", "B"], "+", "x"}], "]"}]}]}], ")"}], " "}]}]], "Input"], Cell[CellGroupData[{ Cell["A check using Plot", "Subsubsection"], Cell[BoxData[ RowBox[{"Plot", "[", RowBox[{ RowBox[{"F", "[", RowBox[{"x", ",", "3", ",", "2"}], "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "4"}], "}"}]}], "]"}]], "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["The next step", "Subsection"], Cell["\<\ The next step in the process is to find the inverse of the \ distribution function. If you have used UnitStep to define your density and \ distribution functions you will need to extract the relevant parts and use \ them in Solve.\ \>", "Text"], Cell[BoxData[ RowBox[{"sol", " ", "=", " ", RowBox[{"Solve", "[", RowBox[{ RowBox[{ RowBox[{ FractionBox["1", "2"], " ", RowBox[{"(", RowBox[{"-", RowBox[{"(", RowBox[{ RowBox[{"-", "1"}], "+", RowBox[{"Cos", "[", RowBox[{"B", " ", RowBox[{"(", RowBox[{"h", "-", "x"}], ")"}]}], "]"}]}], ")"}]}], ")"}]}], "\[Equal]", "y"}], ",", "x"}], "]"}]}]], "Input"], Cell["\<\ We need the second solution here as we will see. This will always \ be defined on the interval {0, 1} thus the UnitStep as a factor.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Inv", "[", RowBox[{"y_", ",", "B_", ",", "h_"}], "]"}], " ", ":=", " ", RowBox[{ FractionBox[ RowBox[{ RowBox[{"B", " ", "h"}], "+", RowBox[{"ArcCos", "[", RowBox[{"1", "-", RowBox[{"2", " ", "y"}]}], "]"}]}], "B"], RowBox[{"UnitStep", "[", RowBox[{"y", " ", RowBox[{"(", RowBox[{"1", "-", "y"}], ")"}]}], "]"}]}]}]], "Input"], Cell[CellGroupData[{ Cell["A check of our inverse using Plot.", "Subsubsection"], Cell[BoxData[ RowBox[{"Plot", "[", RowBox[{ RowBox[{"Inv", "[", RowBox[{"y", ",", "3", ",", "2"}], "]"}], ",", RowBox[{"{", RowBox[{"y", ",", "0", ",", "1"}], "}"}]}], "]"}]], "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Defining our SinRandom function", "Subsection"], Cell["All that is left is to define our random function.", "Text"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"SinRandom", "[", RowBox[{"B_", ",", "h_"}], "]"}], " ", ":=", " ", RowBox[{"Inv", "[", RowBox[{ RowBox[{"Random", "[", "]"}], ",", "B", ",", "h"}], "]"}]}], ";"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["A check of our SinRandom function", "Subsection"], Cell["\<\ To illustrate we have done what we claim we will need some \ functions from the Statistics`DataManipulation` package. So we load it now.\ \ \>", "Text"], Cell[BoxData[ RowBox[{"<<", "Statistics`"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"pts", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"SinRandom", "[", RowBox[{"3", ",", "2"}], "]"}], ",", RowBox[{"{", RowBox[{"t", ",", "100000"}], "}"}]}], "]"}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{"pts", "[", RowBox[{"[", "1", "]"}], "]"}]], "Input"], Cell[CellGroupData[{ Cell["Next we get the frequencies of the values in our list.", "Subsubsection"], Cell[BoxData[ RowBox[{"Clear", "[", "freqs", "]"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"freqs", " ", "=", " ", RowBox[{"18.72", " ", RowBox[{ RowBox[{"BinCounts", "[", RowBox[{"pts", ",", RowBox[{"{", RowBox[{"2.", ",", RowBox[{"2.", "+", " ", RowBox[{"\[Pi]", "/", "3"}]}], ",", RowBox[{"\[Pi]", "/", "60"}]}], "}"}]}], "]"}], "/", "100000"}]}]}], ";"}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"bins", " ", "=", " ", RowBox[{"Range", "[", RowBox[{ RowBox[{"2.", "+", " ", RowBox[{"\[Pi]", "/", "120"}]}], ",", RowBox[{"2.", "+", " ", RowBox[{"\[Pi]", "/", "3"}], "-", RowBox[{"\[Pi]", "/", "120"}]}], ",", RowBox[{"\[Pi]", "/", "60"}]}], "]"}]}], ";"}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["A graphical check", "Subsubsection"], Cell["\<\ We can see that this is a reasonable set of points from the given \ distribution by plotting the frequencies of the points against the density \ function after some appropriate scaling. There will always be some variation \ from the curve but if you run these last few cells a number of times you will \ see that the samples follow the distribution closely.\ \>", "Text"], Cell[BoxData[ RowBox[{"Plot", "[", RowBox[{ RowBox[{"f2", "[", RowBox[{"x", ",", "3", ",", "2"}], "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "4"}], "}"}], ",", RowBox[{"Epilog", "\[Rule]", " ", RowBox[{"{", RowBox[{ RowBox[{"Hue", "[", ".95", "]"}], ",", RowBox[{"AbsolutePointSize", "[", "3", "]"}], ",", " ", RowBox[{"Point", "/@", RowBox[{"Transpose", "[", RowBox[{"{", RowBox[{"bins", ",", " ", "freqs"}], "}"}], "]"}]}]}], "}"}]}]}], "]"}]], "Input"] }, Open ]] }, Open ]] }, Open ]] }, FrontEndVersion->"4.1 for Macintosh", ScreenRectangle->{{0, 1024}, {0, 746}}, WindowSize->{520, 624}, WindowMargins->{{9, Automatic}, {Automatic, 21}} ] (******************************************************************* 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[CellGroupData[{ Cell[1727, 52, 73, 0, 56, "Section"], Cell[1803, 54, 603, 11, 122, "Text"], Cell[CellGroupData[{ Cell[2431, 69, 40, 0, 46, "Subsection"], Cell[2474, 71, 170, 4, 50, "Text"], Cell[2647, 77, 608, 17, 43, "Input"], Cell[CellGroupData[{ Cell[3280, 98, 43, 0, 42, "Subsubsection"], Cell[3326, 100, 233, 6, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3596, 111, 49, 0, 42, "Subsubsection"], Cell[3648, 113, 292, 8, 27, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[3989, 127, 56, 0, 46, "Subsection"], Cell[4048, 129, 210, 4, 50, "Text"], Cell[4261, 135, 1172, 31, 91, "Input"], Cell[CellGroupData[{ Cell[5458, 170, 43, 0, 42, "Subsubsection"], Cell[5504, 172, 232, 6, 27, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[5785, 184, 35, 0, 46, "Subsection"], Cell[5823, 186, 254, 5, 68, "Text"], Cell[6080, 193, 639, 17, 42, "Input"], Cell[6722, 212, 156, 3, 50, "Text"], Cell[6881, 217, 507, 14, 42, "Input"], Cell[CellGroupData[{ Cell[7413, 235, 59, 0, 42, "Subsubsection"], Cell[7475, 237, 234, 6, 27, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[7758, 249, 53, 0, 46, "Subsection"], Cell[7814, 251, 66, 0, 32, "Text"], Cell[7883, 253, 283, 8, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[8203, 266, 55, 0, 46, "Subsection"], Cell[8261, 268, 162, 4, 50, "Text"], Cell[8426, 274, 58, 1, 27, "Input"], Cell[8487, 277, 309, 8, 27, "Input"], Cell[8799, 287, 86, 2, 27, "Input"], Cell[CellGroupData[{ Cell[8910, 293, 79, 0, 42, "Subsubsection"], Cell[8992, 295, 65, 1, 27, "Input"], Cell[9060, 298, 480, 12, 43, "Input"], Cell[9543, 312, 413, 10, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[9993, 327, 42, 0, 42, "Subsubsection"], Cell[10038, 329, 381, 6, 86, "Text"], Cell[10422, 337, 662, 16, 59, "Input"] }, Open ]] }, Open ]] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)