PLearn 0.1
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 8235 2007-11-07 21:32:01Z nouiz $
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(CopiesMap& copies);
00095     PLEARN_DECLARE_ABSTRACT_OBJECT(StatsIterator);
00096     
00097     virtual void build() {}
00098     virtual void oldwrite(ostream& out) const;
00099     /* TODO Remove (deprecate)
00100        virtual void oldread(istream& in);
00101     */
00102 
00103 protected:
00104     static void declareOptions(OptionList& ol);
00105 
00106 };
00107 
00108 DECLARE_OBJECT_PTR(StatsIterator);
00109 
00110 class MeanStatsIterator: public StatsIterator
00111 {
00112     typedef StatsIterator inherited;
00113 
00114 protected:
00115     TVec<int> nsamples;
00116 
00117 public:
00118     virtual string info() const { return "mean"; }
00119     virtual void init(int inputsize);
00120     virtual void update(const Vec& input);
00121     virtual bool finish();
00122     PLEARN_DECLARE_OBJECT(MeanStatsIterator);
00123 
00124     virtual void oldwrite(ostream& out) const;
00125     /* TODO Remove (deprecated)
00126        virtual void oldread(istream& in);
00127     */
00128 
00129 protected:
00130     static void declareOptions(OptionList& ol);
00131 
00132 };
00133 
00134 DECLARE_OBJECT_PTR(MeanStatsIterator);
00135 
00136 class ExpMeanStatsIterator: public StatsIterator
00137 {
00138     typedef StatsIterator inherited;
00139 
00140 protected:
00141     TVec<int> nsamples;
00142 
00143 public:
00144     virtual string info() const { return "exp_mean"; }
00145 
00146     virtual void init(int inputsize);
00147     virtual void update(const Vec& input);
00148     virtual bool finish();
00149     PLEARN_DECLARE_OBJECT(ExpMeanStatsIterator);
00150 
00151     virtual void oldwrite(ostream& out) const;
00152     /* TODO Remove (deprecated)
00153        virtual void oldread(istream& in);
00154     */
00155 
00156 protected:
00157     static void declareOptions(OptionList& ol);
00158 
00159 };
00160 
00161 DECLARE_OBJECT_PTR(ExpMeanStatsIterator);
00162 
00163 class StddevStatsIterator: public StatsIterator
00164 {
00165     typedef StatsIterator inherited;
00166     
00167 protected:
00168     Vec meansquared;
00169     Vec mean;
00170     TVec<int> nsamples;
00171 
00172 public:
00173     virtual string info() const { return "std_dev"; }
00174     virtual void init(int inputsize);
00175     virtual void update(const Vec& input);
00176     virtual bool finish();
00177     PLEARN_DECLARE_OBJECT(StddevStatsIterator);
00178     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00179 
00180     virtual void oldwrite(ostream& out) const;
00181     /* TODO Remove (deprecated)
00182        virtual void oldread(istream& in);
00183     */
00184 
00185 protected:
00186     static void declareOptions(OptionList& ol);
00187 
00188 };
00189 
00190 DECLARE_OBJECT_PTR(StddevStatsIterator);
00191 
00192 class StderrStatsIterator: public StatsIterator
00193 {
00194     typedef StatsIterator inherited;
00195     
00196 protected:
00197     Vec meansquared;
00198     Vec mean;
00199     TVec<int> nsamples;
00200 
00201 public:
00202     virtual string info() const { return "std_err"; }
00203     virtual void init(int inputsize);
00204     virtual void update(const Vec& input);
00205     virtual bool finish();
00206     PLEARN_DECLARE_OBJECT(StderrStatsIterator);
00207     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00208 
00209     virtual void oldwrite(ostream& out) const;
00210     /* TODO Remove (deprecated)
00211        virtual void oldread(istream& in);
00212     */
00213 
00214 protected:
00215     static void declareOptions(OptionList& ol);
00216 
00217 };
00218 
00219 DECLARE_OBJECT_PTR(StderrStatsIterator);
00220 
00227 class SharpeRatioStatsIterator: public StatsIterator
00228 {
00229     typedef StatsIterator inherited;
00230     
00231 protected:
00232     Vec nnonzero;
00233     Vec meansquared;
00234     Vec mean;
00235 
00236 public:
00237     virtual string info() const { return "sharpe_ratio"; }
00238     virtual void init(int inputsize);
00239     virtual void update(const Vec& input);
00240     virtual bool finish();
00241     PLEARN_DECLARE_OBJECT(SharpeRatioStatsIterator);
00242     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00243 
00244     virtual void oldwrite(ostream& out) const;
00245     /* TODO Remove (deprecated)
00246        virtual void oldread(istream& in);
00247     */
00248 
00249 protected:
00250     static void declareOptions(OptionList& ol);
00251 
00252 };
00253 
00254 DECLARE_OBJECT_PTR(SharpeRatioStatsIterator);
00255 
00256 class MinStatsIterator: public StatsIterator
00257 {
00258     typedef StatsIterator inherited;
00259     
00260 public:
00261     virtual string info() const { return "min"; }
00262     virtual void init(int inputsize);
00263     virtual void update(const Vec& input);
00264     virtual bool finish();
00265     PLEARN_DECLARE_OBJECT(MinStatsIterator);
00266 
00267     virtual void oldwrite(ostream& out) const;
00268     /* TODO Remove (deprecated)
00269        virtual void oldread(istream& in);
00270     */
00271 
00272 protected:
00273     static void declareOptions(OptionList& ol);
00274 
00275 };
00276 
00277 DECLARE_OBJECT_PTR(MinStatsIterator);
00278 
00279 class MaxStatsIterator: public StatsIterator
00280 {
00281     typedef StatsIterator inherited;
00282     
00283 public:
00284     virtual string info() const { return "max"; }
00285     virtual void init(int inputsize);
00286     virtual void update(const Vec& input);
00287     virtual bool finish();
00288     PLEARN_DECLARE_OBJECT(MaxStatsIterator);
00289 
00290     virtual void oldwrite(ostream& out) const;
00291     /* TODO Remove (deprecated)
00292        virtual void oldread(istream& in);
00293     */
00294 
00295 protected:
00296     static void declareOptions(OptionList& ol);
00297 
00298 };
00299 
00300 DECLARE_OBJECT_PTR(MaxStatsIterator);
00301 
00307 class LiftStatsIterator: public StatsIterator
00308 {
00309     typedef StatsIterator inherited;
00310     
00311 protected:
00312     int nsamples;
00313     int lift_index;
00314     real lift_fraction;
00315     Mat output_and_pos;
00316     Vec targets;
00317 
00318 public:
00319     virtual string info() const { return "lift"; }
00320     virtual void init(int inputsize);
00321     virtual void update(const Vec& input);
00322     virtual bool finish();
00323 
00324     LiftStatsIterator(int the_index=0, real the_fraction=0.1);
00325     PLEARN_DECLARE_OBJECT(LiftStatsIterator);
00326     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00327 
00328     virtual void oldwrite(ostream& out) const;
00329     /* TODO Remove (deprecated)
00330        virtual void oldread(istream& in);
00331     */
00332 
00333 protected:
00334     static void declareOptions(OptionList& ol);
00335 
00336 };
00337 
00338 DECLARE_OBJECT_PTR(LiftStatsIterator);
00339 
00344 class QuantilesStatsIterator: public StatsIterator
00345 {
00346     typedef StatsIterator inherited;
00347     
00348 protected:
00349     int nsamples;
00350     Vec quantiles;
00351     Array<Vec> data;
00352 
00353 public:
00354     virtual string info() const { return "quantiles(" + tostring(quantiles) + ")"; }
00355     virtual void init(int inputsize);
00356     virtual void update(const Vec& input);
00357     virtual bool finish();
00358     QuantilesStatsIterator(){}
00359     QuantilesStatsIterator(Vec quantiles, int n_data=1000);
00360     PLEARN_DECLARE_OBJECT(QuantilesStatsIterator);
00361     virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00362 
00363     virtual void oldwrite(ostream& out) const;
00364     /* TODO Remove (deprecated)
00365        virtual void oldread(istream& in);
00366     */
00367 
00368 protected:
00369     static void declareOptions(OptionList& ol);
00370 };
00371 
00372 DECLARE_OBJECT_PTR(QuantilesStatsIterator);
00373 
00374 typedef PP<StatsIterator> StatsIt;
00375 
00376 
00377 
00378 class StatsItArray: public Array<StatsIt>
00379 {
00380 public:
00381     StatsItArray();
00382     StatsItArray(const StatsIt& statsit);
00383     StatsItArray(const StatsIt& statsit1, const StatsIt& statsit2);
00384 
00385     StatsItArray(const Array<StatsIt>& va): Array<StatsIt>(va) {}
00386     StatsItArray(Array<StatsIt>& va): Array<StatsIt>(va) {}
00387     StatsItArray(const StatsItArray& va): Array<StatsIt>(va) {}
00388     StatsItArray& operator&=(const StatsIt& v) 
00389     { PLearn::operator&=(*this,v); return *this;}
00390     StatsItArray& operator&=(const StatsItArray& va) 
00391     { PLearn::operator&=(*this,va); return *this; }
00392     StatsItArray operator&(const StatsIt& v) const 
00393     { return PLearn::operator&(*this,v); }
00394     StatsItArray operator&(const StatsItArray& va) const 
00395     { return PLearn::operator&(*this,va); }
00396 
00397     void init(int inputsize);
00398     void update(const Vec& input);
00399     void update(const Mat& inputs);
00400 
00402     bool requiresMultiplePasses();
00403 
00405     StatsItArray finish();
00406 
00407     Array<Vec> getResults();
00408 
00409     Array<Vec> computeStats(VMat data);
00410 };
00411 
00412 DECLARE_TYPE_TRAITS(StatsItArray);
00413 
00414 inline PStream &operator>>(PStream &in, StatsItArray &o)
00415 { in >> static_cast<Array<StatsIt> &>(o); return in; }
00416 
00417 inline PStream &operator<<(PStream &out, const StatsItArray &o)
00418 { out << static_cast<const Array<StatsIt> &>(o); return out; }
00419 
00420 
00421 template <>
00422 inline void deepCopyField(StatsItArray& field, CopiesMap& copies)
00423 { field.makeDeepCopyFromShallowCopy(copies); }
00424 
00425 inline StatsItArray operator&(const StatsIt& statsit1, const StatsIt& statsit2)
00426 { return StatsItArray(statsit1,statsit2); }
00427 
00428 inline StatsIt mean_stats() { return new MeanStatsIterator(); }
00429 inline StatsIt stddev_stats() { return new StddevStatsIterator(); }
00430 inline StatsIt stderr_stats() { return new StderrStatsIterator(); }
00431 inline StatsIt min_stats() { return new MinStatsIterator(); }
00432 inline StatsIt max_stats() { return new MaxStatsIterator(); }
00433 inline StatsIt quantiles_stats(Vec quantiles, int n_data=1000)
00434 { return new QuantilesStatsIterator(quantiles,n_data); }
00435 inline StatsIt lift_stats(int the_index=0, real the_fraction=0.1) { return new LiftStatsIterator(the_index, the_fraction); }
00436 inline StatsIt sharpe_ratio_stats() { return new SharpeRatioStatsIterator(); }
00437 
00439 inline StatsIt exp_mean_stats() { return new ExpMeanStatsIterator(); }
00440 
00441 } // end of namespace PLearn
00442 
00443 #endif
00444 
00445 
00446 /*
00447   Local Variables:
00448   mode:c++
00449   c-basic-offset:4
00450   c-file-style:"stroustrup"
00451   c-file-offsets:((innamespace . 0)(inline-open . 0))
00452   indent-tabs-mode:nil
00453   fill-column:79
00454   End:
00455 */
00456 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines