question one: a.PDF and CDF
trials=0:24
n=24
p=.9
data=dbinom(trials,n,p)
#PDF
plot(data,type="h",xlab="Number of Successes",ylab="P(x)",main="PDF of Data")
#CDF
plot(pbinom(trials,size=24,.9),type="s",xlab="Number of Successes",ylab="F(x)", main="CDF of Data")
question one: b. Mean and standard
theMean=24*.9
cat("Mean of correctly received bits: ", theMean," ")
## Mean of correctly received bits: 21.6
stDEV=sqrt(n*p*(1-p))
cat("and the standard deviation of bits is: ", stDEV)
## and the standard deviation of bits is: 1.469694
question one: c. probability of errors
data=pbinom(3,24,.1)
dataProb=1-data
cat("The probability of more than 3 bit errors: ", dataProb)
## The probability of more than 3 bit errors: 0.2142622
question one: d. median distribution and median value
MedianOfData=qbinom(.5,size=n,prob=p)
cat("The median is: ",MedianOfData," ")
## The median is: 22
qurstion one: e. quartile distribution
quantile=qbinom(.6,24,.9)
quantile
## [1] 22
question two: a.probability of calls
pois20=dpois(20,20)
pois30=dpois(30,20)
cat("Probability of 20 calls:", pois20)
## Probability of 20 calls: 0.08883532
cat(" and the Probability of 30 calls:", pois30)
## and the Probability of 30 calls: 0.008343536
question two: b. 10 operators
split240=240/10
poisSplit=dpois(split240,20)
cat("Probability of 10 operators processing all calls:", poisSplit)
## Probability of 10 operators processing all calls: 0.05573456
question two: c. call rate
less100=ppois(100,85)
less100C=1-less100
cat("the probability of more than 100 calls is:", less100C)
## the probability of more than 100 calls is: 0.04934533
question three: a. period of both the generator
periodA=1
Z0ForA=5
zValueA=c(Z0ForA)
uValueA=c(Z0ForA/16)
while(TRUE){
zNextA=(9*zValueA[length(zValueA)]+1)%%16
if(zNextA %in% zValueA)
break
zValueA=append(zValueA, zNextA)
uValueA=append(uValueA,zNextA/16)
periodA=periodA+1
}
cat("The period for A is:",periodA," ")
## The period for A is: 16
periodB=1
Z0ForB=10
zValueB=c(Z0ForB)
uValueB=c(Z0ForB/32)
while(TRUE){
zNextB=(7*zValueB[length(zValueB)]+3)%%32
if(zNextB %in% zValueB)
break
zValueB=append(zValueB, zNextB)
uValueB=append(uValueB,zNextB/32)
periodB=periodB+1
}
cat("The period for B is:",periodB," ")
## The period for B is: 8
question three: b. parameters
print("they all affect the parameters because a and b affect the generated numbers")
## [1] "they all affect the parameters because a and b affect the generated numbers"
question three: c. scatter plot diagram
plot(zValueA[-length(zValueA)],zValueA[-1], main="Generator of A",pch=19)
plot(zValueB[-length(zValueB)],zValueB[-1], main="Generator B")
print("there is a noticable pattern when looking at A while B looks random.")
## [1] "there is a noticable pattern when looking at A while B looks random."
question three: d. randomness
R=runif(n=100,min=0,max=1)
plot(R,pch=20,main="Runif Numbers")
question three: e. mean value
meanA=mean(zValueA/16)
meanB=mean(zValueB/32)
cat("the mean of A is:", meanA)
## the mean of A is: 0.46875
cat(" and the mean of B is:", meanB)
## and the mean of B is: 0.421875
question three: f. histogram
hist(uValueA, breaks=20, main="Ui Vals for Generator A")
hist(uValueB,breaks=20,main="Ui Vals for Generator B")
print("the bars are not evenly distributed and therefore are not uniform")
## [1] "the bars are not evenly distributed and therefore are not uniform"
question four: a. develop algorithm
print("im unsure of how to do this one.")
## [1] "im unsure of how to do this one."
question four: b. random variables
sym=1
k=5
One=sym*(((-sym)^k)*log(-(0.1-1)))^(1/k)
cat("First value:", One," ")
## First value: 0.6375813
Two=sym*(((-sym)^k)*log(-(0.2-1)))^(1/k)
cat("Second value:", Two," ")
## Second value: 0.7408271
Three=sym*(((-sym)^k)*log(-(0.3-1)))^(1/k)
cat("Third value:", Three," ")
## Third value: 0.8136816
question four: c. histogram
A=runif(10000,min=0,max=1)
sym=1
k=5
x=sym*(((-sym)^k)*log(-(A-1)))^(1/k)
#x
hist(x,breaks=20,main="Histogram")
cat("the homework says to compare to the plot generated from e4, im assuming it's meant to say f3, but this histogram is slightly left skewed/leaning while the others were more aligned")
## the homework says to compare to the plot generated from e4, im assuming it's meant to say f3, but this histogram is slightly left skewed/leaning while the others were more aligned
question five: Monte Carlo
n=10000
sum=0
count=0
theVector=vector()
for (i in 1:n)
{
chance=runif(1)
sum=chance+sum
count=count+1
if(sum>1)
{
newVal=count
theVector=c(theVector,newVal)
sum=0
count=0
}
}
meanOfVector=mean(theVector)
cat("The mean of the simulation is: ", meanOfVector, " ")
## The mean of the simulation is: 2.715838
cat("the answer varies somewhere between 2.70 to 2.75 from what i can see")
## the answer varies somewhere between 2.70 to 2.75 from what i can see
question six: accept/reject algorithm
accept.reject <- function(f, c, g, rg, n) {
n.accepts <- 0
result.sample <- rep(NA, n)
while (n.accepts < n) {
x.star <- rg(1)
u <- runif(1,0,1)
if (u < f(x.star)/(c*g(x.star))) {
n.accepts <- n.accepts+1
result.sample[n.accepts] = x.star
}
}
result.sample
}
f=function(x) 10*x*(1-x)
g=function(x) x/x
rg=function(n) runif(n,0,1)
c=2.5
values=accept.reject(f,c,g,rg,1000)
hist(values,freq=FALSE)
```