Data 605 week 5 assignment

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

# generate 200 random numbers from uniform distribution  
set.seed(NULL)
B <- runif(200, min = 0, max = 1)  

set.seed(NULL)
C <- runif(200, min = 0, max = 1)   

DF_B<- as.data.frame(B)

# Create a df with all false. This is calculate and store the probabily of all values in B
DF_P_B <- rep(F,times=length(B))  

# then calculate the pribability of B
sum_p <- 0
for (i in 1:length(B)){
  occured=1
  for (j in 1:length(B)){
    if (j != i && B[i] == B[j]){
      occured <- occured + 1
    }
  }
  p <- occured/length(B)
  sum_p <- sum_p + p
  # if the probability is between and 1 inclusive, the value in sum_p will change to True correspondingly 
  DF_P_B[i] <- (p >= 0 && p<= 1) 
}

# Ideally the probabily should be between 0 and 1. Lets if any of the probabily has False.

isAnyFalse <- any(DF_P_B == F)
isAnyFalse
## [1] FALSE
paste("The sum of all probabilities: " ,sum_p)
## [1] "The sum of all probabilities:  1"

So this proves that none of the probabily is below 0 and above 1. And also the sum of all probabilities is 1.

Question2:

Find the probability that

  1. B + C < 1/2
P1 <- sum((B+C) < 1/2)/length(B)

paste("The probability for B + C < 1/2. is: ",P1)
## [1] "The probability for B + C < 1/2. is:  0.1"
  1. BC < 1/2
P1 <- sum(B*C < 1/2)/length(B)

paste("The probability for BC < 1/2. is: ",P1)
## [1] "The probability for BC < 1/2. is:  0.87"
  1. |B - C| < 1/2.
P1 <- sum(abs((B-C)) < 1/2)/length(B)

paste("The probability for |B − C| < 1/2. is: ",P1)
## [1] "The probability for |B - C| < 1/2. is:  0.765"
  1. max{B,C} < 1/2.
min_no = 0
max_no = 0
for (i in 1:length(B)){
  if (max(B[i],C[i]) < 1/2){
    max_no=max_no+1
  }
  if (min(B[i],C[i]) < 1/2){
    min_no=min_no+1
  }
}

P1 <- max_no/length(B)

paste("The probability for max{B,C} < 1/2. is: ",P1)
## [1] "The probability for max{B,C} < 1/2. is:  0.215"
  1. min{B,C} < 1/2.
P1 <- min_no/length(B)
paste("The probability for min{B,C} < 1/2. is: ",P1)
## [1] "The probability for min{B,C} < 1/2. is:  0.77"