Skip to main content
added 1173 characters in body
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512

You can also approach this problem differently if you notice that under uniform prior $p$ follows $\mathrm{Beta}(n/2+1, n/2+1)$ distribution (Pires and Amado, 2008, see also here). Knowing this, you can find such $n$ that maximizes probability of $p$ being within $\pm \varepsilon$ of $0.5$, i.e. that $ \Pr(X \le m) \geq p-\varepsilon $ and $ \Pr(X \le m) \leq p+\varepsilon $.

betapr <- function(n, eps) diff(pbeta(c(0.5-eps, 0.5+eps), n/2+1, n/2+1))
betapr <- Vectorize(betapr, "n")

That for $\varepsilon = 0.05$ returns

> betapr(n, eps = 0.05)
 [1] 0.1271114 0.2020283 0.2662452 0.3162281 0.3942332 0.5281417 0.6875108 0.8881346 0.9752351 0.9984892

So with small sample probability that $p$ is anywhere close to $0.5$ is quite small. Beta distribution can be also used for obtaining exact confidence intervals.

Brown, L. D., Cai, T. T., & DasGupta, A. (2001). Interval estimation for a binomial proportion. Statistical science, 101-117.

Pires, A. M., & Amado, C. (2008). Interval estimators for a binomial proportion: Comparison of twenty methods. REVSTAT‒Statistical Journal, 6(2), 165-197.

Brown, L. D., Cai, T. T., & DasGupta, A. (2001). Interval estimation for a binomial proportion. Statistical science, 101-117.

You can also approach this problem differently if you notice that under uniform prior $p$ follows $\mathrm{Beta}(n/2+1, n/2+1)$ distribution (Pires and Amado, 2008, see also here). Knowing this, you can find such $n$ that maximizes probability of $p$ being within $\pm \varepsilon$ of $0.5$, i.e. that $ \Pr(X \le m) \geq p-\varepsilon $ and $ \Pr(X \le m) \leq p+\varepsilon $.

betapr <- function(n, eps) diff(pbeta(c(0.5-eps, 0.5+eps), n/2+1, n/2+1))
betapr <- Vectorize(betapr, "n")

That for $\varepsilon = 0.05$ returns

> betapr(n, eps = 0.05)
 [1] 0.1271114 0.2020283 0.2662452 0.3162281 0.3942332 0.5281417 0.6875108 0.8881346 0.9752351 0.9984892

So with small sample probability that $p$ is anywhere close to $0.5$ is quite small. Beta distribution can be also used for obtaining exact confidence intervals.

Brown, L. D., Cai, T. T., & DasGupta, A. (2001). Interval estimation for a binomial proportion. Statistical science, 101-117.

Pires, A. M., & Amado, C. (2008). Interval estimators for a binomial proportion: Comparison of twenty methods. REVSTAT‒Statistical Journal, 6(2), 165-197.

added 196 characters in body
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512

Another thing that you can do is to findRegarding your question about sample size that enables you to estimatefor estimating median with enough precision, it can be easily calculated. If you think of this problem the other way around, we want $np$ out of $n$ values of $X$ be smaller or equal than $m$. If we define new random variable in terms of counts $Y = \sum_{i=1}^n [x_i \le m ]$, then it follows binomial distribution with parameters $n$ and $p$. Wald confidence interval for $p$ can be easily calculated as

set.seed(123)
 
R <- 1e3
N <- 1e61e5
n <- c(1, 5, 10, 15, 25, 50, 100, 250, 500, 1000)
 
res <- matrix(NA, R, length(n))

for (i in 1:R) {
  for (j in seq_along(n)) {
    m <- median(rnorm(n[j])) # sample median
    res[i, j] <- pnorm(m)    # population Pr(X <= m)
  }
}

