f <- function(x){(x/4)*exp(-x^2/8)} # define the function ex # compute the expected value of X # define the function ex2 # compute the variance of X # define the function ex ex <- function(x){x*f(x)} # compute the expected value of X expected_value <- integrate(ex, 0, Inf)$value # define the function ex2 ex2 <- function(x){x^2*f(x)} # compute the variance of X variance <- integrate(ex2, 0, Inf)$value - expected_value^2 test_function_definition("ex", function_test = { test_expression_result("ex(1)") test_expression_result("ex(4)") test_expression_result("ex(10)") test_expression_result("ex(100)") }) test_function("integrate", args = c("f", "lower", "upper"), eval = c(F, T, T)) test_object("expected_value") test_function_definition("ex2", function_test = { test_expression_result("ex2(1)") test_expression_result("ex2(4)") test_expression_result("ex2(10)") test_expression_result("ex2(100)") }) test_function("integrate", args = c("f", "lower", "upper"), eval = c(F, T, T)) test_object("variance")