|
| 1 | +#' Header for spatstat.sparse/tests/*R |
| 2 | +#' |
| 3 | + |
| 4 | +require(spatstat.sparse) |
| 5 | +ALWAYS <- FULLTEST <- TRUE |
| 6 | +#' tests/sparsemarkov.R |
| 7 | +#' Tests of code for Markov chain with sparse transition matrix |
| 8 | +#' $Revision: 1.1 $ $Date: 2026/04/11 04:29:47 $ |
| 9 | + |
| 10 | +if(!exists("ALWAYS")) ALWAYS <- TRUE |
| 11 | +if(!exists("FULLTEST")) FULLTEST <- ALWAYS |
| 12 | + |
| 13 | +if(FULLTEST) { |
| 14 | + local({ |
| 15 | + testit <- function(P, Pname, xstart=50, ns=10, np=3, walkies=TRUE) { |
| 16 | + cat(paste(">>>>>> ", Pname, " <<<<<<<\n")) |
| 17 | + cat("\tsingle particle, final state..\n") |
| 18 | + X1 <- runSparseMarkovChain(P, x0=xstart, nsteps=ns, result="l") |
| 19 | + cat("\tsingle particle, history..\n") |
| 20 | + X2 <- runSparseMarkovChain(P, x0=xstart, nsteps=ns, result="h") |
| 21 | + cat("\tseveral particles, final states..\n") |
| 22 | + X3 <- runSparseMarkovChain(P, x0=rep(xstart, np), nsteps=ns, |
| 23 | + result="l") |
| 24 | + cat("\tseveral particles, histories..\n") |
| 25 | + X4 <- runSparseMarkovChain(P, x0=rep(xstart, np), nsteps=ns, |
| 26 | + result="h") |
| 27 | + if(length(X1) != 1) |
| 28 | + stop(paste(Pname, "end state was not a single particle")) |
| 29 | + if(abs(X1 - xstart) > ns) |
| 30 | + stop(paste(Pname, "wandered impossibly far")) |
| 31 | + if(length(X2) != (ns + 1)) |
| 32 | + stop(paste(Pname, "history has wrong length")) |
| 33 | + if(walkies && !all(abs(diff(X2)) == 1)) |
| 34 | + stop(paste(Pname, "jumps were not all +- 1")) |
| 35 | + if(length(X3) != np) |
| 36 | + stop(paste(Pname, "final number of particles was not preserved")) |
| 37 | + if(max(abs(X3 - xstart)) > ns) |
| 38 | + stop(paste(Pname, "some particles wandered impossibly far")) |
| 39 | + if(!is.matrix(X4)) |
| 40 | + stop(paste(Pname, "a matrix was expected for the history")) |
| 41 | + if(nrow(X4) != np) |
| 42 | + stop(paste(Pname, "number of particles was not preserved")) |
| 43 | + if(ncol(X4) != (ns + 1)) |
| 44 | + stop(paste(Pname, "histories have wrong length")) |
| 45 | + if(walkies && !all(abs(apply(X4, 1, diff)) == 1)) |
| 46 | + stop(paste(Pname, "jumps were not all +- 1")) |
| 47 | + cat("OK\n") |
| 48 | + return(invisible(list(X1=X1, X2=X2, X3=X3, X4=X4))) |
| 49 | + } |
| 50 | + |
| 51 | + #' Simple random walk |
| 52 | + Pwalk <- matrix(0, 100, 100) |
| 53 | + Pwalk[abs(row(Pwalk) - col(Pwalk)) == 1] <- 1 |
| 54 | + Pwalk <- Pwalk/rowSums(Pwalk) |
| 55 | + testit(Pwalk, "Simple random walk") |
| 56 | + |
| 57 | + #' Absorbing |
| 58 | + Pabsorb <- matrix(0, 10, 10) |
| 59 | + Pabsorb[row(Pabsorb) < col(Pabsorb)] <- 1 |
| 60 | + Pabsorb[10,10] <- 1 |
| 61 | + Pabsorb <- Pabsorb/rowSums(Pabsorb) |
| 62 | + a <- testit(Pabsorb, "Absorbing chain", xstart=1, ns=10, walkies=FALSE) |
| 63 | + if(a$X1 != 10) stop("Absorbing chain didn't absorb") |
| 64 | + if(any(a$X3 != 10)) stop("Absorbing chains didn't absorb") |
| 65 | + } |
| 66 | +)} |
0 commit comments