wald <- function(n, p=0.5) matrix(c(-1, 1), ncol = 1) %*% (1.96 * sqrt(p*(1-p)/n)) + p
> apply(res, 2, quantile, cwald(0.025, 0.975)n)
       [,1]   [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
2.5% [1,] -0.02248 0.1350617 0.23319 0.264247 0.308304 0.368361 0.406402 0.436438 0.453 456 0.47469
97.5%[2,] 0 1.98048 0.8619383 0.77181 0.728753 0.682696 0.626639 0.594598 0.561562 0.540 544 0.53531
> waldapply(nres, 2, quantile, c(0.025, 0.975))
        [,1]   [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
[1,]2.5% - 0.480258 0.0617146 0.19233 0.247266 0.304313 0.361366 0.402403 0.438 0.456 0.469
[2,] 97.5% 10.489745 0.9383853 0.81769 0.753733 0.696687 0.639635 0.598596 0.562 0.544 0.531

As you can see, if $n=1$ then sample median could be possibly any value of $X$, as sample grows, sample median approaches population median, where it is for you to decide if it is precise enough.

Another thing that you can do is to find sample size that enables you to estimate median with enough precision. If you think of this problem the other way around, we want $np$ out of $n$ values of $X$ be smaller or equal than $m$. If we define new random variable in terms of counts $Y = \sum_{i=1}^n [x_i \le m ]$, then it follows binomial distribution with parameters $n$ and $p$. Wald confidence interval for $p$ can be easily calculated as

set.seed(123)
 
R <- 1e3
N <- 1e6
n <- c(1, 5, 10, 15, 25, 50, 100, 250, 500, 1000)
 
res <- matrix(NA, R, length(n))

for (i in 1:R) {
  for (j in seq_along(n)) {
    m <- median(rnorm(n[j]))
    res[i, j] <- pnorm(m)
  }
}

wald <- function(n, p=0.5) matrix(c(-1, 1), ncol = 1) %*% (1.96 * sqrt(p*(1-p)/n)) + p
> apply(res, 2, quantile, c(0.025, 0.975))
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
2.5%  0.022 0.135 0.233 0.264 0.308 0.368 0.406 0.436 0.453  0.47
97.5% 0.980 0.861 0.771 0.728 0.682 0.626 0.594 0.561 0.540  0.53
> wald(n)
      [,1]   [,2] [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
[1,] -0.48 0.0617 0.19 0.247 0.304 0.361 0.402 0.438 0.456 0.469
[2,]  1.48 0.9383 0.81 0.753 0.696 0.639 0.598 0.562 0.544 0.531

Regarding your question about sample size for estimating median, it can be easily calculated. If you think of this problem the other way around, we want $np$ out of $n$ values of $X$ be smaller or equal than $m$. If we define new random variable in terms of counts $Y = \sum_{i=1}^n [x_i \le m ]$, then it follows binomial distribution with parameters $n$ and $p$. Wald confidence interval for $p$ can be easily calculated as

set.seed(123)
R <- 1e5
n <- c(1, 5, 10, 15, 25, 50, 100, 250, 500, 1000)
res <- matrix(NA, R, length(n))

for (i in 1:R) {
  for (j in seq_along(n)) {
    m <- median(rnorm(n[j])) # sample median
    res[i, j] <- pnorm(m)    # population Pr(X <= m)
  }
}

wald <- function(n, p=0.5) matrix(c(-1, 1), ncol = 1) %*% (1.96 * sqrt(p*(1-p)/n)) + p
> wald(n)
      [,1]   [,2] [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
[1,] -0.48 0.0617 0.19 0.247 0.304 0.361 0.402 0.438 0.456 0.469
[2,]  1.48 0.9383 0.81 0.753 0.696 0.639 0.598 0.562 0.544 0.531
> apply(res, 2, quantile, c(0.025, 0.975))
        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
2.5%  0.0258 0.146 0.233 0.266 0.313 0.366 0.403 0.438 0.456 0.469
97.5% 0.9745 0.853 0.769 0.733 0.687 0.635 0.596 0.562 0.544 0.531

As you can see, if $n=1$ then sample median could be possibly any value of $X$, as sample grows, sample median approaches population median, where it is for you to decide if it is precise enough.

deleted 6 characters in body
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512
set.seed(123)

R <- 1e3
N <- 1e6
n <- c(1, 5, 10, 15, 25, 50, 100, 250, 500, 1000)
k <- length(n)
res <- matrix(NA, R, n[k]length(n))

# WARNING: very slow
for (i in 1:R) {
  print(i/R)
  X <- rnorm(N)
  x_small <- sample(X, n[k])
  for (j in 1:kseq_along(n)) {
    m <- median(x_small[1:n[j]]rnorm(n[j]))
    res[i, j] <- sumpnorm(X <= m)
  }
}

res <- res/N

wald <- function(n, p=0.5) matrix(c(-1, 1), ncol = 1) %*% (1.96 * sqrt(p*(1-p)/n)) + p
> waldapply(nres, 2, quantile, c(0.025, 0.975))
       [,1]   [,2]  [,3]  [,4]  [,5]  [,6]  [,7]
[1  [,]8] - [,9] [,10]
2.5%  0.48022 0.0617135 0.19233 0.361264 0.402308 0.438368 0.469
[2,]406 0.436 10.48453  0.938347
97.5% 0.81980 0.639861 0.598771 0.562728 0.531
>682 apply(res,0.626 2,0.594 quantile,0.561 c(0.025,540  0.975))53
 > wald(n)
      [,1]   [,2] [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
2[1,] -0.5%48 0.0617 0.023419 0.152247 0.25304 0.366361 0.403402 0.437438 0.467456 0.469
97[2,]  1.5%48 0.98179383 0.83181 0.76753 0.631696 0.597639 0.564598 0.529562 0.544 0.531
set.seed(123)

R <- 1e3
N <- 1e6
n <- c(1, 5, 10, 50, 100, 250, 1000)
k <- length(n)
res <- matrix(NA, R, n[k])

# WARNING: very slow
for (i in 1:R) {
  print(i/R)
  X <- rnorm(N)
  x_small <- sample(X, n[k])
  for (j in 1:k) {
    m <- median(x_small[1:n[j]])
    res[i, j] <- sum(X <= m)
  }
}

res <- res/N

wald <- function(n, p=0.5) matrix(c(-1, 1), ncol = 1) %*% (1.96 * sqrt(p*(1-p)/n)) + p
> wald(n)
      [,1]   [,2] [,3]  [,4]  [,5]  [,6]  [,7]
[1,] -0.48 0.0617 0.19 0.361 0.402 0.438 0.469
[2,]  1.48 0.9383 0.81 0.639 0.598 0.562 0.531
> apply(res, 2, quantile, c(0.025, 0.975))
        [,1]  [,2] [,3]  [,4]  [,5]  [,6]  [,7]
2.5%  0.0234 0.152 0.25 0.366 0.403 0.437 0.467
97.5% 0.9817 0.831 0.76 0.631 0.597 0.564 0.529
set.seed(123)

R <- 1e3
N <- 1e6
n <- c(1, 5, 10, 15, 25, 50, 100, 250, 500, 1000)

res <- matrix(NA, R, length(n))

for (i in 1:R) {
  for (j in seq_along(n)) {
    m <- median(rnorm(n[j]))
    res[i, j] <- pnorm(m)
  }
}

wald <- function(n, p=0.5) matrix(c(-1, 1), ncol = 1) %*% (1.96 * sqrt(p*(1-p)/n)) + p
> apply(res, 2, quantile, c(0.025, 0.975))
       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
2.5%  0.022 0.135 0.233 0.264 0.308 0.368 0.406 0.436 0.453  0.47
97.5% 0.980 0.861 0.771 0.728 0.682 0.626 0.594 0.561 0.540  0.53
> wald(n)
      [,1]   [,2] [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
[1,] -0.48 0.0617 0.19 0.247 0.304 0.361 0.402 0.438 0.456 0.469
[2,]  1.48 0.9383 0.81 0.753 0.696 0.639 0.598 0.562 0.544 0.531
added 1185 characters in body
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512
Loading
added 50 characters in body
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512
Loading
added 233 characters in body
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512
Loading
edited body
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512
Loading
Source Link
Tim
  • 141.2k
  • 26
  • 270
  • 512
Loading