-
Notifications
You must be signed in to change notification settings - Fork 3
/
statistics.cpp
199 lines (175 loc) · 5.41 KB
/
statistics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* statistics.cpp
*
* Created on: May 11, 2011
* Author: gerlach
*/
// taken some parts for SDW DQMC mrpt (2015-02-07 - )
#include <cmath>
#include "statistics.h"
using namespace std;
//if end==0: compute average over whole vector
//else compute average for elements at start, start+1, ..., end-1
double average(const std::vector<double>* vec, std::size_t start, std::size_t end) {
if (end==0) {
end = vec->size();
}
// assert(end > start);
double avg = 0;
for (std::size_t i = start; i < end; ++i) {
avg += double((*vec)[i]) / (double(end)-double(start));
}
return avg;
}
double average(const std::vector<int>* vec, std::size_t start, std::size_t end) {
if (end==0) {
end = vec->size();
}
// assert(end > start);
double avg = 0;
for (std::size_t i = start; i < end; ++i) {
avg += double((*vec)[i]) / (double(end)-double(start));
}
return avg;
}
//if end==0: compute square average over whole vector
//else compute sq. average for elements at start, start+1, ..., end-1
double sqAverage(const std::vector<double>* vec, std::size_t start, std::size_t end) {
if (end==0) {
end = vec->size();
}
// assert(end > start);
double sqAvg = 0;
for (std::size_t i = start; i < end; ++i) {
sqAvg += std::pow(vec->at(i),2);
}
sqAvg /= (double(end)-double(start));
return sqAvg;
}
double variance(const std::vector<double>* numbers, double meanValue, std::size_t N) {
if (N == 0) {
N = numbers->size();
}
// assert(N > 0);
double sum = 0;
for (std::size_t i = 0; i < N; ++i) {
sum += std::pow( numbers->at(i) - meanValue, 2);
}
return sum / double(N-1);
}
double variance(const std::vector<int>* numbers, double meanValue, std::size_t N) {
if (N == 0) {
N = numbers->size();
}
// assert(N > 0);
double sum = 0;
for (std::size_t i = 0; i < N; ++i) {
sum += std::pow( double(numbers->at(i)) - meanValue, 2);
}
return sum / double(N-1);
}
//integrated autocorrelation time,
//employ a self-consistent cut-off
//\tau_int = 1/2 + \sum_{k=1}^{k_max} A(k)
//k_max \approx 6*\tau_int
double tauint(const std::vector<double>* data, double selfConsCutOff, AutoCorrMap* points) {
std::size_t m = data->size();
double mean = average(data);
double var = 0;
double result = 0.5;
for (int t = 0; t < int(m) - 1; ++t) {
double autoCorr = 0;
for (int k = 0; k < int(m) - t; ++k){
autoCorr += ((*data)[k] - mean) * ((*data)[k + t] - mean);
}
autoCorr /= double(m - t);
if (t == 0) {
var = autoCorr;
} else {
result += autoCorr / var;
if (t > selfConsCutOff * result)
break;
}
if (points) {
points->insert(make_pair(t, autoCorr));
}
}
return result;
}
//integrated autocorrelation time,
//stop accumulating once autoCorr <= 0
double tauint_stopAtZeroCrossing(const std::vector<double>* data, AutoCorrMap* points) {
std::size_t m = data->size();
double mean = average(data);
double var = 0;
double result = 0.5;
for (int t = 0; t < int(m) - 1; ++t) {
double autoCorr = 0;
for (int k = 0; k < int(m) - t; ++k){
autoCorr += ((*data)[k] - mean) * ((*data)[k + t] - mean);
}
autoCorr /= (int(m) - t);
if (t == 0) {
var = autoCorr;
} else {
if (autoCorr <= 0) {
break;
} else {
result += autoCorr / var;
}
}
if (points) {
points->insert(make_pair(t, autoCorr));
}
}
return result;
}
//faster estimation of tauint using an adaptive integration scheme (compare [Chodera2007] pg. 38)
//(also stops at the zero crossing of autoCorr)
double tauint_adaptive(const std::vector<double>* data, AutoCorrMap* points) {
std::size_t m = data->size();
double mean = average(data);
double result = 0.5;
//compute variance
double var = 0;
for (std::size_t k = 0; k < m; ++k){
var += ((*data)[k] - mean) * ((*data)[k] - mean);
}
var /= double(m);
//adaptive integration of autocorrelation function
//high time resolution for small lag times, lower resolution for higher times in the
//slowly decaying tail of the autocorrelation function
int i = 1;
int t_i = 1;
while (t_i < int(m) - 1) {
//lag time for this step of the iteration
t_i = 1 + i * (i - 1) / 2;
//compute autocorrelation function
double autoCorr = 0.0;
for (int k = 0; k < int(m) - t_i; ++k){
autoCorr += ((*data)[k] - mean) * ((*data)[k + t_i] - mean);
}
autoCorr /= double(m - t_i);
autoCorr /= var;
if (autoCorr <= 0) {
break;
} else {
//weighted addition to estimate integrated autocorrelation time
double t_next = 1 + (i+1) * (i) / 2;
result += autoCorr * (t_next - t_i);
}
if (points) {
points->insert(make_pair(t_i, autoCorr));
}
i = i + 1;
}
return result;
}