Mon, 09 Mar 2009 13:28:46 -0700
6814575: Update copyright year
Summary: Update copyright for files that have been modified in 2009, up to 03/09
Reviewed-by: katleman, tbell, ohair
ysr@777 | 1 | /* |
ysr@777 | 2 | * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. |
ysr@777 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ysr@777 | 4 | * |
ysr@777 | 5 | * This code is free software; you can redistribute it and/or modify it |
ysr@777 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ysr@777 | 7 | * published by the Free Software Foundation. |
ysr@777 | 8 | * |
ysr@777 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ysr@777 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ysr@777 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ysr@777 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ysr@777 | 13 | * accompanied this code). |
ysr@777 | 14 | * |
ysr@777 | 15 | * You should have received a copy of the GNU General Public License version |
ysr@777 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ysr@777 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ysr@777 | 18 | * |
ysr@777 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
ysr@777 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
ysr@777 | 21 | * have any questions. |
ysr@777 | 22 | * |
ysr@777 | 23 | */ |
ysr@777 | 24 | |
ysr@777 | 25 | /** |
ysr@777 | 26 | ** This file contains a few classes that represent number sequence, |
ysr@777 | 27 | ** x1, x2, x3, ..., xN, and can calculate their avg, max, and sd. |
ysr@777 | 28 | ** |
ysr@777 | 29 | ** Here's a quick description of the classes: |
ysr@777 | 30 | ** |
ysr@777 | 31 | ** AbsSeq: abstract superclass |
ysr@777 | 32 | ** NumberSeq: the sequence is assumed to be very long and the |
ysr@777 | 33 | ** maximum, avg, sd, davg, and dsd are calculated over all its elements |
ysr@777 | 34 | ** TruncatedSeq: this class keeps track of the last L elements |
ysr@777 | 35 | ** of the sequence and calculates avg, max, and sd only over them |
ysr@777 | 36 | **/ |
ysr@777 | 37 | |
ysr@777 | 38 | #define DEFAULT_ALPHA_VALUE 0.7 |
ysr@777 | 39 | |
ysr@777 | 40 | class AbsSeq { |
ysr@777 | 41 | private: |
ysr@777 | 42 | void init(double alpha); |
ysr@777 | 43 | |
ysr@777 | 44 | protected: |
ysr@777 | 45 | int _num; // the number of elements in the sequence |
ysr@777 | 46 | double _sum; // the sum of the elements in the sequence |
ysr@777 | 47 | double _sum_of_squares; // the sum of squares of the elements in the sequence |
ysr@777 | 48 | |
ysr@777 | 49 | double _davg; // decaying average |
ysr@777 | 50 | double _dvariance; // decaying variance |
ysr@777 | 51 | double _alpha; // factor for the decaying average / variance |
ysr@777 | 52 | |
ysr@777 | 53 | // This is what we divide with to get the average. In a standard |
ysr@777 | 54 | // number sequence, this should just be the number of elements in it. |
ysr@777 | 55 | virtual double total() const { return (double) _num; }; |
ysr@777 | 56 | |
ysr@777 | 57 | public: |
ysr@777 | 58 | AbsSeq(double alpha = DEFAULT_ALPHA_VALUE); |
ysr@777 | 59 | |
ysr@777 | 60 | virtual void add(double val); // adds a new element to the sequence |
ysr@777 | 61 | void add(unsigned val) { add((double) val); } |
ysr@777 | 62 | virtual double maximum() const = 0; // maximum element in the sequence |
ysr@777 | 63 | virtual double last() const = 0; // last element added in the sequence |
ysr@777 | 64 | |
ysr@777 | 65 | // the number of elements in the sequence |
ysr@777 | 66 | int num() const { return _num; } |
ysr@777 | 67 | // the sum of the elements in the sequence |
ysr@777 | 68 | double sum() const { return _sum; } |
ysr@777 | 69 | |
ysr@777 | 70 | double avg() const; // the average of the sequence |
ysr@777 | 71 | double variance() const; // the variance of the sequence |
ysr@777 | 72 | double sd() const; // the standard deviation of the sequence |
ysr@777 | 73 | |
ysr@777 | 74 | double davg() const; // decaying average |
ysr@777 | 75 | double dvariance() const; // decaying variance |
ysr@777 | 76 | double dsd() const; // decaying "standard deviation" |
ysr@777 | 77 | }; |
ysr@777 | 78 | |
ysr@777 | 79 | class NumberSeq: public AbsSeq { |
ysr@777 | 80 | private: |
ysr@777 | 81 | bool check_nums(NumberSeq* total, int n, NumberSeq** parts); |
ysr@777 | 82 | |
ysr@777 | 83 | protected: |
ysr@777 | 84 | double _last; |
ysr@777 | 85 | double _maximum; // keep track of maximum value |
ysr@777 | 86 | |
ysr@777 | 87 | public: |
ysr@777 | 88 | NumberSeq(double alpha = DEFAULT_ALPHA_VALUE); |
ysr@777 | 89 | NumberSeq(NumberSeq* total, int n_parts, NumberSeq** parts); |
ysr@777 | 90 | |
ysr@777 | 91 | virtual void add(double val); |
ysr@777 | 92 | virtual double maximum() const { return _maximum; } |
ysr@777 | 93 | virtual double last() const { return _last; } |
ysr@777 | 94 | }; |
ysr@777 | 95 | |
ysr@777 | 96 | class TruncatedSeq: public AbsSeq { |
ysr@777 | 97 | private: |
ysr@777 | 98 | enum PrivateConstants { |
ysr@777 | 99 | DefaultSeqLength = 10 |
ysr@777 | 100 | }; |
ysr@777 | 101 | void init(); |
ysr@777 | 102 | protected: |
ysr@777 | 103 | double *_sequence; // buffers the last L elements in the sequence |
ysr@777 | 104 | int _length; // this is L |
ysr@777 | 105 | int _next; // oldest slot in the array, i.e. next to be overwritten |
ysr@777 | 106 | |
ysr@777 | 107 | public: |
ysr@777 | 108 | // accepts a value for L |
ysr@777 | 109 | TruncatedSeq(int length = DefaultSeqLength, |
ysr@777 | 110 | double alpha = DEFAULT_ALPHA_VALUE); |
ysr@777 | 111 | virtual void add(double val); |
ysr@777 | 112 | virtual double maximum() const; |
ysr@777 | 113 | virtual double last() const; // the last value added to the sequence |
ysr@777 | 114 | |
ysr@777 | 115 | double oldest() const; // the oldest valid value in the sequence |
ysr@777 | 116 | double predict_next() const; // prediction based on linear regression |
ysr@777 | 117 | }; |