Prove its a proper pdf
to prove integration equals 1 add integral over 0-1 function and 1-2 function
a <- as.numeric(integrate(function(x){x},0,1)[1])
b <- as.numeric(integrate(function(x){2-x},1,2)[1])
a+b
## [1] 1
sim_b_plus_c <- function(){
b <- runif(1)
c <- runif(1)
if (b+c<.5)
return (1)
else
return(0)
}
b_plus_c <-mean(replicate(1000000,sim_b_plus_c()))
paste("b+c< .5 ",b_plus_c*100, " % of the time")
## [1] "b+c< .5 12.4082 % of the time"
\[ .5*z^2... if 1>z>0 \] \[ .5^3= .125 \]
hist_btimes_c <- replicate(100000,(runif(1)*runif(1)))
hist(hist_btimes_c,probability = TRUE)
P = ecdf(hist_btimes_c)
plot(P)
P(.5)
## [1] 0.84691
sim_b_times_c <- function(){
b <- runif(1)
c <- runif(1)
if (b*c<.5)
return (1)
else
return(0)
}
b_times_c <-mean(replicate(1000000,sim_b_times_c()))
paste("b*c< .5 ",b_times_c*100, " % of the time")
## [1] "b*c< .5 84.6854 % of the time"
abs_bminus_c <- replicate(100000,(abs(runif(1)-runif(1))))
hist(abs_bminus_c,probability = TRUE)
P = ecdf(abs_bminus_c)
plot(P)
##extract CDf value
P(.5)
## [1] 0.75092
sim_abs_b_minus_c <- function(){
b <- runif(1)
c <- runif(1)
if (abs(b-c)<.5)
return (1)
else
return(0)
}
abs_b_minus_c <-mean(replicate(1000000,sim_abs_b_minus_c()))
paste("|b-c|< .5 ",abs_b_minus_c*100, " % of the time")
## [1] "|b-c|< .5 74.9758 % of the time"
max_b_c <- replicate(100000,(max(runif(1),runif(1))))
hist(max_b_c,probability = TRUE)
P = ecdf(max_b_c)
plot(P)
##extract CDf value
P(.5)
## [1] 0.24785
sim_max_b_c <- function(){
b <- runif(1)
c <- runif(1)
if (max(b,c)<.5)
return (1)
else
return(0)
}
max_b_c <-mean(replicate(1000000,sim_max_b_c()))
paste("max(b,c) < .5 ",max_b_c*100, " % of the time")
## [1] "max(b,c) < .5 24.9756 % of the time"
min_b_c <- replicate(100000,(min(runif(1),runif(1))))
hist(min_b_c,probability = TRUE)
P = ecdf(min_b_c)
##extract CDf value
P(.5)
## [1] 0.75055
sim_min_b_c <- function(){
b <- runif(1)
c <- runif(1)
if (min(b,c)<.5)
return (1)
else
return(0)
}
min_b_c <-mean(replicate(1000000,sim_min_b_c()))
paste("min(b,c) < .5 ",min_b_c*100, " % of the time")
## [1] "min(b,c) < .5 74.9267 % of the time"