2010-03-09 04:15:10 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2010 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2009-11-04 21:23:56 +00:00
|
|
|
//
|
2010-03-09 04:15:10 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2009-11-04 21:23:56 +00:00
|
|
|
|
2010-03-09 04:15:10 +00:00
|
|
|
// structure for tracking the recent average
|
|
|
|
// of a distribution that may change over time
|
|
|
|
//
|
2009-11-04 21:23:56 +00:00
|
|
|
struct AVERAGE {
|
2009-11-04 23:04:07 +00:00
|
|
|
double n; // double to avoid integer overflow
|
2010-03-09 04:15:10 +00:00
|
|
|
double avg;
|
|
|
|
|
2010-03-29 22:28:20 +00:00
|
|
|
inline void clear() {
|
|
|
|
n = avg = 0;
|
|
|
|
}
|
|
|
|
|
2010-03-09 04:15:10 +00:00
|
|
|
// return true if sample exceeded limit and was truncated
|
|
|
|
//
|
2010-03-29 22:28:20 +00:00
|
|
|
inline bool update(
|
|
|
|
double sample,
|
|
|
|
double n_threshold,
|
|
|
|
// after this many samples, use exponential average
|
|
|
|
double sample_weight,
|
|
|
|
// new samples get this weight in exp avg
|
|
|
|
double sample_limit
|
|
|
|
// truncate samples at avg*limit
|
|
|
|
) {
|
2010-06-25 18:54:37 +00:00
|
|
|
double delta;
|
2010-03-29 22:28:20 +00:00
|
|
|
bool truncated = false;
|
|
|
|
if (sample < 0) return true;
|
|
|
|
if (n && avg) {
|
|
|
|
if (sample > avg*sample_limit) {
|
|
|
|
sample = avg*sample_limit;
|
|
|
|
truncated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
delta = sample - avg;
|
|
|
|
if (n < n_threshold) {
|
|
|
|
avg += delta/n;
|
|
|
|
} else {
|
|
|
|
avg += sample_weight*delta;
|
|
|
|
}
|
|
|
|
return truncated;
|
|
|
|
}
|
2010-03-09 04:15:10 +00:00
|
|
|
|
|
|
|
inline double get_avg() {
|
|
|
|
return avg;
|
2009-11-04 21:23:56 +00:00
|
|
|
}
|
2010-03-09 04:15:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// same, but variance also
|
|
|
|
//
|
|
|
|
struct AVERAGE_VAR : AVERAGE {
|
|
|
|
double var;
|
2010-03-11 17:49:19 +00:00
|
|
|
double q;
|
2010-03-09 04:15:10 +00:00
|
|
|
|
2010-03-29 22:28:20 +00:00
|
|
|
inline bool update_var(
|
|
|
|
double sample,
|
|
|
|
double n_threshold,
|
|
|
|
// after this many samples, use exponential average
|
|
|
|
double sample_weight,
|
|
|
|
// new samples get this weight in exp avg
|
|
|
|
double sample_limit
|
|
|
|
// truncate samples at avg*limit
|
|
|
|
) {
|
2010-06-25 18:54:37 +00:00
|
|
|
double delta;
|
2010-03-29 22:28:20 +00:00
|
|
|
bool truncated = false;
|
|
|
|
if (sample < 0) return true;
|
|
|
|
if (n && avg) {
|
|
|
|
if (sample > avg*sample_limit) {
|
|
|
|
sample = avg*sample_limit;
|
|
|
|
truncated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n++;
|
|
|
|
delta = sample - avg;
|
|
|
|
if (n < n_threshold) {
|
|
|
|
avg += delta/n;
|
|
|
|
q += delta*(sample - avg);
|
|
|
|
var = q/n;
|
|
|
|
} else {
|
|
|
|
avg += sample_weight*delta;
|
|
|
|
double vdelta = (delta*delta - var);
|
|
|
|
var += sample_weight*vdelta;
|
|
|
|
}
|
|
|
|
return truncated;
|
|
|
|
}
|
|
|
|
|
2010-03-09 04:15:10 +00:00
|
|
|
inline double get_var() {
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2010-03-29 22:28:20 +00:00
|
|
|
inline void clear() {
|
|
|
|
AVERAGE::clear();
|
2010-03-09 04:15:10 +00:00
|
|
|
var = 0;
|
2010-03-11 17:49:19 +00:00
|
|
|
q = 0;
|
2009-11-04 21:23:56 +00:00
|
|
|
}
|
|
|
|
};
|