You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looking through the first implemention, we realize that we can
105
99
speed up our code by vectorizing when possible and not performing computations
106
100
more often than we have to. Specifically, we are calculating `sum(y[1:i])` and `sum(y[min((k+1),n):n])` repeatedly in the loops. We can dramatically improve the speed of our implementation by calculating the sum of `y` and the cumulative sum of `y` and then using the stored results appropriately. We now reimplement the pmf and Gibbs sampler functions with this in
107
101
mind.
108
102
109
103
110
104
{% highlight r %}
111
-
gibbsvec <- function(nsim, y, a, b, c, d, kposs, phi, k)
112
-
{
113
-
# matrix to store simulated values from each cycle
114
-
out <- matrix(NA, nrow = nsim, ncol = 3)
115
-
116
-
# determine number of observations
117
-
n <- length(y)
118
-
119
-
# determine sum of y and cumulative sum of y.
120
-
# Then cusum[k] == sum(y[1:k])
121
-
# and sum(y[(k+1):n]) == sumy - cusum[k]
122
-
sumy <- sum(y)
123
-
cusum <- cumsum(y)
124
-
125
-
for(i in 1:nsim)
126
-
{
127
-
# Generate value from full conditional of phi based on
128
-
# current values of other parameters
129
-
lambda <- rgamma(1, a + cusum[k], b + k)
105
+
gibbsvec <- function(nsim, y, a, b, c, d, kposs, phi, k) {
106
+
# matrix to store simulated values from each cycle
107
+
out <- matrix(NA, nrow = nsim, ncol = 3)
108
+
109
+
# determine number of observations
110
+
n <- length(y)
111
+
112
+
# determine sum of y and cumulative sum of y.
113
+
# Then cusum[k] == sum(y[1:k])
114
+
# and sum(y[(k+1):n]) == sumy - cusum[k]
115
+
sumy <- sum(y)
116
+
cusum <- cumsum(y)
117
+
118
+
for (i in 1:nsim) {
119
+
# Generate value from full conditional of phi based on
120
+
# current values of other parameters
121
+
lambda <- rgamma(1, a + cusum[k], b + k)
130
122
131
-
# Generate value from full conditional of phi based on
132
-
# current values of other parameters
133
-
phi <- rgamma(1, c + sumy - cusum[k], d + n - k)
123
+
# Generate value from full conditional of phi based on
124
+
# current values of other parameters
125
+
phi <- rgamma(1, c + sumy - cusum[k], d + n - k)
134
126
135
-
# generate value of k
136
-
pmf <- kprobvec(kposs, cusum, lambda, phi)
137
-
k <- sample(x = kposs, size = 1, prob = pmf)
127
+
# generate value of k
128
+
pmf <- kprobvec(kposs, cusum, lambda, phi)
129
+
k <- sample(x = kposs, size = 1, prob = pmf)
138
130
139
-
out[i, ] <- c(lambda, phi, k)
140
-
}
141
-
out
131
+
out[i, ] <- c(lambda, phi, k)
132
+
}
133
+
out
142
134
}
143
135
144
136
# Determine pmf for full conditional of k based on current values of other
145
137
# variables. Do this efficiently using vectors and stored information.
146
138
# cusum is the cumulative sum of y.
147
139
148
-
kprobvec <- function(kposs, cusum, lambda, phi)
149
-
{
150
-
# calculate exponential argument of numerator of unnormalized pmf
We should be able to improve the speed of our sampler by implementing it in C++. We can reimplement the vectorized version of the sampler fairly easily using Rcpp and RcppArmadillo. Note that the parameterization of `rgamma` used by Rcpp is slightly different from base R.
159
149
160
150
@@ -164,52 +154,47 @@ We should be able to improve the speed of our sampler by implementing it in C++.
0 commit comments