#DIKENS OKOTH BUS-244-083/2022
#EMMANUEL AMALEMBA BUS-244-061/2022
#JOB KENNEDY BUS-244-080/2022
#SOLOMON MWANGI BUS-244-026/2022
#FELISTER MUNENE BUS-244-035/2022
#BRION LUCIOH BUS-244-091/2022
#ANN MUGO BUS-244-019/2022
#GRACE WANDO BUS-244-048/2022
#MELISHIA OMWAKA BUS-244-016/2022
#TIM HEZRON BUS-244-089/2022
#probability that at least 9 out of 10 people who have been infected by a disease will survive if survival probability is 70%
#pmf
1-dbinom(9,10,0.7)
## [1] 0.8789392
# cdf
1-pbinom(9,10,0.7)
## [1] 0.02824752
#25th percentile
qbinom(0.25,10,0.7)
## [1] 6
#simulating random sample of 100 values
rbinom(100,10,0.7)
## [1] 6 6 8 6 3 6 6 9 6 7 6 4 9 6 8 7 4 8 8 8 6 6 5 5 5
## [26] 3 9 7 7 9 6 8 6 6 5 10 6 7 8 7 6 9 8 7 4 8 7 8 5 7
## [51] 6 7 4 6 7 7 9 5 8 6 9 9 7 5 4 7 7 7 7 6 6 6 5 10 8
## [76] 10 7 9 6 8 7 8 9 8 9 7 7 7 9 9 8 5 7 5 8 6 9 6 8 8
set.seed(12345)
#plot
plot(1:5,1-dbinom(1:5,10,0.7),type='l',xlab="survival",ylab="infection",main=" pmf of binomial distribution", col='red')

plot(1:5,1-pbinom(1:5,10,0.7),type='b',xlab="survival",ylab="infection",main=" cdf binomial distribution", col='blue')

par(mfrow=c(1,2))
#The number of home insurance claims a company receives in a month is distributed as a Poisson random variable with mean 2. Calculate the probability that the company receives exactly 30 claims in a year. Treat all months as if they are of equal length.
#pmf
dpois(30,2)
## [1] 5.478363e-25
#cdf
ppois(30,2)
## [1] 1
#5th decile
qpois(0.5,30,2)
## [1] 30
#simulating random sample of 10 values
rpois(10,2)
## [1] 3 4 3 4 2 1 1 2 3 6
set.seed(12345)
#plot
plot(0:10,dpois(0:10,30,2),type='l',xlab="claims recieved",ylab="home insurance",main=" pmf of poisson distribution", col='red')

plot(1:20,ppois(1:20,30,2),type='b',xlab="claims",ylab="home insurace",main=" cdf poison distribution", col='brown')

par(mfrow=c(1,2))
#It is estimated that 45% of people in Fast-Food restaurants order a diet drink with their lunch. Find the probability that the fourth person orders a diet drink. Also find the probability that the first diet drinker of th e day occurs before the 5th person.
#pmf
dgeom(4,0.45)
## [1] 0.04117781
dgeom(5,0.45)
## [1] 0.0226478
#cdf
pgeom(4,0.45)
## [1] 0.9496716
pgeom(5,0.45)
## [1] 0.9723194
# find 8th decile
qgeom(0.8,0.45)
## [1] 2
#simulating random sample of 20 values
rgeom(20,0.45)
## [1] 1 2 1 3 2 0 1 1 5 2 0 0 0 3 1 0 0 0 4 0
set.seed(12345)
#plot
plot(1:10,dgeom(1:10,0.45),type='l',xlab="first person",ylab="drink",main=" pmf of geometric distribution", col='purple')

plot(1:10,pgeom(1:10,0.45),type='l',xlab="first person",ylab="drink",main="cdf of geometric distribution", col='red')

par(mfrow=c(1,2))
#If the probability that a person will believe a rumor about a scandal in politics is 0.8, calculate the probability that the ninth person to hear the rumor will be the fourth person to believe it.
#pmf
dnbinom(9,4,0.8)
## [1] 4.613734e-05
#cdf
pnbinom(9,4,0.8)
## [1] 0.9999839
#75 percentile
qnbinom(0.75,4,0.8)
## [1] 2
#mean
mean(9,4,0.8)
## [1] 9
#simulating random sample of 1000 values
rnbinom(30,4,0.8)
## [1] 2 0 3 0 0 0 2 0 0 1 2 1 1 3 0 1 3 0 2 2 1 2 0 4 0 1 0 3 2 0
set.seed(12345)
#plot
plot(1:5,dnbinom(1:5,4,0.8),type='l',xlab="hearing",ylab="believing",main=" pdf of negative binomial distribution", col='orange')

plot(1:10,pnbinom(1:10,4,0.8),type='l',xlab="hearing",ylab="believing",main="cdf negative binomial distribution", col='brown')

par(mfrow=c(1,2))
#Among the 58 people applying for a job, only 30 have a particular qualification. If 5 of the group are randomly selected for a survey about the job application procedure, determine the probability that none of the group selected have the qualification.
#pmf
dhyper(0,30,28,5)
## [1] 0.02144861
#cdf
phyper(0,30,28,5)
## [1] 0.02144861
#quontile
qhyper(0.80,30,28,5)
## [1] 3
#simulating random sample of 20 values
rhyper(20,30,28,5)
## [1] 2 1 2 1 3 4 3 3 2 0 4 4 2 5 3 3 3 3 4 1
set.seed(12345)
#plot
plot(1:20,dhyper(1:20,30,28,5),type='l',xlab="selected",ylab="have qualification",main=" pdf of hypergeometric distribution", col='purple')

plot(1:20,phyper(1:20,30,28,5),type='l',xlab="hearing",ylab="believing",main="cdf hypergeometric distribution", col='green')

#`If X  Gamma(2,1.5), calculate P (X>4),p(x<10)
#pdf
1-dgamma(4,2,1.5)
## [1] 0.9776912
dgamma(10,2,1.5)
## [1] 6.882802e-06
#cdf
pgamma(10,2,1.5)
## [1] 0.9999951
1-pgamma(4,2,1.5)
## [1] 0.01735127
#90th percentile
qgamma(0.9,2,1.5)
## [1] 2.593147
#simulating random sample of 100 values
rgamma(100,2,1.5)
## [1] 1.53522294 1.66316692 0.91274542 1.01896983 3.78591793 1.35437077
## [7] 0.78144233 2.81623216 0.66706083 1.32551055 1.46985915 0.48107624
## [13] 1.45522273 2.12439086 1.25877952 1.73786036 2.54186191 0.54310123
## [19] 0.09067017 3.01691879 0.64540060 0.99844724 0.87186445 0.54125044
## [25] 3.37302041 2.77703274 1.21838720 1.44126454 0.75288977 1.26244108
## [31] 1.35207626 0.33298643 4.05861132 1.53285536 0.21762375 1.25836926
## [37] 1.26067157 2.33805428 2.09963475 0.45956247 1.64395860 1.78573253
## [43] 3.51832225 0.72321568 1.70928165 1.39655350 1.37338686 2.15889852
## [49] 0.40205595 1.04509057 0.46194891 2.92530366 0.99272569 1.91771310
## [55] 1.78842086 0.44714629 0.86505773 1.59637296 2.03308122 0.76692375
## [61] 0.38070265 3.10548999 1.62399389 0.76436300 2.48141625 1.92668222
## [67] 0.82276313 3.13411385 0.70566789 0.34647945 0.63014519 2.56281470
## [73] 0.54547810 0.66763799 0.37349687 0.21109917 1.50295194 0.87410792
## [79] 0.63011519 1.46439872 1.03419726 2.45900384 0.76730544 2.37000029
## [85] 1.28033260 2.64065688 0.68563304 2.92622449 0.54311598 4.39755897
## [91] 1.03849384 1.93809943 0.68571776 1.86539831 1.11644659 0.11486133
## [97] 1.34816603 1.41367864 0.50080198 0.80559864
set.seed(12345)
#plot
plot(1:20,1-dgamma(1:20,2,1.5),type='l', col='purple')

plot(1:20,1-pgamma(1:20,2,1.5),type='l', col='green')

#plot
plot(1:10,dgamma(1:10,2,1.5),type='l', col='purple')

plot(1:10,pgamma(1:10,2,1.5),type='l', col='green')

#Claims to a general insurance company’s 24 hour call center occur according to a Poisson process with a rate of 3 per hour. Calculate the probability that the next call arrives after more than ½ hour.
#pdf
1-dexp(0.5,3)
## [1] 0.3306095
#cdf
1-pexp(0.5,3)
## [1] 0.2231302
#60th percentile
qexp(0.6,3)
## [1] 0.3054302
#simulating random sample of 30 values
rexp(30,3)
## [1] 0.147269264 0.175824252 0.269489105 0.006149557 0.151803503 0.007979380
## [7] 2.134063079 0.419320174 0.320729362 0.606001013 0.075495837 0.148920970
## [13] 0.417487040 0.132362426 0.029371910 0.574054734 1.794954474 0.629331309
## [19] 0.121222415 0.391187892 0.379883154 0.142655366 0.486182479 0.188128854
## [25] 0.409706181 0.243957389 0.326015291 0.934236814 0.083695198 0.072786978
set.seed(12345)
#plot
plot(1:15,dexp(1:15,0.5,3),type='l',xlab="calls recieved",ylab="rate per hour",main=" pdf of exponential distribution", col='orange')

plot(1:15,pexp(1:15,0.5,3),type='l',xlab="calls recieved",ylab="rate per hour",main="cdf exponential distribution", col='chocolate')

#find the probability of a beta distribution of values of x greater than 7.using.b~(4,3)
#pdf
1-dbeta(7,4,3)
## [1] 1
#cdf
1-pbeta(7,4,3)
## [1] 0
#find 75th percentile
qbeta(0.75,4,3)
## [1] 0.7030835
#simulating random sample of 45 values
rbeta(45,4,3)
## [1] 0.4435028 0.4157194 0.5944344 0.6645920 0.8903153 0.4335269 0.6289067
## [8] 0.6305334 0.7528469 0.5958850 0.4912320 0.4580802 0.7218904 0.3915641
## [15] 0.7469023 0.6401637 0.5070265 0.3999488 0.7017902 0.8554544 0.8616921
## [22] 0.6701614 0.4357039 0.4375526 0.6054722 0.3926948 0.1398121 0.2150784
## [29] 0.5167390 0.4645410 0.6386471 0.8704374 0.5659516 0.3218226 0.9457953
## [36] 0.7776642 0.3645387 0.3831183 0.8349378 0.4475536 0.4440262 0.8184808
## [43] 0.6816931 0.5600334 0.4954076
set.seed(12345)
#plot
plot(1:10,dbeta(1:10,4,3),type='s',main="pdf beta distribution" ,col='blue')

plot(1:20,pbeta(1:20,4,3),type='l',main="cdf beta distribution", col='purple')

#If X ~ N(25,36), calculate: (i) P (X< 28) (ii) P( X> 30) (iii)p(X<20)
#pdf
dnorm(28,25,36)
## [1] 0.01104332
1-dnorm(30,25,36)
## [1] 0.9890246
dnorm(20,25,36)
## [1] 0.01097536
#cdf
pnorm(28,25,36)
## [1] 0.5332068
1-dnorm(30,25,36)
## [1] 0.9890246
dnorm(20,25,36)
## [1] 0.01097536
#find the probability of 50%
qnorm(0.5,25,36)
## [1] 25
#simulating random sample of 50 values
rnorm(50,25,36)
## [1] 46.079037 50.540777 21.065081 8.674102 46.811948 -40.446415
## [7] 47.683548 15.057372 14.770249 -8.095592 20.815079 90.423234
## [13] 38.342603 43.727792 -2.019152 54.408394 -6.908871 13.063207
## [19] 65.345655 35.754053 53.066389 77.408263 1.804177 -30.912947
## [25] -32.517543 89.983511 7.660695 47.333673 47.036446 19.156805
## [31] 54.227434 104.086008 98.770852 83.768043 34.153763 42.682778
## [37] 13.332883 -34.833809 88.638419 25.928838 65.626390 -60.692890
## [43] -13.169560 58.737059 55.760262 77.586259 -25.871556 45.426517
## [49] 45.994756 -22.044758
set.seed(12345)
#plot
plot(1:20,dnorm(1:20,25,36),type='l',main="pdf of normal distribution" ,col='red')

plot(1:20,pnorm(1:20,25,36),type='l',main="cdf normal distribution", col='purple')

#If W N ~ log (5,6) , calculate P (W>3,000) .
#pdf
1-dlnorm(3000,5,6)
## [1] 0.9999805
#cdf
1-plnorm(3000,5,6)
## [1] 0.308164
#find upper quontile
qlnorm(0.75,5,6)
## [1] 8492.498
#simulating random sample of 600 values
rlnorm(200,5,6)
## [1] 4.979939e+03 1.047552e+04 7.702886e+01 9.767074e+00 5.626961e+03
## [6] 2.718335e-03 6.506724e+03 2.830095e+01 2.697853e+01 5.969440e-01
## [11] 7.388525e+01 8.071681e+06 1.371647e+03 3.365389e+03 1.643467e+00
## [16] 1.995827e+04 7.274972e-01 2.029825e+01 1.235345e+05 8.909980e+02
## [21] 1.595826e+04 9.223781e+05 3.108155e+00 1.331553e-02 1.019093e-02
## [26] 7.501287e+06 8.249197e+00 6.138151e+03 5.841489e+03 5.604383e+01
## [31] 1.936532e+04 7.868800e+07 3.244776e+07 2.662228e+06 6.824076e+02
## [36] 2.827448e+03 2.123139e+01 6.927187e-03 5.994801e+06 1.732622e+02
## [41] 1.294519e+05 9.307096e-05 2.562522e-01 4.106220e+04 2.500198e+04
## [46] 9.501512e+05 3.085080e-02 4.466764e+03 4.910475e+03 5.837935e-02
## [51] 5.798989e+00 1.764841e+07 2.046992e+02 1.224119e+03 2.648890e+00
## [56] 7.866018e+02 9.386499e+03 2.080133e+04 5.767822e+07 1.137322e-04
## [61] 3.641449e+02 4.711382e-02 4.104415e+03 2.063217e+06 4.387337e+00
## [66] 2.493012e-03 3.060249e+04 2.107327e+06 3.298186e+03 6.240998e-02
## [71] 2.059624e+02 1.339242e+00 2.735921e-01 1.754863e+08 6.708040e+05
## [76] 4.242975e+04 2.111100e+04 1.139691e+00 2.585023e+03 6.801955e+04
## [81] 7.131654e+03 7.756420e+04 2.389777e+01 4.229081e+08 5.037871e+04
## [86] 1.088172e+07 8.368708e+03 2.338932e+01 3.711318e+03 2.093589e+04
## [91] 4.568460e-01 8.776608e-01 1.225791e+07 1.414108e+01 4.132107e-01
## [96] 9.172752e+03 7.168804e+00 6.222820e+07 4.060128e+00 2.299565e+00
## [101] 5.688134e+02 1.440867e-01 1.871529e+03 5.241662e-02 3.460232e+02
## [106] 5.951909e+00 2.288230e+01 1.683959e+06 1.009258e+01 1.019167e+03
## [111] 9.245498e-02 5.263615e-02 2.870620e+05 4.065208e+05 9.142120e+01
## [116] 7.166814e+00 1.085356e+02 6.458573e+03 7.112962e+07 9.809105e+01
## [121] 1.574080e+06 4.119726e+05 1.025473e+03 1.448053e+06 1.185277e+01
## [126] 1.418584e-01 2.306072e-03 1.538836e+05 4.345067e-04 1.134731e-01
## [131] 2.822552e+06 2.979003e+04 3.460801e+03 1.214862e-01 1.235485e+09
## [136] 2.759647e-01 3.441302e-01 8.213463e+03 3.221650e+02 1.175806e+01
## [141] 1.585659e-01 6.314689e-02 4.186259e+00 1.822639e-02 1.632257e+02
## [146] 3.793398e+03 1.379082e-02 2.429237e+04 3.208292e+04 3.410899e+02
## [151] 8.951180e-03 3.985378e+03 4.789963e+02 1.174699e+00 7.734335e+01
## [156] 3.292790e+01 3.977166e+06 1.880659e+01 2.228797e+02 2.993914e+00
## [161] 7.957930e+00 9.149848e+02 3.474918e+01 8.244931e+00 3.864251e-01
## [166] 2.755283e+01 6.621678e+03 8.725590e-02 5.873049e+06 1.287562e+02
## [171] 4.925140e+02 4.807760e+05 1.842772e+02 2.089963e+04 5.428833e-03
## [176] 2.658988e+03 4.395669e+08 1.649438e+03 5.397268e+02 2.755176e-03
## [181] 6.247295e-01 1.105792e+02 1.303548e+01 1.309133e+05 1.978716e+04
## [186] 2.347462e+02 9.111694e+05 1.400699e+03 5.322753e+01 7.291601e+00
## [191] 3.870476e+03 7.162678e+00 1.666015e+04 9.029764e+02 3.851328e+05
## [196] 1.786507e+04 2.446901e+04 1.036663e+01 1.016908e+01 1.607468e+02
#plot
plot(1:40,dlnorm(1:40,5,6),type='l',main="pdf of lognormal distribution" ,col='blue')

plot(1:40,plnorm(1:40,5,6),type='l',main="cdf lognormal distribution", col='green')

#the average number of donuts a nine-year old child eats per month is uniformly distributed from 0.5 to 4 donuts, inclusive. Determine the probability that a randomly selected nine-year old child eats an average of;a) more than two donuts b) more than two donuts given that his or her amount is more than 1.5 donuts
#pdf
1-dunif(2,0.5,4)
## [1] 0.7142857
1-dunif(2,1.5,4)
## [1] 0.6
#cdf
1-punif(2,0.5,4)
## [1] 0.5714286
1-punif(2,1.5,4)
## [1] 0.8
#quontiles
#find 25th percentile
qunif(0.25,0.5,4)
## [1] 1.375
qunif(0.25,1.5,4)
## [1] 2.125
#simulating random sample of 100 values
runif(100,0.25,4)
## [1] 0.5330517 2.0289409 1.2422108 1.1152978 2.4857477 0.8459709 3.4564556
## [8] 1.1404517 3.2391689 0.5443210 2.2821105 2.9385423 0.4029225 0.3460017
## [15] 3.0273116 3.1380590 2.2700432 1.7055117 2.0103164 0.7093243 3.3977095
## [22] 1.2526395 0.9816291 1.1923756 1.7092484 3.4134985 3.9272404 1.3986989
## [29] 1.2640629 2.5860160 2.5894443 0.4592133 3.0455020 1.4156439 0.4094366
## [36] 2.3773845 0.3114842 3.5344949 2.2280633 0.6604613 3.2772026 3.2840397
## [43] 0.2912852 2.8127774 1.9011522 3.6365691 1.7276830 0.2659536 3.8626749
## [50] 3.8760072 3.0598505 1.7501552 1.8141497 3.3348120 2.1432250 2.8609636
## [57] 3.7656233 1.2098929 2.2405161 0.5946334 2.2422249 3.7165555 1.0671736
## [64] 3.4790730 2.3730315 1.7405540 2.5173261 1.6614173 3.3005372 1.3460837
## [71] 1.4497458 1.8137316 3.1595748 2.6519203 1.2269582 1.4225609 3.0058395
## [78] 3.1587265 2.4927269 0.6872761 1.1572749 1.2413012 1.3195776 2.2518327
## [85] 1.7383539 2.6655282 0.7888390 3.4984952 1.9661685 3.6232127 3.1737898
## [92] 3.3083003 3.9887370 2.4617489 1.9995781 1.4912032 2.8998643 3.0901717
## [99] 3.1533750 3.5172333
set.seed(12345)
#plot
plot(1:20,dunif(1:20,0.5,4),type='l',xlab="number of donuts",ylab="range of donuts",main="pdf of uniform distribution" ,col='pink')

plot(1:20,punif(1:20,0.5,4),type='l',xlab="number of donuts",ylab="range of donuts",main="cdf uniform distribution", col='green')
