# Generate two uniform random numbers between 0 and 1
set.seed(123) # for reproducibile results
#Using 10000 samples for each uniform random number to ensure a large sample size
x1 <- runif(10000)
x2 <- runif(10000)
# Take Y to be their sum
Y <- x1 + x2
# Plot the probability density for Y
hist(Y, breaks = 50, freq = FALSE, xlim = c(0, 2), main = "Probability Density for Y", xlab = "Y")
# Area under the density curve
density <- density(Y) # Non-parametric way for calculating desnity kernel
bin_width <- diff(density$x)[1] # Calculate the width of each bin
area_under_curve <- sum(density$y * bin_width)
print(paste("Area under the density curve:", area_under_curve))
## [1] "Area under the density curve: 1.00097771483429"
# Probability that Y is between 0.75 and 1.5
prob <- sum(density$y[density$x >= 0.75 & density$x <= 1.5]) * diff(density$x)[1]
print(paste("Probability that Y is between 0.75 and 1.5:", prob))
## [1] "Probability that Y is between 0.75 and 1.5: 0.592485169957012"
# Define the piecewise cumulative distribution function (CDF) F(y)
F <- function(y) {
ifelse(y < 1, 0.5*y^2/2,
ifelse(y <= 2.5, 0.25 + 0.5*(y - 1), 0))
}
# Generate a sequence of y values
y_values <- seq(0, 2.5, by = 0.01)
# Calculate the corresponding CDF values
cdf_values <- sapply(y_values, F)
# Plot the CDF
plot(y_values, cdf_values, type = "l",
xlab = "y (mg/ml)", ylab = "F(y)",
main = "Cumulative Distribution Function (CDF)")
# Add a legend
legend("bottomright", legend = "F(y)", lty = 1)
There is no strong correlation between points scored by Michigan vs those scored by opponents. While OLS is appropiate as the aim here is to explain the effect of Michigan’s point on others.
# Define the cumulative distribution function (CDF) F(y)
F <- function(y) {
ifelse(y < 1, 0.5*y^2/2,
ifelse(y <= 2.5, 0.25 + 0.5*(y - 1), 0))
}
# c. Calculate P(0 ≤ Y ≤ 1 mg/ml)
P_0_to_1 <- F(1)
# d. Calculate P(0.5 mg/ml ≤ Y ≤ 2 mg/ml)
P_0.5_to_2 <- F(2) - F(0.5)
# Print the results
cat("P(0 ≤ Y ≤ 1 mg/ml) =", P_0_to_1, "\n")
## P(0 ≤ Y ≤ 1 mg/ml) = 0.25
cat("P(0.5 mg/ml ≤ Y ≤ 2 mg/ml) =", P_0.5_to_2, "\n")
## P(0.5 mg/ml ≤ Y ≤ 2 mg/ml) = 0.6875