(************** 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[ 15204, 431]*) (*NotebookOutlinePosition[ 16046, 462]*) (* CellTagsIndexPosition[ 15974, 456]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Approximation to Brownian Motion on [0,1]", "Title", TextAlignment->Center, Background->RGBColor[0, 1, 1]], Cell["\<\ This notebook is by Steven Amgott. Please send any questions or comments to \ samgott1@swarthmore.edu. Feel free to use and distribute this notebook, but \ keep this author information in any copy you use or distribute.\ \>", "SmallText"], Cell[TextData[{ "In general, anything in ", StyleBox["magenta", FontColor->RGBColor[1, 0, 1]], " is something you can, and possibly should, change." }], "Text"], Cell[CellGroupData[{ Cell["Initialization", "Section"], Cell[BoxData[ \(Needs["\"]\)], "Input", InitializationCell->True], Cell[BoxData[ \(Needs["\"]\)], "Input", InitializationCell->True], Cell[BoxData[ \(RandomNormal[\[Mu]_, \[Sigma]_] := Random[NormalDistribution[\[Mu], \[Sigma]]]\)], "Input", InitializationCell->True, CellTags->"Random"], Cell[BoxData[{ \(Clear[maxY]\), "\[IndentingNewLine]", \(maxY[list_, t_] := Max[\(\(Transpose[list]\)[\([2]\)]\)[\([Flatten[ Position[\(Transpose[ list]\)[\([1]\)], _?\((\(-\[Infinity]\) < # \[LessEqual] t &)\)]]]\)]]\)}], "Input", InitializationCell->True], Cell[BoxData[{ \(Clear[runningMaxList]\), "\[IndentingNewLine]", \(\(runningMaxList = {\ };\)\)}], "Input", InitializationCell->True], Cell["\<\ Clear[Areabargraph]; Areabargraph[data_, xmin_, xmax_, k_] := Block[{sorteddata = {}, lengthData = Length[data], dx = (xmax - xmin)/k, currentupperlim = xmin + dx, result = {}, index = 1, counter, rectanglelist = {}, linelist = {}, graphicslist = {} }, sorteddata = Sort[data]; If[((sorteddata[[1]] < xmin)|| (sorteddata[[lengthData]] > xmax)), Print[\"Note: some data values lie outside the user-defined \ interval.\"] ]; Block[{}, For[i = 1, i <= k, i++, counter = 0; While[((index <= lengthData)&& (sorteddata[[index]] <= currentupperlim)), counter++; index++; ]; result = Append[result, counter]; currentupperlim += dx; ]; rectanglelist = Table[Rectangle[{xmin + (i-1)*dx,0}, {xmin + i*dx,result[[i]]/(lengthData*dx)} ], {i, 1, k} ]; rectanglelist = Prepend[rectanglelist, RGBColor[0, 0, 1] ]; linelist = Table[{Line[{{xmin + (i-1)*dx,0}, {xmin + \ (i-1)*dx,result[[i]]/(lengthData*dx)} } ], Line[{{xmin + i*dx,0}, {xmin + i*dx,result[[i]]/(lengthData*dx)} } ], Line[{{xmin + (i-1)*dx, result[[i]]/(lengthData*dx)}, {xmin + i*dx, result[[i]]/(lengthData*dx)} } ] }, {i, 1, k} ]; linelist = Flatten[linelist,1]; graphicslist = Union[rectanglelist, linelist]; \t\t Show[Graphics[{rectanglelist, linelist}, Axes -> True, PlotRange -> All, Frame -> True ] ] ] ]\ \>", "Input", InitializationCell->True, AspectRatioFixed->True], Cell[CellGroupData[{ Cell["Eliminating some unnecessary warning messages.", "Subsection"], Cell[BoxData[{ \(\(Off[General::"\"];\)\), "\n", \(\(Off[General::"\"];\)\)}], "Input", InitializationCell->True] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Animating the convergence", "Section"], Cell[TextData[{ "The cell below allows you to illustrate the convergence of the \ approximations to a Brownian Motion. The maximum number of points created is \ ", Cell[BoxData[ FormBox[ SuperscriptBox["2", StyleBox["nmax", FontColor->RGBColor[1, 0, 1]]], TraditionalForm]]], ". Increasing ", StyleBox["nmax", FontColor->RGBColor[1, 0, 1]], " improves the approximation to Brownian motion. Each successive frame \ adds points located midway horizontally between the points already present. \ To see the animation, evaluate the cell, select the entire collection of \ output graphs, and choose ", StyleBox["Animate Selected Graphics", FontColor->RGBColor[0, 0, 1]], " from the ", StyleBox["Cell", FontColor->RGBColor[0, 0, 1]], " menu item." }], "Text"], Cell[BoxData[{\(Clear[x, k, n, nmax, points, minPoints, maxPoints]\), "\n", RowBox[{ RowBox[{"nmax", "=", StyleBox["12", FontColor->RGBColor[1, 0, 1]]}], ";"}], "\n", \(x[{0, 1}] = 0;\), "\n", \(x[{1, 1}] = RandomNormal[0, 1];\), "\n", \(points = {{0, 0}, {1, x[{1, 1}]}};\), "\n", \(Do[ points = Union[points, Table[{\(2 k + 1\)\/2\^n, x[{2 k + 1, 2\^n}] = \(1\/2\) \((x[{Numerator[k\/2\^\(n - 1\)], Denominator[k\/2\^\(n - 1\)]}] + x[{Numerator[\(k + 1\)\/2\^\(n - 1\)], Denominator[\(k + 1\)\/2\^\(n - 1\)]}] - RandomNormal[0, 1\/\@2\^\(n - 1\)])\)}, {k, 0, 2\^\(n - 1\) - 1}]], {n, 1, nmax}]\), "\n", \(minPoints = Min[\(Transpose[points]\)[\([2]\)]];\), "\n", \(maxPoints = Max[\(Transpose[points]\)[\([2]\)]];\), "\n", \(Animate[ ListPlot[points[\([Flatten[ Position[ Denominator[\(Transpose[ points]\)[\([1]\)]], _?\((# \[LessEqual] 2\^n &)\)]]]\)], PlotRange \[Rule] {minPoints, maxPoints}, PlotJoined \[Rule] True], {n, 0, nmax, 1}]\)}], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Creating multiple runs of the final approximation", "Section"], Cell[TextData[{ "The cell below allows you to generate several different Brownian motions \ at a time. The number of points created in each run is ", Cell[BoxData[ FormBox[ SuperscriptBox["2", StyleBox["nmax", FontColor->RGBColor[1, 0, 1]]], TraditionalForm]]], ". Increasing ", StyleBox["nmax", FontColor->RGBColor[1, 0, 1]], " improves the approximation to Brownian motion. ", StyleBox["numTimes", FontColor->RGBColor[1, 0, 1]], " is the number of different Brownian motions you wish to generate at a \ time." }], "Text"], Cell[BoxData[{\(Clear[x, k, n, nmax, points, numTimes]\), "\n", RowBox[{ RowBox[{"nmax", "=", StyleBox["12", FontColor->RGBColor[1, 0, 1]]}], ";"}], "\n", RowBox[{ RowBox[{"numTimes", "=", StyleBox["5", FontColor->RGBColor[1, 0, 1]]}], ";"}], "\[IndentingNewLine]", \(Do[ x[{0, 1}] = 0; \nx[{1, 1}] = RandomNormal[0, 1]; \n points = {{0, 0}, {1, x[{1, 1}]}}; \n Do[points = Union[points, Table[{\(2 k + 1\)\/2\^n, x[{2 k + 1, 2\^n}] = \(1\/2\) \((x[{Numerator[k\/2\^\(n - 1\)], Denominator[k\/2\^\(n - 1\)]}] + x[{Numerator[\(k + 1\)\/2\^\(n - 1\)], Denominator[\(k + 1\)\/2\^\(n - 1\)]}] - RandomNormal[0, 1\/\@2\^\(n - 1\)])\)}, {k, 0, 2\^\(n - 1\) - 1}]], {n, 1, nmax}]\n ListPlot[points], {numTimes}]\)}], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Maxima of the approximate Brownian motion", "Section"], Cell[TextData[{ "The function ", StyleBox["maxY[list,t]", FontColor->RGBColor[1, 0, 0]], " (created in the initialization section) takes a list of points in ", Cell[BoxData[ \(TraditionalForm\`R\^2\)]], " and finds the maximum of the second coordinates subject to the first \ coordinate being less than or equal to t." }], "Text"], Cell[BoxData[ \(\(?maxY\)\)], "Input"], Cell[TextData[{ "If you already have a ", StyleBox["runningMaxList", FontColor->RGBColor[1, 0, 0]], " stored in the file ", StyleBox["runningMax ", FontColor->RGBColor[0, 0, 1]], "(in the root ", StyleBox["Mathematica", FontSlant->"Italic"], " directory", "), evaluate the following cell before the first run of the Brownian \ motion cell below in the current ", StyleBox["Mathematica", FontSlant->"Italic"], " session. If not, skip the next cell." }], "Text"], Cell[BoxData[ \(\(runningMaxList = << runningMax;\)\)], "Input"], Cell[TextData[{ "The cell below runs the approximate Brownian motion ", StyleBox["numTimes ", FontColor->RGBColor[1, 0, 1]], "and stores the output of ", StyleBox["maxY", FontColor->RGBColor[1, 0, 0]], " in a list named ", StyleBox["maxList", FontColor->RGBColor[1, 0, 0]], ". It also updates a running list of maxima during the current ", StyleBox["Mathematica", FontSlant->"Italic"], " session named ", StyleBox["runningMaxList", FontColor->RGBColor[1, 0, 0]], " (initialized to be empty at the beginning of the session unless you \ loaded a file using the cell above). It then writes an update file with the \ contents of ", StyleBox["runningMaxList", FontColor->RGBColor[1, 0, 0]], ". Finally, it produces a histogram of ", StyleBox["maxList", FontColor->RGBColor[1, 0, 0]], ", divided into ", StyleBox["numInt", FontColor->RGBColor[1, 0, 1]], " intervals, a histogram of ", StyleBox["runningMaxList", FontColor->RGBColor[1, 0, 0]], ", and a graph overlaying the theoretical density function on this \ histogram. ", StyleBox["upTo", FontColor->RGBColor[1, 0, 1]], " is the upper bound on the first coordinate used to compute the maximum of \ the second coordinate. Be patient - this cell can take a while to evaluate, \ longer as ", StyleBox["numTimes", FontColor->RGBColor[1, 0, 1]], " gets larger." }], "Text"], Cell[BoxData[{\(Clear[x, k, n, nmax, points, noTimes, maxList, upTo, graph1, graph2]\), "\n", RowBox[{ RowBox[{"nmax", "=", StyleBox["12", FontColor->RGBColor[1, 0, 1]]}], ";"}], "\n", RowBox[{ RowBox[{"numTimes", "=", StyleBox["50", FontColor->RGBColor[1, 0, 1]]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"numInt", "=", StyleBox["20", FontColor->RGBColor[1, 0, 1]]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"upTo", "=", StyleBox["1", FontColor->RGBColor[1, 0, 1]]}], ";"}], "\[IndentingNewLine]", \(maxList = {};\), "\[IndentingNewLine]", \ \(Do[x[{0, 1}] = 0; \nx[{1, 1}] = RandomNormal[0, 1]; \n points = {{0, 0}, {1, x[{1, 1}]}}; \n Do[points = Union[points, Table[{\(2 k + 1\)\/2\^n, x[{2 k + 1, 2\^n}] = \(1\/2\) \((x[{Numerator[k\/2\^\(n - 1\)], Denominator[k\/2\^\(n - 1\)]}] + x[{Numerator[\(k + 1\)\/2\^\(n - 1\)], Denominator[\(k + 1\)\/2\^\(n - 1\)]}] - RandomNormal[0, 1\/\@2\^\(n - 1\)])\)}, {k, 0, 2\^\(n - 1\) - 1}]], {n, 1, nmax}]; \n maxList = Append[maxList, maxY[points, upTo]], {numTimes}]\), "\[IndentingNewLine]", \(runningMaxList = Flatten[Append[runningMaxList, maxList]];\), "\[IndentingNewLine]", \(runningMaxList >> runningMax\), "\[IndentingNewLine]", \(Print["\", Sort[maxList], "\<.\>"]\), "\[IndentingNewLine]", \(Print["\"]\), "\[IndentingNewLine]", \(Areabargraph[maxList, Min[maxList], Max[maxList], numInt];\), "\[IndentingNewLine]", \(Print["\", Length[runningMaxList], "\<.\>"]\), "\[IndentingNewLine]", RowBox[{ RowBox[{"graph1", "=", RowBox[{ "Areabargraph", "[", \(runningMaxList, Min[runningMaxList], Max[runningMaxList], numInt\), StyleBox["]", FontColor->GrayLevel[0]]}]}], ";"}], "\[IndentingNewLine]", \(graph2 = \(graph2 = Plot[\(\@\(2\/\(\[Pi]\ upTo\)\)\) \[ExponentialE]\^\(-\(a\^2\/\(2\ \ upTo\)\)\), {a, Min[runningMaxList], Max[runningMaxList]}, DisplayFunction \[Rule] Identity]\);\), "\[IndentingNewLine]", \(Print["\"]\), "\[IndentingNewLine]", \(Show[ graph1, graph2, DisplayFunction \[Rule] $DisplayFunction];\)}], "Input"] }, Closed]] }, Open ]] }, FrontEndVersion->"4.1 for Microsoft Windows", ScreenRectangle->{{0, 1024}, {0, 720}}, AutoGeneratedPackage->None, WindowToolbars->"EditBar", WindowSize->{779, 673}, WindowMargins->{{0, Automatic}, {Automatic, 0}} ] (******************************************************************* 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->{ "Random"->{ Cell[2533, 81, 168, 4, 30, "Input", InitializationCell->True, CellTags->"Random"]} } *) (*CellTagsIndex CellTagsIndex->{ {"Random", 15849, 448} } *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1727, 52, 116, 2, 186, "Title"], Cell[1846, 56, 250, 4, 44, "SmallText"], Cell[2099, 62, 170, 5, 33, "Text"], Cell[CellGroupData[{ Cell[2294, 71, 33, 0, 59, "Section"], Cell[2330, 73, 104, 2, 30, "Input", InitializationCell->True], Cell[2437, 77, 93, 2, 30, "Input", InitializationCell->True], Cell[2533, 81, 168, 4, 30, "Input", InitializationCell->True, CellTags->"Random"], Cell[2704, 87, 334, 7, 70, "Input", InitializationCell->True], Cell[3041, 96, 144, 3, 50, "Input", InitializationCell->True], Cell[3188, 101, 2575, 71, 1200, "Input", InitializationCell->True], Cell[CellGroupData[{ Cell[5788, 176, 68, 0, 47, "Subsection"], Cell[5859, 178, 141, 3, 50, "Input", InitializationCell->True] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[6049, 187, 44, 0, 39, "Section"], Cell[6096, 189, 823, 22, 90, "Text"], Cell[6922, 213, 1301, 25, 400, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[8260, 243, 68, 0, 39, "Section"], Cell[8331, 245, 585, 16, 52, "Text"], Cell[8919, 263, 1013, 21, 362, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[9969, 289, 60, 0, 39, "Section"], Cell[10032, 291, 350, 9, 52, "Text"], Cell[10385, 302, 42, 1, 30, "Input"], Cell[10430, 305, 498, 16, 52, "Text"], Cell[10931, 323, 68, 1, 30, "Input"], Cell[11002, 326, 1412, 40, 128, "Text"], Cell[12417, 368, 2759, 59, 689, "Input"] }, Closed]] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)