-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormgen.R
239 lines (218 loc) · 7.35 KB
/
normgen.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
new.rmvtgauss.lin <- function (n, mu, Sigma, PrecMat = FALSE,
factorised = FALSE, Amat, Avec, start,
burnin = 1, thin = 1) {
if (length(n) == 1) {
n <- as.integer(n)
} else {
n <- length(n)
}
if (is.na(n) || n < 0) {
stop("argument 'n' invalid")
}
if (missing(mu)) {
stop("argument 'mu' is missing with no default")
}
mu <- as.numeric(mu)
d <- length(mu)
if (missing(Sigma)) {
stop("argument 'Sigma' is missing with no default")
}
Sigma <- as.matrix(Sigma)
if (missing(Amat)) {
stop("argument 'Amat' is missing with no default")
}
Amat <- as.matrix(Amat)
if (missing(Avec)) {
stop("argument 'Avec' is missing with no default")
}
Avec <- as.numeric(Avec)
if (missing(start)) {
start <- rep(0, d)
}
start <- as.numeric(start)
if (length(burnin) > 1) {
warning("'burnin' has length > 1; using first component")
burnin <- as.numeric(burnin[1])
}
if (length(thin) > 1) {
warning("'thin' has length > 1; using first component")
thin <- as.numeric(thin[1])
}
if (!is.numeric(Sigma) || !is.numeric(Amat) || !is.numeric(burnin) ||
!is.numeric(thin)) {
stop("some arguments are not numeric")
}
if (any(is.na(mu)) || any(is.na(Sigma)) || any(is.na(Amat)) ||
any(is.na(Avec)) || any(is.na(start)) || is.na(burnin) ||
is.na(thin)) {
stop("some arguments contain NAs")
}
if (!isTRUE(all.equal(dim(Sigma), c(d, d)))) {
stop("arguments 'mu' and 'Sigma' are incompatible")
}
if (length(start) != d) {
stop("arguments 'mu' and 'start' are incompatible")
}
if (NROW(Amat) != d) {
stop("arguments 'mu' and 'Amat' are incompatible")
}
if (NCOL(Amat) != length(Avec)) {
stop("arguments 'Amat' and 'Avec' are incompatible")
}
if (PrecMat) {
Sigma <- solve(Sigma)
}
if (!factorised) {
Right <- chol(Sigma)
}
Bmat <- Right %*% Amat
Bvec <- Avec - crossprod(Amat, mu)
## eigv.Sigma <- eigen(Sigma)
## sqrt.Sigma <- with(eigv.Sigma, tcrossprod(vectors %*% diag(sqrt(values)),
## vectors))
new.start <- backsolve(Right, start - mu, transpose = TRUE)
## new.start <- solve(sqrt.Sigma, start - mu)
res <- matrix(NA, nrow = d, ncol = n)
for (i in 1:burnin) {
tmpmat <- new.start * Bmat
for (dd in 1:d) {
tmpvec <- Bvec - colSums(tmpmat[-dd, , drop = FALSE])
ind <- Bmat[dd, , drop = FALSE] > 0
if (sum(ind) > 0) {
left <- max(tmpvec[ind]/Bmat[dd, ind])
} else {
left <- -Inf
}
ind <- Bmat[dd, , drop = FALSE] < 0
if (sum(ind) > 0) {
right <- min(tmpvec[ind]/Bmat[dd, ind])
} else {
right <- Inf
}
if (left > right) {
browser()
stop("inconsistent constraints")
}
new.start[dd] <- rtgauss(1, left = left, right = right)
tmpmat[dd, ] <- new.start[dd] * Bmat[dd, , drop = FALSE]
}
}
res[, 1] <- new.start
if (n > 1) {
for (i in 2:n) {
for (j in 1:thin) {
tmpmat <- new.start * Bmat
for (dd in 1:d) {
tmpvec <- Bvec - colSums(tmpmat[-dd, , drop = FALSE])
ind <- Bmat[dd, , drop = FALSE] > 0
if (sum(ind) > 0) {
left <- max(tmpvec[ind]/Bmat[dd, ind])
} else {
left <- -Inf
}
ind <- Bmat[dd, , drop = FALSE] < 0
if (sum(ind) > 0) {
right <- min(tmpvec[ind]/Bmat[dd, ind])
} else {
right <- Inf
}
if (left > right) {
stop("inconsistent constraints")
}
new.start[dd] <- rtgauss(1, left = left, right = right)
tmpmat[dd, ] <- new.start[dd] * Bmat[dd, , drop = FALSE]
}
}
res[, i] <- new.start
}
}
return(crossprod(Right, res) + mu)
## return(crossprod(sqrt.Sigma, res) + mu)
}
new.rtmg <- function (n, M, r, initial, f = NULL, g = NULL, q = NULL, burn.in = 30)
{
d = nrow(M)
if (ncol(M) != d) {
stop("Error: M must be a square matrix.")
}
if (length(initial) != d) {
stop("Error: wrong length for initial value vector.")
}
## what's this? to make sure M is symmetric?
M = (M + t(M))/2
eigs = eigen(M, symmetric = T, only.values = T)$values
if (any(eigs <= 0)) {
stop("Error: M must be positive definite.")
}
R = chol(M)
Mir = solve(M, r)
Ri = solve(R)
initial2 = as.vector(R %*% initial) - as.vector(r %*% Ri)
if (is.null(f) & is.null(g)) {
numlin = 0
f2 = NULL
g2 = NULL
}
else {
if (is.matrix(f) & is.vector(g)) {
numlin = nrow(f)
if (length(g) != numlin | ncol(f) != d) {
stop("Error: inconsistent linear constraints. f must be an m-by-d matrix and g an m-dimensional vector.")
}
if (any(f %*% initial + g <= 0)) {
stop("Error: initial point violates linear constraints.")
}
f2 = f %*% Ri
g2 = as.vector(f %*% Mir + g)
}
else {
stop("Error: for linear constraints, f must be a matrix and g a vector.\n")
}
}
if (is.null(q)) {
numquad = 0
quads = NULL
}
else {
if (is.list(q)) {
ll = lapply(q, length)
nc = c(do.call("cbind", ll))
if (any(nc != 3)) {
stop("Error: each element in q must be a list of length 3.\n")
}
numquad = length(q)
quads = matrix(nrow = numquad * (d + 2), ncol = d)
for (i in 1:numquad) {
qci = q[[i]]
t = initial %*% qci[[1]] %*% initial + qci[[2]] %*%
initial + qci[[3]]
if (t <= 0) {
stop("Error: initial point violates quadratic constraints. \n")
}
else {
A = qci[[1]]
B = qci[[2]]
C = qci[[3]]
quads[((i - 1) * (d + 2) + 1):((i - 1) * (d +
2) + d), ] = crossprod(Ri, A %*% Ri)
quads[((i - 1) * (d + 2) + d + 1), ] = 2 *
crossprod(Mir, A %*% Ri) + crossprod(B, Ri)
C = C + crossprod(Mir, A %*% Mir) + crossprod(B, Mir)
quads[i * (d + 2), ] = c(C, rep(0, d - 1))
}
}
}
else {
stop("Error: for quadratic constraints, q must be a list.\n")
}
}
seed = sample(1:1e+07, 1)
samples = .Call("rtmg", n + burn.in, seed, initial2, numlin,
f2, g2, numquad, quads, PACKAGE = "tmg")
samples = samples[(burn.in + 1):(burn.in + n), ]
samples = tcrossprod(samples, Ri) + matrix(rep(Mir, n), nrow = n,
byrow = T)
return(samples)
}
my_rtmg <- function(n, mean, sd, initial, f = NULL, g = NULL, burn.in = 30) {
}