HSG-MCS-HS21_Julia/JuliaTutorial-master/Tutorial_24_Integration.ipynb

702 lines
105 KiB
Plaintext
Raw Normal View History

2021-11-15 20:14:51 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numerical Integration and Differentiation\n",
"\n",
"This notebook illustrates how to perform numerical integration and differentiation. \n",
"\n",
"There are several packages for doing this. Here, the focus is on [QuadGK](https://github.com/JuliaMath/QuadGK.jl) and [ForwardDiff](https://github.com/JuliaDiff/ForwardDiff.jl)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Packages and Extra Functions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"printyellow (generic function with 1 method)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Printf, QuadGK, ForwardDiff\n",
"\n",
"include(\"jlFiles/printmat.jl\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"using Plots\n",
"\n",
"#pyplot(size=(600,400))\n",
"gr(size=(480,320))\n",
"default(fmt = :svg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Numerical Integration\n",
"\n",
"As a simple illustration, the next cells plot and integrate the $N(0,\\sigma)$ pdf."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Pdf of the N() Distribution \n",
"\n",
"Notice: the function takes $\\mu$ and $\\sigma$ (not $\\sigma^2$) as inputs, similar to the Distributions package. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ϕNS (generic function with 3 methods)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function ϕNS(x,μ=0,σ=1) #pdf of N(μ,σ), defaults to N(0,1)\n",
" z = (x - μ)/σ\n",
" pdf = exp(-0.5*z^2)/(sqrt(2*pi)*σ)\n",
" return pdf\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
"<defs>\n",
" <clipPath id=\"clip790\">\n",
" <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip790)\" d=\"\n",
"M0 1280 L1920 1280 L1920 0 L0 0 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip791\">\n",
" <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip790)\" d=\"\n",
"M143.534 1106.38 L1872.76 1106.38 L1872.76 123.472 L143.534 123.472 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip792\">\n",
" <rect x=\"143\" y=\"123\" width=\"1730\" height=\"984\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 143.534,1106.38 143.534,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 431.738,1106.38 431.738,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 719.942,1106.38 719.942,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1008.15,1106.38 1008.15,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1296.35,1106.38 1296.35,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1584.55,1106.38 1584.55,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1872.76,1106.38 1872.76,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,1106.38 1872.76,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,1106.38 143.534,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 431.738,1106.38 431.738,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 719.942,1106.38 719.942,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1008.15,1106.38 1008.15,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1296.35,1106.38 1296.35,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1584.55,1106.38 1584.55,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1872.76,1106.38 1872.76,1094.58 \n",
" \"/>\n",
"<path clip-path=\"url(#clip790)\" d=\"M 0 0 M115.514 1146.38 L145.189 1146.38 L145.189 1150.32 L115.514 1150.32 L115.514 1146.38 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M164.425 1144.58 Q167.782 1145.3 169.657 1147.56 Q171.555 1149.83 171.555 1153.17 Q171.555 1158.28 168.037 1161.08 Q164.518 1163.88 158.037 1163.88 Q155.861 1163.88 153.546 1163.44 Q151.254 1163.03 148.801 1162.17 L148.801 1157.66 Q150.745 1158.79 153.06 1159.37 Q155.375 1159.95 157.898 1159.95 Q162.296 1159.95 164.588 1158.21 Q166.902 1156.48 166.902 1153.17 Q166.902 1150.11 164.75 1148.4 Q162.62 1146.66 158.801 1146.66 L154.773 1146.66 L154.773 1142.82 L158.986 1142.82 Q162.435 1142.82 164.263 1141.45 Q166.092 1140.06 166.092 1137.47 Q166.092 1134.81 164.194 1133.4 Q162.319 1131.96 158.801 1131.96 Q156.879 1131.96 154.68 1132.38 Q152.481 1132.8 149.842 1133.67 L149.842 1129.51 Q152.504 1128.77 154.819 1128.4 Q157.157 1128.03 159.217 1128.03 Q164.541 1128.03 167.643 1130.46 Q170.745 1132.86 170.745 1136.99 Q170.745 1139.86 169.101 1141.85 Q167.458 1143.81 164.425 1144.58 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M404.192 1146.38 L433.868 1146.38 L433.868 1150.32 L404.192 1150.32 L404.192 1146.38 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M442.965 1159.28 L459.284 1159.28 L459.284 1163.21 L437.34 1163.21 L437.34 1159.28 Q440.002 1156.52 444.585 1151.89 Q449.192 1147.24 450.372 1145.9 Q452.617 1143.37 453.497 1141.64 Q454.4 1139.88 454.4 1138.19 Q454.4 1135.43 452.455 1133.7 Q450.534 1131.96 447.432 1131.96 Q445.233 1131.96 442.78 1132.73 Q440.349 1133.49 437.571 1135.04 L437.571 1130.32 Q440.395 1129.18 442.849 1128.61 Q445.303 1128.03 447.34 1128.03 Q452.71 1128.03 455.905 1130.71 Q459.099 1133.4 459.099 1137.89 Q459.099 1140.02 458.289 1141.94 Q457.502 1143.84 455.395 1146.43 Q454.817 1147.1 451.715 1150.32 Q448.613 1153.51 442.965 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M692.21 1146.38 L721.886 1146.38 L721.886 1150.32 L692.21 1150.32 L692.21 1146.38 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M727.766 1159.28 L735.404 1159.28 L735.404 1132.91 L727.094 1134.58 L727.094 1130.32 L735.358 1128.65 L740.034 1128.65 L740.034 1159.28 L747.673 1159.28 L747.673 1163.21 L727.766 1163.21 L727.766 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1008.15 1131.73 Q1004.53 1131.73 1002.71 1135.3 Q1000.9 1138.84 1000.9 1145.97 Q1000.9 1153.07 1002.71 1156.64 Q1004.53 1160.18 1008.15 1160.18 Q1011.78 1160.18 1013.58 1156.64 Q1015.41 1153.07 1015.41 1145.97 Q1015.41 1138.84 1013.58 1135.3 Q1011.78 1131.73 1008.15 1131.73 M1008.15 1128.03 Q1013.96 1128.03 1017.01 1132.63 Q1020.09 1137.22 1020.09 1145.97 Q1020.09 1154.69 1017.01 1159.3 Q1013.96 1163.88 1008.15 1163.88 Q1002.33 1163.88 999.256 1159.3 Q996.201 1154.69 996.201 1145.97 Q996.201 1137.22 999.256 1132.63 Q1002.33 1128.03 1008.15 1128.03 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1286.73 1159.28 L1294.37 1159.28 L1294.37 1132.91 L1286.06 1134.58 L1286.06 1130.32 L1294.32 1128.65 L1299 1128.65 L1299 1159.28 L1306.64 1159.28 L1306.64 1163.21 L1286.73 1163.21 L1286.73 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1579.21 1159.28 L1595.52 1159.28 L1595.52 1163.21 L1573.58 1163.21 L1573.58 1159.28 Q1576.24 1156.52 1580.83 1151.89 Q1585.43 1147.24 1586.61 1145.9 Q1588.86 1143.37 1589.74 1141.64 Q1590.64 1139.88 1590.64 1138.19 Q1590.64 1135.43 1588.7 1133.7 Q1586.77 1131.96 1583.67 1131.96 Q1581.47 1131.96 1579.02 1132.73 Q1576.59 1133.49 1573.81 1135.04 L1573.81 1130.32 Q1576.64 1129.18 1579.09 1128.61 Q1581.54 1128.03 1583.58 1128.03 Q1588.95 1128.03 1592.14 1130.71 Q1595.34
" 143.534,1106.38 1872.76,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 143.534,872.354 1872.76,872.354 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 143.534,638.329 1872.76,638.329 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 143.534,404.303 1872.76,404.303 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 143.534,170.277 1872.76,170.277 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,1106.38 143.534,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,1106.38 164.285,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,872.354 164.285,872.354 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,638.329 164.285,638.329 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,404.303 164.285,404.303 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip790)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,170.277 164.285,170.277 \n",
" \"/>\n",
"<path clip-path=\"url(#clip790)\" d=\"M 0 0 M65.8226 1092.18 Q62.2115 1092.18 60.3828 1095.74 Q58.5773 1099.28 58.5773 1106.41 Q58.5773 1113.52 60.3828 1117.09 Q62.2115 1120.63 65.8226 1120.63 Q69.4569 1120.63 71.2624 1117.09 Q73.0911 1113.52 73.0911 1106.41 Q73.0911 1099.28 71.2624 1095.74 Q69.4569 1092.18 65.8226 1092.18 M65.8226 1088.47 Q71.6328 1088.47 74.6883 1093.08 Q77.767 1097.66 77.767 1106.41 Q77.767 1115.14 74.6883 1119.75 Q71.6328 1124.33 65.8226 1124.33 Q60.0125 1124.33 56.9338 1119.75 Q53.8782 1115.14 53.8782 1106.41 Q53.8782 1097.66 56.9338 1093.08 Q60.0125 1088.47 65.8226 1088.47 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M82.8364 1117.78 L87.7206 1117.78 L87.7206 1123.66 L82.8364 1123.66 L82.8364 1117.78 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M102.79 1092.18 Q99.1789 1092.18 97.3502 1095.74 Q95.5447 1099.28 95.5447 1106.41 Q95.5447 1113.52 97.3502 1117.09 Q99.1789 1120.63 102.79 1120.63 Q106.424 1120.63 108.23 1117.09 Q110.058 1113.52 110.058 1106.41 Q110.058 1099.28 108.23 1095.74 Q106.424 1092.18 102.79 1092.18 M102.79 1088.47 Q108.6 1088.47 111.656 1093.08 Q114.734 1097.66 114.734 1106.41 Q114.734 1115.14 111.656 1119.75 Q108.6 1124.33 102.79 1124.33 Q96.9799 1124.33 93.9012 1119.75 Q90.8456 1115.14 90.8456 1106.41 Q90.8456 1097.66 93.9012 1093.08 Q96.9799 1088.47 102.79 1088.47 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M67.0495 858.153 Q63.4384 858.153 61.6097 861.718 Q59.8041 865.259 59.8041 872.389 Q59.8041 879.495 61.6097 883.06 Q63.4384 886.602 67.0495 886.602 Q70.6837 886.602 72.4892 883.06 Q74.3179 879.495 74.3179 872.389 Q74.3179 865.259 72.4892 861.718 Q70.6837 858.153 67.0495 858.153 M67.0495 854.449 Q72.8596 854.449 75.9152 859.056 Q78.9938 863.639 78.9938 872.389 Q78.9938 881.116 75.9152 885.722 Q72.8596 890.305 67.0495 890.305 Q61.2393 890.305 58.1606 885.722 Q55.1051 881.116 55.1051 872.389 Q55.1051 863.639 58.1606 859.056 Q61.2393 854.449 67.0495 854.449 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M84.0633 883.755 L88.9475 883.755 L88.9475 889.634 L84.0633 889.634 L84.0633 883.755 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M94.8271 885.699 L102.466 885.699 L102.466 859.333 L94.1558 861 L94.1558 856.741 L102.42 855.074 L107.096 855.074 L107.096 885.699 L114.734 885.699 L114.734 889.634 L94.8271 889.634 L94.8271 885.699 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M67.4198 624.127 Q63.8087 624.127 61.98 627.692 Q60.1745 631.234 60.1745 638.363 Q60.1745 645.47 61.98 649.035 Q63.8087 652.576 67.4198 652.576 Q71.0541 652.576 72.8596 649.035 Q74.6883 645.47 74.6883 638.363 Q74.6883 631.234 72.8596 627.692 Q71.0541 624.127 67.4198 624.127 M67.4198 620.424 Q73.23 620.424 76.2855 625.03 Q79.3642 629.613 79.3642 638.363 Q79.3642 647.09 76.2855 651.697 Q73.23 656.28 67.4198 656.28 Q61.6097 656.28 58.531 651.697 Q55.4754 647.09 55.4754 638.363 Q55.4754 629.613 58.531 625.03 Q61.6097 620.424 67.4198 620.424 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M84.4336 649.729 L89.3179 649.729 L89.3179 655.609 L84.4336 655.609 L84.4336 649.729 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M98.415 651.673 L114.734 651.673 L114.734 655.609 L92.7901 655.609 L92.7901 651.673 Q95.4521 648.919 100.035 644.289 Q104.642 639.636 105.822 638.294 Q108.068 635.771 108.947 634.035 Q109.85 632.275 109.85 630.586 Q109.85 627.831 107.906 626.095 Q105.984 624.359 102.883 624.359 Q100.684 624.359 98.2298 625.123 Q95.7993 625.886 93.0215 627.437 L93.0215 622.715 Q95.8456 621.581 98.2993 621.002 Q100.753 620.424 102.79 620.424 Q108.16 620.424 111.355 623.109 Q114.549 625.794 114.549 630.285 Q114.549 632.414 113.
" 143.534,1096.01 172.355,1092.45 201.175,1087.86 229.995,1081.99 258.816,1074.59 287.636,1065.36 316.457,1053.97 345.277,1040.09 374.097,1023.36 402.918,1003.45 \n",
" 431.738,980.027 460.558,952.822 489.379,921.616 518.199,886.281 547.019,846.797 575.84,803.275 604.66,755.979 633.48,705.333 662.301,651.935 691.121,596.55 \n",
" 719.942,540.106 748.762,483.672 777.582,428.427 806.403,375.626 835.223,326.549 864.043,282.457 892.864,244.533 921.684,213.835 950.504,191.24 979.325,177.409 \n",
" 1008.15,172.753 1036.97,177.409 1065.79,191.24 1094.61,213.835 1123.43,244.533 1152.25,282.457 1181.07,326.549 1209.89,375.626 1238.71,428.427 1267.53,483.672 \n",
" 1296.35,540.106 1325.17,596.55 1353.99,651.935 1382.81,705.333 1411.63,755.979 1440.45,803.275 1469.27,846.797 1498.09,886.281 1526.91,921.616 1555.73,952.822 \n",
" 1584.55,980.027 1613.37,1003.45 1642.19,1023.36 1671.01,1040.09 1699.83,1053.97 1728.65,1065.36 1757.47,1074.59 1786.29,1081.99 1815.12,1087.86 1843.94,1092.45 \n",
" 1872.76,1096.01 \n",
" \"/>\n",
"<path clip-path=\"url(#clip792)\" d=\"\n",
"M143.534 1096.01 L172.355 1092.45 L201.175 1087.86 L229.995 1081.99 L258.816 1074.59 L287.636 1065.36 L316.457 1053.97 L345.277 1040.09 L374.097 1023.36 L402.918 1003.45 \n",
" L431.738 980.027 L460.558 952.822 L489.379 921.616 L518.199 886.281 L547.019 846.797 L575.84 803.275 L604.66 755.979 L633.48 705.333 L662.301 651.935 L691.121 596.55 \n",
" L719.942 540.106 L748.762 483.672 L777.582 428.427 L806.403 375.626 L835.223 326.549 L864.043 282.457 L892.864 244.533 L921.684 213.835 L950.504 191.24 L979.325 177.409 \n",
" L1008.15 172.753 L1036.97 177.409 L1065.79 191.24 L1094.61 213.835 L1123.43 244.533 L1152.25 282.457 L1181.07 326.549 L1209.89 375.626 L1238.71 428.427 L1267.53 483.672 \n",
" L1296.35 540.106 L1325.17 596.55 L1353.99 651.935 L1382.81 705.333 L1411.63 755.979 L1440.45 803.275 L1469.27 846.797 L1469.27 1106.38 L1440.45 1106.38 L1411.63 1106.38 \n",
" L1382.81 1106.38 L1353.99 1106.38 L1325.17 1106.38 L1296.35 1106.38 L1267.53 1106.38 L1238.71 1106.38 L1209.89 1106.38 L1181.07 1106.38 L1152.25 1106.38 L1123.43 1106.38 \n",
" L1094.61 1106.38 L1065.79 1106.38 L1036.97 1106.38 L1008.15 1106.38 L979.325 1106.38 L950.504 1106.38 L921.684 1106.38 L892.864 1106.38 L864.043 1106.38 L835.223 1106.38 \n",
" L806.403 1106.38 L777.582 1106.38 L748.762 1106.38 L719.942 1106.38 L691.121 1106.38 L662.301 1106.38 L633.48 1106.38 L604.66 1106.38 L575.84 1106.38 L547.019 1106.38 \n",
" L518.199 1106.38 L489.379 1106.38 L460.558 1106.38 L431.738 1106.38 L402.918 1106.38 L374.097 1106.38 L345.277 1106.38 L316.457 1106.38 L287.636 1106.38 L258.816 1106.38 \n",
" L229.995 1106.38 L201.175 1106.38 L172.355 1106.38 L143.534 1106.38 Z\n",
" \" fill=\"#ff0000\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<polyline clip-path=\"url(#clip792)\" style=\"stroke:#e26f46; stroke-width:8; stroke-opacity:1; fill:none\" points=\"\n",
" 143.534,1096.01 172.355,1092.45 201.175,1087.86 229.995,1081.99 258.816,1074.59 287.636,1065.36 316.457,1053.97 345.277,1040.09 374.097,1023.36 402.918,1003.45 \n",
" 431.738,980.027 460.558,952.822 489.379,921.616 518.199,886.281 547.019,846.797 575.84,803.275 604.66,755.979 633.48,705.333 662.301,651.935 691.121,596.55 \n",
" 719.942,540.106 748.762,483.672 777.582,428.427 806.403,375.626 835.223,326.549 864.043,282.457 892.864,244.533 921.684,213.835 950.504,191.24 979.325,177.409 \n",
" 1008.15,172.753 1036.97,177.409 1065.79,191.24 1094.61,213.835 1123.43,244.533 1152.25,282.457 1181.07,326.549 1209.89,375.626 1238.71,428.427 1267.53,483.672 \n",
" 1296.35,540.106 1325.17,596.55 1353.99,651.935 1382.81,705.333 1411.63,755.979 1440.45,803.275 1469.27,846.797 \n",
" \"/>\n",
"<path clip-path=\"url(#clip790)\" d=\"M 0 0 M1350.78 479.389 L1350.78 486.75 L1359.55 486.75 L1359.55 490.06 L1350.78 490.06 L1350.78 504.134 Q1350.78 507.305 1351.63 508.208 Q1352.51 509.111 1355.18 509.111 L1359.55 509.111 L1359.55 512.676 L1355.18 512.676 Q1350.25 512.676 1348.37 510.847 Q1346.5 508.995 1346.5 504.134 L1346.5 490.06 L1343.37 490.06 L1343.37 486.75 L1346.5 486.75 L1346.5 479.389 L1350.78 479.389 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1385.57 497.028 L1385.57 512.676 L1381.31 512.676 L1381.31 497.167 Q1381.31 493.486 1379.87 491.657 Q1378.44 489.829 1375.57 489.829 Q1372.12 489.829 1370.13 492.028 Q1368.14 494.227 1368.14 498.023 L1368.14 512.676 L1363.86 512.676 L1363.86 476.657 L1368.14 476.657 L1368.14 490.778 Q1369.67 488.44 1371.73 487.282 Q1373.81 486.125 1376.52 486.125 Q1380.99 486.125 1383.28 488.903 Q1385.57 491.657 1385.57 497.028 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1412.21 498.648 L1412.21 500.731 L1392.63 500.731 Q1392.91 505.129 1395.27 507.444 Q1397.65 509.736 1401.89 509.736 Q1404.34 509.736 1406.63 509.134 Q1408.95 508.532 1411.22 507.329 L1411.22 511.356 Q1408.93 512.329 1406.52 512.838 Q1404.11 513.347 1401.63 513.347 Q1395.43 513.347 1391.8 509.736 Q1388.18 506.125 1388.18 499.967 Q1388.18 493.602 1391.61 489.875 Q1395.06 486.125 1400.89 486.125 Q1406.12 486.125 1409.16 489.505 Q1412.21 492.861 1412.21 498.648 M1407.95 497.398 Q1407.91 493.903 1405.99 491.819 Q1404.09 489.736 1400.94 489.736 Q1397.37 489.736 1395.22 491.75 Q1393.09 493.764 1392.77 497.421 L1407.95 497.398 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1443.53 499.643 Q1438.37 499.643 1436.38 500.824 Q1434.39 502.004 1434.39 504.852 Q1434.39 507.12 1435.87 508.463 Q1437.37 509.782 1439.94 509.782 Q1443.49 509.782 1445.62 507.282 Q1447.77 504.759 1447.77 500.592 L1447.77 499.643 L1443.53 499.643 M1452.03 497.884 L1452.03 512.676 L1447.77 512.676 L1447.77 508.741 Q1446.31 511.102 1444.13 512.236 Q1441.96 513.347 1438.81 513.347 Q1434.83 513.347 1432.47 511.125 Q1430.13 508.879 1430.13 505.129 Q1430.13 500.754 1433.05 498.532 Q1435.99 496.31 1441.8 496.31 L1447.77 496.31 L1447.77 495.893 Q1447.77 492.954 1445.82 491.356 Q1443.9 489.736 1440.41 489.736 Q1438.18 489.736 1436.08 490.268 Q1433.97 490.801 1432.03 491.866 L1432.03 487.93 Q1434.37 487.028 1436.56 486.588 Q1438.76 486.125 1440.85 486.125 Q1446.47 486.125 1449.25 489.042 Q1452.03 491.958 1452.03 497.884 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1471.52 490.731 Q1470.8 490.315 1469.94 490.13 Q1469.11 489.921 1468.09 489.921 Q1464.48 489.921 1462.54 492.282 Q1460.62 494.62 1460.62 499.018 L1460.62 512.676 L1456.33 512.676 L1456.33 486.75 L1460.62 486.75 L1460.62 490.778 Q1461.96 488.417 1464.11 487.282 Q1466.26 486.125 1469.34 486.125 Q1469.78 486.125 1470.31 486.194 Q1470.85 486.241 1471.49 486.356 L1471.52 490.731 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1497.12 498.648 L1497.12 500.731 L1477.54 500.731 Q1477.81 505.129 1480.18 507.444 Q1482.56 509.736 1486.8 509.736 Q1489.25 509.736 1491.54 509.134 Q1493.86 508.532 1496.12 507.329 L1496.12 511.356 Q1493.83 512.329 1491.43 512.838 Q1489.02 513.347 1486.54 513.347 Q1480.34 513.347 1476.7 509.736 Q1473.09 506.125 1473.09 499.967 Q1473.09 493.602 1476.52 489.875 Q1479.97 486.125 1485.8 486.125 Q1491.03 486.125 1494.06 489.505 Q1497.12 492.861 1497.12 498.648 M1492.86 497.398 Q1492.81 493.903 1490.89 491.819 Q1488.99 489.736 1485.85 489.736 Q1482.28 489.736 1480.13 491.75 Q1478 493.764 1477.68 497.421 L1492.86 497.398 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip790)\" d=\"M 0 0 M1513.37 499.643 Q1508.21 499.643 1506.22 500.824 Q1504.23 502.004 1504.23 504.852 Q1504.23 507.12 1505.71 508.463 Q1507.21 509.782 1509.78 509.782 Q1513
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = -3:0.1:3\n",
"xb = x[x.<=1.645]\n",
"\n",
"p1 = plot( x,ϕNS.(x),\n",
" linecolor = :red,\n",
" linewidth = 2,\n",
" ylims = (0,0.42),\n",
" xlims = (-3,3),\n",
" legend = nothing,\n",
" title = \"pdf of N(0,1)\",\n",
" xlabel = \"x\",\n",
" ylabel = \"\",\n",
" annotation = (1.75,0.25,text(\"the area covers\\nup to x=1.645\",8)) )\n",
"plot!(xb,ϕNS.(xb),fillcolor=:red,linewidth=2,legend=nothing,fill=(0,:red))\n",
"display(p1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calculating Prob(x<=1.645)\n",
"\n",
"The next cell calculates (by numeric integration)\n",
"\n",
"$\\int_{-\\infty}^{1.645}\\phi(x,0,\\sigma)dx$,\n",
"\n",
"where $\\phi(x,\\mu,\\sigma)$ is the pdf of an $N(\\mu,\\sigma)$ variable.\n",
"\n",
"\n",
"The input to `quadgk` should be a function with only one argument. We do that by creating an anonymous function\n",
"```\n",
"x->fn(x,a)\n",
"```\n",
"assuming that `a` has a value already."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Pr(x<=1.64) according to N(0,1): 0.950\n",
"\n",
"Pr(x<=1.64) according to N(0,2): 0.795\n"
]
}
],
"source": [
"cdf1, = quadgk(x->ϕNS(x),-Inf,1.645) #N(0,1)\n",
"printlnPs(\"\\nPr(x<=1.64) according to N(0,1):\", cdf1)\n",
"\n",
"cdf2, = quadgk(x->ϕNS(x,0,2),-Inf,1.645) #N(0,σ=2)\n",
"printlnPs(\"\\nPr(x<=1.64) according to N(0,2):\", cdf2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Numerical Derivatives\n",
"\n",
"Numerical derivatives can be calculated by a crude finite difference (see `NumDer()` below) or by the much more sophisticated routines in the `ForwardDiff` package.\n",
"\n",
"To do this calculation for many `x` values, we loop (either a `for` loop or list comprehension). Dot syntax, `ForwardDiff.derivative.()`, seems to work, but is not documented."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NumDer (generic function with 1 method)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function NumDer(fun,b0,h) #crude function for a centered numerical derivative\n",
" bminus = b0 .- h\n",
" bplus = b0 .+ h\n",
" hh = bplus - bminus\n",
" fplus = fun(bplus)\n",
" fminus = fun(bminus)\n",
" D = (fplus-fminus)/hh\n",
" return D\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"fn1 (generic function with 1 method)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function fn1(x,a) #a simple function, to be differentiated\n",
" return (x - 1.1)^2 - a\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The derivative at x=2 is (from two different methods): \n",
" 1.800 1.800\n",
"\n"
]
}
],
"source": [
"x0 = 2\n",
"\n",
"dydx_A = NumDer(x->fn1(x,0.5),x0,0.01) #differentiate fn1(x,0.5) at x = x0\n",
"dydx_B = ForwardDiff.derivative(x->fn1(x,0.5),x0)\n",
"\n",
"println(\"The derivative at x=$x0 is (from two different methods): \")\n",
"printmat([dydx_A dydx_B])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"now lets plot this\n"
]
}
],
"source": [
"x = -3:6/99:6 #calculate the derivative at many points\n",
"\n",
"dydx_A = [NumDer(x->fn1(x,0.5),x[i],0.01) for i=1:length(x)] #list comprehension as a quick loop\n",
"\n",
"dydx_B = [ForwardDiff.derivative(x->fn1(x,0.5),x[i]) for i=1:length(x)]\n",
"\n",
"println(\"now lets plot this\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"480\" height=\"320\" viewBox=\"0 0 1920 1280\">\n",
"<defs>\n",
" <clipPath id=\"clip830\">\n",
" <rect x=\"0\" y=\"0\" width=\"1920\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip830)\" d=\"\n",
"M0 1280 L1920 1280 L1920 0 L0 0 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip831\">\n",
" <rect x=\"384\" y=\"0\" width=\"1345\" height=\"1280\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<path clip-path=\"url(#clip830)\" d=\"\n",
"M137.886 1106.38 L1872.76 1106.38 L1872.76 123.472 L137.886 123.472 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<defs>\n",
" <clipPath id=\"clip832\">\n",
" <rect x=\"137\" y=\"123\" width=\"1736\" height=\"984\"/>\n",
" </clipPath>\n",
"</defs>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 369.453,1106.38 369.453,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 734.386,1106.38 734.386,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1099.32,1106.38 1099.32,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1464.25,1106.38 1464.25,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 1829.19,1106.38 1829.19,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,1106.38 1872.76,1106.38 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 369.453,1106.38 369.453,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 734.386,1106.38 734.386,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1099.32,1106.38 1099.32,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1464.25,1106.38 1464.25,1094.58 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 1829.19,1106.38 1829.19,1094.58 \n",
" \"/>\n",
"<path clip-path=\"url(#clip830)\" d=\"M 0 0 M341.907 1146.38 L371.583 1146.38 L371.583 1150.32 L341.907 1150.32 L341.907 1146.38 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M380.68 1159.28 L396.999 1159.28 L396.999 1163.21 L375.055 1163.21 L375.055 1159.28 Q377.717 1156.52 382.3 1151.89 Q386.906 1147.24 388.087 1145.9 Q390.332 1143.37 391.212 1141.64 Q392.115 1139.88 392.115 1138.19 Q392.115 1135.43 390.17 1133.7 Q388.249 1131.96 385.147 1131.96 Q382.948 1131.96 380.494 1132.73 Q378.064 1133.49 375.286 1135.04 L375.286 1130.32 Q378.11 1129.18 380.564 1128.61 Q383.018 1128.03 385.055 1128.03 Q390.425 1128.03 393.619 1130.71 Q396.814 1133.4 396.814 1137.89 Q396.814 1140.02 396.004 1141.94 Q395.217 1143.84 393.11 1146.43 Q392.531 1147.1 389.43 1150.32 Q386.328 1153.51 380.68 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M734.386 1131.73 Q730.775 1131.73 728.946 1135.3 Q727.141 1138.84 727.141 1145.97 Q727.141 1153.07 728.946 1156.64 Q730.775 1160.18 734.386 1160.18 Q738.02 1160.18 739.826 1156.64 Q741.654 1153.07 741.654 1145.97 Q741.654 1138.84 739.826 1135.3 Q738.02 1131.73 734.386 1131.73 M734.386 1128.03 Q740.196 1128.03 743.252 1132.63 Q746.33 1137.22 746.33 1145.97 Q746.33 1154.69 743.252 1159.3 Q740.196 1163.88 734.386 1163.88 Q728.576 1163.88 725.497 1159.3 Q722.442 1154.69 722.442 1145.97 Q722.442 1137.22 725.497 1132.63 Q728.576 1128.03 734.386 1128.03 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M1093.97 1159.28 L1110.29 1159.28 L1110.29 1163.21 L1088.35 1163.21 L1088.35 1159.28 Q1091.01 1156.52 1095.59 1151.89 Q1100.2 1147.24 1101.38 1145.9 Q1103.62 1143.37 1104.5 1141.64 Q1105.41 1139.88 1105.41 1138.19 Q1105.41 1135.43 1103.46 1133.7 Q1101.54 1131.96 1098.44 1131.96 Q1096.24 1131.96 1093.79 1132.73 Q1091.36 1133.49 1088.58 1135.04 L1088.58 1130.32 Q1091.4 1129.18 1093.86 1128.61 Q1096.31 1128.03 1098.35 1128.03 Q1103.72 1128.03 1106.91 1130.71 Q1110.11 1133.4 1110.11 1137.89 Q1110.11 1140.02 1109.3 1141.94 Q1108.51 1143.84 1106.4 1146.43 Q1105.82 1147.1 1102.72 1150.32 Q1099.62 1153.51 1093.97 1159.28 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M1467.26 1132.73 L1455.46 1151.17 L1467.26 1151.17 L1467.26 1132.73 M1466.03 1128.65 L1471.91 1128.65 L1471.91 1151.17 L1476.84 1151.17 L1476.84 1155.06 L1471.91 1155.06 L1471.91 1163.21 L1467.26 1163.21 L1467.26 1155.06 L1451.66 1155.06 L1451.66 1150.55 L1466.03 1128.65 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M1829.59 1144.07 Q1826.44 1144.07 1824.59 1146.22 Q1822.76 1148.37 1822.76 1152.12 Q1822.76 1155.85 1824.59 1158.03 Q1826.44 1160.18 1829.59 1160.18 Q1832.74 1160.18 1834.57 1158.03 Q1836.42 1155.85 1836.42 1152.12 Q1836.42 1148.37 1834.57 1146.22 Q1832.74 1144.07 1829.59 1144.07 M1838.87 1129.42 L1838.87 1133.67 Q1837.11 1132.84 1835.31 1132.4 Q1833.53 1131.96 1831.77 1131.96 Q1827.14 1131.96 1824.68 1135.09 Q1822.25 1138.21 1821.91 1144.53 Q1823.27 1142.52 1825.33 1141.45 Q1827.39 1140.36 1829.87 1140.36 Q1835.08 1140.36 1838.09 1143.54 Q1841.12 1146.68 1841.12 1152.12 Q1841.12 1157.45 1837.97 1160.67 Q1834.82 1163.88 1829.59 1163.88 Q1823.59 1163.88 1820.42 1159.3 Q1817.25 1154.69 1817.25 1145.97 Q1817.25 1137.77 1821.14 1132.91 Q1825.03 1128.03 1831.58 1128.03 Q1833.34 1128.03 1835.12 1128.37 Q1836.93 1128.72 1838.87 1129.42 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M1021.94 1208.4 L1009.05 1225.74 L1022.6 1244.04 L1015.7 1244.04 L1005.32 1230.04 L994.945 1244.04 L988.038 1244.04 L1001.88 1225.39 L989.216 1208.4 L996.123 1208.4 L1005.58 1221.1 L1015.03 1208.4 L1021.94 1208.4 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 137.886,984.105 1872.76,984.105 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 137.886,836.516 1872.76,836.516 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 137.886,688.926 1872.76,688.926 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 137.886,541.337 1872.76,541.337 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 137.886,393.748 1872.76,393.748 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
" 137.886,246.159 1872.76,246.159 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,1106.38 137.886,123.472 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,984.105 158.705,984.105 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,836.516 158.705,836.516 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,688.926 158.705,688.926 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,541.337 158.705,541.337 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,393.748 158.705,393.748 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 137.886,246.159 158.705,246.159 \n",
" \"/>\n",
"<path clip-path=\"url(#clip830)\" d=\"M 0 0 M53.3921 984.556 L83.0679 984.556 L83.0679 988.491 L53.3921 988.491 L53.3921 984.556 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M88.1836 966.825 L106.54 966.825 L106.54 970.76 L92.466 970.76 L92.466 979.232 Q93.4845 978.885 94.503 978.723 Q95.5215 978.538 96.54 978.538 Q102.327 978.538 105.707 981.709 Q109.086 984.88 109.086 990.297 Q109.086 995.875 105.614 998.977 Q102.142 1002.06 95.8225 1002.06 Q93.6465 1002.06 91.378 1001.69 Q89.1327 1001.32 86.7253 1000.57 L86.7253 995.875 Q88.8086 997.01 91.0308 997.565 Q93.253 998.121 95.7299 998.121 Q99.7345 998.121 102.072 996.014 Q104.41 993.908 104.41 990.297 Q104.41 986.686 102.072 984.579 Q99.7345 982.473 95.7299 982.473 Q93.8549 982.473 91.9799 982.889 Q90.128 983.306 88.1836 984.186 L88.1836 966.825 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M97.1419 822.314 Q93.5308 822.314 91.7021 825.879 Q89.8966 829.421 89.8966 836.55 Q89.8966 843.657 91.7021 847.221 Q93.5308 850.763 97.1419 850.763 Q100.776 850.763 102.582 847.221 Q104.41 843.657 104.41 836.55 Q104.41 829.421 102.582 825.879 Q100.776 822.314 97.1419 822.314 M97.1419 818.611 Q102.952 818.611 106.008 823.217 Q109.086 827.8 109.086 836.55 Q109.086 845.277 106.008 849.884 Q102.952 854.467 97.1419 854.467 Q91.3317 854.467 88.2531 849.884 Q85.1975 845.277 85.1975 836.55 Q85.1975 827.8 88.2531 823.217 Q91.3317 818.611 97.1419 818.611 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M88.1836 671.646 L106.54 671.646 L106.54 675.582 L92.466 675.582 L92.466 684.054 Q93.4845 683.707 94.503 683.544 Q95.5215 683.359 96.54 683.359 Q102.327 683.359 105.707 686.531 Q109.086 689.702 109.086 695.119 Q109.086 700.697 105.614 703.799 Q102.142 706.878 95.8225 706.878 Q93.6465 706.878 91.378 706.507 Q89.1327 706.137 86.7253 705.396 L86.7253 700.697 Q88.8086 701.831 91.0308 702.387 Q93.253 702.943 95.7299 702.943 Q99.7345 702.943 102.072 700.836 Q104.41 698.73 104.41 695.119 Q104.41 691.507 102.072 689.401 Q99.7345 687.294 95.7299 687.294 Q93.8549 687.294 91.9799 687.711 Q90.128 688.128 88.1836 689.007 L88.1836 671.646 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M62.1652 554.682 L69.8041 554.682 L69.8041 528.317 L61.4939 529.983 L61.4939 525.724 L69.7578 524.057 L74.4337 524.057 L74.4337 554.682 L82.0725 554.682 L82.0725 558.617 L62.1652 558.617 L62.1652 554.682 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M97.1419 527.136 Q93.5308 527.136 91.7021 530.701 Q89.8966 534.242 89.8966 541.372 Q89.8966 548.478 91.7021 552.043 Q93.5308 555.585 97.1419 555.585 Q100.776 555.585 102.582 552.043 Q104.41 548.478 104.41 541.372 Q104.41 534.242 102.582 530.701 Q100.776 527.136 97.1419 527.136 M97.1419 523.432 Q102.952 523.432 106.008 528.039 Q109.086 532.622 109.086 541.372 Q109.086 550.099 106.008 554.705 Q102.952 559.289 97.1419 559.289 Q91.3317 559.289 88.2531 554.705 Q85.1975 550.099 85.1975 541.372 Q85.1975 532.622 88.2531 528.039 Q91.3317 523.432 97.1419 523.432 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M63.1606 407.093 L70.7994 407.093 L70.7994 380.727 L62.4893 382.394 L62.4893 378.135 L70.7531 376.468 L75.429 376.468 L75.429 407.093 L83.0679 407.093 L83.0679 411.028 L63.1606 411.028 L63.1606 407.093 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M88.1836 376.468 L106.54 376.468 L106.54 380.403 L92.466 380.403 L92.466 388.876 Q93.4845 388.528 94.503 388.366 Q95.5215 388.181 96.54 388.181 Q102.327 388.181 105.707 391.352 Q109.086 394.524 109.086 399.94 Q109.086 405.519 105.614 408.621 Q102.142 411.699 95.8225 411.699 Q93.6465 411.699 91.378 411.329 Q89.1327 410.959 86.7253 410.218 L86.7253 405.519 Q88.8086 406.653 91.0308 407.209 Q93.253 407.764 95.7299 407.764 Q
" 186.986,355.08 198.045,369.641 209.104,383.985 220.162,398.112 231.221,412.023 242.279,425.717 253.338,439.193 264.396,452.453 275.455,465.497 286.514,478.323 \n",
" 297.572,490.932 308.631,503.325 319.689,515.501 330.748,527.46 341.806,539.202 352.865,550.727 363.924,562.035 374.982,573.127 386.041,584.002 397.099,594.659 \n",
" 408.158,605.1 419.216,615.325 430.275,625.332 441.334,635.122 452.392,644.696 463.451,654.053 474.509,663.193 485.568,672.116 496.627,680.822 507.685,689.312 \n",
" 518.744,697.584 529.802,705.64 540.861,713.479 551.919,721.101 562.978,728.506 574.037,735.694 585.095,742.666 596.154,749.421 607.212,755.958 618.271,762.279 \n",
" 629.329,768.384 640.388,774.271 651.447,779.941 662.505,785.395 673.564,790.632 684.622,795.652 695.681,800.455 706.739,805.041 717.798,809.41 728.857,813.563 \n",
" 739.915,817.499 750.974,821.217 762.032,824.72 773.091,828.005 784.15,831.073 795.208,833.925 806.267,836.559 817.325,838.977 828.384,841.178 839.442,843.162 \n",
" 850.501,844.929 861.56,846.48 872.618,847.813 883.677,848.93 894.735,849.83 905.794,850.513 916.852,850.979 927.911,851.229 938.97,851.261 950.028,851.077 \n",
" 961.087,850.676 972.145,850.058 983.204,849.223 994.263,848.171 1005.32,846.903 1016.38,845.417 1027.44,843.715 1038.5,841.796 1049.56,839.66 1060.61,837.307 \n",
" 1071.67,834.738 1082.73,831.951 1093.79,828.948 1104.85,825.728 1115.91,822.291 1126.97,818.637 1138.02,814.766 1149.08,810.679 1160.14,806.375 1171.2,801.853 \n",
" 1182.26,797.115 1193.32,792.16 1204.38,786.989 1215.43,781.6 1226.49,775.995 1237.55,770.173 1248.61,764.133 1259.67,757.878 1270.73,751.405 1281.79,744.715 \n",
" 1292.84,737.809 1303.9,730.685 1314.96,723.345 1326.02,715.788 1337.08,708.014 1348.14,700.024 1359.2,691.816 1370.25,683.392 1381.31,674.751 1392.37,665.892 \n",
" 1403.43,656.818 1414.49,647.526 1425.55,638.017 1436.61,628.292 1447.66,618.35 1458.72,608.19 1469.78,597.814 1480.84,587.222 1491.9,576.412 1502.96,565.386 \n",
" 1514.02,554.142 1525.07,542.682 1536.13,531.005 1547.19,519.111 1558.25,507 1569.31,494.673 1580.37,482.128 1591.43,469.367 1602.48,456.389 1613.54,443.194 \n",
" 1624.6,429.782 1635.66,416.154 1646.72,402.308 1657.78,388.246 1668.84,373.967 1679.89,359.471 1690.95,344.758 1702.01,329.828 1713.07,314.682 1724.13,299.319 \n",
" 1735.19,283.738 1746.25,267.941 1757.3,251.927 1768.36,235.697 1779.42,219.249 1790.48,202.585 1801.54,185.703 1812.6,168.605 1823.66,151.29 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" stroke-dasharray=\"2, 4\" points=\"\n",
" 186.986,1078.56 198.045,1074.98 209.104,1071.41 220.162,1067.83 231.221,1064.25 242.279,1060.67 253.338,1057.09 264.396,1053.52 275.455,1049.94 286.514,1046.36 \n",
" 297.572,1042.78 308.631,1039.2 319.689,1035.63 330.748,1032.05 341.806,1028.47 352.865,1024.89 363.924,1021.32 374.982,1017.74 386.041,1014.16 397.099,1010.58 \n",
" 408.158,1007 419.216,1003.43 430.275,999.848 441.334,996.27 452.392,992.692 463.451,989.114 474.509,985.536 485.568,981.958 496.627,978.38 507.685,974.802 \n",
" 518.744,971.224 529.802,967.646 540.861,964.068 551.919,960.49 562.978,956.912 574.037,953.335 585.095,949.757 596.154,946.179 607.212,942.601 618.271,939.023 \n",
" 629.329,935.445 640.388,931.867 651.447,928.289 662.505,924.711 673.564,921.133 684.622,917.555 695.681,913.977 706.739,910.4 717.798,906.822 728.857,903.244 \n",
" 739.915,899.666 750.974,896.088 762.032,892.51 773.091,888.932 784.15,885.354 795.208,881.776 806.267,878.198 817.325,874.62 828.384,871.042 839.442,867.465 \n",
" 850.501,863.887 861.56,860.309 872.618,856.731 883.677,853.153 894.735,849.575 905.794,845.997 916.852,842.419 927.911,838.841 938.97,835.263 950.028,831.685 \n",
" 961.087,828.107 972.145,824.53 983.204,820.952 994.263,817.374 1005.32,813.796 1016.38,810.218 1027.44,806.64 1038.5,803.062 1049.56,799.484 1060.61,795.906 \n",
" 1071.67,792.328 1082.73,788.75 1093.79,785.172 1104.85,781.594 1115.91,778.017 1126.97,774.439 1138.02,770.861 1149.08,767.283 1160.14,763.705 1171.2,760.127 \n",
" 1182.26,756.549 1193.32,752.971 1204.38,749.393 1215.43,745.815 1226.49,742.237 1237.55,738.659 1248.61,735.082 1259.67,731.504 1270.73,727.926 1281.79,724.348 \n",
" 1292.84,720.77 1303.9,717.192 1314.96,713.614 1326.02,710.036 1337.08,706.458 1348.14,702.88 1359.2,699.302 1370.25,695.724 1381.31,692.147 1392.37,688.569 \n",
" 1403.43,684.991 1414.49,681.413 1425.55,677.835 1436.61,674.257 1447.66,670.679 1458.72,667.101 1469.78,663.523 1480.84,659.945 1491.9,656.367 1502.96,652.789 \n",
" 1514.02,649.212 1525.07,645.634 1536.13,642.056 1547.19,638.478 1558.25,634.9 1569.31,631.322 1580.37,627.744 1591.43,624.166 1602.48,620.588 1613.54,617.01 \n",
" 1624.6,613.432 1635.66,609.854 1646.72,606.277 1657.78,602.699 1668.84,599.121 1679.89,595.543 1690.95,591.965 1702.01,588.387 1713.07,584.809 1724.13,581.231 \n",
" 1735.19,577.653 1746.25,574.075 1757.3,570.497 1768.36,566.919 1779.42,563.341 1790.48,559.764 1801.54,556.186 1812.6,552.608 1823.66,549.03 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip832)\" style=\"stroke:#0000ff; stroke-width:4; stroke-opacity:1; fill:none\" stroke-dasharray=\"16, 10\" points=\"\n",
" 186.986,1078.56 198.045,1074.98 209.104,1071.41 220.162,1067.83 231.221,1064.25 242.279,1060.67 253.338,1057.09 264.396,1053.52 275.455,1049.94 286.514,1046.36 \n",
" 297.572,1042.78 308.631,1039.2 319.689,1035.63 330.748,1032.05 341.806,1028.47 352.865,1024.89 363.924,1021.32 374.982,1017.74 386.041,1014.16 397.099,1010.58 \n",
" 408.158,1007 419.216,1003.43 430.275,999.848 441.334,996.27 452.392,992.692 463.451,989.114 474.509,985.536 485.568,981.958 496.627,978.38 507.685,974.802 \n",
" 518.744,971.224 529.802,967.646 540.861,964.068 551.919,960.49 562.978,956.912 574.037,953.335 585.095,949.757 596.154,946.179 607.212,942.601 618.271,939.023 \n",
" 629.329,935.445 640.388,931.867 651.447,928.289 662.505,924.711 673.564,921.133 684.622,917.555 695.681,913.977 706.739,910.4 717.798,906.822 728.857,903.244 \n",
" 739.915,899.666 750.974,896.088 762.032,892.51 773.091,888.932 784.15,885.354 795.208,881.776 806.267,878.198 817.325,874.62 828.384,871.042 839.442,867.465 \n",
" 850.501,863.887 861.56,860.309 872.618,856.731 883.677,853.153 894.735,849.575 905.794,845.997 916.852,842.419 927.911,838.841 938.97,835.263 950.028,831.685 \n",
" 961.087,828.107 972.145,824.53 983.204,820.952 994.263,817.374 1005.32,813.796 1016.38,810.218 1027.44,806.64 1038.5,803.062 1049.56,799.484 1060.61,795.906 \n",
" 1071.67,792.328 1082.73,788.75 1093.79,785.172 1104.85,781.594 1115.91,778.017 1126.97,774.439 1138.02,770.861 1149.08,767.283 1160.14,763.705 1171.2,760.127 \n",
" 1182.26,756.549 1193.32,752.971 1204.38,749.393 1215.43,745.815 1226.49,742.237 1237.55,738.659 1248.61,735.082 1259.67,731.504 1270.73,727.926 1281.79,724.348 \n",
" 1292.84,720.77 1303.9,717.192 1314.96,713.614 1326.02,710.036 1337.08,706.458 1348.14,702.88 1359.2,699.302 1370.25,695.724 1381.31,692.147 1392.37,688.569 \n",
" 1403.43,684.991 1414.49,681.413 1425.55,677.835 1436.61,674.257 1447.66,670.679 1458.72,667.101 1469.78,663.523 1480.84,659.945 1491.9,656.367 1502.96,652.789 \n",
" 1514.02,649.212 1525.07,645.634 1536.13,642.056 1547.19,638.478 1558.25,634.9 1569.31,631.322 1580.37,627.744 1591.43,624.166 1602.48,620.588 1613.54,617.01 \n",
" 1624.6,613.432 1635.66,609.854 1646.72,606.277 1657.78,602.699 1668.84,599.121 1679.89,595.543 1690.95,591.965 1702.01,588.387 1713.07,584.809 1724.13,581.231 \n",
" 1735.19,577.653 1746.25,574.075 1757.3,570.497 1768.36,566.919 1779.42,563.341 1790.48,559.764 1801.54,556.186 1812.6,552.608 1823.66,549.03 \n",
" \"/>\n",
"<path clip-path=\"url(#clip830)\" d=\"\n",
"M480.21 398.156 L1043.87 398.156 L1043.87 156.236 L480.21 156.236 Z\n",
" \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 480.21,398.156 1043.87,398.156 1043.87,156.236 480.21,156.236 480.21,398.156 \n",
" \"/>\n",
"<polyline clip-path=\"url(#clip830)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
" 499.486,216.716 615.144,216.716 \n",
" \"/>\n",
"<path clip-path=\"url(#clip830)\" d=\"M 0 0 M650.925 197.977 L650.925 201.519 L646.851 201.519 Q644.559 201.519 643.656 202.445 Q642.777 203.371 642.777 205.778 L642.777 208.07 L649.791 208.07 L649.791 211.38 L642.777 211.38 L642.777 233.996 L638.494 233.996 L638.494 211.38 L634.42 211.38 L634.42 208.07 L638.494 208.07 L638.494 206.264 Q638.494 201.936 640.508 199.968 Q642.522 197.977 646.897 197.977 L650.925 197.977 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M676.943 218.348 L676.943 233.996 L672.684 233.996 L672.684 218.487 Q672.684 214.806 671.249 212.977 Q669.814 211.149 666.943 211.149 Q663.494 211.149 661.503 213.348 Q659.513 215.547 659.513 219.343 L659.513 233.996 L655.23 233.996 L655.23 208.07 L659.513 208.07 L659.513 212.098 Q661.041 209.76 663.101 208.602 Q665.184 207.445 667.892 207.445 Q672.36 207.445 674.652 210.223 Q676.943 212.977 676.943 218.348 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M682.823 230.061 L690.462 230.061 L690.462 203.695 L682.152 205.362 L682.152 201.102 L690.415 199.436 L695.091 199.436 L695.091 230.061 L702.73 230.061 L702.73 233.996 L682.823 233.996 L682.823 230.061 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M717.429 198.024 Q714.327 203.348 712.823 208.556 Q711.318 213.764 711.318 219.112 Q711.318 224.459 712.823 229.713 Q714.35 234.945 717.429 240.246 L713.725 240.246 Q710.253 234.806 708.517 229.551 Q706.804 224.297 706.804 219.112 Q706.804 213.95 708.517 208.718 Q710.23 203.487 713.725 198.024 L717.429 198.024 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M721.225 198.024 L724.929 198.024 Q728.401 203.487 730.114 208.718 Q731.85 213.95 731.85 219.112 Q731.85 224.297 730.114 229.551 Q728.401 234.806 724.929 240.246 L721.225 240.246 Q724.304 234.945 725.809 229.713 Q727.336 224.459 727.336 219.112 Q727.336 213.764 725.809 208.556 Q724.304 203.348 721.225 198.024 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><polyline clip-path=\"url(#clip830)\" style=\"stroke:#ff0000; stroke-width:4; stroke-opacity:1; fill:none\" stroke-dasharray=\"2, 4\" points=\"\n",
" 499.486,277.196 615.144,277.196 \n",
" \"/>\n",
"<path clip-path=\"url(#clip830)\" d=\"M 0 0 M654.929 269.545 L654.929 273.527 Q653.124 272.531 651.295 272.045 Q649.49 271.536 647.638 271.536 Q643.494 271.536 641.203 274.175 Q638.911 276.791 638.911 281.536 Q638.911 286.281 641.203 288.92 Q643.494 291.536 647.638 291.536 Q649.49 291.536 651.295 291.05 Q653.124 290.541 654.929 289.545 L654.929 293.48 Q653.147 294.314 651.226 294.73 Q649.328 295.147 647.175 295.147 Q641.318 295.147 637.869 291.466 Q634.42 287.786 634.42 281.536 Q634.42 275.193 637.893 271.559 Q641.388 267.925 647.453 267.925 Q649.42 267.925 651.295 268.342 Q653.17 268.735 654.929 269.545 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M674.42 272.531 Q673.702 272.115 672.846 271.93 Q672.013 271.721 670.994 271.721 Q667.383 271.721 665.439 274.082 Q663.517 276.42 663.517 280.818 L663.517 294.476 L659.235 294.476 L659.235 268.55 L663.517 268.55 L663.517 272.578 Q664.86 270.217 667.013 269.082 Q669.165 267.925 672.244 267.925 Q672.684 267.925 673.216 267.994 Q673.749 268.041 674.397 268.156 L674.42 272.531 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M678.448 284.244 L678.448 268.55 L682.707 268.55 L682.707 284.082 Q682.707 287.763 684.142 289.615 Q685.577 291.443 688.448 291.443 Q691.897 291.443 693.888 289.244 Q695.901 287.045 695.901 283.249 L695.901 268.55 L700.161 268.55 L700.161 294.476 L695.901 294.476 L695.901 290.494 Q694.351 292.855 692.29 294.013 Q690.253 295.147 687.545 295.147 Q683.077 295.147 680.763 292.369 Q678.448 289.591 678.448 284.244 M689.165 267.925 L689.165 267.925 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M721.688 272.485 L721.688 258.457 L725.948 258.457 L725.948 294.476 L721.688 294.476 L721.688 290.587 Q720.346 292.902 718.286 294.036 Q716.249 295.147 713.378 295.147 Q708.679 295.147 705.716 291.397 Q702.776 287.647 702.776 281.536 Q702.776 275.425 705.716 271.675 Q708.679 267.925 713.378 267.925 Q716.249 267.925 718.286 269.059 Q720.346 270.17 721.688 272.485 M707.175 281.536 Q707.175 286.235 709.096 288.92 Q711.04 291.582 714.42 291.582 Q717.799 291.582 719.744 288.92 Q721.688 286.235 721.688 281.536 Q721.688 276.837 719.744 274.175 Q717.799 271.49 714.42 271.49 Q711.04 271.49 709.096 274.175 Q707.175 276.837 707.175 281.536 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M752.591 280.448 L752.591 282.531 L733.008 282.531 Q733.286 286.929 735.647 289.244 Q738.031 291.536 742.267 291.536 Q744.721 291.536 747.012 290.934 Q749.327 290.332 751.596 289.129 L751.596 293.156 Q749.304 294.128 746.897 294.638 Q744.489 295.147 742.012 295.147 Q735.809 295.147 732.174 291.536 Q728.563 287.925 728.563 281.767 Q728.563 275.402 731.989 271.675 Q735.438 267.925 741.272 267.925 Q746.503 267.925 749.535 271.305 Q752.591 274.661 752.591 280.448 M748.332 279.198 Q748.285 275.703 746.364 273.619 Q744.466 271.536 741.318 271.536 Q737.753 271.536 735.6 273.55 Q733.471 275.564 733.147 279.221 L748.332 279.198 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M789.188 272.485 L789.188 258.457 L793.447 258.457 L793.447 294.476 L789.188 294.476 L789.188 290.587 Q787.845 292.902 785.785 294.036 Q783.748 295.147 780.878 295.147 Q776.179 295.147 773.216 291.397 Q770.276 287.647 770.276 281.536 Q770.276 275.425 773.216 271.675 Q776.179 267.925 780.878 267.925 Q783.748 267.925 785.785 269.059 Q787.845 270.17 789.188 272.485 M774.674 281.536 Q774.674 286.235 776.595 288.92 Q778.54 291.582 781.92 291.582 Q785.299 291.582 787.244 288.92 Q789.188 286.235 789.188 281.536 Q789.188 276.837 787.244 274.175 Q785.299 271.49 781.92 271.49 Q778.54 271.49 776.595 274.175 Q774.674 276.837 774.674 281.536 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M820.091 280.448 L820.091 282.531 L800.507 282.531 Q800.785 286.929 803.146 289.244 Q805.53 291.536 809.
" 499.486,337.676 615.144,337.676 \n",
" \"/>\n",
"<path clip-path=\"url(#clip830)\" d=\"M 0 0 M650.925 318.937 L650.925 322.479 L646.851 322.479 Q644.559 322.479 643.656 323.405 Q642.777 324.331 642.777 326.738 L642.777 329.03 L649.791 329.03 L649.791 332.34 L642.777 332.34 L642.777 354.956 L638.494 354.956 L638.494 332.34 L634.42 332.34 L634.42 329.03 L638.494 329.03 L638.494 327.224 Q638.494 322.896 640.508 320.928 Q642.522 318.937 646.897 318.937 L650.925 318.937 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M670.415 333.011 Q669.698 332.595 668.841 332.41 Q668.008 332.201 666.99 332.201 Q663.378 332.201 661.434 334.562 Q659.513 336.9 659.513 341.298 L659.513 354.956 L655.23 354.956 L655.23 329.03 L659.513 329.03 L659.513 333.058 Q660.855 330.697 663.008 329.562 Q665.161 328.405 668.24 328.405 Q668.679 328.405 669.212 328.474 Q669.744 328.521 670.392 328.636 L670.415 333.011 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M683.888 332.016 Q680.462 332.016 678.471 334.701 Q676.48 337.363 676.48 342.016 Q676.48 346.669 678.448 349.354 Q680.439 352.016 683.888 352.016 Q687.29 352.016 689.281 349.331 Q691.272 346.646 691.272 342.016 Q691.272 337.41 689.281 334.724 Q687.29 332.016 683.888 332.016 M683.888 328.405 Q689.443 328.405 692.614 332.016 Q695.786 335.627 695.786 342.016 Q695.786 348.382 692.614 352.016 Q689.443 355.627 683.888 355.627 Q678.309 355.627 675.138 352.016 Q671.99 348.382 671.99 342.016 Q671.99 335.627 675.138 332.016 Q678.309 328.405 683.888 328.405 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M720.438 334.007 Q722.036 331.136 724.258 329.771 Q726.48 328.405 729.489 328.405 Q733.54 328.405 735.739 331.252 Q737.938 334.076 737.938 339.308 L737.938 354.956 L733.656 354.956 L733.656 339.447 Q733.656 335.72 732.336 333.914 Q731.017 332.109 728.309 332.109 Q724.999 332.109 723.077 334.308 Q721.156 336.507 721.156 340.303 L721.156 354.956 L716.874 354.956 L716.874 339.447 Q716.874 335.697 715.554 333.914 Q714.235 332.109 711.48 332.109 Q708.216 332.109 706.295 334.331 Q704.374 336.53 704.374 340.303 L704.374 354.956 L700.091 354.956 L700.091 329.03 L704.374 329.03 L704.374 333.058 Q705.832 330.673 707.869 329.539 Q709.906 328.405 712.707 328.405 Q715.531 328.405 717.499 329.84 Q719.489 331.275 720.438 334.007 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M757.66 320.396 L777.521 320.396 L777.521 324.331 L762.336 324.331 L762.336 334.516 L776.04 334.516 L776.04 338.451 L762.336 338.451 L762.336 354.956 L757.66 354.956 L757.66 320.396 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M790.345 332.016 Q786.919 332.016 784.929 334.701 Q782.938 337.363 782.938 342.016 Q782.938 346.669 784.906 349.354 Q786.896 352.016 790.345 352.016 Q793.748 352.016 795.739 349.331 Q797.73 346.646 797.73 342.016 Q797.73 337.41 795.739 334.724 Q793.748 332.016 790.345 332.016 M790.345 328.405 Q795.901 328.405 799.072 332.016 Q802.243 335.627 802.243 342.016 Q802.243 348.382 799.072 352.016 Q795.901 355.627 790.345 355.627 Q784.767 355.627 781.595 352.016 Q778.447 348.382 778.447 342.016 Q778.447 335.627 781.595 332.016 Q784.767 328.405 790.345 328.405 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M821.734 333.011 Q821.017 332.595 820.16 332.41 Q819.327 332.201 818.308 332.201 Q814.697 332.201 812.753 334.562 Q810.831 336.9 810.831 341.298 L810.831 354.956 L806.549 354.956 L806.549 329.03 L810.831 329.03 L810.831 333.058 Q812.174 330.697 814.327 329.562 Q816.48 328.405 819.558 328.405 Q819.998 328.405 820.53 328.474 Q821.063 328.521 821.711 328.636 L821.734 333.011 Z\" fill=\"#000000\" fill-rule=\"evenodd\" fill-opacity=\"1\" /><path clip-path=\"url(#clip830)\" d=\"M 0 0 M823.725 329.03 L827.984 329.03 L833.308 349.261 L838.609 329.03 L843.632 329.03 L848.956 349.261 L854.257 329.03 L858.516 329.03 L851.734 354.9
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p1 = plot( [x x x],[fn1.(x,0.5) dydx_A dydx_B],\n",
" line = [:solid :dot :dash],\n",
" linecolor = [:black :red :blue],\n",
" label = [\"fn1()\" \"crude derivative\" \"from ForwardDiff\"],\n",
" legend = :top,\n",
" title = \"fn1() and its derivative\",\n",
" xlabel = \"x\",\n",
" ylabel = \"\" )\n",
"display(p1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Comments on Using the ForwardDiff Package (extra)\n",
"\n",
"The `ForwardDiff` package applies an interesting approach to calculate derivatives, using a special number type (\"dual numbers\"). This means that your code must be able to handle such numbers. In most cases, that is not a problem, but you may have to watch out if you create arrays to store (intermediate?) results inside the function. See the examples below"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"fnDoesWork (generic function with 1 method)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function fnDoesNotWork(b,a)\n",
" z = zeros(length(b)) #will not work with ForwardDiff, since\n",
" for i = 1:length(z) #z cannot store dual numbers\n",
" z[i] = b[i]*i\n",
" end\n",
" return sum(z) + a\n",
"end\n",
"\n",
"function fnDoesWork(b,a)\n",
" z = zeros(eltype(b),length(b)) #will work with ForwardDiff, since\n",
" for i = 1:length(z) #when b is a dual number, so is z \n",
" z[i] = b[i]*i #could also start with z = similar(b)\n",
" end\n",
" return sum(z) + a\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2-element Vector{Float64}:\n",
" 1.0\n",
" 2.0"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b0 = [1.5,2]\n",
"\n",
"#ForwardDiff.gradient(b->fnDoesNotWork(b,1),b0) #uncomment to get an error\n",
"ForwardDiff.gradient(b->fnDoesWork(b,1),b0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"@webio": {
"lastCommId": null,
"lastKernelId": null
},
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Julia 1.6.1",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}