1、
tim = 0
for(i in 1:10000){
a = sample(c(1,0),1000, replace=TRUE , prob = c( 0.6 , 0.4 ) )
if(sum(a) <= 600) tim <- tim + 1
}
print(tim/10000)
2、
num = 0
for(i in 1 : 10000){
a= 4
b= 8
one = sample(c(0,1),1,replace=FALSE,prob=c((a*15)/(a*15+b*10),(b*10)/(a*15+b*10)))
if(sum(one) == 0) a<- a- 1
else b<- b- 1
two = sample(c(0,1),1,replace=FALSE,prob=c((a*15)/(a*15+b*10),(b*10)/(a*15+b*10)))
if(sum(one)+sum(two) == 2) num <- num + 1
}
print(num/10000)
3、
set.seed(time(0))
rm=rexp(10000,0.2)
hist(rm,freq = FALSE)
lines(x=rm,y=dexp(rm,0.2),col=12)
lines(density(rm),col=14)
boxplot(rm)
mean(rm)
var(rm)
max(rm)
min(rm)
4、
sigmax_sigmay <- function(alpha,x,y,mux=NA,muy=NA){
if(is.na(mux)|is.na(muy)){
meanx <- mean(x)
m <- length(x)
meany <- mean(y)
n <- length(y)
F1 <- qf(1-alpha/2,m-1,n-1)
F2 <- qf(alpha/2,m-1,n-1)
sigma11 <- 1/F1*sum((x-meanx)^2)*(n-1)/sum((y-meany)^2)/(m-1)
sigma12 <- 1/F2*sum((x-meanx)^2)*(n-1)/sum((y-meany)^2)/(m-1)
}
else{
m <- length(x)
n <- length(y)
F1 <- qf(1-alpha/2,m,n)
F2 <- qf(alpha/2,m,n)
sigma11 <- 1/F1*sum((x-mux)^2)*n/sum((y-muy)^2)/m
sigma12 <- 1/F2*sum((x-mux)^2)*n/sum((y-muy)^2)/m
}
string1 <- paste('以1-',alpha,'为置信水平的sigmax/sigmay双侧置信区间为:[',sigma11,', ',sigma12,']。',sep='')
return(string1)
}
x<-rnorm(100)
#生成随机数
y<-rnorm(100)
print(sigmax_sigmay(0.05,x,y))
5、
X<-c(6,4,5,5,6,5,5,6,4,6,7,4)
Y<-c(2,1,2,2,1,0,3,2,1,0,1,3)
n1 = length(X)
n2 = length(Y)
Sw = ((n1-1)*var(X)+(n2-1)*var(Y))/(n1+n2-2)
t = (mean(X)-mean(Y)-2)/(sqrt(Sw)*sqrt(1/n1+1/n2))
t > qt(0.05,n1+n2-2,lower.tail = FALSE)
t.test(X,Y,alternative = "greater",mu=2)