cachet: Cache calculated Canonical Statistics for Potts Models.

Cache TR Documentation

Cache calculated Canonical Statistics for Potts Models.


Variety of functions to support caching of calculated canonical statistics for Potts Models. There is some attempt at being 'smart' with when to regenerate the statistics.


generate_t_cache(x, ncolor, t_stat, sizeA, npixel, f,
                 fapply=lapply, gridcache=NULL)

gengridcache(ncolor, sizeCA, ncol)


singleton(x, ncolor, a, idx, gridcache=NULL)


twopixel(x, ncolor, a, idx, gridcache=NULL)

twopixel.nonoverlap(x, ncolor, a, idx, gridcache=NULL)


fourpixel(x, ncolor, a, idx, gridcache=NULL)

fourpixel.nonoverlap(x, ncolor, a, idx, gridcache=NULL)


ninepixel.nonoverlap(x, ncolor, a, idx, gridcache=NULL)


sixteenpixel.nonoverlap(x, ncolor, a, idx, gridcache=NULL)



numerical vector of length ncolor. Contains the canonical statistic for the whole image.


numerical. The number of elements in sA.


numerical. The number of elements in C^A.


numerical. The number of pixels in one element of sA.


function. Takes arguments x, ncolor, a, idx and ncolor. Returns value of t_calc_innergrid with window A(a) replaced by the \code{idx}-th element of C^A.


function. It should behave exactly as lapply does. You can use this argument to enable parallel computing.


list. Optional. If non-null, it is a list of the elements of C^A.


numeric, 2 dimensional matrix, elements in 1, ..., ncolor, representing a Potts model.


numeric. Number of colors in this Potts Model.


numeric. Gives the number of columns in a rectangular window.


numeric. Indicates which member of sA is being referenced.


numeric. Indicates which element of C^A is being referenced.


For a description of notation and terminology, see composite.ll.

This set of functions is used to generate cached calculations of the canonical statistic of a Potts model suitable for passing into composite.ll or gr.composite.ll.

All of the calculations using composite.ll and these caching functions need one of the color components to be dropped for the model to be identifiable. For simplicity, the first color is dropped by generate_t_cache. In computing the composite log likelihood for a Potts model with ncolor colors, we are interested in many calculations across C^A, the set of all permutations of colors across a window. These functions facilitate those calculations. gridcache is a list of C^A.

generate_t_cache is the main function, and the others are intended to be used in conjunction with it. generate_t_cache creates a list of arrays. Each array represents one window in the image, and each row of the array contains the value of t(x) (with one component dropped) found by replacing the pixels in that window with one of the elements of C^A.

gengridcache can generate the gridcache for any rectangular window, give the number of colors, size of C^A, and number of columns in the window. gensingleton, gentwopixel, genfourpixel, genthreebythree and genfourbyfour are all just simple wrappers for gengridcache.

singleton, twopixel, twopixel.nonoverlap, fourpixel, fourpixel.nonoverlap, ninepixel.nonoverlap and sixteenpixel.nonoverlap are intended to be passed to generate_t_cache in the argument f. They are used to calculate \code{t( ca(idx) union X \ A(a) )} for the idx-th element of C^A(a).

Functions that have overlap and nonoverlap versions generate a overlapping and nonoverlapping set of windows respectively.

singleton is for a single pixel window (Besag or MPLE).

twopixel does a two horizontal pixel window.

fourpixel does a two by two pixel window.

ninepixel does a three by three pixel window.

sixteenpixel does a four by four pixel window.


Functions that start with gen return a list of the elements of C^A.

The other functions (e.g. twopixel, fourpixel, ...) return the result of replacing the a-th window of x with the idx-th element of C^A and calculating calc_t_innergrid for that window.

See Also

composite.ll, calc_t.


ncolor <- 4
beta   <- log(1+sqrt(ncolor))
theta  <- c(rep(0,ncolor), beta)

nrow <- 32
ncol <- 32

x <- matrix(sample(ncolor, nrow*ncol, replace=TRUE), nrow=nrow, ncol=ncol)
foo <- packPotts(x, ncolor)
out <- potts(foo, theta, nbatch=10)
x <- unpackPotts(out$final)

t_stat <- calc_t(x, ncolor)
t_cache_mple <- generate_t_cache(x, ncolor, t_stat, nrow*ncol, 1,

## Not run: 
# use multicore to speed things up.
t_cache_mple <- generate_t_cache(x, ncolor, t_stat, nrow*ncol, 1,
                                 singleton, fapply=mclapply)

## End(Not run)

potts documentation built on Aug. 12, 2022, 5:07 p.m.