1 /*
  2  * Copyright (c) 2001, 2014, 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     // Note: alpha is actually (1.0 - _alpha) in our code
 51     double diff = val - _davg;
 52     double incr = (1.0 - _alpha) * diff;
 53     _davg += incr;
 54     _dvariance = _alpha * (_dvariance + diff * incr);
 55   }
 56 }
 57 
 58 double AbsSeq::avg() const {
 59   if (_num == 0)
 60     return 0.0;
 61   else
 62     return _sum / total();
 63 }
 64 
 65 double AbsSeq::variance() const {
 66   if (_num <= 1)
 67     return 0.0;
 68 
 69   double x_bar = avg();
 70   double result = _sum_of_squares / total() - x_bar * x_bar;
 71   if (result < 0.0) {
 72     // due to loss-of-precision errors, the variance might be negative
 73     // by a small bit
 74 
 75     //    guarantee(-0.1 < result && result < 0.0,
 76     //        "if variance is negative, it should be very small");
 77     result = 0.0;
 78   }
 79   return result;
 80 }
 81 
 82 double AbsSeq::sd() const {
 83   double var = variance();
 84   guarantee( var >= 0.0, "variance should not be negative" );
 85   return sqrt(var);
 86 }
 87 
 88 double AbsSeq::davg() const {
 89   return _davg;
 90 }
 91 
 92 double AbsSeq::dvariance() const {
 93   if (_num <= 1)
 94     return 0.0;
 95 
 96   double result = _dvariance;
 97   if (result < 0.0) {
 98     // due to loss-of-precision errors, the variance might be negative
 99     // by a small bit
100 
101     guarantee(-0.1 < result && result < 0.0,
102                "if variance is negative, it should be very small");
103     result = 0.0;
104   }
105   return result;
106 }
107 
108 double AbsSeq::dsd() const {
109   double var = dvariance();
110   guarantee( var >= 0.0, "variance should not be negative" );
111   return sqrt(var);
112 }
113 
114 NumberSeq::NumberSeq(double alpha) :
115   AbsSeq(alpha), _last(0.0), _maximum(0.0) {
116 }
117 
118 bool NumberSeq::check_nums(NumberSeq *total, int n, NumberSeq **parts) {
119   for (int i = 0; i < n; ++i) {
120     if (parts[i] != NULL && total->num() != parts[i]->num())
121       return false;
122   }
123   return true;
124 }
125 
126 void NumberSeq::add(double val) {
127   AbsSeq::add(val);
128 
129   _last = val;
130   if (_num == 0) {
131     _maximum = val;
132   } else {
133     if (val > _maximum)
134       _maximum = val;
135   }
136   _sum += val;
137   _sum_of_squares += val * val;
138   ++_num;
139 }
140 
141 
142 TruncatedSeq::TruncatedSeq(int length, double alpha):
143   AbsSeq(alpha), _length(length), _next(0) {
144   _sequence = NEW_C_HEAP_ARRAY(double, _length, mtInternal);
145   for (int i = 0; i < _length; ++i)
146     _sequence[i] = 0.0;
147 }
148 
149 TruncatedSeq::~TruncatedSeq() {
150   FREE_C_HEAP_ARRAY(double, _sequence);
151 }
152 
153 void TruncatedSeq::add(double val) {
154   AbsSeq::add(val);
155 
156   // get the oldest value in the sequence...
157   double old_val = _sequence[_next];
158   // ...remove it from the sum and sum of squares
159   _sum -= old_val;
160   _sum_of_squares -= old_val * old_val;
161 
162   // ...and update them with the new value
163   _sum += val;
164   _sum_of_squares += val * val;
165 
166   // now replace the old value with the new one
167   _sequence[_next] = val;
168   _next = (_next + 1) % _length;
169 
170   // only increase it if the buffer is not full
171   if (_num < _length)
172     ++_num;
173 
174   guarantee( variance() > -1.0, "variance should be >= 0" );
175 }
176 
177 // can't easily keep track of this incrementally...
178 double TruncatedSeq::maximum() const {
179   if (_num == 0)
180     return 0.0;
181   double ret = _sequence[0];
182   for (int i = 1; i < _num; ++i) {
183     double val = _sequence[i];
184     if (val > ret)
185       ret = val;
186   }
187   return ret;
188 }
189 
190 double TruncatedSeq::last() const {
191   if (_num == 0)
192     return 0.0;
193   unsigned last_index = (_next + _length - 1) % _length;
194   return _sequence[last_index];
195 }
196 
197 double TruncatedSeq::oldest() const {
198   if (_num == 0)
199     return 0.0;
200   else if (_num < _length)
201     // index 0 always oldest value until the array is full
202     return _sequence[0];
203   else {
204     // since the array is full, _next is over the oldest value
205     return _sequence[_next];
206   }
207 }
208 
209 double TruncatedSeq::predict_next() const {
210   if (_num == 0)
211     return 0.0;
212 
213   double num           = (double) _num;
214   double x_squared_sum = 0.0;
215   double x_sum         = 0.0;
216   double y_sum         = 0.0;
217   double xy_sum        = 0.0;
218   double x_avg         = 0.0;
219   double y_avg         = 0.0;
220 
221   int first = (_next + _length - _num) % _length;
222   for (int i = 0; i < _num; ++i) {
223     double x = (double) i;
224     double y =  _sequence[(first + i) % _length];
225 
226     x_squared_sum += x * x;
227     x_sum         += x;
228     y_sum         += y;
229     xy_sum        += x * y;
230   }
231   x_avg = x_sum / num;
232   y_avg = y_sum / num;
233 
234   double Sxx = x_squared_sum - x_sum * x_sum / num;
235   double Sxy = xy_sum - x_sum * y_sum / num;
236   double b1 = Sxy / Sxx;
237   double b0 = y_avg - b1 * x_avg;
238 
239   return b0 + b1 * num;
240 }
241 
242 
243 // Printing/Debugging Support
244 
245 void AbsSeq::dump() { dump_on(tty); }
246 
247 void AbsSeq::dump_on(outputStream* s) {
248   s->print_cr("\t _num = %d, _sum = %7.3f, _sum_of_squares = %7.3f",
249                   _num,      _sum,         _sum_of_squares);
250   s->print_cr("\t _davg = %7.3f, _dvariance = %7.3f, _alpha = %7.3f",
251                   _davg,         _dvariance,         _alpha);
252 }
253 
254 void NumberSeq::dump_on(outputStream* s) {
255   AbsSeq::dump_on(s);
256   s->print_cr("\t\t _last = %7.3f, _maximum = %7.3f", _last, _maximum);
257 }
258 
259 void TruncatedSeq::dump_on(outputStream* s) {
260   AbsSeq::dump_on(s);
261   s->print_cr("\t\t _length = %d, _next = %d", _length, _next);
262   for (int i = 0; i < _length; i++) {
263     if (i%5 == 0) {
264       s->cr();
265       s->print("\t");
266     }
267     s->print("\t[%d]=%7.3f", i, _sequence[i]);
268   }
269   s->cr();
270 }