1 /*
  2  * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "memory/allocation.inline.hpp"
 27 #include "utilities/debug.hpp"
 28 #include "utilities/globalDefinitions.hpp"
 29 #include "utilities/numberSeq.hpp"
 30 
 31 AbsSeq::AbsSeq(double alpha) :
 32   _num(0), _sum(0.0), _sum_of_squares(0.0),
 33   _davg(0.0), _dvariance(0.0), _alpha(alpha) {
 34 }
 35 
 36 void AbsSeq::add(double val) {
 37   if (_num == 0) {
 38     // if the sequence is empty, the davg is the same as the value
 39     _davg = val;
 40     // and the variance is 0
 41     _dvariance = 0.0;
 42   } else {
 43     // otherwise, calculate both
 44     // Formula from "Incremental calculation of weighted mean and variance" by Tony Finch
 45     // diff := x - mean
 46     // incr := alpha * diff
 47     // mean := mean + incr
 48     // variance := (1 - alpha) * (variance + diff * incr)
 49     // PDF available at https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf
 50     double diff = val - _davg;
 51     double incr = _alpha * diff;
 52     _davg += incr;
 53     _dvariance = (1.0 - _alpha) * (_dvariance + diff * incr);
 54   }
 55 }
 56 
 57 double AbsSeq::avg() const {
 58   if (_num == 0)
 59     return 0.0;
 60   else
 61     return _sum / total();
 62 }
 63 
 64 double AbsSeq::variance() const {
 65   if (_num <= 1)
 66     return 0.0;
 67 
 68   double x_bar = avg();
 69   double result = _sum_of_squares / total() - x_bar * x_bar;
 70   if (result < 0.0) {
 71     // due to loss-of-precision errors, the variance might be negative
 72     // by a small bit
 73 
 74     //    guarantee(-0.1 < result && result < 0.0,
 75     //        "if variance is negative, it should be very small");
 76     result = 0.0;
 77   }
 78   return result;
 79 }
 80 
 81 double AbsSeq::sd() const {
 82   double var = variance();
 83   guarantee( var >= 0.0, "variance should not be negative" );
 84   return sqrt(var);
 85 }
 86 
 87 double AbsSeq::davg() const {
 88   return _davg;
 89 }
 90 
 91 double AbsSeq::dvariance() const {
 92   if (_num <= 1)
 93     return 0.0;
 94 
 95   double result = _dvariance;
 96   if (result < 0.0) {
 97     // due to loss-of-precision errors, the variance might be negative
 98     // by a small bit
 99 
100     guarantee(-0.1 < result && result < 0.0,
101                "if variance is negative, it should be very small");
102     result = 0.0;
103   }
104   return result;
105 }
106 
107 double AbsSeq::dsd() const {
108   double var = dvariance();
109   guarantee( var >= 0.0, "variance should not be negative" );
110   return sqrt(var);
111 }
112 
113 void AbsSeq::merge(AbsSeq& abs2, bool clear_this) {
114 
115   if (num() == 0) return;  // nothing to do
116 
117   abs2._num += _num;
118   abs2._sum += _sum;
119   abs2._sum_of_squares += _sum_of_squares;
120 
121   // Decaying stats need a bit more thought
122   assert(abs2._alpha == _alpha, "Caution: merge incompatible?");
123   // Until JDK-8298902 is fixed, we taint the decaying statistics
124   if (abs2._davg != NAN) {
125     abs2._davg = NAN;
126     abs2._dvariance = NAN;
127   }
128 
129   if (clear_this) {
130     _num = 0;
131     _sum = 0;
132     _sum_of_squares = 0;
133     _davg = 0;
134     _dvariance = 0;
135   }
136 }
137 
138 
139 NumberSeq::NumberSeq(double alpha) :
140   AbsSeq(alpha), _last(0.0), _maximum(0.0) {
141 }
142 
143 bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) {
144   for (int i = 0; i < n; ++i) {
145     if (parts[i] != nullptr && total->num() != parts[i]->num())
146       return false;
147   }
148   return true;
149 }
150 
151 void NumberSeq::add(double val) {
152   AbsSeq::add(val);
153 
154   _last = val;
155   if (_num == 0) {
156     _maximum = val;
157   } else {
158     if (val > _maximum)
159       _maximum = val;
160   }
161   _sum += val;
162   _sum_of_squares += val * val;
163   ++_num;
164 }
165 
166 void NumberSeq::merge(NumberSeq& nseq2, bool clear_this) {
167 
168   if (num() == 0) return;  // nothing to do
169 
170   nseq2._last = _last;   // this is newer than that
171   nseq2._maximum = MAX2(_maximum, nseq2._maximum);
172 
173   AbsSeq::merge(nseq2, clear_this);
174 
175   if (clear_this) {
176     _last = 0;
177     _maximum = 0;
178     assert(num() == 0, "Not cleared");
179   }
180 }
181 
182 
183 TruncatedSeq::TruncatedSeq(int length, double alpha):
184   AbsSeq(alpha), _length(length), _next(0) {
185   _sequence = NEW_C_HEAP_ARRAY(double, _length, mtInternal);
186   for (int i = 0; i < _length; ++i)
187     _sequence[i] = 0.0;
188 }
189 
190 TruncatedSeq::~TruncatedSeq() {
191   FREE_C_HEAP_ARRAY(double, _sequence);
192 }
193 
194 void TruncatedSeq::add(double val) {
195   AbsSeq::add(val);
196 
197   // get the oldest value in the sequence...
198   double old_val = _sequence[_next];
199   // ...remove it from the sum and sum of squares
200   _sum -= old_val;
201   _sum_of_squares -= old_val * old_val;
202 
203   // ...and update them with the new value
204   _sum += val;
205   _sum_of_squares += val * val;
206 
207   // now replace the old value with the new one
208   _sequence[_next] = val;
209   _next = (_next + 1) % _length;
210 
211   // only increase it if the buffer is not full
212   if (_num < _length)
213     ++_num;
214 
215   guarantee( variance() > -1.0, "variance should be >= 0" );
216 }
217 
218 // can't easily keep track of this incrementally...
219 double TruncatedSeq::maximum() const {
220   if (_num == 0)
221     return 0.0;
222   double ret = _sequence[0];
223   for (int i = 1; i < _num; ++i) {
224     double val = _sequence[i];
225     if (val > ret)
226       ret = val;
227   }
228   return ret;
229 }
230 
231 double TruncatedSeq::last() const {
232   if (_num == 0)
233     return 0.0;
234   unsigned last_index = (_next + _length - 1) % _length;
235   return _sequence[last_index];
236 }
237 
238 double TruncatedSeq::oldest() const {
239   if (_num == 0)
240     return 0.0;
241   else if (_num < _length)
242     // index 0 always oldest value until the array is full
243     return _sequence[0];
244   else {
245     // since the array is full, _next is over the oldest value
246     return _sequence[_next];
247   }
248 }
249 
250 double TruncatedSeq::predict_next() const {
251   if (_num == 0)
252     return 0.0;
253 
254   double num           = (double) _num;
255   double x_squared_sum = 0.0;
256   double x_sum         = 0.0;
257   double y_sum         = 0.0;
258   double xy_sum        = 0.0;
259   double x_avg         = 0.0;
260   double y_avg         = 0.0;
261 
262   int first = (_next + _length - _num) % _length;
263   for (int i = 0; i < _num; ++i) {
264     double x = (double) i;
265     double y =  _sequence[(first + i) % _length];
266 
267     x_squared_sum += x * x;
268     x_sum         += x;
269     y_sum         += y;
270     xy_sum        += x * y;
271   }
272   x_avg = x_sum / num;
273   y_avg = y_sum / num;
274 
275   double Sxx = x_squared_sum - x_sum * x_sum / num;
276   double Sxy = xy_sum - x_sum * y_sum / num;
277   double b1 = Sxy / Sxx;
278   double b0 = y_avg - b1 * x_avg;
279 
280   return b0 + b1 * num;
281 }
282 
283 
284 // Printing/Debugging Support
285 
286 void AbsSeq::dump() { dump_on(tty); }
287 
288 void AbsSeq::dump_on(outputStream* s) {
289   s->print_cr("\t _num = %d, _sum = %7.3f, _sum_of_squares = %7.3f",
290                   _num,      _sum,         _sum_of_squares);
291   s->print_cr("\t _davg = %7.3f, _dvariance = %7.3f, _alpha = %7.3f",
292                   _davg,         _dvariance,         _alpha);
293 }
294 
295 void NumberSeq::dump_on(outputStream* s) {
296   AbsSeq::dump_on(s);
297   s->print_cr("\t\t _last = %7.3f, _maximum = %7.3f", _last, _maximum);
298 }
299 
300 void TruncatedSeq::dump_on(outputStream* s) {
301   AbsSeq::dump_on(s);
302   s->print_cr("\t\t _length = %d, _next = %d", _length, _next);
303   for (int i = 0; i < _length; i++) {
304     if (i%5 == 0) {
305       s->cr();
306       s->print("\t");
307     }
308     s->print("\t[%d]=%7.3f", i, _sequence[i]);
309   }
310   s->cr();
311 }