knitr::opts_chunk$set(echo = TRUE)

Task 1

getwd()

Task 2

mybin=function(iter=100,n=10, p=0.5){
# make a matrix to hold the samples
#initially filled with NA's
sam.mat=matrix(NA,nr=n,nc=iter, byrow=TRUE)
#Make a vector to hold the number of successes in each trial
succ=c()
for( i in 1:iter){
#Fill each column with a new sample
sam.mat[,i]=sample(c(1,0),n,replace=TRUE, prob=c(p,1-p))
#Calculate a statistic from the sample (this case it is the sum)
succ[i]=sum(sam.mat[,i])
}
#Make a table of successes
succ.tab=table(factor(succ,levels=0:n))
#Make a barplot of the proportions
barplot(succ.tab/(iter), col=rainbow(n+1), main="Binomial simulation", xlab="Number of successes")
succ.tab/iter
}

100 iterations

mybin(100,10,0.7)

200 iterations

mybin(200,10,0.7)

500 iterations

mybin(500,10,0.7)

1000 iterations

mybin(1000,10,0.7)

10000 iterations

mybin(10000,10,0.7)
dbinom(0:10,10,0.7)

Task #3

Replace = False

freq <- c(12,8)
options <- c(1,0)
bag_o_marbles <- rep(options, freq)

sample(bag_o_marbles,5,replace = F)

Replace = True

sample(bag_o_marbles,5,replace = T)

myhyper()

myhyper=function(iter=100,N=20,r=12,n=5){
# make a matrix to hold the samples
#initially filled with NA's
sam.mat=matrix(NA,nr=n,nc=iter, byrow=TRUE)
#Make a vector to hold the number of successes over the trials
succ=c()
for( i in 1:iter){
#Fill each column with a new sample
sam.mat[,i]=sample(rep(c(1,0),c(r,N-r)),n,replace=FALSE)
#Calculate a statistic from the sample (this case it is the sum)
succ[i]=sum(sam.mat[,i])
}
#Make a table of successes
succ.tab=table(factor(succ,levels=0:n))
#Make a barplot of the proportions
barplot(succ.tab/(iter), col=rainbow(n+1), main="HYPERGEOMETRIC simulation", xlab="Number of successes")
succ.tab/iter
}

100

myhyper(iter=100,n=5, N=20,r=12)

200

myhyper(iter=200,n=5, N=20,r=12)

500

myhyper(iter=500,n=5, N=20,r=12)

1000

myhyper(iter=1000,n=5, N=20,r=12)

10000

myhyper(iter=10000,n=5, N=20,r=12)
dhyper(x=0:5, m=12, n=8,5)

Task #4

What does mysample() do?

It is a function that takes 3 arguments:

It samples a vector of values 1 through 10 and displays a chart of each iteration, with a small pause.

If I want to sample a vector of values 1 through 10 choosing 6 and running 1000 iterations while pausing 1 second per iteration I would call mysample(6, 1000, 1)

Run the function. What do I see?

I'm given 30 plots which Seem to show that the distribution is approximately even.

Plot

mysample=function(n, iter=10,time=0.5){
for( i in 1:iter){
#make a sample
s=sample(1:10,n,replace=TRUE)
# turn the sample into a factor
sf=factor(s,levels=1:10)
#make a barplot
barplot(table(sf)/n,beside=TRUE,col=rainbow(10),
main=paste("Example sample()", " iteration ", i, " n= ", n,sep="") ,
ylim=c(0,0.2)
)

#release the table
Sys.sleep(time)
}
}
mysample(1000, 1, 1)

Task #5

8 choose 4

choose(8,4)

dpois(4, lambda = 2)

dpois(4, lambda = 2)

(Y=10),Y~NegBin(p=0.4,r=3)

dnbinom(10-3, size = 3, prob = 0.40)

P(Y≤8),Y~Bin(n=15,p=0.4)

pbinom(8, size = 15, prob = 0.40)

Task #6

grac0009MATH4753::mysample(5, 1, 1)


agracy2246/MATH4753grac0009 documentation built on April 26, 2020, 9:39 a.m.