forked from jbischof/hmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmc_gauss_functions.R
46 lines (39 loc) · 1.35 KB
/
hmc_gauss_functions.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
# Want to sample from highly correlated normal
dens.normal <- function(x,mean.vec,Sigma.inv,sig.diag=FALSE){
deviat.vec <- x - mean.vec
if(sig.diag){dens <- -0.5*deviat.vec^2*Sigma.inv
} else {dens <- -0.5*deviat.vec%*%Sigma.inv%*%deviat.vec}
return(dens)
}
grad.normal <- function(x,mean.vec,Sigma.inv,sig.diag=FALSE){
deviat.vec <- x - mean.vec
if(sig.diag){grad <- -Sigma.inv*deviat.vec
} else {grad <- -Sigma.inv%*%deviat.vec}
return(grad)
}
U.normal <- function(x){
out <- -dens.normal(x=x,mean.vec=rep(0,length(x)),Sigma.inv=Sigma.inv)
return(as.numeric(out))
}
U.normal.grad <- function(x){
out <- -grad.normal(x=x,mean.vec=rep(0,length(x)),Sigma.inv=Sigma.inv)
return(as.numeric(out))
}
K.normal <- function(p,Sigma.inv=diag(length(p))){
out <- -dens.normal(x=p,mean.vec=rep(0,length(p)),Sigma.inv=Sigma.inv)
return(as.numeric(out))
}
# Draw momentum variables of the same dimension as the position
# sd.M is a vector of standard deviations for momemtum vars
p.draw.diag <- function(x,sd.M=NULL){
if(is.null(sd.M)){p <- rnorm(n=length(x))
} else {p <- rnorm(n=length(x),sd=sd.M)}
return(p)
}
# Draw momentum variables of the same dimension as the position,
# in the case from non-diagonal covariance matrix
p.draw <- function(x,M.chol){
z <- rnorm(n=length(x))
p <- as.vector(M.chol%*%z)
return(p)
}