-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFuncBernMetropolis.R
80 lines (68 loc) · 2.25 KB
/
FuncBernMetropolis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
## An arbitrary number of jumps to try in Metropolis
## algorithm.
trajLength = 11112
## The data represented as a vector.
myData = c( 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 )
## An arbitrary seed presumably used in generating random
## values from the various distributions.
set.seed(47405)
## We need some proposed jumps; following the example we
## generate these from a normal distribution.
normals = rep (0, trajLength)
for (j in 1:trajLength) {
normals[j] = rnorm( 1, mean = 0, sd = 0.1 )
}
## We generate proposed jumps which are either one step up
## or one step down.
proposedJumps = rep (0, trajLength)
for (j in 1:trajLength) {
proposedJumps[j] =
if ( runif( 1 ) < 0.5 )
{ -1 }
else
{ 1 }
}
## We generate samples from the uniform distribution on [0,
## 1] which will allow us to determine whether to accept or
## reject a proposed jump.
acceptOrRejects = rep (0, trajLength)
for (j in 1:trajLength) { acceptOrRejects[j] = runif( 1 ) }
likelihood = function( z, n, theta ) {
x = theta^z * (1 - theta)^(n - z)
x[ theta > 1 | theta < 0 ] = 0
return ( x )
}
prior = function( position, xs ) {
n = length( xs )
z = sum ( xs == 1)
return ( likelihood ( z, n, position ) )
}
## We define a function which defines one step of the
## Metropolis algorithm.
oneStep = function ( currPosition, propJumpAorR ) {
proposedJump = propJumpAorR [1]
acceptOrReject = propJumpAorR [2]
probAccept = min( 1,
prior( currPosition + proposedJump , myData )
/ prior( currPosition , myData ) )
if ( acceptOrReject < probAccept ) {
trajectory = currPosition + proposedJump
} else {
trajectory = currPosition
}
return ( trajectory )
}
## Pair together the proposed jumps and the probability
## whether a given proposal will be accepted or rejected.
nsAorRs <- list ()
for (i in 1:trajLength)
nsAorRs[[i]] <- c(normals[i], acceptOrRejects[i])
## Fold (well really scanl) over the pairs returning all the
## intermediate results.
trajectory = Reduce( function(a,b) oneStep (a, b),
nsAorRs, accumulate=T,init= 0.5 )
## Drop the values for the burn in period.
burnIn = ceiling( .1 * trajLength )
acceptedTraj = trajectory[burnIn:trajLength]
## Finally return the mean of the posterior.
result = mean ( acceptedTraj )