Tutorial3 Solutions

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

##### ex.

1 #####

X1<-runif(1000)
X2<-runif(1000,min=-1,max=1)

#or X3<--2*runif(1000)+1

#iii)
punif(1/2,-1,1)-punif(-1/2,-1,1)
dunif(0)
dunif(0,-1,1)
1-punif(0,-1,1)

#iv)
hist(X2,prob=T,ylim=c(0,0.65),main="Histogram of Uniform(-1,1)")
abline(h=1/2,col=2,lwd=2)
legend("topright",lty=1,lwd=2,col=2,"theoretic distribution")

X3<-cut(X1,breaks=c(0,1/4,2/4,3/4,1),labels=c(1,2,3,4))
barplot(table(X3))

#or
X4<-(X1<=1/4)*1+(X1<=1/2 & X1>1/4)*2+(X1<=3/4 & X1>1/2)*3+(X1>3/4)*4

##### ex. 2 #####

#i#
dnorm(0)
1/sqrt(2*pi)

#ii#
pnorm(1)-pnorm(-1)

#iii#
2*pnorm(-3)

#iv#
(pnorm(2.5)-pnorm(2))/(1-pnorm(2))

#v#
qnorm(0.7)
qnorm(0.8)

#vi#
x=seq(-4,4,by=0.0001)
y=dnorm(x)

plot(x,y,type="l",main="Density Plot\nN(0,1)", ylab="Density", xlab="x")


X=c(seq(-4,-2,by=0.001),seq(2,4,by=0.001))
segments(X,0,X,dnorm(X),col="red")

#or#
curve(dnorm(x),xlim=c(-3,3),main="Density Plot\nN(0,1)", ylab="Density", xlab="x")

X=c(seq(-4,-2,by=0.001),seq(2,4,by=0.001))
segments(X,0,X,dnorm(X),col="red")

##### ex. 3 #####

#i#
x=rnorm(1000)
y=rnorm(1000,2,1)
z=rnorm(1000,0,2)
w=rnorm(1000,-1,0.5)

#ii#
library(psych)
describe(x)
describe(y)
describe(z)
describe(w)

#iii#
mean(y-z>0)
#1-pnorm(0,2,sqrt(5))

mean(2*w+1>0)
#1-pnorm(0,-1,2*0.5)

mean(x^2>((7/9)^3))
#pchisq(((7/9)^3),df=1)

x11()
par(mfrow=c(2,2))

hist(x, prob=T,col=rainbow(20))
X=seq(-4,4,by=0.001)
lines(X,dnorm(X),lwd=3)
legend("topleft",legend="N(0,1)",lty=1,lwd=3)

hist(y, prob=T,breaks=30)
Y=seq(-2,6,by=0.001)
lines(Y,dnorm(Y,2,1),col="red")
legend("topleft",legend="N(2,1)",lty=1,col="red")

hist(z, prob=T)
curve(dnorm(x,0,2), col = 20, lty = 2, lwd = 2, add = TRUE)
legend("topleft",legend=expression(paste("N(0,",2^2,")")),lty=2,col=20,lwd=2)

