├── .gitignore ├── c++ ├── blocker ├── Makefile ├── main.cpp ├── blocker.h └── blocker.cpp ├── armadillo ├── Makefile ├── main.cpp ├── blocker.h └── blocker.cpp ├── julia ├── block_benchmark.jl └── block.jl ├── benchmark ├── R.txt ├── julia.txt ├── c++.txt ├── python.txt └── armadillo.txt ├── block.ris ├── block.bib ├── R └── tinker.bell.R ├── README.md └── python └── tictoc.py /.gitignore: -------------------------------------------------------------------------------- 1 | **/resources/test/ 2 | **/blockbatch/ 3 | -------------------------------------------------------------------------------- /c++/blocker: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/computative/block/HEAD/c++/blocker -------------------------------------------------------------------------------- /c++/Makefile: -------------------------------------------------------------------------------- 1 | output: main.cpp blocker.cpp blocker.h 2 | g++ -o blocker main.cpp blocker.cpp 3 | clean: 4 | rm blocker 5 | -------------------------------------------------------------------------------- /armadillo/Makefile: -------------------------------------------------------------------------------- 1 | output: main.cpp blocker.cpp blocker.h 2 | g++ -o blocker main.cpp blocker.cpp 3 | clean: 4 | rm blocker 5 | -------------------------------------------------------------------------------- /julia/block_benchmark.jl: -------------------------------------------------------------------------------- 1 | include("block.jl") 2 | 3 | using DelimitedFiles: readdlm 4 | x = readdlm("../resources/data.txt") 5 | print(block(x)) -------------------------------------------------------------------------------- /benchmark/R.txt: -------------------------------------------------------------------------------- 1 | Running 2 | 3 | $ Rscript tinker.bell.R 4 | 5 | in the /peter.pan/R directory will print the variance of the mean of the data in /peter.pan/resources/data.txt: 6 | 7 | [1] 1.542889e-05 8 | -------------------------------------------------------------------------------- /benchmark/julia.txt: -------------------------------------------------------------------------------- 1 | Running 2 | 3 | $ julia block_benchmark.jl 4 | 5 | in the /block/julia directory will print the variance of the mean of the data in /block/resources/data.txt: 6 | 7 | 1.5428892359209023e-5 -------------------------------------------------------------------------------- /benchmark/c++.txt: -------------------------------------------------------------------------------- 1 | In the ../c++/-directory, execute 2 | 3 | $ make 4 | $ ./blocker 5 | Expected value = 2.97804 (with mean sq. err. = 1.54289e-05) 6 | Standard error = 0.00392796 (with mean sq. err. = 7.38097e-12) -------------------------------------------------------------------------------- /benchmark/python.txt: -------------------------------------------------------------------------------- 1 | Running 2 | 3 | $ python captain.hook.py 4 | 5 | in the /peter.pan/python directory will print the variance of the mean of the data in /peter.pan/resources/data.txt: 6 | 7 | 1.54288923592e-05 8 | -------------------------------------------------------------------------------- /benchmark/armadillo.txt: -------------------------------------------------------------------------------- 1 | In the ../armadillo/-directory, execute 2 | 3 | $ make 4 | $ ./blocker 5 | Expected value = 2.97804 (with mean sq. err. = 1.54289e-05) 6 | Standard error = 0.00392796 (with mean sq. err. = 7.38097e-12) -------------------------------------------------------------------------------- /block.ris: -------------------------------------------------------------------------------- 1 | TY - JOUR 2 | TI - Standard error estimation by an automated blocking method 3 | AU - Jonsson, Marius 4 | T2 - Physical Review E 5 | DA - 2018/10// 6 | PY - 2018 7 | DO - 10.1103/PhysRevE.98.043304 8 | VL - 98 9 | IS - 4 10 | SP - 043304 11 | ER - 12 | 13 | -------------------------------------------------------------------------------- /block.bib: -------------------------------------------------------------------------------- 1 | 2 | @article{PhysRevE.98.043304, 3 | title = {Standard error estimation by an automated blocking method}, 4 | volume = {98}, 5 | doi = {10.1103/PhysRevE.98.043304}, 6 | number = {4}, 7 | journal = {Physical Review E}, 8 | author = {Jonsson, Marius}, 9 | month = oct, 10 | year = {2018}, 11 | note = {Number of pages: 17 12 | Publisher: American Physical Society}, 13 | pages = {043304}, 14 | } 15 | -------------------------------------------------------------------------------- /R/tinker.bell.R: -------------------------------------------------------------------------------- 1 | data = read.table("../resources/data.txt") 2 | 3 | x = data$V1 4 | nk = length(x) 5 | d = log2(nk) 6 | mu = mean(x) 7 | s = matrix(0,d) 8 | gamma = rep(0,d) 9 | for (i in 0:(d-1) ) { 10 | nk = length(x) 11 | X = x - mu 12 | gamma[i+1] = sum( X[1:(nk-1)]*X[2:nk] )/(nk-1) 13 | s[i+1] = sum( X^2 )/nk 14 | x = 0.5*(x[2*(1:(nk/2))-1] + x[2*(1:(nk/2))]) 15 | } 16 | s[d] = s[d-1] 17 | ind = which.max(as.integer( rev( cumsum( rev( (gamma/s)^2*2^(d-0:(d-1) ) ) ) ) < qchisq(0.99, d:1) ) ) 18 | s[ind]/2^(d-(ind-1)) 19 | -------------------------------------------------------------------------------- /armadillo/main.cpp: -------------------------------------------------------------------------------- 1 | #include "blocker.h" 2 | 3 | int main(int, char *[]) 4 | { 5 | arma::vec x; 6 | x.load("../resources/data.txt"); 7 | 8 | // Constructor accepts a vector 9 | // This vector must have a size which is a power of 2. 10 | Blocker block(x); 11 | 12 | // the public variables mean, mse_mean, stdErr and mse_stdErr are output 13 | printf("Expected value = %g (with mean sq. err. = %g) \n", block.mean, block.mse_mean); 14 | printf("Standard error = %g (with mean sq. err. = %g) \n", block.stdErr, block.mse_stdErr); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /c++/main.cpp: -------------------------------------------------------------------------------- 1 | #include "blocker.h" 2 | #include 3 | #include 4 | 5 | int main(int, char *[]) 6 | { 7 | std::ifstream infile("../resources/test/data1.txt"); 8 | std::vector x; 9 | std::string line; 10 | while(std::getline(infile,line)) x.push_back(strtod(line.c_str(),nullptr) ); 11 | 12 | // Constructor accepts a vector 13 | // This vector must have a size which is a power of 2. 14 | Blocker block(x); 15 | 16 | // the public variables mean, mse_mean, stdErr and mse_stdErr are output 17 | printf("Expected value = %g (with mean sq. err. = %g) \n", block.mean, block.mse_mean); 18 | printf("Standard error = %g (with mean sq. err. = %g) \n", block.stdErr, block.mse_stdErr); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Standard error estimation by an automated blocking method 2 | This is the GitHub-repository for a so-called 'blocking method', which can estimate expected values and the associated error for ultra-large data series. The key property of the method, is that this is achieved in O(n)-time. The mathematical description of the method is contained in an article with DOI: 10.1103/PhysRevE.98.043304 3 | 4 | ### How to cite 5 | 6 | - Jonsson, M. (2018). *Standard error estimation by an automated blocking method*. Physical Review E, 98(4), 043304. 7 | - [RIS](https://github.com/computative/block/blob/master/block.ris) 8 | - [BibTeX](https://github.com/computative/block/blob/master/block.bib) 9 | 10 | ### About 11 | 12 | In this repository, there are implementations for: 13 | - Python 14 | - R 15 | - C++ 16 | - Armadillo 17 | - Julia 18 | 19 | After pulling a copy of the repository to your work space, you can inspect the code for the language of interest to you in directories of the same name. Benchmarks for the language of your choice are in the benchmark directory. You can contact me with questions at mj539@cam.ac.uk. As you can see, with thanks to collegues in Oslo, Karl Henrik, there's julia support now. If you like it, please star, tell your friends and cite the paper: 20 | 21 | Jonsson, M. (2018). *Standard error estimation by an automated blocking method*. Physical Review E, 98(4), 043304. 22 | -------------------------------------------------------------------------------- /armadillo/blocker.h: -------------------------------------------------------------------------------- 1 | #ifndef BLOCKER_H 2 | #define BLOCKER_H 3 | 4 | #include 5 | 6 | class Blocker 7 | { 8 | public: 9 | unsigned long long nK; 10 | // variables that hold the results 11 | double stdErr, mean, mse_stdErr, mse_mean; 12 | // a vector of magic numbers 13 | arma::vec quantile { 14 | 3.841459, 5.991465, 7.814728, 9.487729, 11.070498, 12.591587, 14.067140, 15.507313, 15 | 16.918978, 18.307038, 19.675138, 21.026070, 22.362032, 23.684791, 24.995790, 26.296228, 16 | 27.587112, 28.869299, 30.143527, 31.410433, 32.670573, 33.924438, 35.172462, 36.415029, 17 | 37.652484, 38.885139, 40.113272, 41.337138, 42.556968, 43.772972, 44.985343, 46.194260, 18 | 47.399884, 48.602367, 49.801850, 50.998460, 52.192320, 53.383541, 54.572228, 55.758479, 19 | 56.942387, 58.124038, 59.303512, 60.480887, 61.656233, 62.829620, 64.001112, 65.170769, 20 | 66.338649, 67.504807, 68.669294, 69.832160, 70.993453, 72.153216, 73.311493, 74.468324, 21 | 75.623748, 76.777803, 77.930524, 79.081944, 80.232098, 81.381015, 82.528727, 83.675261}; 22 | 23 | Blocker(arma::vec& x); 24 | 25 | void setData(arma::vec& x); 26 | 27 | // checks that integer n is a power of 2 28 | bool isPowerOfTwo(unsigned long long n); 29 | 30 | private: 31 | 32 | // the algorithm which computes the variance of the sample mean. 33 | double estimate(arma::vec& x, unsigned long long& n); 34 | 35 | // performs blocking transformation 36 | void transform(arma::vec& X, arma::vec& x, unsigned long long& n); 37 | 38 | // estimates gamma_h(1) for all h 39 | double gamma1(arma::vec& X, unsigned long long& n); 40 | 41 | // estimates gamma_h(0) for all h 42 | double var(arma::vec& X, unsigned long long& n); 43 | }; 44 | 45 | #endif // BLOCKER_H 46 | -------------------------------------------------------------------------------- /python/tictoc.py: -------------------------------------------------------------------------------- 1 | from numpy import log2, zeros, mean, var, sum, loadtxt, arange, \ 2 | array, cumsum, dot, transpose, diagonal, floor 3 | from numpy.linalg import inv 4 | 5 | def block(x): 6 | # preliminaries 7 | d = log2(len(x)) 8 | if (d - floor(d) != 0): 9 | print("Warning: Data size = %g, is not a power of 2." % floor(2**d)) 10 | print("Truncating data to %g." % 2**floor(d) ) 11 | x = x[:2**int(floor(d))] 12 | d = int(floor(d)) 13 | n = 2**d 14 | s, gamma = zeros(d), zeros(d) 15 | mu = mean(x) 16 | 17 | # estimate the auto-covariance and variances 18 | # for each blocking transformation 19 | for i in arange(0,d): 20 | n = len(x) 21 | # estimate autocovariance of x 22 | gamma[i] = (n)**(-1)*sum( (x[0:(n-1)]-mu)*(x[1:n]-mu) ) 23 | # estimate variance of x 24 | s[i] = var(x) 25 | # perform blocking transformation 26 | x = 0.5*(x[0::2] + x[1::2]) 27 | 28 | # generate the test observator M_k from the theorem 29 | M = (cumsum( ((gamma/s)**2*2**arange(1,d+1)[::-1])[::-1] ) )[::-1] 30 | 31 | # we need a list of magic numbers 32 | q =array([6.634897, 9.210340, 11.344867, 13.276704, 15.086272, 33 | 16.811894, 18.475307, 20.090235, 21.665994, 23.209251, 34 | 24.724970, 26.216967, 27.688250, 29.141238, 30.577914, 35 | 31.999927, 33.408664, 34.805306, 36.190869, 37.566235, 36 | 38.932173, 40.289360, 41.638398, 42.979820, 44.314105, 37 | 45.641683, 46.962942, 48.278236, 49.587884, 50.892181]) 38 | 39 | # use magic to determine when we should have stopped blocking 40 | for k in arange(0,d): 41 | if(M[k] < q[k]): 42 | break 43 | if (k >= d-1): 44 | print("Warning: Use more data") 45 | return s[k]/2**(d-k) 46 | 47 | 48 | x = loadtxt("../resources/data.txt") 49 | print("Standard error = %g" % block(x)**0.5) 50 | -------------------------------------------------------------------------------- /c++/blocker.h: -------------------------------------------------------------------------------- 1 | #ifndef BLOCKER_H 2 | #define BLOCKER_H 3 | 4 | #include 5 | #include 6 | 7 | 8 | class Blocker 9 | { 10 | public: 11 | unsigned long long nK; 12 | // variables that hold the results 13 | double stdErr, mean, mse_stdErr, mse_mean; 14 | // a vector of magic numbers 15 | std::vector quantile { 16 | 3.841459, 5.991465, 7.814728, 9.487729, 11.070498, 12.591587, 14.067140, 15.507313, 17 | 16.918978, 18.307038, 19.675138, 21.026070, 22.362032, 23.684791, 24.995790, 26.296228, 18 | 27.587112, 28.869299, 30.143527, 31.410433, 32.670573, 33.924438, 35.172462, 36.415029, 19 | 37.652484, 38.885139, 40.113272, 41.337138, 42.556968, 43.772972, 44.985343, 46.194260, 20 | 47.399884, 48.602367, 49.801850, 50.998460, 52.192320, 53.383541, 54.572228, 55.758479, 21 | 56.942387, 58.124038, 59.303512, 60.480887, 61.656233, 62.829620, 64.001112, 65.170769, 22 | 66.338649, 67.504807, 68.669294, 69.832160, 70.993453, 72.153216, 73.311493, 74.468324, 23 | 75.623748, 76.777803, 77.930524, 79.081944, 80.232098, 81.381015, 82.528727, 83.675261}; 24 | 25 | Blocker(std::vector& x); 26 | 27 | void setData(std::vector& x); 28 | 29 | // checks that integer n is a power of 2 30 | bool isPowerOfTwo(unsigned long long n); 31 | 32 | private: 33 | 34 | // the algorithm which computes the variance of the sample mean. 35 | double estimate(std::vector& x, unsigned long long& n); 36 | 37 | // performs blocking transformation 38 | void transform(std::vector& X, std::vector& x, unsigned long long& n); 39 | 40 | // estimates gamma_h(1) for all h 41 | double gamma1(std::vector& X, unsigned long long& n); 42 | 43 | // estimates gamma_h(0) for all h 44 | double var(std::vector& X, unsigned long long& n); 45 | 46 | std::vector cumsum(std::vector M); 47 | 48 | std::vector flip(std::vector M); 49 | 50 | double avg(std::vector& x); 51 | }; 52 | 53 | #endif // BLOCKER_H 54 | -------------------------------------------------------------------------------- /julia/block.jl: -------------------------------------------------------------------------------- 1 | import Statistics; 2 | # Returns an estimate for the sample variance of the mean which corrects for correlation in the data 3 | function block(x) 4 | n = length(x) 5 | d = log2(n) 6 | if d%1 != 0 7 | d = Int(floor(d)) 8 | println("Length of data = $(n) is not a power of 2, truncating to $(Int(round((2^d),digits=0))) elements") 9 | n = 2^d 10 | x = x[1:n] 11 | else 12 | d = Int(d) 13 | end 14 | 15 | s, gamma = zeros(d), zeros(d) 16 | mu = Statistics.mean(x) 17 | 18 | # Estimate the auto-covariance and variances for each blocking transformation 19 | for i in 1:d 20 | # n changes in length 21 | n = length(x) 22 | 23 | # Estimate autocovariance of x 24 | gamma[i] = (1 / n) * sum((x[1:n-1] .- mu) .* (x[2:n] .- mu)) 25 | 26 | # Estimate variance of x 27 | s[i] = Statistics.var(x, corrected=false) 28 | 29 | # Blocking transformation 30 | x_1 = x[1:2:end] # Extracting all numbers at odd positions 31 | x_2 = x[2:2:end] # Numbers at even positions 32 | 33 | x = 0.5 .* (x_1 .+ x_2) 34 | end 35 | 36 | # Test observator from theorem (chi^2-distributed) 37 | factor_1 = (gamma ./ s).^2 38 | factor_2 = 2 .^[i for i in 1:d] 39 | 40 | M = (cumsum( ( factor_1 .* factor_2[end:-1:1] )[end:-1:1]))[end:-1:1] 41 | 42 | # Test percentiles 43 | q = [6.634897, 9.210340, 11.344867, 13.276704, 15.086272, 44 | 16.811894, 18.475307, 20.090235, 21.665994, 23.209251, 45 | 24.724970, 26.216967, 27.688250, 29.141238, 30.577914, 46 | 31.999927, 33.408664, 34.805306, 36.190869, 37.566235, 47 | 38.932173, 40.289360, 41.638398, 42.979820, 44.314105, 48 | 45.641683, 46.962942, 48.278236, 49.587884, 50.892181] 49 | 50 | # The actual Chi squared test - should we have stopped blocking? 51 | k_end = 1 52 | for k in 1:d 53 | k_end = k 54 | if (M[k] < q[k]) 55 | break 56 | end 57 | end 58 | if (k_end >= d) 59 | print("More data is needed!") 60 | end 61 | result = s[k_end] / 2^(d-(k_end-1)) 62 | 63 | return result 64 | end -------------------------------------------------------------------------------- /armadillo/blocker.cpp: -------------------------------------------------------------------------------- 1 | #include "blocker.h" 2 | 3 | Blocker::Blocker(arma::vec& x) 4 | { 5 | setData(x); 6 | } 7 | 8 | void Blocker::setData(arma::vec& x) 9 | { 10 | unsigned long long n = arma::size(x).n_rows; 11 | // checks if size of input vector is a power of two 12 | if ( !isPowerOfTwo(n) ) 13 | { 14 | // Error 15 | stdErr = mean = mse_stdErr = mse_mean = -1.0f; 16 | nK = 0; 17 | printf( "Error: Data size must be a power of two." ); 18 | } 19 | else { 20 | // success. Proceeds to compute results 21 | mean = arma::mean(x); 22 | mse_mean = 0 + estimate(x, n); 23 | stdErr = sqrt(mse_mean); 24 | mse_stdErr = (2*nK-1)*mse_mean*mse_mean/(nK*nK); 25 | } 26 | } 27 | 28 | // checks that integer n is a power of 2 29 | bool Blocker::isPowerOfTwo(unsigned long long n) 30 | { 31 | return (n>0 && ((n & (n-1)) == 0)); 32 | } 33 | 34 | // the algorithm which computes the variance of the sample mean. 35 | double Blocker::estimate(arma::vec& x, unsigned long long& n) 36 | { 37 | // since n is a power of two, d is an integer. 38 | int d = log2(n); 39 | arma::vec X = x - mean; 40 | arma::vec s = arma::zeros(d); 41 | arma::vec gamma = arma::zeros(d); 42 | // computes covariance and variance and applies blocking transform 43 | 44 | // Constructor accepts a vector 45 | // This vector must have a size which is a power of 2. 46 | for (int k = 0; k <= d-1; k++) { 47 | gamma[k] = this->gamma1(X, n); 48 | s[k] = this->var(X, n); 49 | this->transform(X, x, n); 50 | } 51 | // computes the test statistic Mj 52 | arma::vec M = arma::zeros(d); 53 | for(int j = 0; j <= d-1; j++) { 54 | M[j] = pow(gamma[j]/s[j],2 )*pow(2,d-j); 55 | } 56 | M = arma::cumsum(arma::flipud(M)); 57 | // finds the first k such that Mk < quantile[k] 58 | int k; 59 | for(k = d-1; k >= 0; k--) { 60 | if(M[k] < quantile[k]) 61 | break; 62 | } 63 | int K = d-(k+1); 64 | // returns answer 65 | nK = pow(2, d-K); 66 | return s[K]/nK; 67 | } 68 | 69 | // performs blocking transformation 70 | void Blocker::transform(arma::vec& X, arma::vec& x, unsigned long long& n) 71 | { 72 | for (unsigned long long i = 0; i < n/2; i++) { 73 | x[i] = 0.5*( x[2*i] + x[2*i+1] ); 74 | X[i] = x[i] - mean; 75 | } 76 | n = n/2; 77 | } 78 | 79 | // estimates gamma_h(1) for all h 80 | double Blocker::gamma1(arma::vec& X, unsigned long long& n) 81 | { 82 | double s = 0; 83 | for (unsigned long long i = 0; i < n-1; i++) { 84 | s += X[i]*X[i+1]; 85 | } 86 | return s/n; 87 | } 88 | 89 | // estimates gamma_h(0) for all h 90 | double Blocker::var(arma::vec& X, unsigned long long& n) 91 | { 92 | double s = 0; 93 | for (unsigned long long i = 0; i < n; i++) { 94 | s += X[i]*X[i]; 95 | } 96 | return s/n; 97 | } 98 | -------------------------------------------------------------------------------- /c++/blocker.cpp: -------------------------------------------------------------------------------- 1 | #include "blocker.h" 2 | #include 3 | 4 | std::vector operator-(std::vector v, double scalar ) 5 | { 6 | for(double& vi : v) vi = vi - scalar; 7 | return v; 8 | } 9 | 10 | Blocker::Blocker(std::vector& x) 11 | { 12 | setData(x); 13 | } 14 | 15 | void Blocker::setData(std::vector& x) 16 | { 17 | unsigned long long n = x.size(); 18 | // checks if size of input vector is a power of two 19 | if ( !isPowerOfTwo(n) ) 20 | { 21 | // Error 22 | stdErr = mean = mse_stdErr = mse_mean = -1.0f; 23 | nK = 0; 24 | printf( "Error: Data size must be a power of two." ); 25 | } 26 | else { 27 | // success. Proceeds to compute results 28 | mean = this->avg(x); 29 | mse_mean = 0 + estimate(x, n); 30 | stdErr = sqrt(mse_mean); 31 | mse_stdErr = (2*nK-1)*mse_mean*mse_mean/(nK*nK); 32 | } 33 | } 34 | 35 | // checks that integer n is a power of 2 36 | bool Blocker::isPowerOfTwo(unsigned long long n) 37 | { 38 | return (n>0 && ((n & (n-1)) == 0)); 39 | } 40 | 41 | // the algorithm which computes the variance of the sample mean. 42 | double Blocker::estimate(std::vector& x, unsigned long long& n) 43 | { 44 | // since n is a power of two, d is an integer. 45 | int d = log2(n); 46 | std::vector X = x - mean; 47 | std::vector s(d, 0.0f); 48 | std::vector gamma(d, 0.0f); 49 | // computes covariance and variance and applies blocking transform 50 | for (int k = 0; k <= d-1; k++) { 51 | gamma[k] = this->gamma1(X, n); 52 | s[k] = this->var(X, n); 53 | this->transform(X, x, n); 54 | } 55 | // computes the test statistic Mj 56 | std::vector M(d, 0.0f); 57 | for(int j = 0; j <= d-1; j++) { 58 | M[j] = pow(gamma[j]/s[j],2 )*pow(2,d-j); 59 | } 60 | M = this->cumsum(this->flip(M)); 61 | // finds the first k such that Mk < quantile[k] 62 | int k; 63 | for(k = d-1; k >= 0; k--) { 64 | if(M[k] < quantile[k]) 65 | break; 66 | } 67 | int K = d-(k+1); 68 | // returns answer 69 | nK = pow(2, d-K); 70 | return s[K]/nK; 71 | } 72 | 73 | // performs blocking transformation 74 | void Blocker::transform(std::vector& X, std::vector& x, unsigned long long& n) 75 | { 76 | for (unsigned long long i = 0; i < n/2; i++) { 77 | x[i] = 0.5*( x[2*i] + x[2*i+1] ); 78 | X[i] = x[i] - mean; 79 | } 80 | n = n/2; 81 | } 82 | 83 | // reverses the order of the std::vector M 84 | std::vector Blocker::flip(std::vector M) 85 | { 86 | int _n = M.size(); 87 | std::vector _M(_n,0.0f); 88 | int i = _n-1; 89 | for (double& m : M) _M[i--] = m; 90 | return _M; 91 | } 92 | 93 | // performs cumulative sum on the elements of M 94 | std::vector Blocker::cumsum(std::vector M) 95 | { 96 | int _n = M.size(); 97 | std::vector _M(_n,0.0f); 98 | for (int i = 0; i < _n; i++) { 99 | for(int j = 0; j<=i; j++ ) _M[i] += M[j]; 100 | } 101 | return _M; 102 | } 103 | 104 | // estimates gamma_h(1) for all h 105 | double Blocker::gamma1(std::vector& X, unsigned long long& n) 106 | { 107 | double s = 0; 108 | for (unsigned long long i = 0; i < n-1; i++) s += X[i]*X[i+1]; 109 | return s/n; 110 | } 111 | 112 | // estimates gamma_h(0) for all h 113 | double Blocker::var(std::vector& X, unsigned long long& n) 114 | { 115 | double s = 0; 116 | for (unsigned long long i = 0; i < n; i++) s += X[i]*X[i]; 117 | return s/n; 118 | } 119 | 120 | // estimates mean of x 121 | double Blocker::avg(std::vector& x) 122 | { 123 | double s = 0; 124 | for (double& xi : x) s += xi; 125 | return s/double(x.size()); 126 | } 127 | --------------------------------------------------------------------------------