Grinstead and Snell’s Introduction to Probability

Chapter 1 Exercise 14:

Let X be a random variable with distribution function mX (x) defined by mX(−1) = 1/5, mX(0) = 1/5, mX(1) = 2/5, mX(2) = 1/5 .
(a) Let Y be the random variable defined by the equation Y = X + 3. Find the distribution function mY (y) of Y.
(b) Let Z be the random variable defined by the equation Z = X^2. Find the distribution function mZ(z) of Z.

# Given distribution function of X
# Defining the distribution function for X

mX <- function(x) {
  if (x == -1) {
    return(1/5)
  } else if (x == 0) {
    return(1/5)
  } else if (x == 1) {
    return(2/5)
  } else if (x == 2) {
    return(1/5)
  } else {
    return(0)
  }
}

# (a) Distribution function of Y = X + 3
# Defining the distribution function for Y using X
mY <- function(y) {
  return(mX(y - 3))
}

# (b) Distribution function of Z = X^2
# Defining the distribution function for Z using X
mZ <- function(z) {
  if (z < 0) {
    return(0)
  } else if (z < 1) {
    return(mX(-1) + mX(1))
  } else if (z < 4) {
    return(mX(-1) + mX(0) + mX(1) + mX(2))
  } else if (z < 9) {
    return(mX(0) + mX(2))
  } else {
    return(mX(2))
  }
}

# Example usage
# Calculating distribution function of Y for specific values
# Finding the distribution function of Y for different Y values
y_values <- c(-1, 0, 1, 2, 3, 4, 5)
mY_values <- sapply(y_values, mY)
print("Distribution function of Y:")
## [1] "Distribution function of Y:"
print(data.frame(Y = y_values, mY = mY_values))
##    Y  mY
## 1 -1 0.0
## 2  0 0.0
## 3  1 0.0
## 4  2 0.2
## 5  3 0.2
## 6  4 0.4
## 7  5 0.2
# Calculating distribution function of Z for specific values
# Finding the distribution function of Z for different Z values
z_values <- c(0, 1, 2, 3, 4)
mZ_values <- sapply(z_values, mZ)
print("Distribution function of Z:")
## [1] "Distribution function of Z:"
print(data.frame(Z = z_values, mZ = mZ_values))
##   Z  mZ
## 1 0 0.6
## 2 1 1.0
## 3 2 1.0
## 4 3 1.0
## 5 4 0.4