Monte Carlo Integral

Author

Arthur Zefanya Lauale

(0). Library.

library(ggplot2)
library(knitr)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.2     ✔ tidyr     1.3.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

(1). Question 1.

Estimate the integral \(\int_{2}^{4}\frac{1}{x} dx\).

  • Count manually.

  • Plot the area that you count its integral.

(1). Answer 1.

  • Process to Count \(\int_{2}^{4}\frac{1}{x}dx\) Manually.

    • Monte Carlo Formula.

      Here is the Monte Carlo formula:

      \[ \begin{flalign*} \begin{aligned} & \int_{a}^{b} f(x) \, dx \approx \frac{b - a}{N} \sum_{i = 1}^{N} f(X_i). \\ \end{aligned} \end{flalign*} \]

      where \(X_1, X_2, \, ... \, , X_N\) are independently sampled from \(U(a,b)\).

    • Generate \(N = 1500\) Random Numbers From \(U(a = 2, b = 4)\).

      set.seed(9403)
      
      # N_1
      N_1 <- 1500
      
      # a_1
      a_1 <- 2
      
      # b_1 
      b_1 <- 4
      
      # r_num_1
      r_num_1 <- runif(N_1,a_1,b_1)
      r_num_1
         [1] 3.425241 2.886604 3.397999 3.040470 3.326888 3.703931 3.200365 2.102236
         [9] 3.240980 2.350584 3.682150 3.524440 3.455379 3.651217 3.502942 3.248643
        [17] 2.278532 2.454506 3.084418 2.173086 2.115848 3.881378 2.473834 2.947407
        [25] 2.625237 3.378809 3.825669 3.020916 2.917116 3.869341 2.279916 3.850284
        [33] 2.405093 3.373651 2.986747 3.599932 2.300535 3.282243 2.170525 2.222051
        [41] 3.610400 3.289945 2.986695 3.499844 2.488522 3.638996 3.245198 2.897007
        [49] 3.439692 2.142381 2.333873 2.331336 3.835865 3.862272 2.226549 3.634342
        [57] 3.038744 3.087160 3.223497 3.630369 3.228108 2.128240 3.984684 2.357919
        [65] 2.082524 3.224689 2.211217 3.226715 2.431285 2.203234 3.700406 2.560769
        [73] 2.748347 3.937310 2.215463 2.332560 2.659325 3.442913 3.845061 2.032683
        [81] 3.369189 3.851586 3.699328 2.843316 3.304331 2.459203 2.917795 3.186151
        [89] 2.331187 2.963537 2.796768 2.905376 3.865133 3.053111 3.715729 3.430906
        [97] 3.505312 3.407667 3.887440 3.030395 3.277016 3.455259 3.165711 3.409376
       [105] 2.286540 3.863179 3.645122 2.676959 3.728921 2.358728 2.793483 2.865154
       [113] 3.335476 2.078865 2.454218 2.389921 2.206750 2.841717 2.115275 2.108384
       [121] 2.300651 3.206823 3.260259 3.978520 3.755832 3.519882 3.953765 2.834667
       [129] 2.705489 3.766148 3.129912 3.374955 2.551216 3.474994 2.616320 3.994159
       [137] 3.224007 2.007893 3.786545 2.201395 3.101144 3.036438 2.158034 3.429961
       [145] 2.001732 3.642686 2.661297 3.669386 3.876473 3.406679 3.894692 2.482405
       [153] 2.473137 3.065252 2.307602 3.493870 2.166570 2.472266 2.191034 2.211794
       [161] 2.893021 2.062691 3.346302 3.315269 2.205721 3.777168 2.564414 2.758975
       [169] 2.155493 3.494073 2.880318 3.414606 3.039815 3.354639 3.151867 2.658625
       [177] 2.120259 3.711731 3.501487 2.255332 2.388507 3.521635 3.237172 3.746035
       [185] 3.169537 3.391019 3.049975 2.431728 2.539442 3.052756 3.829812 3.353291
       [193] 2.789947 3.878370 2.996126 3.026349 3.923086 2.094806 2.409035 2.475991
       [201] 3.258332 2.648286 2.230218 3.903694 3.278905 3.452809 3.699105 3.548560
       [209] 2.437508 2.186263 3.528637 2.025573 3.383283 3.342733 2.168215 3.846844
       [217] 3.051203 2.220837 2.891278 2.894573 2.625770 2.294591 3.618705 3.494203
       [225] 3.084215 2.031114 2.040425 2.697852 3.762413 3.450726 3.245183 3.784537
       [233] 3.972720 3.380558 2.442604 2.459495 2.200912 3.729588 2.371840 3.129199
       [241] 2.486186 2.703856 2.504758 3.031400 3.818983 3.615324 2.351706 3.298631
       [249] 3.115632 2.204806 3.349913 2.637592 3.412779 2.294831 3.544251 3.759557
       [257] 3.364522 2.380596 2.873723 3.571569 3.035717 3.875827 3.747043 3.355805
       [265] 3.573280 3.671218 3.992170 2.651074 3.592543 2.584888 3.389825 2.102622
       [273] 3.084333 3.551055 2.174169 3.178510 2.246437 2.674126 3.638895 2.669215
       [281] 2.533171 2.442340 3.164308 3.365502 2.223543 3.788945 3.486509 3.648607
       [289] 2.772306 2.460749 3.426621 2.014259 2.446789 2.925438 2.416042 3.307374
       [297] 2.521898 3.562735 3.560886 3.546525 3.175627 2.408252 3.612329 2.740689
       [305] 2.853976 3.436041 2.089483 2.231431 2.333981 2.320611 3.276817 3.536505
       [313] 3.426215 2.412070 2.841568 2.527067 3.159938 3.300026 2.062449 3.004254
       [321] 3.094249 2.317098 3.283057 2.975536 2.863082 3.461067 2.132323 2.921233
       [329] 3.946029 2.308443 3.213816 3.218595 3.439759 3.862341 2.244061 3.313514
       [337] 2.991969 2.903125 3.677427 2.195951 3.983376 2.182589 3.737118 2.691785
       [345] 2.659047 2.162812 2.260069 3.092891 2.907387 3.620697 3.703593 3.029404
       [353] 3.797296 3.331599 3.405630 2.159648 2.114999 3.449912 2.750474 3.571862
       [361] 2.454527 2.302259 2.506482 3.929621 3.792203 3.558995 3.423641 2.390891
       [369] 2.337680 3.616803 2.395926 3.945046 3.957813 3.834912 3.451713 2.636014
       [377] 2.452812 2.231503 3.410084 2.557634 3.425812 2.623852 3.184937 2.420035
       [385] 3.748801 3.403330 2.153614 2.432276 3.588647 2.270412 3.446623 3.962745
       [393] 3.635183 2.407718 2.716225 2.750742 2.489171 2.246771 2.114863 2.473966
       [401] 3.781274 2.554295 2.791048 2.819889 2.508545 2.907042 2.768676 3.838236
       [409] 2.067166 3.801574 2.880102 3.056755 2.596222 3.550028 2.256978 2.338964
       [417] 2.131068 2.778373 2.107976 3.437939 3.981142 2.849807 3.139365 3.195261
       [425] 2.725073 2.644179 2.646823 2.488653 3.876959 3.727141 3.560639 2.145960
       [433] 3.617728 3.892337 3.198320 3.010686 3.637831 3.237701 3.223132 3.073772
       [441] 3.677388 2.609114 3.122370 2.946194 3.247214 2.684978 3.419993 2.666634
       [449] 3.572257 3.241621 2.323240 3.651459 3.937642 3.848619 3.168258 3.398400
       [457] 2.940555 3.335890 3.722257 3.633337 3.221002 2.623818 2.868730 3.894064
       [465] 3.391262 2.834029 3.589472 2.983038 2.593471 2.069643 2.201110 2.476302
       [473] 3.071930 3.660276 2.517189 3.198181 3.763776 3.432136 3.035841 2.792202
       [481] 3.693531 2.711904 2.407203 2.415928 3.608525 3.425232 2.526904 2.489762
       [489] 3.221232 2.039101 3.526829 2.272771 2.356035 2.927031 2.991347 3.432515
       [497] 2.575911 2.568717 2.019385 3.441970 3.650881 2.919565 3.535270 2.572456
       [505] 3.605338 3.960084 3.504485 2.891606 2.246965 2.610903 2.318219 3.728157
       [513] 2.015016 2.757003 3.068679 2.919503 2.993190 3.976741 3.394510 2.987682
       [521] 2.290923 2.349156 3.266796 3.828458 2.482485 2.375090 2.861156 2.841567
       [529] 2.423079 2.836873 3.474049 2.861111 2.430517 2.347856 2.344455 3.699551
       [537] 3.798585 2.617685 3.848072 2.566744 2.757923 2.192290 2.823474 2.810271
       [545] 3.078620 2.946761 3.713806 3.156246 2.473106 2.224805 3.715093 2.920573
       [553] 3.503012 2.164551 2.833662 2.369859 2.884944 3.653432 2.254023 3.915488
       [561] 2.781077 2.521266 3.519486 3.257948 3.412420 3.655072 3.854472 3.337397
       [569] 3.995274 2.774128 2.142531 3.189276 3.106312 3.584589 2.632931 2.356275
       [577] 3.986719 3.085305 3.103722 3.540142 3.936913 3.905256 3.420774 3.420625
       [585] 2.239696 2.634101 3.215347 3.108102 2.389821 3.148306 3.851660 2.730078
       [593] 2.333192 3.009682 2.757438 3.571379 2.175764 3.505897 2.030224 3.908849
       [601] 2.227141 3.213355 2.413236 3.366373 2.143131 3.802460 2.810851 2.824811
       [609] 3.930022 2.262968 3.178473 2.749900 3.028960 3.290203 2.084282 2.555675
       [617] 2.843774 2.913571 2.349939 3.159768 3.316299 2.346630 3.033257 2.079421
       [625] 2.797487 3.994078 2.540166 2.417613 2.273500 2.838234 2.462082 2.370134
       [633] 2.342197 2.125862 2.347405 2.895308 3.552988 3.657766 2.700397 3.447819
       [641] 3.400043 2.148084 3.954053 2.569698 2.347830 3.624235 3.523732 2.509058
       [649] 3.926001 3.472561 3.071024 2.386019 3.229720 2.224292 2.872709 2.196755
       [657] 2.667573 3.438456 3.380621 3.186046 2.807022 3.590879 3.536270 2.412889
       [665] 3.139635 2.290073 2.981262 3.065963 2.123823 2.158937 2.410515 3.621012
       [673] 3.288172 2.931451 3.796576 2.695650 3.421943 2.281820 3.365134 3.056947
       [681] 3.479815 3.069401 3.616809 3.759688 3.484784 3.861980 3.456902 3.010889
       [689] 3.308425 2.604430 2.375838 3.748338 3.509059 2.373999 3.148501 3.829376
       [697] 2.611500 3.371770 2.729188 2.089319 3.717664 2.557696 3.134678 3.126551
       [705] 3.687482 2.516060 3.058095 3.293974 3.832442 2.749960 3.677858 2.629309
       [713] 3.077688 3.023843 2.641073 3.189956 3.658431 2.798926 2.937942 2.784179
       [721] 2.440911 2.089546 3.301664 3.119015 2.732556 3.621571 2.708244 2.203512
       [729] 2.499982 2.387779 2.789092 3.566873 2.647223 3.824497 2.433852 3.613886
       [737] 3.368569 2.201273 3.515828 2.455781 3.227473 2.869419 3.258456 3.043223
       [745] 3.705903 2.345761 3.735405 2.589919 2.222280 2.467401 3.197580 3.177478
       [753] 3.774175 3.334860 3.464739 2.265261 3.557359 3.169188 2.917064 2.642220
       [761] 3.569571 2.194278 2.375782 3.445012 3.553146 3.200266 3.976217 3.907679
       [769] 2.728378 3.183950 2.532788 2.964083 3.480601 2.098349 2.620386 3.173038
       [777] 3.352662 2.401799 2.519257 2.118303 3.021841 2.898352 2.666149 2.215134
       [785] 2.853332 2.670163 2.432382 2.944331 2.616932 2.674473 2.716613 2.257525
       [793] 2.908050 2.846695 3.182097 3.920878 2.609212 3.962846 3.302590 3.385088
       [801] 2.604048 3.254947 2.068173 3.581390 3.833255 3.532031 2.702628 2.280432
       [809] 2.520486 3.011320 2.914575 3.292764 3.298096 2.958235 2.271238 3.401112
       [817] 3.109679 2.626895 2.340432 3.641386 2.926926 2.974082 2.519556 3.867145
       [825] 3.073209 3.120880 2.946622 2.227889 3.021484 3.788000 2.630381 3.118690
       [833] 2.154233 3.547500 3.442084 2.314099 2.912406 3.778193 3.769395 3.918592
       [841] 3.954538 3.914393 2.218720 2.245924 2.922310 2.561423 3.920945 2.357505
       [849] 2.142755 3.477764 3.869017 3.589644 3.616120 3.282483 3.545034 3.093084
       [857] 3.614597 2.163063 2.651023 2.015315 3.620755 2.975490 2.293170 3.258076
       [865] 2.756332 2.062868 2.906813 2.714160 3.738167 3.874330 3.773145 2.943210
       [873] 2.259280 2.540968 2.484062 2.999172 2.529180 3.706392 3.131211 2.080783
       [881] 2.547460 2.419177 2.421504 2.544041 3.317003 2.968169 3.700438 3.835495
       [889] 3.035818 3.711647 2.309292 2.922992 2.149837 3.557388 2.101840 3.900595
       [897] 3.900954 2.713025 3.198698 2.156220 2.454630 3.543875 2.085272 2.546307
       [905] 3.332273 3.892899 3.300297 2.651502 2.483797 3.061075 2.489683 3.630163
       [913] 3.971517 3.187235 3.601818 2.257211 3.979268 2.778267 3.376093 2.314825
       [921] 2.680480 3.548027 3.059034 2.232237 3.145982 2.744999 3.144559 3.285242
       [929] 2.341820 2.052979 3.680423 3.096760 2.756891 2.127102 3.849479 3.535037
       [937] 2.876939 2.970859 3.575475 2.434163 2.981493 3.667651 2.810600 3.886416
       [945] 2.897475 2.130144 2.004791 2.987342 3.710574 2.446747 3.330614 2.192337
       [953] 2.279213 2.137105 3.251182 3.219484 2.146915 3.030737 3.699613 3.140327
       [961] 3.720031 2.934509 3.181473 2.556423 2.678525 3.008245 3.126684 3.195139
       [969] 3.693206 2.646958 3.723715 3.368242 3.128341 3.225439 3.426725 2.870127
       [977] 2.326788 2.469200 2.086922 3.321630 2.277648 2.279422 2.013010 2.589063
       [985] 2.264209 2.548228 2.808093 2.937158 2.651148 2.852093 2.895888 2.946091
       [993] 3.672810 2.027775 3.334081 3.758624 3.527218 2.146714 2.121107 3.672330
      [1001] 3.065168 2.221589 3.965374 3.415884 2.666629 2.348887 3.065535 3.259514
      [1009] 2.578597 3.695120 2.500086 2.498960 3.084737 2.838351 2.785372 3.909224
      [1017] 2.320724 3.914902 2.794579 3.305740 3.461172 3.240663 2.876894 2.194689
      [1025] 3.067115 3.204192 3.107534 3.025935 3.653345 3.775515 2.164680 3.639317
      [1033] 2.651190 3.203745 2.886747 2.764035 3.795088 3.469467 3.578755 3.152991
      [1041] 2.988004 3.160393 3.122247 3.766844 2.793459 3.304539 3.871573 3.204953
      [1049] 3.512930 2.673160 2.984274 2.932155 2.203280 2.088725 2.749580 2.737865
      [1057] 3.772841 3.765121 2.385384 2.979457 2.282028 3.993926 3.777951 2.937245
      [1065] 3.256837 2.912487 3.657874 3.934114 3.801485 3.147584 2.038701 3.173328
      [1073] 3.523197 2.920149 2.107485 3.329851 2.239402 2.486354 3.896281 3.786687
      [1081] 3.353759 2.951242 3.875872 3.691922 2.707769 2.293038 2.670283 2.485440
      [1089] 2.196314 3.635644 3.108816 3.871168 3.855251 3.844001 3.890365 3.777385
      [1097] 3.927221 3.991890 2.320799 2.018668 2.945607 2.832581 3.207723 3.983940
      [1105] 2.319013 2.410918 2.537068 3.293494 2.051435 3.458489 2.988648 3.629587
      [1113] 2.741904 2.203202 2.401570 2.419150 3.717284 3.337774 3.155871 3.984446
      [1121] 2.018231 3.945387 3.965777 2.530347 3.275539 3.698663 3.817112 2.414421
      [1129] 3.524336 2.787871 2.450389 2.633666 2.570804 2.730325 2.683932 2.735970
      [1137] 2.314071 3.390392 2.293764 2.989860 2.671762 2.368263 3.036818 3.224445
      [1145] 2.464545 3.610966 2.482611 2.276115 2.253335 3.894804 2.563128 3.985942
      [1153] 2.105551 3.667318 3.358732 2.600739 3.959154 2.275185 2.479126 2.529538
      [1161] 3.548224 2.822867 3.443866 2.933167 2.187302 3.023472 3.045403 2.300827
      [1169] 3.967304 3.233026 3.094594 2.273506 3.761882 3.786509 3.233753 3.977339
      [1177] 2.096709 3.833463 2.536512 2.160113 2.596946 2.992522 2.345167 3.201181
      [1185] 2.714439 3.175794 2.549775 3.913012 2.645105 2.357288 3.431043 2.651526
      [1193] 3.817834 2.321037 2.908114 3.124421 3.942962 2.383355 3.118742 3.055737
      [1201] 2.044348 3.352387 3.055633 3.057568 2.567162 2.319428 2.417775 3.893489
      [1209] 2.203418 2.396595 3.354225 3.080086 3.922101 2.765468 2.208877 3.293050
      [1217] 2.995649 2.559637 2.362629 2.795087 2.429756 2.298941 2.572242 3.534657
      [1225] 3.126118 2.652237 3.267097 2.028237 3.017440 3.609077 2.086127 2.900338
      [1233] 3.075652 3.582793 2.218675 3.050941 2.325982 2.536429 2.806843 2.472714
      [1241] 3.330451 3.303677 2.684467 3.386544 3.636554 3.594721 2.963591 2.680954
      [1249] 3.159775 3.829432 3.188585 3.941339 3.523185 2.041213 2.453962 2.040628
      [1257] 2.736124 2.914860 2.858994 2.575681 3.054240 3.186819 3.094485 2.955020
      [1265] 2.797442 2.992588 2.455685 2.091426 2.765909 2.283484 3.384350 3.276515
      [1273] 2.037940 2.260577 2.054272 2.664476 2.249062 3.120265 3.920591 2.769486
      [1281] 2.832874 3.062552 3.389039 2.052793 3.638369 3.633887 2.623438 2.469412
      [1289] 3.246251 3.491272 2.895142 2.482730 3.964047 3.742004 3.146522 3.463513
      [1297] 3.703178 2.019346 2.454566 2.879176 2.927141 3.115426 2.428932 3.654291
      [1305] 2.246009 3.232393 2.932092 2.151584 3.691434 2.989940 3.076296 3.805598
      [1313] 3.451243 2.725951 3.920607 2.535580 3.722544 2.919948 2.041207 3.778376
      [1321] 2.066434 3.132933 2.310914 2.845289 2.739242 2.443128 3.907626 2.091690
      [1329] 3.058796 3.292162 2.761070 2.366110 3.068500 3.109218 2.236946 2.847818
      [1337] 3.395903 2.585113 2.349733 3.127846 3.249188 3.020193 2.834215 2.178577
      [1345] 3.263748 3.909364 2.939776 3.865213 2.572403 2.899997 2.877162 3.341734
      [1353] 3.529271 3.045740 2.671336 2.002079 3.182111 2.010688 2.885764 2.048285
      [1361] 3.445100 3.879577 3.230951 2.973594 3.151363 3.790119 3.568737 3.281044
      [1369] 2.741184 2.909671 2.248500 2.222920 2.025936 2.913183 3.183244 3.844835
      [1377] 3.391817 3.143445 3.303813 3.653236 3.805057 3.781240 2.953143 2.772727
      [1385] 3.119737 3.756764 3.127011 2.462790 2.697960 3.983115 2.190734 2.101743
      [1393] 2.638315 2.444381 3.585457 3.837418 2.884336 3.976130 2.674679 3.325012
      [1401] 3.463835 2.512938 3.224404 2.115756 2.945163 3.196784 2.228088 3.024859
      [1409] 3.130509 2.109394 3.462546 2.404948 3.773156 2.413141 3.999494 2.496737
      [1417] 2.651565 3.301897 3.000670 3.890315 2.846123 2.004021 2.352379 3.871875
      [1425] 2.112617 2.694873 2.833409 3.208902 3.042933 2.453310 3.783237 2.201133
      [1433] 2.987757 3.680734 2.394666 3.783443 2.455209 3.935759 3.294375 3.527844
      [1441] 3.390388 3.548751 2.468618 3.189451 3.901254 3.461235 2.394592 3.920987
      [1449] 2.612578 2.215599 2.917433 3.504678 3.063912 3.878249 2.910922 2.493998
      [1457] 3.118920 2.516285 2.165378 3.215677 3.023148 3.119660 3.786674 3.216690
      [1465] 3.081090 2.817463 3.921020 3.771498 2.268979 2.461374 2.160852 3.364274
      [1473] 3.881793 2.402704 2.786198 2.235946 2.189682 3.632843 3.090980 3.474479
      [1481] 2.636089 3.205588 3.967736 3.848542 2.493782 2.449431 2.623722 3.216466
      [1489] 2.064151 2.690333 3.098815 2.179828 3.330208 3.662500 3.514695 2.008075
      [1497] 2.210527 2.219465 3.334712 3.344303
    • Define Function \(f(x) = \frac{1}{x} dx\).

      f_1 <- function(x)1/x
    • Count The Integral.

      # int_1
      int_1 <- 
        ((b_1 - a_1)/N_1)*sum(f_1(r_num_1))
      
      int_1
      [1] 0.6934251
    • Compare Obtained Integral With Integral From R-Generic-Integral-Function.

      data.frame(
        version = c(
          paste0("monte carlo (N = ",N_1,")"),
          "r-generic-integral-function"
        ),
        integral_result = c(
          int_1,
          integrate(f_1,a_1,b_1)$value
        )
      ) %>%
        knitr::kable(
          caption = "comparison between monte carlo 
          integral & r-general-integral-function result"
        )
      comparison between monte carlo integral & r-general-integral-function result
      version integral_result
      monte carlo (N = 1500) 0.6934251
      r-generic-integral-function 0.6931472
  • Plot The Area That You Count Its Integral.

    • Define x_1, y_1, and f_1_data.

      # x_1
      x_1 <- r_num_1
      
      # y_1
      y_1 <- f_1(x_1)
      
      # f_1_data
      f_1_data <- data.frame(x_1,y_1)
    • Draw The Plot.

      ggplot(data = f_1_data,
             aes(x_1,y_1)) +
        geom_line(colour = "deepskyblue",
                  lty = 1,
                  lwd = 2) + 
        geom_area(fill = "red4",
                  colour = "red4",
                  alpha = 0.5) + 
        geom_vline(xintercept = a_1,
                    colour = "gold",
                    lty = 2,
                    lwd = 1.5) + 
        geom_vline(xintercept = b_1,
                    colour = "gold",
                    lty = 2,
                    lwd = 1.5) + 
        labs(
          title = "plot the area of integral from 2 to 4 of (1/x) dx",
          x = "x",
          y = "y = f(x)"
        ) + theme_linedraw(ink = "black",
                           paper = alpha("lightgreen",0.25)
                           )