-
Notifications
You must be signed in to change notification settings - Fork 13
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
Origin/reweighting #185
base: master
Are you sure you want to change the base?
Origin/reweighting #185
Changes from 61 commits
9932c84
b0ed038
9b67c8b
b678374
3dc4bfa
c1eb13c
754d287
4b26f9f
12f0654
add1d1a
32e577a
bed0b43
b88a01a
95e7ce7
7275044
c9aa2a5
a871557
6e6664d
ed4879c
b35557c
1240d30
191f0ba
430be22
08ef589
0bbd5a2
fc6e7dd
1cf72f3
17c3058
81db664
0dda8b2
548eb99
0fb2b42
f17f73a
2cf8814
0d5ab14
865dcaf
74b3f53
67ad0c1
25e64dc
284c9dd
dd99b7c
184f86e
bd71437
dbcbe71
567e108
d60a89e
f93326d
b3d9073
b226aa5
7e0d32d
4140e6e
6d6d7ae
4f9111e
d64dec0
266da59
603f177
a714c51
ea0b6e6
b43b5d3
aa378e9
71bbeb8
530a082
b32a9d5
54f47d9
0c3ece7
f36f656
c6af092
75367e6
60bdf87
c848a72
235d318
300b289
f49b156
95ca6dc
922457d
607a131
1785420
04b99ed
69b0f2a
35f7cac
5ef9a26
9553a55
540a1d2
039d134
710ae7b
2718d04
577becf
1ac22f2
27b27e8
673a119
04ad406
3fe0426
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ Maintainer: Carsten Urbach <[email protected]> | |
SystemRequirements: Gnu Scientific Library version >= 1.8 | ||
Description: Toolkit to extract hadronic quantities from Lattice QCD simulations. It contains functionality for IO, plotting, bootstrap and jackknife resampling, fitting, GEVP solving, error and autocorrelation estimation as well as other areas. | ||
Imports: | ||
dplyr, | ||
abind, | ||
boot, | ||
R6, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,8 @@ cf_meta <- function (.cf = cf(), nrObs = 1, Time = NA, nrStypes = 1, symmetrised | |
return (.cf) | ||
} | ||
|
||
#' Bootstrapped CF mixin constructor | ||
|
||
#' Bootstrapped CF mixin constructor | ||
#' | ||
#' @param .cf `cf` object to extend. | ||
#' @param boot.R Integer, number of bootstrap samples used. | ||
|
@@ -108,6 +109,7 @@ cf_boot <- function (.cf = cf(), boot.R, boot.l, seed, sim, cf.tsboot, icf.tsboo | |
return (.cf) | ||
} | ||
|
||
|
||
#' Estimates error from jackknife samples | ||
#' | ||
#' Currently this uses the mean over the jackknife samples in order to compute | ||
|
@@ -374,6 +376,78 @@ gen.block.array <- function(n, R, l, endcorr=TRUE) { | |
return(list(starts = st, lengths = lens)) | ||
} | ||
|
||
#' Computes the samples for reweighted correlation function | ||
#' | ||
#' @param cf `cf` object. | ||
#' @param rw `rw` object. | ||
#' @param boot.R Integer | ||
#' @param boot.l Integer | ||
#' @param seed Integer | ||
#' @param sim string | ||
#' @param endcorr boolean | ||
#' @export | ||
bootstrap_rw.cf <- function(cf, rw, boot.R=400, boot.l=2, seed=1234, sim="geom", endcorr=TRUE) { | ||
stopifnot(inherits(cf, 'cf_orig')) | ||
stopifnot(inherits(rw, 'rw_orig')) | ||
stopifnot(inherits(rw, 'rw_meta')) | ||
stopifnot(inherits(cf, 'cf_indexed')) | ||
|
||
|
||
##We should also check that the cf object and the rw object contains the same gauge configurations | ||
|
||
stopifnot(rw$conf.index == cf$conf.index) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be wrapped in an |
||
|
||
stopifnot( nrow(cf$cf) == length(rw$conf.index) ) | ||
|
||
boot.l <- ceiling(boot.l) | ||
boot.R <- floor(boot.R) | ||
|
||
stopifnot(boot.l >= 1) | ||
stopifnot(boot.l <= nrow(cf$cf)) | ||
stopifnot(boot.R >= 1) | ||
|
||
##Construct correlation function for the reweighting samples | ||
rw_cf <- cf | ||
rw_cf$cf <- replicate(ncol(cf$cf), rw$rw) | ||
|
||
## we set the seed for reproducability and correlation | ||
old_seed <- swap_seed(seed) | ||
|
||
## now we bootstrap the correlators*reweighting factor | ||
rwcf.tsboot <- boot::tsboot(cf$cf*rw_cf$cf, statistic = function(x){ return(apply(x, MARGIN=2L, FUN=mean))}, | ||
R = boot.R, l=boot.l, sim=sim, endcorr=endcorr) | ||
|
||
|
||
restore_seed(old_seed) | ||
|
||
## we set the seed for reproducability and correlation | ||
old_seed <- swap_seed(seed) | ||
|
||
## now we bootstrap the reweighting factor | ||
rw.tsboot <- boot::tsboot(rw_cf$cf, statistic = function(x){ return(apply(x, MARGIN=2L, FUN=mean))}, | ||
R = boot.R, l=boot.l, sim=sim, endcorr=endcorr) | ||
|
||
|
||
rwcf.tsboot$t0<- rwcf.tsboot$t0/rw.tsboot$t0 | ||
rwcf.tsboot$t <- rwcf.tsboot$t/rw.tsboot$t | ||
|
||
cf <- cf_boot(cf, | ||
boot.R = boot.R, | ||
boot.l = boot.l, | ||
seed = seed, | ||
sim = sim, | ||
cf.tsboot = rwcf.tsboot) | ||
|
||
class(cf) <- append(class(cf), 'cfrw_boot') | ||
|
||
class(cf) <- setdiff(class(cf), 'cf_orig') | ||
|
||
|
||
restore_seed(old_seed) | ||
|
||
return(invisible(cf)) | ||
} | ||
|
||
bootstrap.cf <- function(cf, boot.R=400, boot.l=2, seed=1234, sim="geom", endcorr=TRUE) { | ||
stopifnot(inherits(cf, 'cf_orig')) | ||
|
||
|
@@ -412,6 +486,7 @@ bootstrap.cf <- function(cf, boot.R=400, boot.l=2, seed=1234, sim="geom", endcor | |
return(invisible(cf)) | ||
} | ||
|
||
|
||
jackknife.cf <- function(cf, boot.l = 1) { | ||
stopifnot(inherits(cf, 'cf_orig')) | ||
|
||
|
@@ -469,8 +544,83 @@ jackknife.cf <- function(cf, boot.l = 1) { | |
resampling_method = 'jackknife') | ||
|
||
return (invisible(cf)) | ||
|
||
} | ||
|
||
#' Computes the jackknife samples for reweighted correlation function | ||
#' | ||
#' @param cf `cf` object. | ||
#' @param rw `rw` object. | ||
#' @param boot.l Integer | ||
#' @export | ||
jackknife_rw.cf <- function(cf, rw, boot.l = 1) { | ||
stopifnot(inherits(cf, 'cf_orig')) | ||
stopifnot(inherits(rw, 'rw_orig')) | ||
stopifnot(inherits(rw, 'rw_meta')) | ||
stopifnot(inherits(cf, 'cf_indexed')) | ||
|
||
stopifnot(rw1$conf.index == rw2$conf.index) | ||
|
||
##We should also check that the cf object and the rw object contains the same gauge configurations | ||
|
||
stopifnot( nrow(cf$cf) == length(rw$conf.index) ) | ||
|
||
|
||
stopifnot(boot.l >= 1) | ||
boot.l <- ceiling(boot.l) | ||
|
||
##Construct correlation function for the reweighting samples | ||
rw_cf <- cf | ||
rw_cf$cf <- replicate(ncol(cf$cf), rw$rw) | ||
|
||
|
||
## blocking with fixed block length, but overlapping blocks | ||
## number of observations | ||
n <- nrow(cf$cf) | ||
## number of overlapping blocks | ||
N <- n-boot.l+1 | ||
|
||
|
||
numerator <- apply(cf$cf*rw_cf$cf, 2, mean) | ||
denominator <- apply(rw_cf$cf, 2, mean) | ||
t0 <- numerator/denominator | ||
|
||
t <- array(NA, dim = c(N, ncol(cf$cf))) | ||
for (i in 1:N) { | ||
## The measurements that we are going to leave out. | ||
ii <- c(i:(i+boot.l-1)) | ||
## jackknife replications of the mean | ||
t[i, ] <- | ||
|
||
numerator <- apply(cf$cf[-ii, ]* rw_cf$cf[ ii, ], 2L, mean) | ||
denominator <- apply( rw_cf$cf[ ii, ] , 2L, mean ) | ||
t[i, ] < numerator/denominator | ||
} | ||
|
||
|
||
cf <- invalidate.samples.cf(cf) | ||
|
||
cf.tsboot <- list(t = t, | ||
t0 = t0, | ||
R = N, | ||
l = boot.l) | ||
|
||
|
||
cf <- cf_boot(cf, | ||
boot.R = cf.tsboot$R, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this now needs |
||
boot.l = cf.tsboot$l, | ||
seed = 0, | ||
sim = 'geom', | ||
cf.tsboot = cf.tsboot, | ||
resampling_method = 'jackknife') | ||
|
||
class(cf) <- append(class(cf), 'cfrw_boot') | ||
|
||
class(cf) <- setdiff(class(cf), 'cf_orig') | ||
|
||
return (invisible(cf)) | ||
} | ||
# Gamma method analysis on all time-slices in a 'cf' object | ||
#' uwerr.cf | ||
#' @description | ||
#' Gamma method analysis on all time-slices in a 'cf' object | ||
|
@@ -513,10 +663,31 @@ addConfIndex2cf <- function(cf, conf.index) { | |
if(is.null(cf$conf.index)) { | ||
cf$conf.index <- conf.index | ||
} | ||
class(cf) <- append(class(cf), 'cf_indexed') | ||
return(cf) | ||
} | ||
|
||
addStat.cf <- function(cf1, cf2) { | ||
#' Combine correlation function from different replicas | ||
#' | ||
#' @param cf1 `cf` object: correlation function for replicum A | ||
#' @param cf2 `cf` object: correlation function for replicum B | ||
#' @param reverse1 `boolean` After the bifurcation point one of | ||
#' the replicas (chain of correlation | ||
#' functions in simulation time) has | ||
#' to be reversed. | ||
#' @param reverse2 `boolean` | ||
#' | ||
#' @examples | ||
#' Suppose we have correlation functions in replicum A from 0 to 500 | ||
#' in steps of 4 and in replicum B from 4 to 500 in steps of 4. | ||
#' To combined the two replicas we have to use | ||
#' | ||
#' addstat.cf(cf_replicumB, cf_replicumA, TRUE, FALSE) | ||
#' which means | ||
#' combined=(cf500 from B, cf496 from B,...,cf004 from B, cf000 from A, .. | ||
#' cf500 from A) | ||
#' @export | ||
addStat.cf <- function(cf1, cf2,reverse1=FALSE, reverse2=FALSE) { | ||
stopifnot(inherits(cf1, 'cf')) | ||
stopifnot(inherits(cf2, 'cf')) | ||
|
||
|
@@ -530,15 +701,47 @@ addStat.cf <- function(cf1, cf2) { | |
stopifnot(inherits(cf1, 'cf_meta')) | ||
stopifnot(inherits(cf2, 'cf_meta')) | ||
|
||
##Either both should have an index or none of them | ||
stopifnot(inherits(cf1, 'cf_indexed') == inherits(cf1, 'cf_indexed') ) | ||
|
||
stopifnot(cf1$Time == cf2$Time) | ||
stopifnot(dim(cf1$cf)[2] == dim(cf2$cf)[2]) | ||
stopifnot(cf1$nrObs == cf2$nrObs ) | ||
stopifnot(cf1$nrStypes == cf2$nrStypes) | ||
|
||
cf <- cf1 | ||
|
||
cf$cf <- rbind(cf1$cf, cf2$cf) | ||
cf$icf <- rbind(cf1$icf, cf2$icf) | ||
cf1_temp<- cf1$cf | ||
icf1_temp <- cf1$icf | ||
if (reverse1 == TRUE){ | ||
apply(cf1_temp,2,rev) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this isn't assigned to anything? |
||
if ( has_icf(cf1)){ | ||
apply(icf1_temp,2,rev) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. none of these |
||
} | ||
} | ||
cf2_temp <- cf2$cf | ||
icf2_temp <- cf2$icf | ||
if (reverse2 == TRUE){ | ||
apply(cf2_temp,2,rev) | ||
if ( has_icf(cf2)){ | ||
apply(icf2_temp,2,rev) | ||
} | ||
} | ||
if (inherits(cf1, 'cf_indexed')){ | ||
conflist_temp1 <- cf1$conf.index | ||
if (reverse1 == TRUE){ | ||
conflist_temp1 <- rev(conflist_temp1) | ||
} | ||
conflist_temp2 <- cf2$conf.index | ||
if (reverse2 == TRUE){ | ||
conflist_temp2 <- rev(conflist_temp2) | ||
} | ||
cf$conf.index <- c(conflist_temp1,conflist_temp2) | ||
} | ||
|
||
|
||
cf$cf <- rbind(cf1_temp, cf2_temp) | ||
cf$icf <- rbind(icf1_temp, icf2_temp) | ||
|
||
cf <- invalidate.samples.cf(cf) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be okay to make this an optional dependency as reweighting is a bit of an edge-case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now included a stop condition, when dplyr is not available. That was also done in cyprus_readutils
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still, it would be nice if dplyr were an optional dependency at this stage