Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

StatsIterator.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 /* ******************************************************* 00038 * $Id: StatsIterator.h,v 1.9 2004/07/21 16:30:53 chrish42 Exp $ 00039 * This file is part of the PLearn library. 00040 ******************************************************* */ 00041 00042 00045 #ifndef StatsIterator_INC 00046 #define StatsIterator_INC 00047 00048 #include <plearn/base/stringutils.h> 00049 #include <plearn/base/Object.h> 00050 #include "Mat.h" 00051 #include <plearn/vmat/VMat.h> 00052 #include "TMat_maths.h" 00053 00054 namespace PLearn { 00055 using namespace std; 00056 00057 00058 class StatsIterator: public Object 00059 { 00060 typedef Object inherited; 00061 00062 protected: 00063 Vec result; 00064 00065 public: 00066 00069 virtual bool requiresMultiplePasses(); 00070 00072 virtual void init(int inputsize)=0; 00073 00075 virtual void update(const Vec& input)=0; 00076 virtual void update(const Mat& inputs) 00077 { 00078 for (int i=0;inputs.length();i++) 00079 { 00080 Vec input = inputs(i); 00081 update(input); 00082 } 00083 } 00084 00089 virtual bool finish()=0; 00090 00092 virtual Vec getResult(); 00093 00094 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00095 PLEARN_DECLARE_ABSTRACT_OBJECT(StatsIterator); 00096 00097 virtual void build() {} 00098 virtual void oldwrite(ostream& out) const; 00099 virtual void oldread(istream& in); 00100 00101 protected: 00102 static void declareOptions(OptionList& ol); 00103 00104 }; 00105 00106 DECLARE_OBJECT_PTR(StatsIterator); 00107 00108 class MeanStatsIterator: public StatsIterator 00109 { 00110 typedef StatsIterator inherited; 00111 00112 protected: 00113 TVec<int> nsamples; 00114 00115 public: 00116 virtual string info() const { return "mean"; } 00117 virtual void init(int inputsize); 00118 virtual void update(const Vec& input); 00119 virtual bool finish(); 00120 PLEARN_DECLARE_OBJECT(MeanStatsIterator); 00121 00122 virtual void oldwrite(ostream& out) const; 00123 virtual void oldread(istream& in); 00124 00125 protected: 00126 static void declareOptions(OptionList& ol); 00127 00128 }; 00129 00130 DECLARE_OBJECT_PTR(MeanStatsIterator); 00131 00132 class ExpMeanStatsIterator: public StatsIterator 00133 { 00134 typedef StatsIterator inherited; 00135 00136 protected: 00137 TVec<int> nsamples; 00138 00139 public: 00140 virtual string info() const { return "exp_mean"; } 00141 00142 virtual void init(int inputsize); 00143 virtual void update(const Vec& input); 00144 virtual bool finish(); 00145 PLEARN_DECLARE_OBJECT(ExpMeanStatsIterator); 00146 00147 virtual void oldwrite(ostream& out) const; 00148 virtual void oldread(istream& in); 00149 00150 protected: 00151 static void declareOptions(OptionList& ol); 00152 00153 }; 00154 00155 DECLARE_OBJECT_PTR(ExpMeanStatsIterator); 00156 00157 class StddevStatsIterator: public StatsIterator 00158 { 00159 typedef StatsIterator inherited; 00160 00161 protected: 00162 Vec meansquared; 00163 Vec mean; 00164 TVec<int> nsamples; 00165 00166 public: 00167 virtual string info() const { return "std_dev"; } 00168 virtual void init(int inputsize); 00169 virtual void update(const Vec& input); 00170 virtual bool finish(); 00171 PLEARN_DECLARE_OBJECT(StddevStatsIterator); 00172 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00173 00174 virtual void oldwrite(ostream& out) const; 00175 virtual void oldread(istream& in); 00176 00177 protected: 00178 static void declareOptions(OptionList& ol); 00179 00180 }; 00181 00182 DECLARE_OBJECT_PTR(StddevStatsIterator); 00183 00184 class StderrStatsIterator: public StatsIterator 00185 { 00186 typedef StatsIterator inherited; 00187 00188 protected: 00189 Vec meansquared; 00190 Vec mean; 00191 TVec<int> nsamples; 00192 00193 public: 00194 virtual string info() const { return "std_err"; } 00195 virtual void init(int inputsize); 00196 virtual void update(const Vec& input); 00197 virtual bool finish(); 00198 PLEARN_DECLARE_OBJECT(StderrStatsIterator); 00199 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00200 00201 virtual void oldwrite(ostream& out) const; 00202 virtual void oldread(istream& in); 00203 00204 protected: 00205 static void declareOptions(OptionList& ol); 00206 00207 }; 00208 00209 DECLARE_OBJECT_PTR(StderrStatsIterator); 00210 00217 class SharpeRatioStatsIterator: public StatsIterator 00218 { 00219 typedef StatsIterator inherited; 00220 00221 protected: 00222 Vec nnonzero; 00223 Vec meansquared; 00224 Vec mean; 00225 00226 public: 00227 virtual string info() const { return "sharpe_ratio"; } 00228 virtual void init(int inputsize); 00229 virtual void update(const Vec& input); 00230 virtual bool finish(); 00231 PLEARN_DECLARE_OBJECT(SharpeRatioStatsIterator); 00232 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00233 00234 virtual void oldwrite(ostream& out) const; 00235 virtual void oldread(istream& in); 00236 00237 protected: 00238 static void declareOptions(OptionList& ol); 00239 00240 }; 00241 00242 DECLARE_OBJECT_PTR(SharpeRatioStatsIterator); 00243 00244 class MinStatsIterator: public StatsIterator 00245 { 00246 typedef StatsIterator inherited; 00247 00248 public: 00249 virtual string info() const { return "min"; } 00250 virtual void init(int inputsize); 00251 virtual void update(const Vec& input); 00252 virtual bool finish(); 00253 PLEARN_DECLARE_OBJECT(MinStatsIterator); 00254 00255 virtual void oldwrite(ostream& out) const; 00256 virtual void oldread(istream& in); 00257 00258 protected: 00259 static void declareOptions(OptionList& ol); 00260 00261 }; 00262 00263 DECLARE_OBJECT_PTR(MinStatsIterator); 00264 00265 class MaxStatsIterator: public StatsIterator 00266 { 00267 typedef StatsIterator inherited; 00268 00269 public: 00270 virtual string info() const { return "max"; } 00271 virtual void init(int inputsize); 00272 virtual void update(const Vec& input); 00273 virtual bool finish(); 00274 PLEARN_DECLARE_OBJECT(MaxStatsIterator); 00275 00276 virtual void oldwrite(ostream& out) const; 00277 virtual void oldread(istream& in); 00278 00279 protected: 00280 static void declareOptions(OptionList& ol); 00281 00282 }; 00283 00284 DECLARE_OBJECT_PTR(MaxStatsIterator); 00285 00291 class LiftStatsIterator: public StatsIterator 00292 { 00293 typedef StatsIterator inherited; 00294 00295 protected: 00296 int nsamples; 00297 int lift_index; 00298 real lift_fraction; 00299 Mat output_and_pos; 00300 Vec targets; 00301 00302 public: 00303 virtual string info() const { return "lift"; } 00304 virtual void init(int inputsize); 00305 virtual void update(const Vec& input); 00306 virtual bool finish(); 00307 00308 LiftStatsIterator(int the_index=0, real the_fraction=0.1); 00309 PLEARN_DECLARE_OBJECT(LiftStatsIterator); 00310 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00311 00312 virtual void oldwrite(ostream& out) const; 00313 virtual void oldread(istream& in); 00314 00315 protected: 00316 static void declareOptions(OptionList& ol); 00317 00318 }; 00319 00320 DECLARE_OBJECT_PTR(LiftStatsIterator); 00321 00326 class QuantilesStatsIterator: public StatsIterator 00327 { 00328 typedef StatsIterator inherited; 00329 00330 protected: 00331 int nsamples; 00332 Vec quantiles; 00333 Array<Vec> data; 00334 00335 public: 00336 virtual string info() const { return "quantiles(" + tostring(quantiles) + ")"; } 00337 virtual void init(int inputsize); 00338 virtual void update(const Vec& input); 00339 virtual bool finish(); 00340 QuantilesStatsIterator(){} 00341 QuantilesStatsIterator(Vec quantiles, int n_data=1000); 00342 PLEARN_DECLARE_OBJECT(QuantilesStatsIterator); 00343 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00344 00345 virtual void oldwrite(ostream& out) const; 00346 virtual void oldread(istream& in); 00347 00348 protected: 00349 static void declareOptions(OptionList& ol); 00350 }; 00351 00352 DECLARE_OBJECT_PTR(QuantilesStatsIterator); 00353 00354 typedef PP<StatsIterator> StatsIt; 00355 00356 00357 00358 class StatsItArray: public Array<StatsIt> 00359 { 00360 public: 00361 StatsItArray(); 00362 StatsItArray(const StatsIt& statsit); 00363 StatsItArray(const StatsIt& statsit1, const StatsIt& statsit2); 00364 00365 StatsItArray(const Array<StatsIt>& va): Array<StatsIt>(va) {} 00366 StatsItArray(Array<StatsIt>& va): Array<StatsIt>(va) {} 00367 StatsItArray(const StatsItArray& va): Array<StatsIt>(va) {} 00368 StatsItArray& operator&=(const StatsIt& v) 00369 { PLearn::operator&=(*this,v); return *this;} 00370 StatsItArray& operator&=(const StatsItArray& va) 00371 { PLearn::operator&=(*this,va); return *this; } 00372 StatsItArray operator&(const StatsIt& v) const 00373 { return PLearn::operator&(*this,v); } 00374 StatsItArray operator&(const StatsItArray& va) const 00375 { return PLearn::operator&(*this,va); } 00376 00377 void init(int inputsize); 00378 void update(const Vec& input); 00379 void update(const Mat& inputs); 00380 00382 bool requiresMultiplePasses(); 00383 00385 StatsItArray finish(); 00386 00387 Array<Vec> getResults(); 00388 00389 Array<Vec> computeStats(VMat data); 00390 }; 00391 00392 DECLARE_TYPE_TRAITS(StatsItArray); 00393 00394 inline PStream &operator>>(PStream &in, StatsItArray &o) 00395 { in >> static_cast<Array<StatsIt> &>(o); return in; } 00396 00397 inline PStream &operator<<(PStream &out, const StatsItArray &o) 00398 { out << static_cast<const Array<StatsIt> &>(o); return out; } 00399 00400 00401 template <> 00402 inline void deepCopyField(StatsItArray& field, CopiesMap& copies) 00403 { field.makeDeepCopyFromShallowCopy(copies); } 00404 00405 inline StatsItArray operator&(const StatsIt& statsit1, const StatsIt& statsit2) 00406 { return StatsItArray(statsit1,statsit2); } 00407 00408 inline StatsIt mean_stats() { return new MeanStatsIterator(); } 00409 inline StatsIt stddev_stats() { return new StddevStatsIterator(); } 00410 inline StatsIt stderr_stats() { return new StderrStatsIterator(); } 00411 inline StatsIt min_stats() { return new MinStatsIterator(); } 00412 inline StatsIt max_stats() { return new MaxStatsIterator(); } 00413 inline StatsIt quantiles_stats(Vec quantiles, int n_data=1000) 00414 { return new QuantilesStatsIterator(quantiles,n_data); } 00415 inline StatsIt lift_stats(int the_index=0, real the_fraction=0.1) { return new LiftStatsIterator(the_index, the_fraction); } 00416 inline StatsIt sharpe_ratio_stats() { return new SharpeRatioStatsIterator(); } 00417 00419 inline StatsIt exp_mean_stats() { return new ExpMeanStatsIterator(); } 00420 00421 } // end of namespace PLearn 00422 00423 #endif 00424 00425 00426 00427

Generated on Tue Aug 17 16:07:31 2004 for PLearn by doxygen 1.3.7