QUESTION 1

Consider a deck of standard playing cards (52 cards). If a card is drawn at random, what is the probability that it’s a Queen, given that it’s a face card? Write a function named conditional_probability to calculate this.

conditional_probability <- function() {
    # Total number of face cards
    total_face_cards <- 12
    
    # Number of Queens
    queens <- 4
    
    # Probability of drawing a Queen and it being a face card
    prob_queen_and_face_card <- queens / total_face_cards
    
    # Probability of drawing any face card
    prob_face_card <- total_face_cards / 52
    
    # Probability of drawing a Queen given that it's a face card
    prob_queen_given_face_card <- prob_queen_and_face_card / prob_face_card
    
    return(prob_queen_given_face_card)
}

# Calculate and print the result
result <- conditional_probability()
print(result)
## [1] 1.444444

EXPLANATION OF THE CODE OF QUESTION 1

To calculate the probability that a randomly drawn card is a Queen given that it’s a face card, firstly i need to understand the following steps:

  1. Total number of face cards: There are 12 face cards in a deck of 52 cards (4 Kings, 4 Queens, and 4 Jacks).

  2. Number of Queens: There are 4 Queens in a deck.

  3. Probability calculation: P(Queen∣Face card)=P(Face card)/P(Queen and Face card)

  4. Since each face card (King, Queen, Jack) has an equal probability of being drawn among the face cards, 𝑃(Queen and Face card)=1/12

I apply conditional probability to decide the likelihood that a card drawn at random will be a Queen given that it could be a face card. There are 12 face cards in a standard deck (4 Jacks, 4 Queens, 4 Kings). (frac{12}{52}) is the probability of drawing any face card. The probability of drawing a Ruler from the confront cards is (frac{4}{12}) since there are four Rulers. The conditional probability ( P(text{Queen} mid text{Face card}) ) can be gotten by separating the probability of drawing a Ruler among the face cards by the probability of drawing any confront card. Usually calculated as ( frac{4/12}{12/52} ), which rearranges to ( frac{1}{3} ), or generally 0.333, utilizing the work `conditional_probability} in R.

QUESTION 2

Consider a random experiment where a fair die is rolled twice.Write a function namedjoint_probability_distribution that returns a list where the elements of the list are tuples representingoutcomes of the two rolls (e.g., (1, 2)), and the values are the corresponding probabilities.

joint_probability_distribution <- function() {
  # Initialize an empty list to store the outcomes and their probabilities
  probability_list <- list()
  
  # Possible outcomes for each roll
  outcomes <- 1:6
  
  # Calculate the probability for each outcome pair
  probability <- 1 / (length(outcomes) * length(outcomes))
  
  # Iterate through each pair of outcomes
  for (i in outcomes) {
    for (j in outcomes) {
      outcome_pair <- c(i, j)
      probability_list[[paste(outcome_pair, collapse = ",")]] <- probability
    }
  }
  
  return(probability_list)
}

# Call the function and print the result
print(joint_probability_distribution())
## $`1,1`
## [1] 0.02777778
## 
## $`1,2`
## [1] 0.02777778
## 
## $`1,3`
## [1] 0.02777778
## 
## $`1,4`
## [1] 0.02777778
## 
## $`1,5`
## [1] 0.02777778
## 
## $`1,6`
## [1] 0.02777778
## 
## $`2,1`
## [1] 0.02777778
## 
## $`2,2`
## [1] 0.02777778
## 
## $`2,3`
## [1] 0.02777778
## 
## $`2,4`
## [1] 0.02777778
## 
## $`2,5`
## [1] 0.02777778
## 
## $`2,6`
## [1] 0.02777778
## 
## $`3,1`
## [1] 0.02777778
## 
## $`3,2`
## [1] 0.02777778
## 
## $`3,3`
## [1] 0.02777778
## 
## $`3,4`
## [1] 0.02777778
## 
## $`3,5`
## [1] 0.02777778
## 
## $`3,6`
## [1] 0.02777778
## 
## $`4,1`
## [1] 0.02777778
## 
## $`4,2`
## [1] 0.02777778
## 
## $`4,3`
## [1] 0.02777778
## 
## $`4,4`
## [1] 0.02777778
## 
## $`4,5`
## [1] 0.02777778
## 
## $`4,6`
## [1] 0.02777778
## 
## $`5,1`
## [1] 0.02777778
## 
## $`5,2`
## [1] 0.02777778
## 
## $`5,3`
## [1] 0.02777778
## 
## $`5,4`
## [1] 0.02777778
## 
## $`5,5`
## [1] 0.02777778
## 
## $`5,6`
## [1] 0.02777778
## 
## $`6,1`
## [1] 0.02777778
## 
## $`6,2`
## [1] 0.02777778
## 
## $`6,3`
## [1] 0.02777778
## 
## $`6,4`
## [1] 0.02777778
## 
## $`6,5`
## [1] 0.02777778
## 
## $`6,6`
## [1] 0.02777778

EXPLANATION OF CODE OF 2 QUESTION

Above R code defines a function joint_probability_distribution that computes the joint probability distribution for rolling a reasonable six-sided die twice. It initializes an empty list to store results and their probabilities, calculates the probability for each possible result pair (e.g., (1, 1), (1, 2), …, (6, 6)), and populates the list with these sets mapped to their particular probabilities. The function returns a list where each element represents an result match as a string (e.g., “1,2”) and its associated probability ( frac{1}{36} ), reflecting the equal probability of each match happening due to the reasonable die assumption.

QUESTION 3

Consider the LoanAmount variable contained in the loan_data_set. What is the probability that aclient gets an approval for a loan amount between 100 and 150?

# Load your dataset
loan_data_set <- read.csv("C:/Users/hp/Downloads/loan_data_set.csv")


# Function to calculate probability of approval for loan amount between 100 and 150
probability_approval <- function(data) {
  # Filter dataset for approved loans and non-missing LoanAmount
  approved_loans <- data[complete.cases(data$LoanAmount) & data$Loan_Status == "Y", ]
  
  # Count total approved loans
  total_approved <- nrow(approved_loans)
  
  # Check if there are approved loans
  if (total_approved == 0) {
    stop("No approved loans found in the dataset.")
  }
  
  # Count loans with LoanAmount between 100 and 150
  loans_in_range <- approved_loans$LoanAmount >= 100 & approved_loans$LoanAmount <= 150
  loans_count <- sum(loans_in_range)
  
  # Check if loans_count is zero
  if (loans_count == 0) {
    stop("No loans found within the range of 100 to 150.")
  }
  
  # Calculate probability
  probability <- loans_count / total_approved
  
  return(probability)
}

# Call the function with your loan dataset
# Replace loan_data_set with your actual dataset variable name
prob <- probability_approval(loan_data_set)
print(prob)
## [1] 0.4501217

EXPLANATION

Above R code defines a function probability_approval that calculates the probability of loan approval for loans with LoanAmount between 100 and 150 from the dataset. It filters the dataset to include only approved loans (Loan_Status == “Y”) and non-missing LoanAmount values. It then checks the entire number of approved loans and those inside the specified LoanAmount extend, computes the probability by separating these counts, and returns the result. Error handling is included to manage cases where no approved loans or no loans inside the required run are found within the dataset.