(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 4.0, MathReader 4.0, or any compatible application. 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[ 9423, 295]*) (*NotebookOutlinePosition[ 10187, 322]*) (* CellTagsIndexPosition[ 10143, 318]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["AnnuitySolve Examples", "Title"], Cell[CellGroupData[{ Cell["Start up", "Section"], Cell[BoxData[ \(Off[General::spell1]\)], "Input"], Cell[BoxData[ \(<< AnnuitySolve`\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Example 1: Getting started. One-factor model", "Section"], Cell[CellGroupData[{ Cell["Setup", "Subsection"], Cell[BoxData[{ \(\(pde\ = \ \ k \((m - x)\) D[A[x, t], \ x]\ + \ x\ s^2/2\ D[A[x, t], \ {x, \ 2}]\ - \n\tD[A[x, t], \ t]\ - \ x\ A[x, t]\ + \ \[Nu]\ x\ s^2/2\ D[A[x, t], \ x]^2/ A[x, t];\)\n\), "\n", \(\(Npde[ k_, \ \[Nu]_]\ = \ \ pde\ + 1 /. \ {m\ -> \ 1/20, \ s\ -> \ 1/5};\)\), "\n", \(\(Nbondpde[k_, \ \[Nu]_]\ = \ pde\ /. \ {m\ -> \ 1/20, \ s\ -> \ 1/5};\)\n\), "\n", \(\(x0\ = \ 1/20;\)\)}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Solve", "Subsection"], Cell[CellGroupData[{ Cell["First part: Getting started", "Subsubsection"], Cell["Solve linear pde with k = 1, 3rd-order polynomial", "Text"], Cell[BoxData[ \(\(Asoln\ = \ NAnnuitySolve[Npde[1, \ 0], \ A, \ {t, \ 0, \ 1000}, \ {x, \ x0}, \ \ 3];\)\)], "Input"], Cell["Plot the solution", "Text"], Cell[BoxData[ \(\(Plot3D[ Asoln[x, t], \ {x, \ 0, \ .2}, \ {t, \ 0, \ 1000}];\)\)], "Input"], Cell["How well does the solution fit the pde?", "Text"], Cell[BoxData[{ \(\(resid\ = \ ResidualFromPDE[Npde[1, \ 0], \ \ Asoln];\)\), "\n", \(\(Plot3D[ Abs[resid[x, t]], \ {x, \ 0, \ .2}, \ {t, \ 0, \ 1000}];\)\)}], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Second part: More complicated", "Subsubsection"], Cell["\<\ Loop over values of k. (Series solution with low k converges slowly.)\ \>", "Text"], Cell[BoxData[ \(\(Atab\ = \ Table[\n\t NAnnuitySolve[Npde[10^\((\(-i\))\), \ 0], \ A, \ {t, \ 0, \ 1000}, {x, x0}, \ \ order], \n\t{i, \ 0, \ 3}, \ {order, \ 1, \ 5}];\)\)], "Input"], Cell["Get annuity series from bond prices", "Text"], Cell[BoxData[{ \(\(Asoln\ = \ NAnnuitySolve[Npde[1/1000, \ 0], \ A, \ \ {t, \ 0, \ 1000}, \ {x, \ x0}, 3];\)\), "\n", \(\(resid\ = \ ResidualFromPDE[Npde[1/1000, 0], \ Asoln];\)\), "\n", \(\(Bsoln\ = \ NAnnuitySolve[Nbondpde[1/1000, 0], \ A, \ \ {t, \ 0, \ 1000}, {x, x0}, \ 1, \n\t FunctionalForm\ -> \ Exp];\)\), "\n", \(\(Bcoeffs\ = \ NBondToAnnuitySeries[Bsoln[x, t], {t, \ 0, \ 1000}, \ {x, x0}, \ 5];\)\)}], "Input"], Cell["Use Bcoeffs to fix series solution ", "Text"], Cell[BoxData[{ \(\(Asoln1\ = \ NAnnuitySolve[Npde[1/1000, 0], \ A, \ {t, \ 0, \ 1000}, \ {x, x0}, 3, \n\tPolynomialOrderDifferential\ -> \ 2, \ DifferentialLoadings\ -> \ Bcoeffs];\)\), "\n", \(\(resid1\ = \ ResidualFromPDE[Npde[1/1000, 0], \ Asoln1];\)\)}], "Input"], Cell["Compare the two solutions", "Text"], Cell[BoxData[{ \(\(Plot3D[ Asoln[x, t], \ {x, \ 0, \ .2}, \ {t, \ 0, \ 1000}];\)\), "\n", \(\(Plot3D[ Asoln1[x, t], \ {x, \ 0, \ .2}, \ {t, \ 0, \ 1000}];\)\), "\n", \(\(Plot3D[ Abs[resid[x, t]], \ {x, \ 0, \ .2}, \ {t, \ 0, \ 1000}];\)\), "\n", \(\(Plot3D[ Abs[resid1[x, t]], \ {x, \ 0, \ .2}, \ {t, \ 0, \ 1000}];\)\)}], "Input"] }, Closed]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Example 2: Two-factor model", "Section"], Cell[CellGroupData[{ Cell["Setup", "Subsection"], Cell[BoxData[{ \(\(pde2\ = \ \ 1\ - \ \((x1\ + \ x2)\)* A[x1, \ x2, \ t]\ - \ \(\(Derivative[0, \ 0, \ 1]\)[A]\)[x1, \ x2, \ t]\ + \n\ \ \((k2*\((m2\ - \ x2)\)\ - \ sX2*x2*\[Lambda]2)\)*\(\(Derivative[0, \ 1, \ 0]\)[A]\)[x1, \ x2, \ t]\ + \n\ \ \((k1*\((m1\ - \ x1)\)\ - \ sX1*x1*\[Lambda]1)\)*\(\(Derivative[1, \ 0, \ 0]\)[A]\)[x1, \ x2, \ t]\ - \n\ \ \((\[Nu]*\((\((sX2^2* x2*\(\(Derivative[0, \ 1, \ 0]\)[A]\)[x1, \ x2, \ t]^2)\)/ A[x1, \ x2, \ t]^2\ + \n\ \ \((sX1^2* x1*\(\(Derivative[1, \ 0, \ 0]\)[A]\)[x1, \ x2, \ t]^2)\)/A[x1, \ x2, \ t]^2)\))\)/ 2\ + \n\ \ \((sX2^2* x2*\(\(Derivative[0, \ 2, \ 0]\)[A]\)[x1, \ x2, \ t]\ + \n\ \ \ \ \ sX1^2* x1*\(\(Derivative[2, \ 0, \ 0]\)[A]\)[x1, \ x2, \ t])\)/ 2\ ;\)\n\), "\n", \(\(nrule2\ = \ {k1\ -> \ 97/125, \ m1\ -> \ 3209/100000, \ sX1\ -> \ 82/625, \n\ \ \[Lambda]1\ -> \ \(-593\)/5000, \ k2\ -> \ 8991/10000000, \n\ \ m2\ -> \ 1061/50000, \ sX2\ -> \ 2657/50000, \n\ \ \[Lambda]2\ -> \ \(-4151\)/100000, \ k3\ -> \ 3/500, \ m3\ -> \ 1/100, \n\ \ sX3\ -> \ 9/50, \ \[Lambda]3\ -> \ \(-13\)/100, \ \[Nu]\ -> \ 0}\ ;\)\n\), "\n", \(\(npde2\ = \ pde2\ /. \ nrule2;\)\), "\n", \(\(xargs\ = \ Sequence\ @@ \ \((Transpose[{{x1, \ x2}, \ {m1, \ m2}}]\ /. \ nrule2)\);\)\)}], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Solve", "Subsection"], Cell[BoxData[ \(\(Asoln2\ = \ NAnnuitySolve[npde2, \ A, \ {t, \ 0, \ 10000}, \ xargs, \ 3];\)\)], "Input"], Cell["Get annuity series from bond prices", "Text"], Cell[BoxData[{ \(\(Bsoln2\ = \ NAnnuitySolve[npde2\ - \ 1, \ A, {t, \ 0, \ 1000}, \ xargs, \ 1, \ FunctionalForm\ -> \ Exp];\)\), "\n", \(\(Bcoeffs2\ = \ NBondToAnnuitySeries[Bsoln2[x1, \ x2, \ t], \ {t, \ 0, \ 1000}, \ xargs, \ 5];\)\)}], "Input"], Cell["Use Bcoeffs to fix series solution", "Text"], Cell[BoxData[ \(\(Asoln3\ = \ NAnnuitySolve[npde2, \ A, {t, \ 0, \ 1000}, xargs, \ 3, \ PolynomialOrderDifferential\ -> \ 2, \n\t DifferentialLoadings\ -> \ Bcoeffs2];\)\)], "Input"], Cell["Compare with direct bond series at t = 1000", "Text"], Cell[BoxData[{ \(\(bondseries\ = \ MakePolynomial[{x1, x2} - {m1, m2}\ /. \ nrule2, \ t, \n\t5]\ /. \ Bcoeffs2;\)\), "\n", \(\(Plot3D[ Asoln3[x1, \ x2, \ 1000]\ - \ bondseries\ /. \ t\ -> \ 1000, \n\t{x1, \ 0, \ .2}, \ {x2, \ 0, \ .2}];\)\)}], "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Example 3: Symbolic solution to Vasicek bond prices.", "Section"], Cell[BoxData[ \(\(pde\ = \ k \((m - x)\) D[B[x, t], \ x]\ + \ s^2/2\ D[B[x, t], \ {x, \ 2}]\ - \n\tD[B[x, t], \ t]\ - \ x\ B[x, t];\)\)], "Input"], Cell[BoxData[ \(Bsoln\ = \ AnnuitySolve[pde, \ B, \ t, \ {x, \ m}, \ \ 1, \ FunctionalForm\ -> \ Exp]\)], "Input"], Cell[BoxData[ \(\(ResidualFromPDE[pde, \ Bsoln]\)[x, t] // Simplify\)], "Input"], Cell[BoxData[ \(BondToAnnuitySeries[Bsoln[x, t] /. s -> 0, \ t, \ {x, \ m}, \ 2]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["\<\ Example 4: Symbolic solution for 1st 3 terms for annuity with deterministic \ state variable.\ \>", "Section"], Cell["Compare this to the last part of the previous example.", "Text"], Cell[BoxData[ \(\(pde\ = \ 1\ + \ k \((m - x)\) D[A[x, t], \ x]\ - \ D[A[x, t], \ t]\ - \ x\ A[x, t];\)\)], "Input"], Cell[BoxData[ \(Asoln\ = \ Simplify\ /@ \ \(AnnuitySolve[pde, \ A, \ t, {x, \ m}, \ 2]\)[x, t]\)], "Input"] }, Closed]] }, Open ]] }, FrontEndVersion->"4.0 for Microsoft Windows", ScreenRectangle->{{0, 1280}, {0, 975}}, WindowToolbars->"EditBar", WindowSize->{614, 696}, WindowMargins->{{Automatic, 22}, {Automatic, 33}}, PrintingCopies->1, PrintingPageRange->{Automatic, Automatic}, Magnification->1.5 ] (*********************************************************************** 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[1739, 51, 38, 0, 220, "Title"], Cell[CellGroupData[{ Cell[1802, 55, 27, 0, 79, "Section"], Cell[1832, 57, 53, 1, 42, "Input"], Cell[1888, 60, 49, 1, 42, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[1974, 66, 63, 0, 47, "Section"], Cell[CellGroupData[{ Cell[2062, 70, 27, 0, 68, "Subsection"], Cell[2092, 72, 507, 10, 302, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2636, 87, 27, 0, 68, "Subsection"], Cell[CellGroupData[{ Cell[2688, 91, 52, 0, 61, "Subsubsection"], Cell[2743, 93, 65, 0, 47, "Text"], Cell[2811, 95, 142, 3, 68, "Input"], Cell[2956, 100, 33, 0, 47, "Text"], Cell[2992, 102, 105, 2, 68, "Input"], Cell[3100, 106, 55, 0, 47, "Text"], Cell[3158, 108, 197, 4, 120, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3392, 117, 54, 0, 61, "Subsubsection"], Cell[3449, 119, 93, 2, 47, "Text"], Cell[3545, 123, 232, 5, 120, "Input"], Cell[3780, 130, 51, 0, 47, "Text"], Cell[3834, 132, 516, 11, 276, "Input"], Cell[4353, 145, 51, 0, 47, "Text"], Cell[4407, 147, 320, 6, 172, "Input"], Cell[4730, 155, 41, 0, 47, "Text"], Cell[4774, 157, 397, 9, 224, "Input"] }, Closed]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[5232, 173, 46, 0, 47, "Section"], Cell[CellGroupData[{ Cell[5303, 177, 27, 0, 68, "Subsection"], Cell[5333, 179, 1695, 29, 822, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[7065, 213, 27, 0, 68, "Subsection"], Cell[7095, 215, 131, 3, 68, "Input"], Cell[7229, 220, 51, 0, 40, "Text"], Cell[7283, 222, 301, 6, 172, "Input"], Cell[7587, 230, 50, 0, 47, "Text"], Cell[7640, 232, 219, 4, 120, "Input"], Cell[7862, 238, 59, 0, 47, "Text"], Cell[7924, 240, 319, 7, 224, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[8292, 253, 71, 0, 78, "Section"], Cell[8366, 255, 185, 4, 94, "Input"], Cell[8554, 261, 136, 3, 68, "Input"], Cell[8693, 266, 84, 1, 68, "Input"], Cell[8780, 269, 104, 2, 68, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[8921, 276, 120, 3, 78, "Section"], Cell[9044, 281, 70, 0, 47, "Text"], Cell[9117, 283, 144, 3, 68, "Input"], Cell[9264, 288, 131, 3, 94, "Input"] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)