!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ! reblock6.f90 31/12/2004 ! ! In-situ binary reblocking algorithm for error estimation (Flyvbjerg) ! demonstrated for mock Markov chain Monte Carlo simulation. ! USAGE: ! gmake Reblock3; Reblock3 ! x energy.out ! x -settype xydy error.out ! !&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& !&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ! Mathematical constants and functions which might be useful elsewhere !&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& MODULE maths IMPLICIT NONE INTEGER, PARAMETER :: DP=KIND(1.0d0) REAL(DP), PARAMETER :: PI=3.14159265358979323846_DP END MODULE maths !&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ! Binary ReBlocking "class" ! As data comes in, we arrange it along a row ... leaf nodes. ! Whenever there are two root nodes at the same level l, ! we merge them into a root node at level (l+1). ! Example: Consider N=11. Suppose x(n)=n. The nodes are labelled with their ! weights. There are three separate heaps: ! ! l=3 28 ! / \ ! / \ ! / \ ! / \ ! / \ ! l=2 6 22 ! / \ / \ ! / \ / \ ! l=1 1 5 9 13 17 ! / \ / \ / \ / \ / \ ! l=0 0 1 2 3 4 5 6 7 8 9 10 ! ! When node 11 is added, N changes from 11 to 12, i.e., from 1011b to 1100b. ! We then compute 10+11=21 and 17+21=38 and add them: ! ! l=3 28 ! / \ ! / \ ! / \ ! / \ ! / \ ! l=2 6 22 38 ! / \ / \ / \ ! / \ / \ / \ ! l=1 1 5 9 13 17 21 ! / \ / \ / \ / \ / \ / \ ! l=0 0 1 2 3 4 5 6 7 8 9 10 11 ! ! For the binary reblocking application it is not necessary to maintain all the nodes. ! Instead, all one requires is: ! (a) the weight of the root node of each distinct heap. ! In the case N=11, we have a({0,1,2,3}) = {10, 17, 0, 28}. ! (b) the sums of the squares of the weights of all nodes generated so far ! at each level l. ! In the case N=11, we have ! b(0) = 0**2 + 1**2 + ... + 10**2 ! b(1) = 1**2 + 5**2 + ... + 17**2 ! b(2) = 6**2 + 22**2 ! b(3) = 28**2 ! !------------------------------------------------------------------------------ ! In the MODULE brb, Lmax is the maximum height of the heap (that we plan for). ! Let ! N = number of data points ! L = block level (must be <= Lmax) ! S = block size = 2**l. ! M = number of blocks=FLOOR(N/S), ! a(l) = if there is a separate l-l heap, a_l is its root weight; ! otherwise undefined (say zero) ! c(l) = sum of weights of all l-l nodes (not used) ! b(l) = sum of squares of weights of all l-l nodes ! ! Data collection (heap-building) stage: recursive version of algorithm ! MAIN PROGRAM: add(x,n,l=0) ! SUBROUTINE add(x,n,l=0) ! b(l) += x**2 ! IF (n is odd) add(a(l)+x, n/2, l+1); ELSE; a(l)=x; ENDIF ! END SUBROUTINE ! ! Output stage: ! SoB = sum of all complete blocks of size S ! = sum of weights of all existing heaps of level l or higher ! MoBM = mean of all complete block means of size S ! = SoB/S/M ! MoSoBM = Mean of Squares of Block Means ! = buf%b(:,l)/S**2/M ! VoBM = Variance of Block Means ! = (MoSoBM - (MoBM)**2) *M/(M-1) (note correction factor) ! VoMoBM = Variance of Mean of Block Means = VoBM/M ! EoMoBM = Error of Mean of Block Means = SQRT(VoMoBM) ! EoEoMoBM = Error of Error of Mean of Block Means = EoMoBM/SQRT(2(M-1)) ! ! Example: N=11=1101b , L=1, S=2, M=5 ! a(0) = x12 ! a(1) = x8+x9 ! a(2) = ? ! a(3) = x0+x1+x2+x3+x4+x5+x6+x7 ! a(4) = undef ! etc. ! b(0) = x0**2 + x1**2 + ... + x12**2 ! b(1) = (x0+x1)**2 + ... + (x10+x11)**2 ! b(2) = (x0+x1+x2+x3)**2 + (x4+x5+x6+7)**2 ! b(3) = (x0+x1+x2+x3+x4+x5+x6+x7)**2 ! ! SoB(3) = a(3) = (x0+x1+x2+x3+x4+x5+x6+x7) ! SoB(2) = a(3) = (x0+x1+x2+x3)+(x4+x5+x6+x7) ! SoB(1) = a(3)+a(1) = (x0+x1)+(x2+x3)+(x4+x5)+(x6+x7)+(x8+x9) ! SoB(0) = a(3)+a(1)+a(0) = x0+x1+x2+x3+x4+x5+x6+x7+x8+x9+x10 ! ! ( x0+x1+x2+x3 x4+x5+x6+x7 ) ! VoBM(2) = (-------------**2 + -------------**2 ) ! ( 4 4 ) ! ------------------------------------------ ! 2 ! ! ( x0+x1+x2+x3 x4+x5+x6+x7 ) ! ( ------------- + ------------- ) ! ( 4 4 ) ! - (------------------------------- )**2 ! ( 2 ) ! ! MULTIPLIED BY THE CORRECTION FACTOR 2/(2-1) ! ! Ultimately we are interested in the mean of all values, MoB(l=0), and ! the error in this mean, EoMoBM, at a suitable value of l. ! The error in the error is only used to determine the autocorrelation "time. ! ! The variance at level l is calculated using complete blocks only; ! for l