diff --git a/notebooks/compute_table2.ipynb b/notebooks/compute_table2.ipynb index 385b0b1..62c3aa2 100644 --- a/notebooks/compute_table2.ipynb +++ b/notebooks/compute_table2.ipynb @@ -38,7 +38,7 @@ "from src.Option import OptionType, EuropeanOption\n", "\n", "import pandas as pd\n", - "import time" + "import time\n" ] }, { @@ -63,7 +63,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "60fde912-9ae3-4d14-8c08-700d43de8059", "metadata": {}, "outputs": [ @@ -73,7 +73,7 @@ "9.462492596167081" ] }, - "execution_count": 5, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -84,7 +84,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 5, "id": "cbb9be73-ad6b-48df-8662-83596b6f4daf", "metadata": {}, "outputs": [], @@ -92,14 +92,12 @@ "l_values = [5, 7, 11, 15, 15, 25, 25, 25, 35, 51, 65]\n", "m_values = [1, 2, 2, 2, 3, 4, 5, 6, 8, 8, 8]\n", "n_values = [4, 5, 5, 6, 7, 9, 12, 15, 16, 24, 32]\n", - "p_values = [15, 20, 31, 41, 41, 51, 61, 61, 81, 101, 101]\n", - "\n", - "results = []" + "p_values = [15, 20, 31, 41, 41, 51, 61, 61, 81, 101, 101]\n" ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 6, "id": "07c801a3-4e96-4c42-ab48-32d91c6644f2", "metadata": { "scrolled": true @@ -109,6 +107,7 @@ "name": "stdout", "output_type": "stream", "text": [ + "tanh_sinh!\n", "Starting iteration 1/16\n", "Iteration 1/16 completed.\n", "Starting iteration 2/16\n", @@ -150,7 +149,7 @@ "0.10695282147080121" ] }, - "execution_count": 11, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -179,9 +178,17 @@ "benchmark_premium" ] }, + { + "cell_type": "markdown", + "id": "4f9e66f4-1bec-4876-8758-b26507e00ff5", + "metadata": {}, + "source": [ + "# Table2" + ] + }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 8, "id": "cb31441b-62ba-4f23-a30e-2cf4febf3df7", "metadata": { "scrolled": true @@ -199,16 +206,19 @@ "Starting iteration 2/2\n", "Iteration 2/2 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/2\n", "Iteration 1/2 completed.\n", "Starting iteration 2/2\n", "Iteration 2/2 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/2\n", "Iteration 1/2 completed.\n", "Starting iteration 2/2\n", "Iteration 2/2 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/3\n", "Iteration 1/3 completed.\n", "Starting iteration 2/3\n", @@ -216,6 +226,7 @@ "Starting iteration 3/3\n", "Iteration 3/3 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/4\n", "Iteration 1/4 completed.\n", "Starting iteration 2/4\n", @@ -225,6 +236,7 @@ "Starting iteration 4/4\n", "Iteration 4/4 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/5\n", "Iteration 1/5 completed.\n", "Starting iteration 2/5\n", @@ -236,6 +248,7 @@ "Starting iteration 5/5\n", "Iteration 5/5 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/6\n", "Iteration 1/6 completed.\n", "Starting iteration 2/6\n", @@ -249,6 +262,7 @@ "Starting iteration 6/6\n", "Iteration 6/6 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/8\n", "Iteration 1/8 completed.\n", "Starting iteration 2/8\n", @@ -266,6 +280,7 @@ "Starting iteration 8/8\n", "Iteration 8/8 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/8\n", "Iteration 1/8 completed.\n", "Starting iteration 2/8\n", @@ -283,6 +298,7 @@ "Starting iteration 8/8\n", "Iteration 8/8 completed.\n", "Jacobi-Newton iterations completed.\n", + "tanh_sinh!\n", "Starting iteration 1/8\n", "Iteration 1/8 completed.\n", "Starting iteration 2/8\n", @@ -304,6 +320,8 @@ } ], "source": [ + "\n", + "results = []\n", "for l, m, n, p in zip(l_values, m_values, n_values, p_values):\n", " if l == 5 or l == 7:\n", " solver = AmericanOptionPricing(\n", @@ -350,12 +368,16 @@ " relative_error = abs(premium - benchmark_premium) / benchmark_premium\n", " results.append([f\"({l},{m},{n})\", p, premium, relative_error, cpu_seconds])\n", "\n", - "df = pd.DataFrame(results, columns=[\"(l,m,n)\", \"p\", \"American Premium\", \"Relative Error\", \"CPU Seconds\"])\n" + "df = pd.DataFrame(results, columns=[\"(l,m,n)\", \"p\", \"American Premium\", \"Relative Error\", \"CPU Seconds\"])\n", + "\n", + "df[\"Relative Error\"] = df[\"Relative Error\"].apply(lambda x: f\"{x:.2E}\")\n", + "df[\"CPU Seconds\"] = df[\"CPU Seconds\"].apply(lambda x: f\"{x:.2E}\")\n", + "df[\"American Premium\"] = df[\"American Premium\"].apply(lambda x: f\"{x:.12f}\")" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 14, "id": "6881e5e7-16a2-4995-a94c-fc1e74b6f2c1", "metadata": {}, "outputs": [ @@ -392,110 +414,110 @@ " 0\n", " (5,1,4)\n", " 15\n", - " 0.109893\n", - " 0.027492\n", - " 0.028666\n", + " 0.109893160420\n", + " 2.75E-02\n", + " 2.93E-02\n", " \n", " \n", " 1\n", " (7,2,5)\n", " 20\n", - " 0.108360\n", - " 0.013159\n", - " 0.039296\n", + " 0.108360233651\n", + " 1.32E-02\n", + " 3.50E-02\n", " \n", " \n", " 2\n", " (11,2,5)\n", " 31\n", - " 0.108361\n", - " 0.013167\n", - " 0.038025\n", + " 0.108361063777\n", + " 1.32E-02\n", + " 3.84E-02\n", " \n", " \n", " 3\n", " (15,2,6)\n", " 41\n", - " 0.108376\n", - " 0.013311\n", - " 0.049134\n", + " 0.108376475656\n", + " 1.33E-02\n", + " 5.05E-02\n", " \n", " \n", " 4\n", " (15,3,7)\n", " 41\n", - " 0.107673\n", - " 0.006738\n", - " 0.072467\n", + " 0.107673499674\n", + " 6.74E-03\n", + " 7.35E-02\n", " \n", " \n", " 5\n", " (25,4,9)\n", " 51\n", - " 0.107321\n", - " 0.003442\n", - " 0.156964\n", + " 0.107320971004\n", + " 3.44E-03\n", + " 1.63E-01\n", " \n", " \n", " 6\n", " (25,5,12)\n", " 61\n", - " 0.107142\n", - " 0.001766\n", - " 0.239185\n", + " 0.107141725128\n", + " 1.77E-03\n", + " 2.21E-01\n", " \n", " \n", " 7\n", " (25,6,15)\n", " 61\n", - " 0.107050\n", - " 0.000908\n", - " 0.342008\n", + " 0.107049901336\n", + " 9.08E-04\n", + " 3.47E-01\n", " \n", " \n", " 8\n", " (35,8,16)\n", " 81\n", - " 0.106978\n", - " 0.000239\n", - " 0.627159\n", + " 0.106978404798\n", + " 2.39E-04\n", + " 5.72E-01\n", " \n", " \n", " 9\n", " (51,8,24)\n", " 101\n", - " 0.106978\n", - " 0.000239\n", - " 1.308139\n", + " 0.106978404366\n", + " 2.39E-04\n", + " 1.22E+00\n", " \n", " \n", " 10\n", " (65,8,32)\n", " 101\n", - " 0.106978\n", - " 0.000239\n", - " 2.078899\n", + " 0.106978404238\n", + " 2.39E-04\n", + " 2.03E+00\n", " \n", " \n", "\n", "" ], "text/plain": [ - " (l,m,n) p American Premium Relative Error CPU Seconds\n", - "0 (5,1,4) 15 0.109893 0.027492 0.028666\n", - "1 (7,2,5) 20 0.108360 0.013159 0.039296\n", - "2 (11,2,5) 31 0.108361 0.013167 0.038025\n", - "3 (15,2,6) 41 0.108376 0.013311 0.049134\n", - "4 (15,3,7) 41 0.107673 0.006738 0.072467\n", - "5 (25,4,9) 51 0.107321 0.003442 0.156964\n", - "6 (25,5,12) 61 0.107142 0.001766 0.239185\n", - "7 (25,6,15) 61 0.107050 0.000908 0.342008\n", - "8 (35,8,16) 81 0.106978 0.000239 0.627159\n", - "9 (51,8,24) 101 0.106978 0.000239 1.308139\n", - "10 (65,8,32) 101 0.106978 0.000239 2.078899" + " (l,m,n) p American Premium Relative Error CPU Seconds\n", + "0 (5,1,4) 15 0.109893160420 2.75E-02 2.93E-02\n", + "1 (7,2,5) 20 0.108360233651 1.32E-02 3.50E-02\n", + "2 (11,2,5) 31 0.108361063777 1.32E-02 3.84E-02\n", + "3 (15,2,6) 41 0.108376475656 1.33E-02 5.05E-02\n", + "4 (15,3,7) 41 0.107673499674 6.74E-03 7.35E-02\n", + "5 (25,4,9) 51 0.107320971004 3.44E-03 1.63E-01\n", + "6 (25,5,12) 61 0.107141725128 1.77E-03 2.21E-01\n", + "7 (25,6,15) 61 0.107049901336 9.08E-04 3.47E-01\n", + "8 (35,8,16) 81 0.106978404798 2.39E-04 5.72E-01\n", + "9 (51,8,24) 101 0.106978404366 2.39E-04 1.22E+00\n", + "10 (65,8,32) 101 0.106978404238 2.39E-04 2.03E+00" ] }, - "execution_count": 13, + "execution_count": 14, "metadata": {}, "output_type": "execute_result" } @@ -504,15 +526,344 @@ "df" ] }, + { + "cell_type": "markdown", + "id": "6876b57f-d035-41bb-94ba-6e219e78ebbc", + "metadata": {}, + "source": [ + "# purely Gauss-Legendre" + ] + }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "8c970c37-a214-4a61-b1d6-cdef1027dc9a", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Starting iteration 1/1\n", + "Iteration 1/1 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/2\n", + "Iteration 1/2 completed.\n", + "Starting iteration 2/2\n", + "Iteration 2/2 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/2\n", + "Iteration 1/2 completed.\n", + "Starting iteration 2/2\n", + "Iteration 2/2 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/2\n", + "Iteration 1/2 completed.\n", + "Starting iteration 2/2\n", + "Iteration 2/2 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/3\n", + "Iteration 1/3 completed.\n", + "Starting iteration 2/3\n", + "Iteration 2/3 completed.\n", + "Starting iteration 3/3\n", + "Iteration 3/3 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/4\n", + "Iteration 1/4 completed.\n", + "Starting iteration 2/4\n", + "Iteration 2/4 completed.\n", + "Starting iteration 3/4\n", + "Iteration 3/4 completed.\n", + "Starting iteration 4/4\n", + "Iteration 4/4 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/5\n", + "Iteration 1/5 completed.\n", + "Starting iteration 2/5\n", + "Iteration 2/5 completed.\n", + "Starting iteration 3/5\n", + "Iteration 3/5 completed.\n", + "Starting iteration 4/5\n", + "Iteration 4/5 completed.\n", + "Starting iteration 5/5\n", + "Iteration 5/5 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/6\n", + "Iteration 1/6 completed.\n", + "Starting iteration 2/6\n", + "Iteration 2/6 completed.\n", + "Starting iteration 3/6\n", + "Iteration 3/6 completed.\n", + "Starting iteration 4/6\n", + "Iteration 4/6 completed.\n", + "Starting iteration 5/6\n", + "Iteration 5/6 completed.\n", + "Starting iteration 6/6\n", + "Iteration 6/6 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/8\n", + "Iteration 1/8 completed.\n", + "Starting iteration 2/8\n", + "Iteration 2/8 completed.\n", + "Starting iteration 3/8\n", + "Iteration 3/8 completed.\n", + "Starting iteration 4/8\n", + "Iteration 4/8 completed.\n", + "Starting iteration 5/8\n", + "Iteration 5/8 completed.\n", + "Starting iteration 6/8\n", + "Iteration 6/8 completed.\n", + "Starting iteration 7/8\n", + "Iteration 7/8 completed.\n", + "Starting iteration 8/8\n", + "Iteration 8/8 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/8\n", + "Iteration 1/8 completed.\n", + "Starting iteration 2/8\n", + "Iteration 2/8 completed.\n", + "Starting iteration 3/8\n", + "Iteration 3/8 completed.\n", + "Starting iteration 4/8\n", + "Iteration 4/8 completed.\n", + "Starting iteration 5/8\n", + "Iteration 5/8 completed.\n", + "Starting iteration 6/8\n", + "Iteration 6/8 completed.\n", + "Starting iteration 7/8\n", + "Iteration 7/8 completed.\n", + "Starting iteration 8/8\n", + "Iteration 8/8 completed.\n", + "Jacobi-Newton iterations completed.\n", + "Starting iteration 1/8\n", + "Iteration 1/8 completed.\n", + "Starting iteration 2/8\n", + "Iteration 2/8 completed.\n", + "Starting iteration 3/8\n", + "Iteration 3/8 completed.\n", + "Starting iteration 4/8\n", + "Iteration 4/8 completed.\n", + "Starting iteration 5/8\n", + "Iteration 5/8 completed.\n", + "Starting iteration 6/8\n", + "Iteration 6/8 completed.\n", + "Starting iteration 7/8\n", + "Iteration 7/8 completed.\n", + "Starting iteration 8/8\n", + "Iteration 8/8 completed.\n", + "Jacobi-Newton iterations completed.\n" + ] + } + ], + "source": [ + "\n", + "results = []\n", + "for l, m, n, p in zip(l_values, m_values, n_values, p_values):\n", + " solver = AmericanOptionPricing(\n", + " K=K,\n", + " r=r,\n", + " q=q,\n", + " vol=sigma,\n", + " tau_max=tau_max,\n", + " n=n,\n", + " l=l,\n", + " p=p,\n", + " m=m,\n", + " option_type=OptionType.Put,\n", + " quadrature_type=QuadratureType.Gauss_Legendre,\n", + " eta=eta\n", + " )\n", + "\n", + " start_time = time.time()\n", + "\n", + " solver.run_full_algorithm()\n", + " solver.compute_pricing_points()\n", + " solver.compute_option_pricing(S=S)\n", + " premium = solver.compute_option_pricing(S=S)[1]\n", + "\n", + " end_time = time.time()\n", + "\n", + " cpu_seconds = end_time - start_time\n", + "\n", + " relative_error = abs(premium - benchmark_premium) / benchmark_premium\n", + " results.append([f\"({l},{m},{n})\", p, premium, relative_error, cpu_seconds])\n", + "\n", + "df = pd.DataFrame(results, columns=[\"(l,m,n)\", \"p\", \"American Premium\", \"Relative Error\", \"CPU Seconds\"])\n", + "\n", + "df[\"Relative Error\"] = df[\"Relative Error\"].apply(lambda x: f\"{x:.2E}\")\n", + "df[\"CPU Seconds\"] = df[\"CPU Seconds\"].apply(lambda x: f\"{x:.2E}\")\n", + "df[\"American Premium\"] = df[\"American Premium\"].apply(lambda x: f\"{x:.12f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "fc1fde90-8d63-43c8-9a0b-df80474b8aa4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
(l,m,n)pAmerican PremiumRelative ErrorCPU Seconds
0(5,1,4)150.1098931604202.75E-025.06E-02
1(7,2,5)200.1083602336511.32E-024.28E-02
2(11,2,5)310.1083607750441.32E-024.13E-02
3(15,2,6)410.1083764002421.33E-025.11E-02
4(15,3,7)410.1076734422256.74E-037.02E-02
5(25,4,9)510.1073209518853.44E-031.87E-01
6(25,5,12)610.1071417177881.77E-032.29E-01
7(25,6,15)610.1070498949709.08E-043.20E-01
8(35,8,16)810.1069784026612.39E-045.72E-01
9(51,8,24)1010.1069784034812.39E-041.24E+00
10(65,8,32)1010.1069784031922.39E-042.07E+00
\n", + "
" + ], + "text/plain": [ + " (l,m,n) p American Premium Relative Error CPU Seconds\n", + "0 (5,1,4) 15 0.109893160420 2.75E-02 5.06E-02\n", + "1 (7,2,5) 20 0.108360233651 1.32E-02 4.28E-02\n", + "2 (11,2,5) 31 0.108360775044 1.32E-02 4.13E-02\n", + "3 (15,2,6) 41 0.108376400242 1.33E-02 5.11E-02\n", + "4 (15,3,7) 41 0.107673442225 6.74E-03 7.02E-02\n", + "5 (25,4,9) 51 0.107320951885 3.44E-03 1.87E-01\n", + "6 (25,5,12) 61 0.107141717788 1.77E-03 2.29E-01\n", + "7 (25,6,15) 61 0.107049894970 9.08E-04 3.20E-01\n", + "8 (35,8,16) 81 0.106978402661 2.39E-04 5.72E-01\n", + "9 (51,8,24) 101 0.106978403481 2.39E-04 1.24E+00\n", + "10 (65,8,32) 101 0.106978403192 2.39E-04 2.07E+00" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "df" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef8a98b6-e23b-4e13-8459-7093a4956c75", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86bdc211-850d-4673-9adc-e15613dfc972", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {