Library

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.4.3
## -- Attaching packages ---------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1     v purrr   0.2.4
## v tibble  1.4.1     v dplyr   0.7.4
## v tidyr   0.7.2     v stringr 1.2.0
## v readr   1.1.1     v forcats 0.2.0
## Warning: package 'tibble' was built under R version 3.4.3
## Warning: package 'tidyr' was built under R version 3.4.3
## Warning: package 'readr' was built under R version 3.4.3
## Warning: package 'purrr' was built under R version 3.4.3
## Warning: package 'dplyr' was built under R version 3.4.2
## Warning: package 'forcats' was built under R version 3.4.3
## -- Conflicts ------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(distr)
## Warning: package 'distr' was built under R version 3.4.4
## Loading required package: startupmsg
## Warning: package 'startupmsg' was built under R version 3.4.4
## Utilities for Start-Up Messages (version 0.9.5)
## For more information see ?"startupmsg", NEWS("startupmsg")
## Loading required package: sfsmisc
## Warning: package 'sfsmisc' was built under R version 3.4.4
## 
## Attaching package: 'sfsmisc'
## The following object is masked from 'package:dplyr':
## 
##     last
## Object Oriented Implementation of Distributions (version 2.7.0)
## Attention: Arithmetics on distribution objects are understood as operations on corresponding random variables (r.v.s); see distrARITH().
## Some functions from package 'stats' are intentionally masked ---see distrMASK().
## Note that global options are controlled by distroptions() ---c.f. ?"distroptions".
## For more information see ?"distr", NEWS("distr"), as well as
##   http://distr.r-forge.r-project.org/
## Package "distrDoc" provides a vignette to this package as well as to several extension packages; try vignette("distr").
## 
## Attaching package: 'distr'
## The following objects are masked from 'package:dplyr':
## 
##     location, n
## The following objects are masked from 'package:stats':
## 
##     df, qqplot, sd

Choose indepenently two numbers B and C from the interval [0,1] with uniform density. Prove that B and c are proper probability distributions. Note that point (B,C) is then chosen at random in the unit square.

(A) Find the probability that B+C < 1/2

First, we create 10,000 random numbers from 0 to 1 and assign them to variables BA & CA respectively; the A suffix corresponds to question A.

BA <- runif(10000, min = 0, max = 1)
CA <- runif(10000, min = 0, max = 1)

Next, we create a variable ZA which will host the sum of the variables BA and CA

ZA <- BA + CA

To provide a visual we will create a tibble that will showcase the variables created

(ZAT <- tibble(BA,CA,ZA))
## # A tibble: 10,000 x 3
##       BA     CA    ZA
##    <dbl>  <dbl> <dbl>
##  1 0.728 0.462  1.19 
##  2 0.170 0.839  1.01 
##  3 0.660 0.766  1.43 
##  4 0.688 0.692  1.38 
##  5 0.189 0.640  0.829
##  6 0.556 0.852  1.41 
##  7 0.372 0.752  1.12 
##  8 0.136 0.803  0.939
##  9 0.876 0.273  1.15 
## 10 0.112 0.0375 0.150
## # ... with 9,990 more rows

We can also see that there is no variables in ZAT that are below 0 by running a summary.

ZAT %>% summary()
##        BA                  CA                  ZA          
##  Min.   :0.0000804   Min.   :0.0000112   Min.   :0.005683  
##  1st Qu.:0.2484283   1st Qu.:0.2500285   1st Qu.:0.707749  
##  Median :0.4987503   Median :0.5057283   Median :1.002737  
##  Mean   :0.4995242   Mean   :0.5021684   Mean   :1.001693  
##  3rd Qu.:0.7508814   3rd Qu.:0.7569676   3rd Qu.:1.293915  
##  Max.   :0.9999382   Max.   :0.9999994   Max.   :1.997156

In order to visualize the ZA variable we will plot its findings on a histogram.

hist(ZA, xlab="ZA",col="beige",breaks=10,probability = TRUE,ylim=c(0,1.0))

Using the integrate function we set the upper limits to 0.5 to obtain the probability of A

f <- function(x){x}
PA <- integrate(f, lower = 0, upper= 0.5)
PA
## 0.125 with absolute error < 1.4e-15

(B) Find the probability that BC < 1/2

First, we create 10,000 random numbers from 0 to 1 and assign them to variables BB & CB respectively.

BB <- runif(10000, min = 0, max = 1)
CB <- runif(10000, min = 0, max = 1)

Next, we create a variable ZB which will host the product of the variables BB and CB

ZB <- BB * CB

To provide a visual we will create a tibble that will showcase the variables created

(ZBT <- tibble(BB,CB,ZB))
## # A tibble: 10,000 x 3
##        BB     CB     ZB
##     <dbl>  <dbl>  <dbl>
##  1 0.383  0.949  0.364 
##  2 0.799  0.825  0.659 
##  3 0.585  0.0307 0.0179
##  4 0.309  0.923  0.286 
##  5 0.515  0.372  0.191 
##  6 0.0794 0.679  0.0539
##  7 0.0911 0.237  0.0216
##  8 0.908  0.385  0.349 
##  9 0.593  0.811  0.481 
## 10 0.651  0.258  0.168 
## # ... with 9,990 more rows

We can also see that there is no variables in ZBT that are below 0 by running a summary.

ZBT %>% summary()
##        BB                  CB                  ZB           
##  Min.   :0.0000421   Min.   :0.0001521   Min.   :0.0000025  
##  1st Qu.:0.2535308   1st Qu.:0.2475493   1st Qu.:0.0696264  
##  Median :0.5065082   Median :0.4964271   Median :0.1894975  
##  Mean   :0.5019821   Mean   :0.4987519   Mean   :0.2507308  
##  3rd Qu.:0.7508229   3rd Qu.:0.7515797   3rd Qu.:0.3849211  
##  Max.   :0.9997904   Max.   :0.9998306   Max.   :0.9812452

In order to visualize the ZB we will plot its findings on a histogram as we did with ZA

hist(ZB, xlab="ZB",col="purple",breaks=10,probability = TRUE,ylim=c(0,3.5))

As we can see the output is skewed to the left and don’t posses a symmetric look as ZA

Calling the lower limit of intergration of 0 and the upper limit as 0.5 based off the anonymous function.

f1 <- function(x){x}
PB <- integrate(f1, lower = 0, upper = 0.5)
PB
## 0.125 with absolute error < 1.4e-15

(C) Find the probability that |B-C| < 1/2

First, we create 10,000 random numbers from 0 to 1 and assign them to variables BC & CC respectively.

BC <- runif(10000, min = 0, max = 1)
CC <- runif(10000, min = 0, max = 1)

Next, we create a variable ZC which will host the absolute difference of the variables BC and CC

ZC <- abs(BC * CC)

To provide a visual we will create a tibble that will showcase the variables created

(ZCT <- tibble(BC,CC,ZC))
## # A tibble: 10,000 x 3
##        BC     CC      ZC
##     <dbl>  <dbl>   <dbl>
##  1 0.216  0.229  0.0494 
##  2 0.0116 0.306  0.00356
##  3 0.335  0.554  0.186  
##  4 0.454  0.225  0.102  
##  5 0.571  0.256  0.146  
##  6 0.466  0.0412 0.0192 
##  7 0.0375 0.867  0.0326 
##  8 0.535  0.825  0.441  
##  9 0.969  0.378  0.366  
## 10 0.407  0.716  0.292  
## # ... with 9,990 more rows

We can also see that there is no variables in ZCT that are below 0 by running a summary.

ZCT %>% summary()
##        BC                  CC                  ZC           
##  Min.   :0.0000565   Min.   :0.0000532   Min.   :0.0000152  
##  1st Qu.:0.2519704   1st Qu.:0.2541732   1st Qu.:0.0683726  
##  Median :0.5051390   Median :0.5005201   Median :0.1921402  
##  Mean   :0.5023966   Mean   :0.5000019   Mean   :0.2509183  
##  3rd Qu.:0.7523445   3rd Qu.:0.7418004   3rd Qu.:0.3815652  
##  Max.   :0.9998818   Max.   :0.9999990   Max.   :0.9851663

In order to visualize the ZC we will plot its findings on a histogram.

hist(ZC, xlab="ZC",col="green",breaks=10,probability = TRUE,ylim=c(0,3.5))

f3 <- function(x){2-2*x}
PC <- integrate(f3, lower = 0, upper = 0.5)
PC
## 0.75 with absolute error < 8.3e-15

(D) Find the probability that max{B,C} < 1/2

First, we create 10,000 random numbers from 0 to 1 and assign them to variables BD & CD respectively.

BD <- runif(10000, min = 0, max = 1)
CD <- runif(10000, min = 0, max = 1)

Next, we create a variable ZD which will host the maximum values of the variables BD and CD

ZD <- pmax(BD,CD)

To provide a visual we will create a tibble that will showcase the variables created

(ZDT <- tibble(BD,CD,ZD))
## # A tibble: 10,000 x 3
##        BD    CD    ZD
##     <dbl> <dbl> <dbl>
##  1 0.329  0.633 0.633
##  2 0.550  0.413 0.550
##  3 0.157  0.958 0.958
##  4 0.0391 0.860 0.860
##  5 0.384  0.228 0.384
##  6 0.990  0.910 0.990
##  7 0.0567 0.661 0.661
##  8 0.222  0.625 0.625
##  9 0.996  0.602 0.996
## 10 0.170  0.245 0.245
## # ... with 9,990 more rows

In order to visualize the ZD we will plot its findings on a histogram.

hist(ZD, xlab="ZD",col="red",breaks=10,probability = TRUE,ylim=c(0,2.5))

f4 <- function(x){2*x}
PD <- integrate(f4, lower = 0, upper = 0.5)
PD
## 0.25 with absolute error < 2.8e-15

(E) Find the probability that min{B,C} < 1/2

First, we create 10,000 random numbers from 0 to 1 and assign them to variables BE & CE respectively.

BE <- runif(10000, min = 0, max = 1)
CE <- runif(10000, min = 0, max = 1)

Next, we create a variable ZE which will host the maximum values of the variables BE and CE

ZE <- pmin(BE,CE)

To provide a visual we will create a tibble that will showcase the variables created

(ZET <- tibble(BE,CE,ZE))
## # A tibble: 10,000 x 3
##        BE    CE     ZE
##     <dbl> <dbl>  <dbl>
##  1 0.765  0.745 0.745 
##  2 0.883  0.851 0.851 
##  3 0.0838 0.265 0.0838
##  4 0.584  0.352 0.352 
##  5 0.978  0.625 0.625 
##  6 0.725  0.886 0.725 
##  7 0.720  0.170 0.170 
##  8 0.856  0.151 0.151 
##  9 0.985  0.648 0.648 
## 10 0.950  0.567 0.567 
## # ... with 9,990 more rows

In order to visualize the ZE we will plot its findings on a histogram.

hist(ZE, xlab="ZE",col="yellow",breaks=10,probability = TRUE,ylim=c(0,2.5))

f5 <- function(x){2-2*x}
PE <- integrate(f5, lower = 0, upper = 0.5)
PE
## 0.75 with absolute error < 8.3e-15