Description Usage Arguments Details Value Author(s) References See Also Examples
This function allows a user to generate a sample from a user-defined unormalized continuos distribution using the Polya tree sampler algorithm.
1 2 |
ltarget |
a function giving the log of the target density. |
dim.theta |
an integer indicating the dimension of the target density. |
mcmc |
an optional list giving the MCMC parameters. The list must include
the following integers: |
support |
an optional matrix, of dimension dim.theta * npoints, giving the initial support points. By default the function generates 400 support points from a dim.theta normal distribution with mean 0 and diagonal covariance matrix with 1000 in the diagonal. |
pts.options |
an optional list of giving the parameters needed for the PTsampler
algorithm. The list must include: |
status |
a logical variable indicating whether this run is new ( |
state |
a list giving the starting points for the MCMC algorithm. The list must
include: |
PTsampler produces a sample from a user-defined multivariate distribution using the Polya tree sampler algorithm. The algorithm constructs an independent proposal based on an approximation of the target density. The approximation is built from a set of support points and the predictive density of a finite multivariate Polya tree. In an initial warm-up phase, the support points are iteratively relocated to regions of higher support under the target distribution to minimize the distance between the target distribution and the Polya tree predictive distribution. In the sampling phase, samples from the final approximating mixture of finite Polya trees are used as candidates which are accepted with a standard Metropolis-Hastings acceptance probability. We refer to Hanson, Monteiro, and Jara (2011) for more details on the Polya tree sampler.
An object of class PTsampler
representing the MCMC sampler. Generic functions such as print
,
plot
, and summary
have methods to show the results of the fit.
The list state
in the output object contains the current value of the parameters
necessary to restart the analysis. If you want to specify different starting values
to run multiple chains set status=TRUE
and create the list state based on
this starting values.
The object thetsave
in the output list save.state
contains the samples from the target density.
Alejandro Jara <atjara@uc.cl>
Tim Hanson <hansont@stat.sc.edu>
Hanson, T., Monteiro, J.V.D, and Jara, A. (2011) The Polya Tree Sampler: Toward Efficient and Automatic Independent Metropolis-Hastings Proposals. Journal of Computational and Graphical Statistics, 20: 41-62.
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 | ## Not run:
###############################
# EXAMPLE 1 (Dog Bowl)
###############################
# Target density
target <- function(x,y)
{
out <- (-3/2)*log(2*pi)-0.5*(sqrt(x^2+y^2)-10)^2-
0.5*log(x^2+y^2)
exp(out)
}
ltarget <- function(x)
{
out <- -0.5*((sqrt(x[1]^2+x[2]^2)-10)^2)-
0.5*log(x[1]^2+x[2]^2)
out
}
# MCMC
mcmc <- list(nburn=5000,
nsave=10000,
ndisplay=500)
# Initial support points (optional)
support <- cbind(rnorm(300,15,1),rnorm(300,15,1))
# Scanning the posterior
fit <- PTsampler(ltarget,dim.theta=2,mcmc=mcmc,support=support)
fit
summary(fit)
plot(fit,ask=FALSE)
# Samples saved in
# fit$save.state$thetasave
# Here is an example of how to use them
par(mfrow=c(1,2))
plot(acf(fit$save.state$thetasave[,1],lag=100))
plot(acf(fit$save.state$thetasave[,1],lag=100))
# Plotting resulting support points
x1 <- seq(-15,15,0.2)
x2 <- seq(-15,15,0.2)
z <- outer(x1,x2,FUN="target")
par(mfrow=c(1,1))
image(x1,x2,z,xlab=expression(theta[1]),ylab=expression(theta[2]))
points(fit$state$support,pch=19,cex=0.25)
# Plotting the samples from the target density
par(mfrow=c(1,1))
image(x1,x2,z,xlab=expression(theta[1]),ylab=expression(theta[2]))
points(fit$save.state$thetasave,pch=19,cex=0.25)
# Re-starting the chain from the last sample
state <- fit$state
fit <- PTsampler(ltarget,dim.theta=2,mcmc=mcmc,
state=state,status=FALSE)
###############################
# EXAMPLE 2 (Ping Pong Paddle)
###############################
bivnorm1 <- function(x1,x2)
{
eval <- (x1)^2+(x2)^2
logDET <- 0
logPDF <- -(2*log(2*pi)+logDET+eval)/2
out <- exp(logPDF)
out
}
bivnorm2 <- function(x1,x2)
{
mu <- c(-3,-3)
sigmaInv <- matrix(c(5.263158,-4.736842,
-4.736842,5.263158),
nrow=2,ncol=2)
eval <- (x1-mu[1])^2*sigmaInv[1,1]+
2*(x1-mu[1])*(x2-mu[2])*sigmaInv[1,2]+
(x2-mu[2])^2*sigmaInv[2,2]
logDET <- -1.660731
logPDF <- -(2*log(2*pi)+logDET+eval)/2
out <- exp(logPDF)
out
}
bivnorm3 <- function(x1,x2)
{
mu <- c(2,2)
sigmaInv <- matrix(c(5.263158,4.736842,
4.736842,5.263158),
nrow=2,ncol=2)
eval <- (x1-mu[1])^2*sigmaInv[1,1]+
2*(x1-mu[1])*(x2-mu[2])*sigmaInv[1,2]+
(x2-mu[2])^2*sigmaInv[2,2]
logDET <- -1.660731
logPDF <- -(2*log(2*pi)+logDET+eval)/2
out <- exp(logPDF)
out
}
target <- function(x,y)
{
out <- 0.34*bivnorm1(x,y)+
0.33*bivnorm2(x,y)+
0.33*bivnorm3(x,y)
out
}
ltarget <- function(theta)
{
out <- 0.34*bivnorm1(x1=theta[1],x2=theta[2])+
0.33*bivnorm2(x1=theta[1],x2=theta[2])+
0.33*bivnorm3(x1=theta[1],x2=theta[2])
log(out)
}
# MCMC
mcmc <- list(nburn=5000,
nsave=10000,
ndisplay=500)
# Initial support points (optional)
support <- cbind(rnorm(300,6,1),rnorm(300,6,1))
# Scanning the posterior
fit <- PTsampler(ltarget,dim.theta=2,mcmc=mcmc,support=support)
fit
summary(fit)
plot(fit,ask=FALSE)
# Samples saved in
# fit$save.state$thetasave
# Here is an example of how to use them
par(mfrow=c(1,2))
plot(acf(fit$save.state$thetasave[,1],lag=100))
plot(acf(fit$save.state$thetasave[,1],lag=100))
# Plotting resulting support points
x1 <- seq(-6,6,0.05)
x2 <- seq(-6,6,0.05)
z <- outer(x1,x2,FUN="target")
par(mfrow=c(1,1))
image(x1,x2,z,xlab=expression(theta[1]),ylab=expression(theta[2]))
points(fit$state$support,pch=19,cex=0.25)
# Plotting the samples from the target density
par(mfrow=c(1,1))
image(x1,x2,z,xlab=expression(theta[1]),ylab=expression(theta[2]))
points(fit$save.state$thetasave,pch=19,cex=0.25)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.