R Markdown

3.39 Grade distributions.

Each row in the table below is a proposed grade distribution for a class. Identify each as a valid or invalid probability distribution, and explain your reasoning

    1. 0.3 0.3 0.3 0.2 0.1
    1. 0 0 1 0 0
    1. 0.3 0.3 0.3 0 0
    1. 0.3 0.5 0.2 0.1 -0.1
    1. 0.2 0.4 0.2 0.1 0.1
    1. 0 -0.1 1.1 0 0
is_valid <- function(vec) { 
  result <- "Valid"
  if (sum(vec) != 1) {
    result <- "Invalid, Sum needs to total 1"
  }
  if (any(vec < 0 ) ) { 
    result <- "Invalid, probability cannot be negative"
  }
  if (any(vec > 1 )) { 
    result <- "Invalid, probabilities must be between 0 and 1"
  }
  
  return(result)
} 
cat("Vector a is: ", is_valid(c(0.3, 0.3, 0.3, 0.2, 0.1)), "\n")
## Vector a is:  Invalid, Sum needs to total 1
cat("Vector b is: ", is_valid(c(0, 0, 1, 0, 0)), "\n")
## Vector b is:  Valid
cat("Vector c is: ", is_valid(c(0.3, 0.3, 0.3, 0, 0)), "\n")
## Vector c is:  Invalid, Sum needs to total 1
cat("Vector d is: ", is_valid(c(0.3, 0.5, 0.2, 0.1, -0.1)), "\n")
## Vector d is:  Invalid, probability cannot be negative
cat("Vector e is: ", is_valid(c(0.2, 0.4, 0.2, 0.1, 0.1)), "\n")
## Vector e is:  Valid
cat("Vector f is: ", is_valid(c(0,-0.1,1.1,0,0)), "\n")
## Vector f is:  Invalid, probabilities must be between 0 and 1