一
> choose(10,2)/choose(16,2)
[1] 0.375
a=c(1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0)
val = 0
cnt = 1
while (cnt <= 100000) {
x = sample(16, 2)
y = a[x]
if(y[1] + y[2] == 2) {
val = val + 1
}
cnt = cnt + 1
}
print(val / cnt)
二
x=c(0,1)
plot(x, x, type='o')
abline(v = 0)
abline(v = 1)
abline(h = 0)
abline(h = 1)
val = 0
cnt = 1
while (cnt <= 100000) {
xx = runif(1)
yy = runif(1)
if(xx^2 + yy^2 <= 1) {
val = val + 1
points(x = xx, y = yy, pch = 20, cex = 1, col = "red")
}
else
points(x = xx, y = yy, pch = 20, cex = 1, col = "blue")
cnt = cnt + 1
}
print( (val / cnt) * 4 )
三
set.seed(time(0))
rm=rpois(10000,20)
hist(rm,freq = FALSE)
lines(density(rm),col=14)
boxplot(rm) # 箱线图
mean(rm)
var(rm)
max(rm)
min(rm)
四
x=rnorm(10,2,4)
y=rnorm(10,2,4)
n1=length(x)
n2=length(y)
Sw=((n1-1)*var(x)+(n2-1)*var(y))/(n1+n2-2)
(mean(x)-mean(y)+qt(0.025,n1+n2-2,lower.tail = FALSE)*sqrt(Sw)*sqrt(1/n1+1/n2))
(mean(x)-mean(y)-qt(0.025,n1+n2-2,lower.tail = FALSE)*sqrt(Sw)*sqrt(1/n1+1/n2))
五
X = c(6.6,7.0,8.3,8.2,5.2,9.3,7.9,8.5,7.8,7.5,6.1,8.9,6.1,9.4,9.1)
Y = c(7.4,5.4,8.8,8.0,6.8,9.1,6.3,7.5,7.0,6.5,4.4,7.7,4.2,9.4,9.1)
n1 = length(X)
n2 = length(Y)
Sw = ((n1-1)*var(X)+(n2-1)*var(Y))/(n1+n2-2)
t = (mean(X)-mean(Y))/(sqrt(Sw)*sqrt(n1+n2-2))
t > qt(0.05,n1+n2-2,lower.tail = FALSE)
t.test(x,y,alternative = "greater")
X=c(6.6,7.0,8.3,8.2,5.2,9.3,7.9,8.5,7.8,7.5,6.1,8.9,6.1,9.4,9.1)
Y=c(7.4,5.4,8.8,8.0,6.8,9.1,6.3,7.5,7.0,6.5,4.4,7.7,4.2,9.4,9.1)
n=length(X)
d=rep(0,n)
for(i in 1:n) d[i]=X[i]-Y[i]
m=mean(d)
s_d=sd(d)
t=m/s_d*sqrt(n)
t>qt(0.05,n-1,lower.tail = FALSE)