Question 1: [ 5 points] Toss a coin until you obtain \(k\) consecutive heads. Write a function with call form ngtm(k,m,nreps) that uses the Monte Carlo simulation to find and return the approximate probability that it takes more than \(m\) tosses to achieve the goal.

set.seed(123)
ngtm = function(k,m,nreps){
  morem= 0
  for (i in 1:nreps) {
    conshead= 0
    toss= 0
    
    while(conshead<k){
      toss = toss+1
      if(runif(1) <0.5){
        conshead=0
        
      }
      else{
        conshead =conshead+1
      }
    }
    if (toss>m){
      morem = morem+1
    }
  }
  morem/nreps
}
ngtm(3,10,10000)
## [1] 0.4921

Question 2: [5 points] Use the inverse transform method to generate a random variable having distribution function:

\(F(x)=\frac{x^2+x}{2}\), where \(0\le x \le 1\)

inverseu= function(fx){
  (-1+sqrt(1+8*fx))/2
}

u = runif(1000)
inverseu(u)
##    [1] 0.9799829975 0.3960038068 0.9541034375 0.8633581887 0.9175981452
##    [6] 0.8521557175 0.6199194942 0.9474275187 0.8544050371 0.0147741723
##   [11] 0.4251169037 0.1077985811 0.2132161224 0.5671396258 0.9402827983
##   [16] 0.8988235173 0.7411833754 0.6676860909 0.8844328596 0.7948972855
##   [21] 0.9365878844 0.8575903772 0.0698914456 0.4710029294 0.1057251357
##   [26] 0.0424446516 0.0785404707 0.5049536428 0.1368216727 0.4919073923
##   [31] 0.2017978156 0.6918383374 0.5266200348 0.5573203830 0.9080670939
##   [36] 0.4497161004 0.7512810571 0.8878003486 0.5487884542 0.5027046079
##   [41] 0.8228851551 0.8817117655 0.1649790329 0.5668503460 0.0289729744
##   [46] 0.6106591122 0.4848174981 0.6003658551 0.8705317823 0.7513832911
##   [51] 0.7615792300 0.5722660471 0.4153088360 0.1062461738 0.6438765759
##   [56] 0.9121662634 0.4056685839 0.7779008854 0.3613700675 0.5904510457
##   [61] 0.1926389561 0.4387787257 0.0198947846 0.7377988830 0.6548555520
##   [66] 0.8396513207 0.3948179167 0.6709315993 0.5355215984 0.9708352133
##   [71] 0.0664393810 0.8768225897 0.3888436958 0.4209235267 0.5122838090
##   [76] 0.9901314776 0.6567554936 0.8387745789 0.6742548280 0.9242703436
##   [81] 0.0489930579 0.6862339689 0.9041279090 0.9690806172 0.1618687574
##   [86] 0.7232532638 0.5538051888 0.6067695317 0.0967556871 0.6181580641
##   [91] 0.7265575012 0.2849129563 0.2406635846 0.1096130771 0.5975628112
##   [96] 0.0175897075 0.4170311281 0.8303212738 0.6520387585 0.5967047513
##  [101] 0.6615618712 0.8420150313 0.4019610400 0.5382338530 0.7109264898
##  [106] 0.4930252988 0.1061893881 0.2265585222 0.9918845186 0.9942841190
##  [111] 0.5046288231 0.6265229352 0.9038359593 0.6905479007 0.9127146455
##  [116] 0.1119588236 0.4850052583 0.3371627968 0.8376744426 0.4715020789
##  [121] 0.9615082149 0.0804705914 0.7787280831 0.9571679516 0.7324077835
##  [126] 0.6007966026 0.2163515852 0.1826541915 0.1210839047 0.9929167488
##  [131] 0.7050382022 0.3606682689 0.4204778343 0.8134450425 0.1256516219
##  [136] 0.7780072466 0.5128109955 0.9403097690 0.5494487436 0.0427972682
##  [141] 0.7914045117 0.7267932371 0.7583806405 0.7759663913 0.4349465958
##  [146] 0.9361890320 0.9190927284 0.6569730680 0.1951143754 0.2041424347
##  [151] 0.7506339202 0.1295358393 0.7087199776 0.8254894490 0.5980713457
##  [156] 0.8009060094 0.3087275652 0.8228274484 0.9247220604 0.7112033148
##  [161] 0.5137872649 0.7365116764 0.9895908999 0.7241467343 0.8772564250
##  [166] 0.6310086671 0.6844381921 0.7579481630 0.4586174082 0.4303210045
##  [171] 0.4966996629 0.6440721069 0.0406858290 0.9147904700 0.4632159229
##  [176] 0.7724570935 0.9599283597 0.7183889600 0.2901648839 0.8830813188
##  [181] 0.9934559751 0.0317433565 0.5830747884 0.5658756814 0.5160862164
##  [186] 0.8235334588 0.3502113284 0.9696961688 0.0117760100 0.2943413862
##  [191] 0.7169352988 0.2948560699 0.0349974753 0.9963678880 0.9340077783
##  [196] 0.7314528581 0.5195074540 0.9324667516 0.7238706556 0.0873220592
##  [201] 0.6336841508 0.7671608670 0.9697479807 0.9016472806 0.5688493895
##  [206] 0.7111107170 0.6762297613 0.7267610075 0.5872566126 0.4900566554
##  [211] 0.2769323856 0.7412117790 0.1363900695 0.4824759500 0.1320580740
##  [216] 0.9307879069 0.8095193335 0.5467374597 0.6050234220 0.9447145469
##  [221] 0.1770341259 0.6633596214 0.9057568507 0.4994794310 0.1248050986
##  [226] 0.0105242935 0.7660362192 0.4448612758 0.7545800480 0.6203266759
##  [231] 0.4224812564 0.4222020800 0.6963935147 0.6765967469 0.9677147720
##  [236] 0.4060703078 0.8115779982 0.9353639137 0.7431846246 0.9395411554
##  [241] 0.8244747160 0.7253212381 0.7567770541 0.9305895991 0.4798258946
##  [246] 0.4483894723 0.5624599756 0.8241918635 0.4542646352 0.9225499785
##  [251] 0.5170114854 0.5405638331 0.8647712983 0.8485732405 0.8721272771
##  [256] 0.3464673969 0.5111239104 0.9863735238 0.6090415423 0.1461327481
##  [261] 0.6076257823 0.6912027711 0.7208725302 0.9823759467 0.6770647840
##  [266] 0.5031912824 0.8220078025 0.3083874703 0.5158489320 0.0711703630
##  [271] 0.9349508720 0.6461736406 0.6106773664 0.1529156546 0.2635285918
##  [276] 0.8378283725 0.6110871991 0.4520346468 0.6574123171 0.4953963999
##  [281] 0.5516064056 0.8875726109 0.7611544075 0.7573088643 0.1235269346
##  [286] 0.2251174314 0.6224730045 0.6562925485 0.8526772086 0.4218861480
##  [291] 0.0890752463 0.3612405325 0.8559512967 0.9298479128 0.6122064401
##  [296] 0.4785959588 0.8941906280 0.5109526143 0.8235552157 0.3069748258
##  [301] 0.0817589511 0.7241098039 0.9517763008 0.6958681785 0.1396023683
##  [306] 0.6707884388 0.6164272531 0.0484623865 0.3842227454 0.8703196968
##  [311] 0.8083747764 0.5825727841 0.6533509667 0.5934084514 0.6003357354
##  [316] 0.8866263451 0.4357850057 0.4319998433 0.9541346379 0.4114128065
##  [321] 0.7451493401 0.7359654341 0.9548676897 0.7083687280 0.7091776641
##  [326] 0.0619193812 0.0330789945 0.6763618561 0.1386997293 0.8513143362
##  [331] 0.7467194316 0.4190266000 0.8108612734 0.1813005091 0.1097013978
##  [336] 0.8908473131 0.9515320217 0.4871936247 0.9914559100 0.5735104966
##  [341] 0.8692235384 0.3142660304 0.8865534506 0.7666635969 0.9011955733
##  [346] 0.7447389615 0.5192073793 0.2896357699 0.9917275682 0.8249968612
##  [351] 0.9503249698 0.8979600632 0.9572296834 0.9938775746 0.8465823867
##  [356] 0.3738651561 0.5002823264 0.8980186184 0.4885712199 0.4785835067
##  [361] 0.9870525561 0.6978020071 0.3195094918 0.2556350280 0.9692430848
##  [366] 0.3412287824 0.9249261276 0.6651810636 0.0871879427 0.3573925840
##  [371] 0.7574752250 0.8223339481 0.5151515435 0.8020230901 0.7442057529
##  [376] 0.6931160109 0.1809251379 0.9795620264 0.4253409449 0.7348623044
##  [381] 0.9042360464 0.5796828277 0.6161301224 0.3763465329 0.4832234528
##  [386] 0.3923577236 0.0401083835 0.1423770335 0.5235346291 0.8099710870
##  [391] 0.9309059629 0.4948724194 0.8810427716 0.5525996884 0.7638183919
##  [396] 0.9402514724 0.9473414160 0.7503459126 0.5701066286 0.7254269860
##  [401] 0.3823382781 0.4081763120 0.8110907944 0.9623549210 0.8151354494
##  [406] 0.7241114719 0.6005151530 0.9078573157 0.1714991108 0.9791411364
##  [411] 0.0980625789 0.8471452235 0.8367035408 0.1349564409 0.1978788354
##  [416] 0.9238057259 0.9092790013 0.4117858336 0.3332379949 0.7442920872
##  [421] 0.1525950683 0.5303178886 0.5907180528 0.5058162951 0.3008288614
##  [426] 0.3543643649 0.9938540517 0.8932395390 0.0514720920 0.5134878221
##  [431] 0.3900614103 0.2299873486 0.3360947245 0.3190126671 0.1111528413
##  [436] 0.9959691356 0.7439655728 0.3533214342 0.7678476085 0.9304669965
##  [441] 0.8584083535 0.4080199023 0.4673341267 0.2719121423 0.4043713185
##  [446] 0.2694395336 0.4847907875 0.7208985254 0.7184056319 0.6872521508
##  [451] 0.8286244675 0.2554809451 0.1736822205 0.5475926842 0.3509496473
##  [456] 0.8968569072 0.8174092258 0.8724013124 0.5067347544 0.3524364692
##  [461] 0.8804710367 0.2775265500 0.7025959737 0.7973834321 0.9811280147
##  [466] 0.8312654381 0.8957171532 0.6535373069 0.7649958825 0.1394324397
##  [471] 0.8072581480 0.6721461745 0.4941494228 0.6269892035 0.8831827478
##  [476] 0.7862762717 0.9047614567 0.9827947647 0.9965114105 0.4342371766
##  [481] 0.8157322383 0.7488468445 0.9632021960 0.9278370216 0.7157140789
##  [486] 0.7112448525 0.7006320979 0.8614092258 0.4740146127 0.9750182504
##  [491] 0.5229913912 0.1240119713 0.0532965051 0.9349943298 0.9228275553
##  [496] 0.3216495715 0.1833502012 0.6373696663 0.4064637150 0.6862926062
##  [501] 0.9413140997 0.9615323310 0.2968247637 0.7524126791 0.6676519343
##  [506] 0.9558956279 0.7626754886 0.7839266421 0.1867195330 0.9245347340
##  [511] 0.9458865880 0.1790860261 0.7654207454 0.7090381179 0.3239494766
##  [516] 0.9799639320 0.7308078647 0.8161407822 0.0890445075 0.6180198195
##  [521] 0.2023843182 0.0111583069 0.3514788430 0.2015258263 0.7367560396
##  [526] 0.9164036714 0.7214429994 0.7001401605 0.8471903065 0.8891882458
##  [531] 0.8792161397 0.3197274282 0.9016323972 0.9526559163 0.9174362863
##  [536] 0.9678434836 0.3110043390 0.9437219340 0.9468983390 0.7452779040
##  [541] 0.5458000243 0.9653577837 0.6739536689 0.9973395468 0.8848193931
##  [546] 0.2316236002 0.4686586213 0.7108873417 0.6533867559 0.4709231367
##  [551] 0.9458241210 0.9805163032 0.9751012724 0.9370485951 0.1621970091
##  [556] 0.4588160234 0.4394913044 0.5968539500 0.6241467808 0.9046728806
##  [561] 0.7975761661 0.7288570282 0.6226043644 0.7247166338 0.7254241487
##  [566] 0.1596772958 0.0436758876 0.0733467267 0.4115604864 0.5942093191
##  [571] 0.4641271207 0.5610344424 0.9141945259 0.6773952922 0.6350679604
##  [576] 0.8537683354 0.6962382582 0.6069438536 0.5370950624 0.4177966081
##  [581] 0.8015005052 0.8135479181 0.7449846115 0.8058144048 0.0011600526
##  [586] 0.6949757375 0.7719648216 0.0413374288 0.9557702060 0.9625989538
##  [591] 0.9956471566 0.8052975742 0.3140627745 0.9318259690 0.0006805172
##  [596] 0.5881788272 0.9715757409 0.2496240081 0.3571154228 0.1794791495
##  [601] 0.1450488306 0.3202854334 0.8096083508 0.9351141045 0.6022948389
##  [606] 0.1807598881 0.5226217064 0.1918020567 0.0916439766 0.5068299849
##  [611] 0.3094849412 0.3225468613 0.2766833264 0.0730499403 0.6043853464
##  [616] 0.7768931288 0.9588932182 0.1612128976 0.6776744195 0.3990701775
##  [621] 0.9973848777 0.5910448379 0.9747413549 0.6566188194 0.9660359109
##  [626] 0.8335139290 0.7364938480 0.2277094453 0.6311295799 0.8246241929
##  [631] 0.2600677620 0.9239302731 0.5331107278 0.7351491003 0.0511577909
##  [636] 0.2774561862 0.3838630482 0.8817031326 0.0672741542 0.1888017269
##  [641] 0.3771473857 0.2122511540 0.0370719398 0.9768496834 0.7377915681
##  [646] 0.7986891706 0.5420174245 0.7210131770 0.4806253735 0.8769439741
##  [651] 0.5383880506 0.6746571998 0.0449602065 0.3830733230 0.2253009514
##  [656] 0.2996290463 0.6899689766 0.4373149970 0.4101800637 0.8329724557
##  [661] 0.8107376614 0.5461850344 0.6522751955 0.8073781671 0.2718886074
##  [666] 0.4287433799 0.3128318731 0.9584008701 0.1464792796 0.8842195723
##  [671] 0.1030370997 0.4033492182 0.0433240704 0.7203219761 0.6112426459
##  [676] 0.4358383217 0.8051135002 0.8702697708 0.8438787851 0.7628026750
##  [681] 0.3734467852 0.2458467590 0.9168902979 0.1264823099 0.7423877650
##  [686] 0.1468809657 0.0875806316 0.2813806383 0.3346854634 0.4457892512
##  [691] 0.3865553736 0.9813919998 0.8162729230 0.3918440615 0.6345119133
##  [696] 0.7664399099 0.7454106301 0.7939979292 0.7986556258 0.9610981434
##  [701] 0.4929006386 0.3843830336 0.9217681946 0.7977049060 0.4437612419
##  [706] 0.3958049347 0.5622440240 0.9625716231 0.7103257866 0.3394000212
##  [711] 0.6733060894 0.3459548796 0.6321026257 0.2215061534 0.8833699122
##  [716] 0.1765584556 0.7997870324 0.4106414780 0.8006838930 0.1775516822
##  [721] 0.8137713653 0.9328477672 0.7888080030 0.5419206798 0.5240142686
##  [726] 0.9567009426 0.9654517597 0.5647149136 0.8862290132 0.7314576457
##  [731] 0.9946554690 0.2545327912 0.6461596640 0.6080193314 0.7706477699
##  [736] 0.2649576517 0.0115917632 0.8003944020 0.9558099498 0.6771729774
##  [741] 0.9300258266 0.8400989219 0.8372266420 0.7318233901 0.3870704779
##  [746] 0.4310409922 0.1518201322 0.1075099317 0.5239739391 0.3767780640
##  [751] 0.9514231331 0.7341954898 0.4978105758 0.8280577418 0.3215198020
##  [756] 0.0995144082 0.5886788177 0.7937076075 0.8166746606 0.4053424839
##  [761] 0.8256412891 0.5847360713 0.8208122886 0.1410321470 0.2211460084
##  [766] 0.9690896538 0.9147776493 0.1549186393 0.8981552718 0.7015111363
##  [771] 0.3229492253 0.7019509367 0.8420553005 0.7515803398 0.6868206998
##  [776] 0.8710752488 0.8425743384 0.5795972072 0.6114846942 0.1817994425
##  [781] 0.3601578403 0.8496530661 0.9974731258 0.3872515342 0.5591924844
##  [786] 0.8808559633 0.9112772863 0.1822356098 0.4493120260 0.7744734677
##  [791] 0.0209843917 0.6397310481 0.9110695279 0.5818416431 0.7522619902
##  [796] 0.8191348231 0.0548049326 0.6764120866 0.3038920906 0.7186168929
##  [801] 0.3180248247 0.5934708682 0.7543824059 0.1108354260 0.8186924491
##  [806] 0.9825140658 0.3246550959 0.6543760928 0.9599633359 0.0454854337
##  [811] 0.5590280411 0.7232187020 0.5004289797 0.8614701826 0.9591194370
##  [816] 0.8371846837 0.7217973495 0.7516929290 0.6823713213 0.4573034162
##  [821] 0.9549889062 0.3053013468 0.6179753232 0.9129121993 0.9651009107
##  [826] 0.9469855005 0.3068171489 0.3395157667 0.8560492969 0.5497212146
##  [831] 0.6714179602 0.3537420430 0.3871545172 0.7315191516 0.5255052348
##  [836] 0.7161656085 0.0560441117 0.2970870144 0.4923761517 0.1199869537
##  [841] 0.9393614597 0.7813137398 0.3123311646 0.7552257663 0.5714752803
##  [846] 0.9288574241 0.7907309418 0.6248098079 0.8909543324 0.2192986878
##  [851] 0.4087984910 0.4001993865 0.7274390850 0.4222193715 0.6797816651
##  [856] 0.3420151800 0.2857468964 0.0829774838 0.7033792297 0.8141621803
##  [861] 0.3666769296 0.5630996155 0.5587848123 0.2997255857 0.7188770380
##  [866] 0.3415792664 0.9772683466 0.6034020768 0.1846226517 0.5779447094
##  [871] 0.4549624767 0.8504537284 0.8045876775 0.2215002202 0.0703033955
##  [876] 0.0357567088 0.2551813183 0.1552302002 0.1106918165 0.8140312091
##  [881] 0.5794322204 0.8974064479 0.5548178533 0.8210127484 0.7170954716
##  [886] 0.2682666960 0.8802835896 0.1820514272 0.9333751793 0.1214773295
##  [891] 0.8329527650 0.4901133017 0.7090535712 0.9280852126 0.1294226644
##  [896] 0.7004070340 0.4289628877 0.2029621058 0.8818898078 0.3741631023
##  [901] 0.8359516708 0.1523100257 0.6053542587 0.9271932576 0.3661423442
##  [906] 0.8371556522 0.9370136817 0.1905670107 0.3751257185 0.5363079506
##  [911] 0.9234845454 0.1552500220 0.0117301639 0.5395722477 0.7613321367
##  [916] 0.6015558997 0.6025904839 0.1020315110 0.5615079913 0.4450825447
##  [921] 0.4563887023 0.0927422655 0.4723774762 0.2456178795 0.2239257143
##  [926] 0.7559524219 0.0835091095 0.4537952943 0.8004627907 0.4824983733
##  [931] 0.8528847266 0.7109433578 0.8198378515 0.2693025992 0.8436473383
##  [936] 0.4116830797 0.8025282982 0.1835761524 0.5444596886 0.4851795470
##  [941] 0.3633486082 0.5803730583 0.7686865069 0.9438703967 0.6054600148
##  [946] 0.0520721144 0.3108347181 0.9160607796 0.7088992184 0.1654787578
##  [951] 0.6480195094 0.6747404267 0.1777636044 0.4051865204 0.7516658510
##  [956] 0.3079027579 0.6791434727 0.6151782548 0.3380820985 0.8700006319
##  [961] 0.6437212096 0.9621558599 0.8377692947 0.2731596193 0.7101615921
##  [966] 0.1595200984 0.9741081739 0.9275540769 0.9272564737 0.6725945868
##  [971] 0.1537059229 0.2582634025 0.9224176432 0.1596030684 0.9240486989
##  [976] 0.2986087337 0.7430864499 0.0089912685 0.5891002378 0.7991481518
##  [981] 0.6099013124 0.5617993039 0.5191044650 0.8605384036 0.2561596504
##  [986] 0.1080523091 0.9887617723 0.6729657684 0.5131025620 0.5603487269
##  [991] 0.3445865945 0.9524965732 0.2696603537 0.8111494934 0.1692907218
##  [996] 0.2986546412 0.3990636846 0.6851088150 0.7655652245 0.5831541770

Question 3: [5 points] Use the rejection method to find an efficient way to generate a random variable having density function:

\(f(x)= \frac{1}{2} (1 + x)e^{−x}\) , where \(0 <x< \infty\)

f= function(x){
  return(0.5 *(1+x)*exp(-x))
}

g= function(x){
  return(exp(-x))
}

gen= function(n){
  samples = numeric(n)
  count =1
  
  while (count <=n) {
    x= rexp(1)
    u = runif(1)
    if(u*g(x)<=f(x)){
      samples[count] =x
      count= count+1
    }
  }
  return(samples)
}

set.seed(123)
generated_values= gen(1000)
hist(generated_values)

N.B.

  1. Assignments are set to help students learn and identify what they know and what they do not know. They are effective practices for students to test their knowledge and understanding of the course materials to score good marks in the examinations. Therefore, take responsibility for your behaviour while answering the questions and be honest with yourself!
  2. Submit your answers as html or pdf file.