xsample {limSolve}R Documentation

Randomly samples an under- or overdetermined problem with linear equality and inequality conditions

Description

Bayesian samping of linear problems with linear inequality conditions. Uses either the "hit and run" (or directions algorithm) or a mirroring technique for sampling

The monte carlo method produces a sample solution for

Ex=f

Ax~=B

Gx>=h

where Ex=F have to be met exactly, and Ax~=B approximately.

Usage

xsample(A=NULL, B=NULL, E=NULL, F=NULL, 
G=NULL, H=NULL, sdB=1, iter=3000, outputlength=iter,burninlength=NULL,
type = "mirror", jmp = NULL, tol=sqrt(.Machine$double.eps),
x0=NULL, fulloutput = FALSE, test=TRUE)

Arguments

A numeric matrix containing the coefficients of the (approximate) equality constraints, Ax~=B
B numeric vector containing the right-hand side of the (approximate) equality constraints
E numeric matrix containing the coefficients of the (exact) equality constraints, Ex=F
F numeric vector containing the right-hand side of the (exact) equality constraints
G numeric matrix containing the coefficients of the inequality constraints, Gx>=H
H numeric vector containing the right-hand side of the inequality constraints
sdB vector with standard deviation on B, used for weighing; default = equal weights
iter integer determining the number of iterations
outputlength
burninlength a number of extra iterations, performed at first, to "warm up" the algorithm.
type type of algorithm: one of: "mirror", (mirroring algorithm), "rda" (random directions algorithm) or "cda" (coordinates directions algorithm)
jmp jump length of the transformed variables q: x=x0+Zq (only if type=="mirror"); if jmp is NULL, a reasonable value is determined by xsample, depending on the size of the NULL space.
tol tolerance for equality and inequality constraints; numbers whose absolute value is smaller than tol are set to zero
x0 initial (particular) solution
fulloutput if TRUE, also outputs the transformed variables q
test if TRUE, xsample will test for hidden equalities (see details). This may be necessary for large problems, but slows down execution a bit

Details

The algorithm proceeds in two steps.

  • (1) the equality constraints Ex=F are eliminated, and the system Ex=f, Gx>=h is rewritten as G(p+Zq)>= h, i.e. containing only inequality constraints and where Z is a basis for the null space of E.
  • (2) the probability distribution of q is sampled numerically using a random walk (based on the Metropolis algorithm). There are three algorithms for selecting new samples: rda, cda (two hit-and-run algorithms) and a novel mirror algoithm.
  • In the rda algorithm first a random direction is selected, and the new sample obtained by uniformly sampling the line connecting the old sample and the intersection with the planes defined by the inequality constraints.
  • the cda algorithm is similar, except that the direction is chosen along one of the coordinate axes.
  • the mirror algorithm is unpublished; it uses the inequality constraints as "reflecting planes" along which jumps are reflected.
    In contrast to cda and rda, this algorithm also works with unbounded problems (i.e. for which some of the unknowns can attain Inf).

    For more information, see the package vignette vignette(xsample) or the file xsample.pdf in the packages ‘docs’ subdirectory.

    Raftery and Lewis (1996) suggest a minimum of 3000 iterations to reach the extremes

    If provided, then x0 should be a valid particular solution (i.e. E*x0=b and G*x0>=h), else the algorithm will fail.

    For larger problems, a central solution may be necessary as a starting point for the rda and cda algorithms. A good starting value is provided by the "central" value when running the function xranges with option central equal to TRUE.

    If the particular solution (x0) is not provided, then the parsimonious solution is sought, see ldei.
    For underdetermined systems, this may not be the most efficient way to start the algorithm. The parsimonious solution is usually located near the edges, and the rda and cda algorithms may not get out of this corner. The mirror algorithm is insensitive to that. Here it may be even better to start in a corner (as this position will always never be reached by random sampling).

    The algorithm will fail if there are hidden equalities. For instance, two inequalities may together impose an equality on an unknown, or, inequalities may impose equalities on a linear combination of two or more unknowns.

    In this case, the basis of the null space Z will be deficient. Therefore, xsample starts by checking if such hidden equalities exist. If it is suspected that this is NOT the case, set test to FALSE. This will speed up execution slightly.

    It is our experience that for small problems either the rda and cda algorithms are often more efficient. For really large problems, the mirror algorithm is usually much more efficient; select a jump length (jmp) that ensures good random coverage, while still keeping the number of reflections reasonable. If unsure about the size of jmp, the default will do.

    See E_coli for an example where a relatively large problem is sampled.

    Value

    a list containing:

    X matrix whose rows contain the sampled values of x
    acceptedratio ratio of acceptance (i.e. the ratio of the accepted runs / total iterations)
    Q only outputted if fulloutput is TRUE: the transformed samples Q
    p only outputted if fulloutput is TRUE: probability vector for all samples (e.g. one value for each row of X)
    jmp the jump length used for the random walk. Can be used to check the automated jump length

    Author(s)

    Karel Van den Meersche<k.vdMeersche@nioo.knaw.nl>
    Karline Soetaert <k.soetaert@nioo.knaw.nl>

    Examples

    # Sample the underdetermined Mink diet problem
    E <- rbind(Minkdiet$Prey,rep(1,7))
    F <- c(Minkdiet$Mink,1)
    
    pairs(xsample(E=E,F=F,G=diag(7),H=rep(0,7),iter=5000,output=1000,type="cda")$X,
         main="Minkdiet 1000 solutions, - cda")
    
    # sample the overdetermined Chemtax problem
    Nx     <-nrow(Chemtax$Ratio)
      
    # equations that have to be met exactly Ex=f: 
    # sum of all fraction must be equal to 1.
    E <- rep(1,Nx)
    F <- 1
      
    # inequalities, Gx>=h:
    # all fractions must be positive numbers
    G <- diag(nrow=Nx)
    H <- rep(0,Nx)
      
    # equations that must be reproduced as closely as possible, Ax ~ b
    # the field data; the input ratio matrix and field data are rescaled
    A     <- t(Chemtax$Ratio/rowSums(Chemtax$Ratio))
    B     <- Chemtax$Field/sum(Chemtax$Field)
      
    # Sample
    xs <- xsample(A=A,B=B,E=E,F=F,G=G,H=H,iter=2000,outputlen=1000)$X
    pairs(xs, main="Chemtax 1000 solutions - 2000 iterations")
    
         

    [Package limSolve version 1.3 Index]