作者:黄宇骐

说明:这里选取的是\(N(0,1)\)的高斯分布。

(1) 固定\(n\),画出\(\hat F_n(x)\)图并与\(F(x)\)比较

library(ggplot2)
set.seed(20241022)
xep <- rnorm(30)
Fx <- ecdf(xep)
x=seq(-3,3,length.out=1000)
df <- data.frame(
  x=x,
  yep=Fx(x),
  y=pnorm(x)
)

ggplot(df,aes(x))+
  geom_line(aes(y=y,colour="N(0,1)的累计密度曲线"))+
  geom_line(aes(y=yep,colour="累计经验分布曲线"))+
  theme_bw()

(2) 固定\(x\)\(n\to \infty\)\(\hat F_n(x) \to F(x)\)

set.seed(20241022)
x=0.5
N=1000
F_hat <- numeric(N)
Fn <- function(x,n){
  y <- ecdf(rnorm(n))
  return(y(x))
}
for (i in 1:N) {
  F_hat[i]=Fn(x,i)
}
df <- data.frame(
  x=1:N,
  y1=F_hat,
  y2=pnorm(0.5)
)
ggplot(df,aes(x))+
  geom_line(aes(y=y1,colour="F_n(x), x=0.5"))+
  geom_line(aes(y=y2,colour="F(x), x=0.5"))+
  theme_bw()

(3) 固定\(x\),重复多次实验,验证\([\hat F_n(x) - F(x)]\)的正态性

set.seed(20241022)
x=0.9
N=10000
F_hat <- numeric(N)
for (i in 1:N) {
  y <- ecdf(rnorm(100))
  F_hat[i]=y(x)
}
Fx=pnorm(x)
ggplot() +
  geom_histogram(aes(F_hat-Fx),binwidth=0.01,color="black",fill="#84CAC0")+
  theme_bw()

(4) 对给定分布,随机生成若干个点,使用公式计算窗宽,并以该窗宽绘制频率直方图

给定分布:Exp(3)

样本大小:3000

n=3000
lambda=2
h=1/n^(1/3)*(6/(lambda/2))^(1/3)
h
## [1] 0.1259921
ExpSample <- rexp(n,lambda)
ggplot() +
  geom_histogram(aes(ExpSample),binwidth=h,color="black",fill="#F5AE6B")+
  theme_bw()