Skip to content

Instantly share code, notes, and snippets.

@Akiyah
Created October 26, 2013 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akiyah/7172219 to your computer and use it in GitHub Desktop.
Save Akiyah/7172219 to your computer and use it in GitHub Desktop.
分散と不偏分散を調べる
x <- rnorm(mean=0,sd=1,n=10000)
mean(x) # 平均
sd(x) # 標準偏差
var(x) # 分散(varは実は不偏分散)
sum((x - mean(x)) ** 2) / (length(x) - 1) # varはこれと一致する
vx <- sum((x - mean(x)) ** 2) / length(x) # 母分散(不偏分散で無い方の分散)。varとほとんど同じ。
n <- 10 # サンプルサイズ
vy1s <- c()
vy2s <- c()
vy1s_mean <- c()
vy2s_mean <- c()
for(i in 1:1000) {
y <- sample(x, n)
vy1 <- sum((y - mean(y)) ** 2) / (length(y) - 1) # 不偏分散
vy1s <- c(vy1s, vy1)
vy1s_mean <- c(vy1s_mean, mean(vy1s))
vy2 <- sum((y - mean(y)) ** 2) / length(y) # 分散(標本分散)
vy2s <- c(vy2s, vy2)
vy2s_mean <- c(vy2s_mean, mean(vy2s))
}
require(ggplot2)
require(reshape)
df <- data.frame(time = 1:1000,
vx = vx,
vy1 = vy1s_mean,
vy2 = vy2s_mean)
df2 <- melt(df , id = 'time', variable_name = 'series')
ggplot(df2, aes(time, value)) + geom_line(aes(colour = series))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment