In this assignment we will write an R function to compute the expected value and standard deviation of an array of values. These results will then be compare to the results obtained by using the R built in functions for mean and standard deviation.
The function will then be extended to handle an “infinite number stream of values”.
Let us consider X to be a random variable taking the values in a given nx2 matrix denoted ‘x_matrix’ where the first column represent the various values of X and the 2nd column represent the corresponding probability. We will write a function that will calculate the expected value of X, denoted: Expected_Value. This function will take a nx2 matrix as an input parameter and return the corresponding expected value.
We will also calculate the standard deviation with function denoted; Standard_Deviation. Similarly, this function will take in an nx2 matrix and return the corresponding standard deviation (rounded to 4 decimal places).
Expected_Value <- function (m){
# function that input a matrix corresponding to x,p(x) pairs and return Expected Value. We expect the input parameter to be as expected.
# No validation will be done in this function.
# determine the number of rows for input matrix
l <- nrow(m)
exp_value <- 0.0000
for (i in 1:l){
exp_value <- exp_value + m[i,1]*m[i,2]
}
return(round(exp_value,4))
}
Standard_Deviation <- function (m){
# function that take a matrix corresponding to x,p(x) pairs and return Standar Deviation. We expect the input parameter to be as expected.
# We also expected that function to calculate Expected Value to be availaible
# determine the number of rows for input matrix
l <- nrow(m)
exp_value <- Expected_Value(m)
var_value <- 0.0000
for (i in 1:l){
var_value <- var_value + ((m[i,1] - exp_value)^2*m[i,2])
}
std_value <- sqrt(var_value)
return(round(std_value,4))
}
m <- matrix(c(0, 1/32, 1, 5/32, 2, 10/32, 3, 10/32, 4, 5/32, 5, 1/32), nrow =6, ncol=2, byrow = TRUE)
ev <- Expected_Value(m)
st <- Standard_Deviation(m)
ev
## [1] 2.5
st
## [1] 1.118
We will now compare these results with the built-in function in R.
r_ev <- mean(m[,1])
r_st <- sd(m[,1])
r_ev
## [1] 2.5
r_st
## [1] 1.870829
We will now test the functions with a different data set.
m2 <- matrix(c(-1, 0.30, 0, 0.40, 3, 0.20, 5, 0.10), nrow = 4, ncol = 2, byrow = TRUE)
ev2 <- Expected_Value(m2)
st2 <- Standard_Deviation(m2)
ev2
## [1] 0.8
st2
## [1] 1.99
Let us now compare the 2nd test results with the built-in R functions.
r_ev2 <- mean(m2[,1])
r_st2 <- sd(m2[,1])
r_ev2
## [1] 1.75
r_st2
## [1] 2.753785
From the 2 examples above, we are not getting the same results. Let’s us now consider the weighted average function in R.
r_evw <- weighted.mean(m[,1], m[,2])
r_evw2 <- weighted.mean(m2[,1], m2[,2])
r_evw
## [1] 2.5
r_evw2
## [1] 0.8