(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 17899, 622] NotebookOptionsPosition[ 13751, 478] NotebookOutlinePosition[ 14874, 516] CellTagsIndexPosition[ 14625, 507] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["JRandomArray", "Title"], Cell["A package for producing pseudo random numbers via Java", "Subtitle"], Cell[CellGroupData[{ Cell["Preliminaries", "Section"], Cell[TextData[{ "This package requires ", StyleBox["colt.jar", FontFamily->"Courier New"], " and ", StyleBox["JRandomArray.class", FontFamily->"Courier New"], ". You will need to modify the line" }], "Text"], Cell[BoxData[ RowBox[{"AddToClassPath", "[", "\"\\"", "]"}]], "Input", Evaluatable->False], Cell[TextData[{ "in ", StyleBox["JRandomArray.m", FontFamily->"Courier New"], " to reflect the directory where you place ", StyleBox["colt.jar", FontFamily->"Courier New"], " and ", StyleBox["JRandomArray.class", FontFamily->"Courier New"], ". " }], "Text"], Cell[TextData[{ "Arrays of ", Cell[BoxData[ FormBox[ SuperscriptBox["10", "6"], TraditionalForm]]], " random numbers can be handled with the default settings for ", StyleBox["InstallJava[]", FontFamily->"Courier New"], ". However, in order to get an array of ", Cell[BoxData[ FormBox[ SuperscriptBox["10", "7"], TraditionalForm]]], " random numbers you will need to use" }], "Text"], Cell[BoxData[ RowBox[{"InstallJava", "[", RowBox[{"CommandLine", "\[Rule]", "\"\\""}], "]"}]], "Input",\ Evaluatable->False], Cell[TextData[{ "which increases the maximum memory allocated to Java to 200MB (from its \ default of 60MB). Note: If ", StyleBox["InstallJava", FontFamily->"Courier New"], " has already been invoked in the current kernel session, then later \ invocations have no effect. In this case you will have to ", StyleBox["UninstallJava[]", FontFamily->"Courier New"], " first (which might break some other code). One way to implement this is to \ edit the file JRandomArray.m. Another way to to load ", StyleBox["JLink", FontFamily->"Courier New"], " and invoke InstallJava prior to loading ", StyleBox["JRandomArray", FontFamily->"Courier New"], ". " }], "Text"], Cell[BoxData[{ RowBox[{"Needs", "[", "\"\\"", "]"}], "\[IndentingNewLine]", RowBox[{"InstallJava", "[", RowBox[{"CommandLine", "\[Rule]", "\"\\""}], "]"}], "\[IndentingNewLine]", RowBox[{"<<", "JRandomArray`"}]}], "Input", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell["Usage", "Section"], Cell[BoxData[ RowBox[{"<<", "JRandomArray`"}]], "Input"], Cell[CellGroupData[{ Cell["Usage notes", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"?", "SeedRandomEngine"}]], "Input"], Cell[BoxData["\<\"SeedRandomEngine[randomEngineClass] returns a random engine \ object from the specified class using JavaNew[\\\"java.util.Date\\\"] as the \ default seed. Alternatively, one can specify the seed explicitly using \ JavaNew[randomEngineClass, seed], where seed is a positive integer or Java \ date object. See the usage notes for RandomEngineClass for a list of valid \ random engine class names. Example:\\n\\n engineObj = \ SeedRandomEngineClass[\\\"MersenneTwister64\\\"].\"\>"], "Print", CellTags->"Info3313555012-4838425"] }, Closed]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"?", "InitializeDistribution"}]], "Input"], Cell[BoxData["\<\"InitializeDistribution[distributionClass, \ randomEngineObject][pars] returns a distribution object from the specified \ class driven by the given random engine object and using the specified \ parameters. For example,\\n\\n normalObj = \ InitializeDistribution[\\\"Normal\\\", engineObj][0,1],\\n\\n where \ mersenneTwister64 is a seeded random engine object. To reseed a distribution \ object, one must first reseed the random engine and then reinitialize the \ distribution. Each distribution has a method to change the parameter values. \ For example, to change the standard deviation of the normal distribution from \ 1 to 3, use\\n\\n normalObj@setState[0, 3].\\n\\n See the usage notes for \ DistributionClass for a list of valid distribution class names and parameter \ reset methods.\"\>"], "Print", CellTags->"Info3313555035-9489212"] }, Closed]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"?", "JRandomArray"}]], "Input"], Cell[BoxData["\<\"JRandomArray[distributionObject, n] returns a vector of n \ pseudo random numbers generated according to the distribution object. \ JRandomArray[distributionObject, {n1, n2, ...}] returns a higher dimensional \ array of random numbers. JRandomArray[distributionObject] returns a single \ element.\"\>"], "Print", CellTags->"Info3313555048-1859798"] }, Closed]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"?", "RandomEngineClass"}]], "Input"], Cell[BoxData["\<\"RandomEngineClass[className] returns the JLink object that \ represents the named random engine class. Valid class names are the \ strings\\nDRand\\nMersenneTwister\\nMersenneTwister64\\nRanecu\\nRanlux\\\ nRanmar\"\>"], "Print", CellTags->"Info3313555103-2445246"] }, Closed]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"?", "DistributionClass"}]], "Input"], Cell[BoxData["\<\"DistributionClass[className] returns the JLink object that \ represents the named distribution class. Valid distribution classes are the \ strings given in first column of the following list:\\nBeta \ setState(double, double)\\nBinomial setNandP(int, \ double)\\nChiSquare setState(double)\\nExponential \ setState(double)\\nGamma setState(double, \ double)\\nHyperGeometric setState(int, int, int)\\nLogarithmic \ setState(double)\\nNegativeBinomial setNandP(int, double)\\nNormal \ setState(double, double)\\nPoisson setMean(double)\\nStudentT \ setState(double)\\nUniform setState(double, double)\\nThe \ second column provides the name of the method used to reset the parameter \ values and also gives the number and type of parameters for each \ class.\"\>"], "Print", CellTags->"Info3313555080-8534106"] }, Closed]] }, Open ]], Cell[CellGroupData[{ Cell["Usage", "Subsection"], Cell["Create an instance of the 64 bit Mersenne Twister engine. ", "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"engineObj", "=", RowBox[{ "SeedRandomEngine", "[", "\"\\"", "]"}]}]], "Input"], Cell[BoxData[ InterpretationBox[ RowBox[{ RowBox[{ "\[LeftGuillemet]JavaObject", "[", "\<\"cern.jet.random.engine.MersenneTwister64\"\>", "]"}], "\[RightGuillemet]"}], JLink`Objects`JavaObject515422315085825]], "Output"] }, Open ]], Cell["\<\ Create instances of uniform (0,1) and normal (0,1) distributions using the \ Twister engine\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"uniformObj", "=", RowBox[{ RowBox[{"InitializeDistribution", "[", RowBox[{"\"\\"", ",", "engineObj"}], "]"}], "[", RowBox[{"0", ",", "1"}], "]"}]}], "\n", RowBox[{"normalObj", "=", RowBox[{ RowBox[{"InitializeDistribution", "[", RowBox[{"\"\\"", ",", "engineObj"}], "]"}], "[", RowBox[{"0", ",", "1"}], "]"}]}]}], "Input"], Cell[BoxData[ InterpretationBox[ RowBox[{ RowBox[{ "\[LeftGuillemet]JavaObject", "[", "\<\"cern.jet.random.Uniform\"\>", "]"}], "\[RightGuillemet]"}], JLink`Objects`JavaObject29010071388161]], "Output"], Cell[BoxData[ InterpretationBox[ RowBox[{ RowBox[{ "\[LeftGuillemet]JavaObject", "[", "\<\"cern.jet.random.Normal\"\>", "]"}], "\[RightGuillemet]"}], JLink`Objects`JavaObject57870322237441]], "Output"] }, Open ]], Cell["Generate a single random number:", "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"normalObj", "@", RowBox[{"nextDouble", "[", "]"}]}]], "Input"], Cell[BoxData["1.7268252328348768`"], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"uniformObj", "@", RowBox[{"nextDouble", "[", "]"}]}]], "Input"], Cell[BoxData["0.5281957510762215`"], "Output"] }, Open ]], Cell["However, generating a long list is not efficient:", "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{"normalObj", "@", RowBox[{"nextDouble", "[", "]"}]}], ",", RowBox[{"{", SuperscriptBox["10", "3"], "}"}]}], "]"}], ";"}], "//", "AbsoluteTiming"}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"0.7410656`7.321401647436168", " ", "Second"}], ",", "Null"}], "}"}]], "Output"] }, Open ]], Cell[TextData[{ "By contrast, ", StyleBox["JRandomArray", FontFamily->"Courier New"], " is quite efficient:" }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"JRandomArray", "[", RowBox[{"normalObj", ",", SuperscriptBox["10", "3"]}], "]"}], ";"}], "//", "AbsoluteTiming"}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"0.0200288`5.753199923369175", " ", "Second"}], ",", "Null"}], "}"}]], "Output"] }, Open ]], Cell["Generate an array of random numbers", "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"JRandomArray", "[", RowBox[{"uniformObj", ",", "10"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ "0.8372603526093829`", ",", "0.5342824008050298`", ",", "0.8015983880528867`", ",", "0.13320110766269905`", ",", "0.44066228405663505`", ",", "0.7976839746307822`", ",", "0.055895767324792345`", ",", "0.5363895848351288`", ",", "0.9252802861904754`", ",", "0.843929787413952`"}], "}"}]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"JRandomArray", "[", RowBox[{"normalObj", ",", "10"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"-", "1.1023425971839913`"}], ",", RowBox[{"-", "0.3380996059931252`"}], ",", "0.16551473599613178`", ",", "1.3392219042426046`", ",", RowBox[{"-", "0.4960886215081657`"}], ",", RowBox[{"-", "1.1052236128153419`"}], ",", RowBox[{"-", "0.05836770008175889`"}], ",", "0.8996898565299261`", ",", RowBox[{"-", "0.24122647252044532`"}], ",", RowBox[{"-", "0.08320063717953267`"}]}], "}"}]], "Output"] }, Open ]], Cell["\<\ Methods exist to override the distribution parameter values used to construct \ the distribution objects:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"normalObj", "@", RowBox[{"nextDouble", "[", RowBox[{"0", ",", "100"}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"-", "61.628080972929695`"}]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"uniformObj", "@", RowBox[{"nextDoubleFromTo", "[", RowBox[{ RowBox[{"-", "100"}], ",", "100"}], "]"}]}]], "Input"], Cell[BoxData["20.91155141509158`"], "Output"] }, Open ]], Cell[TextData[{ "However, this facility is not implemented in ", StyleBox["JRandomArray", FontFamily->"Courier New"], ", which only calls the default method ", StyleBox["nextDouble[]", FontFamily->"Courier New"], " or ", StyleBox["nextInt[]", FontFamily->"Courier New"], " for the distribution. Consequently, one must create a new instance of the \ distribution object using the new parameter values in order to produce a list \ of random numbers using ", StyleBox["JRandomArray", FontFamily->"Courier New"], ":" }], "Text"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"normalObj10", "=", RowBox[{ RowBox[{"InitializeDistribution", "[", RowBox[{"\"\\"", ",", "engineObj"}], "]"}], "[", RowBox[{"0", ",", "100"}], "]"}]}]], "Input"], Cell[BoxData[ InterpretationBox[ RowBox[{ RowBox[{ "\[LeftGuillemet]JavaObject", "[", "\<\"cern.jet.random.Normal\"\>", "]"}], "\[RightGuillemet]"}], JLink`Objects`JavaObject278540809404417]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"JRandomArray", "[", RowBox[{"normalObj10", ",", RowBox[{"{", RowBox[{"10", ",", "2"}], "}"}]}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "74.5402220039427`"}], ",", "60.40271694782293`"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "146.842245150108`"}], ",", RowBox[{"-", "89.98867069111608`"}]}], "}"}], ",", RowBox[{"{", RowBox[{"128.33889992225056`", ",", RowBox[{"-", "26.922917663427015`"}]}], "}"}], ",", RowBox[{"{", RowBox[{"26.80501524651333`", ",", "38.013794861974404`"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "45.500514176539085`"}], ",", "16.263973043654307`"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "17.231219273624465`"}], ",", RowBox[{"-", "163.47269943435916`"}]}], "}"}], ",", RowBox[{"{", RowBox[{"67.23024864249518`", ",", RowBox[{"-", "163.62042687849438`"}]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "115.11881855528507`"}], ",", RowBox[{"-", "16.59173900482347`"}]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "20.84916324309908`"}], ",", RowBox[{"-", "14.036944295374798`"}]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "33.3835648647798`"}], ",", "72.49343863500413`"}], "}"}]}], "}"}]], "Output"] }, Open ]] }, Open ]] }, Open ]] }, Open ]] }, WindowSize->{900, 888}, WindowMargins->{{Automatic, -6}, {Automatic, 0}}, ShowSelection->True, FrontEndVersion->"6.0 for Microsoft Windows (32-bit) (October 10, 2006)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{ "Info3313555012-4838425"->{ Cell[3144, 124, 544, 7, 235, "Print", CellTags->"Info3313555012-4838425"]}, "Info3313555035-9489212"->{ Cell[3793, 139, 863, 12, 560, "Print", CellTags->"Info3313555035-9489212"]}, "Info3313555048-1859798"->{ Cell[4751, 159, 367, 5, 135, "Print", CellTags->"Info3313555048-1859798"]}, "Info3313555103-2445246"->{ Cell[5218, 172, 284, 4, 235, "Print", CellTags->"Info3313555103-2445246"]}, "Info3313555080-8534106"->{ Cell[5602, 184, 938, 13, 485, "Print", CellTags->"Info3313555080-8534106"]} } *) (*CellTagsIndex CellTagsIndex->{ {"Info3313555012-4838425", 14079, 489}, {"Info3313555035-9489212", 14189, 492}, {"Info3313555048-1859798", 14300, 495}, {"Info3313555103-2445246", 14410, 498}, {"Info3313555080-8534106", 14520, 501} } *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 29, 0, 123, "Title"], Cell[622, 25, 74, 0, 117, "Subtitle"], Cell[CellGroupData[{ Cell[721, 29, 32, 0, 105, "Section"], Cell[756, 31, 216, 8, 42, "Text"], Cell[975, 41, 119, 3, 39, "Input", Evaluatable->False], Cell[1097, 46, 270, 11, 68, "Text"], Cell[1370, 59, 401, 13, 69, "Text"], Cell[1774, 74, 146, 4, 39, "Input", Evaluatable->False], Cell[1923, 80, 673, 17, 143, "Text"], Cell[2599, 99, 279, 6, 93, "Input", Evaluatable->False] }, Open ]], Cell[CellGroupData[{ Cell[2915, 110, 24, 0, 105, "Section"], Cell[2942, 112, 57, 1, 39, "Input"], Cell[CellGroupData[{ Cell[3024, 117, 33, 0, 54, "Subsection"], Cell[CellGroupData[{ Cell[3082, 121, 59, 1, 39, "Input"], Cell[3144, 124, 544, 7, 235, "Print", CellTags->"Info3313555012-4838425"] }, Closed]], Cell[CellGroupData[{ Cell[3725, 136, 65, 1, 32, "Input"], Cell[3793, 139, 863, 12, 560, "Print", CellTags->"Info3313555035-9489212"] }, Closed]], Cell[CellGroupData[{ Cell[4693, 156, 55, 1, 32, "Input"], Cell[4751, 159, 367, 5, 135, "Print", CellTags->"Info3313555048-1859798"] }, Closed]], Cell[CellGroupData[{ Cell[5155, 169, 60, 1, 32, "Input"], Cell[5218, 172, 284, 4, 235, "Print", CellTags->"Info3313555103-2445246"] }, Closed]], Cell[CellGroupData[{ Cell[5539, 181, 60, 1, 32, "Input"], Cell[5602, 184, 938, 13, 485, "Print", CellTags->"Info3313555080-8534106"] }, Closed]] }, Open ]], Cell[CellGroupData[{ Cell[6589, 203, 27, 0, 54, "Subsection"], Cell[6619, 205, 74, 0, 41, "Text"], Cell[CellGroupData[{ Cell[6718, 209, 127, 3, 39, "Input"], Cell[6848, 214, 240, 7, 38, "Output"] }, Open ]], Cell[7103, 224, 115, 3, 41, "Text"], Cell[CellGroupData[{ Cell[7243, 231, 398, 10, 66, "Input"], Cell[7644, 243, 218, 6, 38, "Output"], Cell[7865, 251, 216, 6, 38, "Output"] }, Open ]], Cell[8096, 260, 48, 0, 41, "Text"], Cell[CellGroupData[{ Cell[8169, 264, 89, 2, 39, "Input"], Cell[8261, 268, 46, 0, 38, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[8344, 273, 90, 2, 39, "Input"], Cell[8437, 277, 46, 0, 38, "Output"] }, Open ]], Cell[8498, 280, 65, 0, 41, "Text"], Cell[CellGroupData[{ Cell[8588, 284, 267, 9, 42, "Input"], Cell[8858, 295, 134, 4, 38, "Output"] }, Open ]], Cell[9007, 302, 125, 5, 42, "Text"], Cell[CellGroupData[{ Cell[9157, 311, 185, 6, 42, "Input"], Cell[9345, 319, 134, 4, 38, "Output"] }, Open ]], Cell[9494, 326, 51, 0, 41, "Text"], Cell[CellGroupData[{ Cell[9570, 330, 98, 2, 39, "Input"], Cell[9671, 334, 355, 7, 63, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[10063, 346, 97, 2, 39, "Input"], Cell[10163, 350, 474, 10, 63, "Output"] }, Open ]], Cell[10652, 363, 129, 3, 41, "Text"], Cell[CellGroupData[{ Cell[10806, 370, 120, 3, 39, "Input"], Cell[10929, 375, 63, 1, 38, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[11029, 381, 149, 4, 61, "Input"], Cell[11181, 387, 45, 0, 60, "Output"] }, Open ]], Cell[11241, 390, 541, 16, 128, "Text"], Cell[CellGroupData[{ Cell[11807, 410, 209, 5, 61, "Input"], Cell[12019, 417, 217, 6, 60, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[12273, 428, 148, 4, 61, "Input"], Cell[12424, 434, 1275, 38, 135, "Output"] }, Open ]] }, Open ]] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)