hist(w,prob=T, col="cyan3")
curve(dnorm(x,-1,0.5), col = "magenta", lwd = 2, add = TRUE)
legend("topleft",legend=expression(paste("N(-
1,0.5",phantom()^2,")")),col="magenta",lwd=2)

m=matrix(c(1,2,3,4,5,5),nrow = 3,ncol = 2,byrow = TRUE)


layout(m, heights=c(0.44,0.44,0.12))

x11()
plot(density(x), main="Density Plot of X",xlab="")
X=seq(-4,4,by=0.001)
lines(X,dnorm(X), col="red")

plot(density(y), main="Density Plot of Y",xlab="")


Y=seq(-2,6,by=0.001)
lines(Y,dnorm(Y,2,1),col="red")

plot(density(z), main="Density Plot of Z",xlab="")


curve(dnorm(x,0,2), col = "red", add = TRUE)

plot(density(w), main="Density Plot of W",xlab="")


curve(dnorm(x,-1,0.5), col = "red", add = TRUE)

par(mai=c(0,0,0,0))
plot.new()
legend(x="center", legend=c("Empirical","Theoritical"),lty=1,
col=c("black","red"),cex=1.5)

x11()
plot(-100,-100, xlim=c(-6,6),ylim=c(0,0.8), ylab="Density", xlab="")
abline(h=seq(0,0.8,by=0.1),col="gray90")
abline(v=-6:6,col="gray90")

curve(dnorm(x,0,2), col = "blue", lty =1, lwd = 2, xlim=c(-6,6),ylim=c(0,0.8),


ylab="Density", xlab="", add=T)
curve(dnorm(x,2,1), col = "green", lty =1, lwd = 2,add=T,xlim=c(-2,6))
curve(dnorm(x), col = "red", lty =1, lwd = 2, add=T,xlim=c(-4,4))
curve(dnorm(x,-1,0.5), col = "yellow2", lty =1, lwd = 2, add=T, xlim=c(-3.5,1.5))

legend("topleft",legend=c("N(0,1)","N(2,1)", expression(paste("N(0,",2^2,")")),
expression(paste("N(-1,0.5",phantom()^2,")"))),
col=c("red","green","blue", "yellow2"), lty=1, lwd=2 , bg="white")
##### ex. 4 #####

X<-rnorm(1000,5,3)
hist(X)
summary(X)
library(psych)
describe(X)

library(truncnorm)
Y<-rtruncnorm(1000,a=2,b=Inf,5,3)
hist(Y)
summary(Y)
library(psych)
describe(Y)

#or

Y<-NULL
k<-1
n<-0
while (n<1000){
x<-rnorm(1,5,3)
if (x>2) Y<-c(Y,x)
k<-k+1
n<-length(Y)
}
Y
length(Y)

hist(Y)
summary(Y)
library(psych)
describe(Y)

par(mfrow=c(1,2))
hist(X,main=expression(paste("Hist of N(5,", 3^2, ")")))
hist(Y,main="Hist of truncated Normal")

dev.off()

trunc<-function(x){
dnorm( (x-5)/3 )/ (3* (1-pnorm(-1)) )
}

x<-seq(-5,18,0.05)
y<-seq(2,18,0.05)
plot(x,dnorm(x,5,3),type="l",main="density plots",ylab="density",ylim=c(0,0.18))
lines(y,dtruncnorm(y,a=2,b=Inf,5,3),col=3)
#or
lines(y,trunc(y),col=2)

##### ex. 5 #####


#i)
X<-rexp(100000,1/2)
#ii)
describe(X)

#iii)
1-pexp(1,1/2)
1-(1-exp(-1/2*1))

pexp(4,1/2)-pexp(2,1/2)

dexp(0,1/2)

qexp(0.95,1/2)
qexp(0.99,1/2)

#iv)
s<-1
t<-4

Y<-X[X>t]
p1<-sum(Y>s+t)/sum(Y>t)
p1
p2<-sum(X>s)/length(X)
p2

#v)
hist(X)
x<-seq(0,20,0.05)
plot(x,dexp(x,1/2),type="l",main="Exponential distributions",ylab="Density")
lines(x,dexp(x,2),col=2)
lines(x,dexp(x,0.8),col=3)
legend("topright",lty=c(1,1,1),col=c(1,2,3),c("Exp(1/2)","Exp(2)","Exp(1/4)"))

##### ex. 6 #####

#i,ii,iii)

X<-matrix(rexp(10000,5),1000,10)
Z<-apply(X,1,sum)

#ii,iii)

Y<-rgamma(1000,10,5)

summary(Z)
describe(X)
summary(Y)
describe(Y)

#iv)
hist(X)
hist(Y)
x<-seq(0,5,0.01)
plot(x,dexp(x,5),ylim=c(0,4),type="l",lwd=2,ylab="Density",main="Density Plots")
lines(x,dgamma(x,10,5),col=2)
lines(x,dgamma(x,2,5),col=3)
lines(x,dgamma(x,5,5),col=4)

legend("topright",lty=c(1,1,1,1),col=c(1,2,3,4),c("Exp(5)","Gamma(10,5)","Gamma(2,5
)","Gamma(5,5)"))

#v)
qgamma(0.95,10,5)
qgamma(0.99,10,5)

1-pgamma(2,10,5)

You might also like