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

Bayesian Statistics

3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 1.17

> #Problem 1.17


> #==============
>
> system.time({y=c();for (t in 1:100) y[t]=exp(t)})
user system elapsed
0 0 0
> system.time({y=exp(1:100)})
user system elapsed
0 0 0
> system.time({y=sapply(1:100,exp)})
user system elapsed
0 0 0

All of the processes finished in similar time.

Problem 1.19

> #Problem 1.19


> #==============
>
> y=c();for (t in 1:100) y[t]=exp(t)
> A=matrix(runif(4),ncol=2)
> A=A/apply(A,1,sum)
> apply(A%*%A,1,sum)
[1] 1 1
> B=A;for (t in 1:100) B=B%*%B
> apply(B,1,sum)
[1] 0 0

Problem 1.21

> #Problem 1.21


> #==============
>
> library(package = "lattice")
> xyplot(age ~ circumference, data=Orange)

Page 1 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> barchart(age ~ circumference, data=Orange)

> bwplot(age ~ circumference, data=Orange)

Page 2 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> dotplot(age ~ circumference, data=Orange)

> summary(lm(age ~ circumference+Tree, data=Orange))

Call:
lm(formula = age ~ circumference + Tree, data = Orange)

Residuals:
Min 1Q Median 3Q Max
-245.00 -93.69 -19.76 86.99 262.96

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -90.0596 55.5795 -1.620 0.116

Page 3 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

circumference 8.7366 0.4354 20.066 < 2e-16 ***


Tree.L -348.8982 54.9975 -6.344 6.23e-07 ***
Tree.Q -22.0154 52.1881 -0.422 0.676
Tree.C 72.2267 52.3006 1.381 0.178
Tree^4 41.0233 52.2167 0.786 0.438
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 138 on 29 degrees of freedom


Multiple R-squared: 0.9328, Adjusted R-squared: 0.9212
F-statistic: 80.53 on 5 and 29 DF, p-value: 4.221e-16

Tree.L was Significant

Problem 1.23

> #Problem 1.23(a)


> #==============
> s=matrix(0,ncol=9,nrow=9)
> s[1,c(6,8)]=c(6,4)
> s[2,c(1:3,8)]=c(2,7,9,5)
> s[3,c(2,4,9)]=c(5,8,2)
> s[4,3:4]=c(2,6)
> s[6,c(3,5,7:9)]=c(1,9,6,7,3)
> s[7,c(1,3:4,7)]=c(8,5,2,4)
> s[8,c(1,8:9)]=c(3,8,5)
> s[9,c(1,7,9)]=c(6,9,1)
>s
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 0 0 0 0 0 6 0 4 0
[2,] 2 7 9 0 0 0 0 5 0
[3,] 0 5 0 8 0 0 0 0 2
[4,] 0 0 2 6 0 0 0 0 0
[5,] 0 0 0 0 0 0 0 0 0
[6,] 0 0 1 0 9 0 6 7 3
[7,] 8 0 5 2 0 0 4 0 0
[8,] 3 0 0 0 0 0 0 8 5
[9,] 6 0 0 0 0 0 9 0 1
>
>
> #Problem 1.23(b)
> #==============
> pool=array(TRUE,dim=c(9,9,9))
> for (i in 1:9)
Page 4 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

+ for (j in 1:9){
+ if (s[i,j]>0) pool[i,j,-s[i,j]]=FALSE
+}
>
>

Problem 1.23(c)
In R, matrices (and arrays) are also considered as vectors. Hence s[i] represents the (1 + [(i -
1)/9], (i - 1) mod 9 + 1) entry of the grid.

> #Problem 1.23(d)


> #==============
> a=2
> b=5
> boxa=3*trunc((a-1)/3)+1
> boxa=boxa:(boxa+2)
> boxa
[1] 1 2 3
> boxb=3*trunc((b-1)/3)+1
> boxb=boxb:(boxb+2)
> boxb
[1] 4 5 6

> #Problem 1.23(e)


> #==============

> #Problem 1.23(f)


> #==============
> while (sum(s==0)>0){
+ for (i in sample(1:81)){
+ if (s[i]==0){
+ a=((i-1)%%9)+1
+ b=trunc((i-1)/9)+1
+ boxa=3*trunc((a-1)/3)+1
+ boxa=boxa:(boxa+2)
+ boxb=3*trunc((b-1)/3)+1
+ boxb=boxb:(boxb+2)
+ for (u in (1:9)[pool[a,b,]]){
+ pool[a,b,u]=(sum(u==s[a,])+sum(u==s[,b])
+ +sum(u==s[boxa,boxb]))==0
+}
+ if (sum(pool[a,b,])==1){

Page 5 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

+ s[i]=(1:9)[pool[a,b,]]
+}
+ if (sum(pool[a,b,])==0){
+ print("wrong sudoku")
+ break()
+}
+}
+}
+}
>s
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 3 8 5 2 6 7 4 9
[2,] 2 7 9 3 4 1 8 5 6
[3,] 4 5 6 8 7 9 3 1 2
[4,] 7 4 2 6 3 5 1 9 8
[5,] 9 6 3 1 8 7 5 2 4
[6,] 5 8 1 4 9 2 6 7 3
[7,] 8 9 5 2 1 3 4 6 7
[8,] 3 1 7 9 6 4 2 8 5
[9,] 6 2 4 7 5 8 9 3 1
>

