# Derive the expectation and variance of a log Winsorized F distribution by
# simulation.
random_logwinf <- function(n, df1, df2, p_low, p_up) {
x <- rf(n, df1, df2)
q_low <- qf(p_low, df1, df2, lower.tail = TRUE)
q_up <- qf(p_up, df1, df2, lower.tail = FALSE)
x[x < q_low] <- q_low
x[x > q_up] <- q_up
x <- log(x)
c(mean(x), var(x))
}
# Set parameters.
n <- 10000
df1 <- 2
df2 <- 2 ^ (0:10)
p_low <- 0.01
p_up <- 0.1
# Compare simulation results with those from numerical integration.
set.seed(100)
res1 <- vapply(df2, function(x) random_logwinf(n, df1, x, p_low, p_up),
numeric(2))
res2 <- mean_var_logwinf(df1, df2, p_low, p_up)
# Compare mean.
plot(0:10, res1[1, ], type = "l", lwd = 2, col = "red", xlab = "Log2(df2)",
ylab = "Mean")
lines(0:10, res2$mu, lty = 5, lwd = 2, col = "blue")
legend("topright", c("Simulation", "Numerical integration"), lty = c(1, 5),
lwd = 2, col = c("red", "blue"))
# Compare variance.
plot(0:10, res1[2, ], type = "l", lwd = 2, col = "red", xlab = "Log2(df2)",
ylab = "Var")
lines(0:10, res2$v, lty = 5, lwd = 2, col = "blue")
legend("topright", c("Simulation", "Numerical integration"), lty = c(1, 5),
lwd = 2, col = c("red", "blue"))
# When df2 is Inf.
random_logwinf(n, df1, Inf, p_low, p_up)
mean_var_logwinf(df1, Inf, p_low, p_up)
Run the code above in your browser using DataLab