The question I have been given is:
Let X be Discrete Uniform on 1, 2, . . . , n. Please note that your answers to the questions below can depend on whether n is even or odd.
(a) Use simulations in R (the statistical programming language) to numerically estimate all medians and all modes of X for n = 1, 2, . . . , 10.
And here is the R code I have written:
set.seed(1987)
rDiscUnif <- function(n,k) sample(1:k, n, replace=T) #sample randomly from disc. unif.
findModes <- function(x) {
ux <- unique(x) #extracts unique elements of a vector
matchx <- match(x, ux) #stores index of ux match for ea. x
tab <- tabulate(matchx) #counts how many times each index appears
(effectively counting the times each unique value appeared)
ux[tab == max(tab)] #returns the mode (or modes)
}
for(i in 1:10) { #for each required value of k (n in problem statement)
x <- rDiscUnif(1000, i) #make sample
print(table(x)) #make a table so you can spot the mode manually as well
plot(table(x)) #plot the table so you can see it looks uniformish
print(paste("for n = ", i, " median is ", median(x)))
z <- findModes(x)
print(paste("for n = ", i, " mode(s) is/are ", z))
}
So my question is: does this satisfy the question? In particular I'm not sure if I should write a function that will find the median based on a formula (which I found in another part of the question; med=(n+1)/2 when n is odd and $x_{n/2} \leq med \leq x_{(n/2)+1}$ when n is even), or if I should use the regular median function for the "numerical estimate".
For the median, when $n = 5.$ Here is one of several possible simple approaches:
For each of a million samples of size 100, we find the sample median
h. Then the median of the vectorh(with a million elements) estimates the population median $\eta = 3.$For $n = 6:$
Finding the mode must be some kind of bad joke. A discrete uniform distribution puts the same probability on each point in its support, so the mode is undefined.
Unless this is for an R class in which you are supposed to practice writing functions, I think it is bad form to write a function for a task that can be accomplished trivially with a 'built-in' function, such as
median.Also, there are problems for which the answer is at least moderately difficult to obtain analytically, so that simulation is actually helpful. It seems a waste to use simulation to show something that is obvious. [Example: You are given a deck of 52 cards with numbers 1, 2, ..., 52. The deck has been shuffled in random order. You turn over cards one at a time. If the number of the turn matches the number on the card, you lose. What is the probability you lose? Given that you do lose, what is the expected number of the turn on which you lose? The game is called 'frustration solitaire', because losing is so likely. You can Google it.]
Finally, such a question would be a better fit on a programming site than on a mathematics site.
I will not down-vote your Question because it is the task I find to be flawed, not especially your attempt to accomplish it.