Last active
December 25, 2018 09:49
-
-
Save unaoya/e11eaaeda5d5c8a6c7d5b627a36486f3 to your computer and use it in GitHub Desktop.
標本平均と標本中央値の比較
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def compare(N, n, p, rng): | |
sample = rng(N,n) | |
sample_mea = np.mean(sample, axis=1) | |
sample_med = np.sort(sample, axis=1)[:,p] | |
fig = plt.figure() | |
ax = fig.add_subplot(1,1,1) | |
ax.hist(sample_mea, bins=50, normed=True, alpha = 0.5) | |
ax.hist(sample_med, bins=50, normed=True, alpha = 0.5) | |
fig.show() | |
N = int(1e+5) | |
n = 1001 #奇数にする | |
p = n // 2 + 1 | |
rng = np.random.rand | |
#dist = np.random.random | |
#dist = np.random.exponential | |
compare(N,n,p,rng) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
library(ggplot2) | |
compare <- function(N,n,p,dist){ | |
rng <- get(dist) | |
sample.med <- c() | |
sample.mea <- c() | |
for(i in 1:N){ | |
sample <- rng(n) | |
sample.med[i] <- sort(sample)[p] | |
sample.mea[i] <- mean(sample) | |
} | |
data.frame(mean = sample.mea, median = sample.med) %>% | |
tidyr::gather() %>% | |
ggplot(aes(x=value, fill=key)) + | |
geom_histogram(position = "identity", alpha = 0.6) + | |
labs(title = paste(dist,"n =",as.character(n))) | |
} | |
N <- 1e+5 | |
n <- 1001 #奇数にする | |
p <- ceiling(n/2) | |
dist <- "rnorm" | |
compare(N,n,p,dist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment