Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize uwerrcf #316

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions R/UWerr.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' @param pl logical: if TRUE, the autocorrelation function, the integrated
#' autocorrelation time as function of the integration cut-off and (for primary
#' quantities) the time history of the observable are plotted with plot.uwerr
#' @param main character, title of the plots
#' @param ... arguments passed to function \code{f}.
#' @return In case of a primary observable (\code{uwerrprimary}), an object of
#' class \code{uwerr} with basis class \code{\link{list}} containing the
Expand Down Expand Up @@ -60,7 +61,7 @@
#' plot(plaq.res)
#'
#' @export uwerr
uwerr <- function(f, data, nrep, S=1.5, pl=FALSE, ...) {
uwerr <- function(f, data, nrep, S=1.5, pl=FALSE, main="", ...) {
# f: scalar function handle, needed for derived quantities
# data: the matrix of data with dim. (Nalpha x N)
# N = total number of measurements
Expand All @@ -71,18 +72,18 @@ uwerr <- function(f, data, nrep, S=1.5, pl=FALSE, ...) {
if(missing(nrep)) {
nrep <- c(length(data))
}
return(invisible(uwerrprimary(data=data, nrep=nrep, S=S, pl=pl)))
return(invisible(uwerrprimary(data=data, nrep=nrep, S=S, pl=pl, main=main)))
}
else {
if(missing(nrep)) {
nrep <- c(length(data[1,]))
}
return(invisible(uwerrderived(f=f, data=data, nrep=nrep, S=S, pl=pl, ...)))
return(invisible(uwerrderived(f=f, data=data, nrep=nrep, S=S, pl=pl, main=main, ...)))
}
}

#' @export
uwerrprimary <- function(data, nrep, S=1.5, pl=FALSE) {
uwerrprimary <- function(data, nrep, S=1.5, pl=FALSE, main="") {

N = length(data)
if(missing(nrep)) {
Expand Down Expand Up @@ -202,14 +203,14 @@ uwerrprimary <- function(data, nrep, S=1.5, pl=FALSE) {
attr(res, "class") <- c("uwerr", "list")

if(pl) {
plot(res)
plot(res, main=main)
}

return(invisible(res))
}

#' @export
uwerrderived <- function(f, data, nrep, S=1.5, pl=FALSE, ...) {
uwerrderived <- function(f, data, nrep, S=1.5, pl=FALSE, main="", ...) {
Nalpha <- dim(data)[2]
N <- dim(data)[1]
if(missing(nrep)) {
Expand Down Expand Up @@ -392,7 +393,7 @@ uwerrderived <- function(f, data, nrep, S=1.5, pl=FALSE, ...) {
attr(res, "class") <- c("uwerr", "list")

if(pl) {
plot(res)
plot(res, main=main)
}

options(error = NULL)
Expand Down
21 changes: 14 additions & 7 deletions R/cf.R
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ jackknife.cf <- function(cf, boot.l = 1) {
#' Gamma method analysis on all time-slices in a 'cf' object
#'
#' @param cf Object of type `cf` containing `cf_orig`
#' @param S initial guess for the ratio tau/tauint, with tau the exponetial
#' autocorrelation length.
#' @param pl logical: if TRUE, the autocorrelation function, the integrated
#' autocorrelation time as function of the integration cut-off and (for primary
#' quantities) the time history of the observable are plotted with plot.uwerr
#' @param main character, title of the plots
#'
#' @return A list with a named element `uwcf` which contains a data frame
#' with six columns, `value`, `dvalue`, `ddvalue`, `tauint`, `dtauint`
Expand All @@ -690,31 +696,32 @@ jackknife.cf <- function(cf, boot.l = 1) {
#' uwerr.cf(samplecf)
#'
#' @export
uwerr.cf <- function(cf){
uwerr.cf <- function(cf, S=1.5, pl=FALSE, main=""){
stopifnot(inherits(cf, 'cf_orig'))

uw_wrapper <- function(x){
uw_tmp <- try(uwerrprimary(data=x), silent=TRUE)
uw_wrapper <- function(x, S, pl, main){
uw_tmp <- try(uwerrprimary(data=x, S=S, pl=pl, main=main), silent=TRUE)
if( any(class(uw_tmp) == "try-error") ){
c(value=NA, dvalue=NA, ddvalue=NA, tauint=NA, dtauint=NA)
c(value=NA, dvalue=NA, ddvalue=NA, tauint=NA, dtauint=NA, S=S)
} else {
c(value=uw_tmp$value, dvalue=uw_tmp$dvalue, ddvalue=uw_tmp$ddvalue,
tauint=uw_tmp$tauint, dtauint=uw_tmp$dtauint)
tauint=uw_tmp$tauint, dtauint=uw_tmp$dtauint, S=S)
}
}

res <- list()
res[["uwcf"]] <- cbind(as.data.frame(t(apply(X=cf$cf, MARGIN=2L, FUN=uw_wrapper))),
res[["uwcf"]] <- cbind(as.data.frame(t(apply(X=cf$cf, MARGIN=2L, FUN=uw_wrapper, S=S, pl=pl, main=main))),
t=(1:ncol(cf$cf)))
if( has_icf(cf) ){
res[["uwicf"]] <- cbind(as.data.frame(t(apply(X=cf$icf, MARGIN=2L, FUN=uw_wrapper))),
res[["uwicf"]] <- cbind(as.data.frame(t(apply(X=cf$icf, MARGIN=2L, FUN=uw_wrapper, S=S, pl=pl, main=main))),
t=(1:ncol(cf$icf)))
}
return(res)
}




#' add a configuration index to an \code{cf} object
#'
#' add a configuration number index to \code{cf} object.
Expand Down