Problem 2.11

> #Problem 2.11(a)


> #==============
> n=25;p=.2;
> cp=pbinom(c(0:n),n,p)
> X=array(0,c(nsim,1))
> X=array(0,c(nsim,1))
> for(i in 1:nsim){
+ u=runif(1)
+ X[i]=sum(cp<u)
+}
> hist(X,freq=F)
> lines(1:n,dbinom(1:n,n,p),lwd=2)
>

Page 6 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> MYbinom<-function(s0,n0,p0){
+ cp=pbinom(c(0:n0),n0,p0)
+ X=array(0,c(s0,1))
+ for (i in 1:s0){
+ u=runif(1)
+ X[i]=sum(cp<u)
+}
+ return(X)
+}
> system.time(rbinom(5000,25,.2))
user system elapsed
0.00 0.00 0.02
> system.time(MYbinom(5000,25,.2))
user system elapsed
0.02 0.00 0.01
>
>
> #Problem 2.11(b)
> #==============
> Wait<-function(s0,alpha){
+ U=array(0,c(s0,1))
+ for (i in 1:s0){
+ u=runif(1)

Page 7 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

+ while (u > alpha) u=runif(1)


+ U[i]=u
+}
+ return(U)
+}
> Trans<-function(s0,alpha){
+ U=array(0,c(s0,1))
+ for (i in 1:s0) U[i]=alpha*runif(1)
+ return(U)
+}
> hist(Wait(1000,.5))
> hist(Trans(1000,.5))
>

Page 8 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 2.12

> #Problem 2.12(a)


> #==============
> a=3;beta=2
> U=matrix(runif(a*10^4),nrow=a)
> X=apply(-log(U),2,sum)/beta
> plot(function(x) dgamma(x,3,2),0,10)
> hist(X,freq=F,add=T)

Page 9 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> a=3;b=2
> U1=matrix(runif(a*10^4),nrow=a)
> U2=matrix(runif(b*10^4),nrow=b)
> X1=apply(-log(U1),2,sum)
> X2=apply(-log(U2),2,sum)
> X=X1/(X1+X2)
> plot(function(x) dbeta(x,3,2),0,1)
> hist(X,freq=F,add=T,breaks=50)

Page 10 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 2.12(b)
This is a consequence of F(x) = 1 - exp(-λx) being the Ɛxp(λ) cdf and of 1 - U being also
distributed as a U[0,1] random variable.

Problem 2.12(c)
This is due to the fact that F(x) = exp(x)/{1+exp(x)} is the cdf of the Logistic(0,1) distribution.

Problem 2.14

> #Problem 2.14(a)


> #==============
> Nsim=10^4; lambda=100
> spread=3*sqrt(lambda)
> t=round(seq(max(0,lambda-spread),lambda+spread,1))
> prob=ppois(t, lambda)
> X=rep(0,Nsim)
> for (i in 1:Nsim){
+ u=runif(1)
+ X[i]=t[1]+sum(prob<u) }
> range(prob)
[1] 0.000971444 0.998293160

Page 11 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

>
>
> r=10;p=.01;spread=3*sqrt(r*(1-p)/p^2)
> t=round(seq(max(0,r*(1-p)/p-spread),r*(1-p)/p+spread,1))
> prob=pnbinom(t,r,p)
> range(prob)
[1] 2.341061e-10 9.932656e-01
>

> #Problem 2.14(b)


> #==============
> Nsim=10^3; r=10;p=0.01
> m=r*(1-p)/p;var=r*(1-p)/p^2
> spread=3*sqrt(var)
> t=round(seq(max(0,m-spread),m+spread,1))
> prob=pnbinom(t,r,p)
> X=rep(0,Nsim)
> for (i in 1:Nsim){
+ u=runif(1)}
> X[i]=t[1]+sum(prob<u)
> par(mfrow=c(1,3))
> hist(X,main="Negative binomial simulated",sub="p=0.01")
> library(package = "MASS")
> hist(rnegbin(Nsim,m,r),main="Result of rnegbin")
> plot(t,dnbinom(t,r,p))
>

Page 12 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> #Problem 2.14(c)


> #==============
> Nsim=10^3; p=0.001
> m=-(1-p)/(p*log(p)); var=-(1-p)/(p*p*log(p))
> logs=function(x,p){
+ -((1-p)^x)/(x*log(p))
+}
> spread=3*sqrt(var)
> t=round(seq(max(0,m-spread),m+spread,1))
> l=length(t)
> prob=rep(0,l)
> prob[1]=logs(1,p)
> for (i in 2 :l)
+ prob[i]=prob[i-1]+logs(i,p)
> X=rep(0,Nsim)
> for (i in 1 :Nsim){
+ u=runif(1)
+ X[i]=t[1]+sum(prob<u)
+}
> par(mfrow=c(1,3))
> hist( X,main="simulated log series",sub="p=0.001")
> plot(t,logs(t,p),main="probability distribution")

Page 13 of 14
Bayesian Statistics
3470: 589-001
HW - 03 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> plot(t,prob,main="cumulated distribution fonction")


>

Page 14 of 14

You might also like