Professional Documents
Culture Documents
4
5
6
7
8
9
10
#Task 1
# Generating 3000 samples using the Gibbs sampling method.
n = 3000
#set the mean and covariance parameters
mu = [0,0]
cov = [[4,1],[1,4]]
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
X1 = zeros(n)
X2 = zeros(n)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#Task 2
figure(0)
plot(X1[2900:],X2[2900:])
figure(1)
plot(X1, b )
figure(2)
plot(X2, r )
show()
45
46
47
48
49
#Task 3
#estimate the probability
prob = zeros(n)
prob[0]= (X1[0]>=0 and X2[0]>=0)
50
51
52
53
for i in xrange(1,n):
prob[i]= prob[i-1]+(X1[i]>=0 and X2[i]>=0)
Prob = prob/xrange(1,n+1)
54
55
print(Prob)
56
57
figure(3)
58
59
60
plot(Prob)
axis([-200,n+200,0.2,0.6])
show()
61
62
63
64
#Task 4
#set the new covariance parameter, and repeat steps above
cov = [[4,2.8],[2.8,4]]
Model code in R
1
2
3
4
5
#Tasks 1 and 3
#set the mean parameter
mu <- c(0,0)
#set the covarince parameter
sigma <- matrix(c(4,1,1,4), nrow=2)
6
7
8
9
10
#Gibbs sampling
#set the desired sample size
n <- 3000
x <- matrix(nrow=n, ncol=2)
11
12
13
14
15
16
17
18
19
20
21
22
23
for(i in 1:n){
mean1 <- mu[1] + sigma[1,2]/sigma[2,2] * (cur.x[2] - mu[2])
cur.x[1] <- rnorm(1, mean = mean1, sd = sd1)
24
25
26
27
if(i == 1){
prop[i] <- as.numeric((cur.x[1] >= 0) && (cur.x[2] >= 0))
}
else{
prop[i] <- as.numeric((cur.x[1] >= 0) && (cur.x[2] >= 0)) + prop[i-1]
}
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#Task 2
#plot the last 100 values, with subsequent values linked by a line
plot(x[901:1000,], type= b , xlab=X_1 , ylab=X_2 )
43
44
45
46
47
48
49
50
51
52
53
54
#Task 4
#Now set the covarince parameter as follows, and repeat.
sigma <- matrix(c(4,2.8,2.8,2), nrow=2)