PLearn 0.1
RemoveObservationTest.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // RemoveObservationTest.cc
00004 //
00005 // Copyright (C) 2006 Christian Dorion
00006 //
00007 // Redistribution and use in source and binary forms, with or without
00008 // modification, are permitted provided that the following conditions are met:
00009 //
00010 //  1. Redistributions of source code must retain the above copyright
00011 //     notice, this list of conditions and the following disclaimer.
00012 //
00013 //  2. Redistributions in binary form must reproduce the above copyright
00014 //     notice, this list of conditions and the following disclaimer in the
00015 //     documentation and/or other materials provided with the distribution.
00016 //
00017 //  3. The name of the authors may not be used to endorse or promote
00018 //     products derived from this software without specific prior written
00019 //     permission.
00020 //
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 //
00032 // This file is part of the PLearn library. For more information on the PLearn
00033 // library, go to the PLearn Web site at www.plearn.org
00034 
00035 // Authors: Christian Dorion
00036 
00040 #include "RemoveObservationTest.h"
00041 #include <plearn/math/VecStatsCollector.h>
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 PLEARN_IMPLEMENT_OBJECT(
00047     RemoveObservationTest,
00048     "Test for the remove observation mechanism.",
00049     ""
00050 );
00051 
00053 // RemoveObservationTest //
00055 RemoveObservationTest::RemoveObservationTest()
00056 {}
00057 
00059 // build //
00061 void RemoveObservationTest::build()
00062 {
00063     inherited::build();
00064     build_();
00065 }
00066 
00068 // makeDeepCopyFromShallowCopy //
00070 void RemoveObservationTest::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00071 {
00072     deepCopyField(m_windowed_vsc, copies);
00073     inherited::makeDeepCopyFromShallowCopy(copies);
00074 }
00075 
00077 // declareOptions //
00079 void RemoveObservationTest::declareOptions(OptionList& ol)
00080 {
00081     // Now call the parent class' declareOptions
00082     inherited::declareOptions(ol);
00083 }
00084 
00086 // build_ //
00088 void RemoveObservationTest::build_()
00089 {
00090 }
00091 
00093 // compareStats //
00095 bool RemoveObservationTest::
00096 compareStats(int t, const string& stat,
00097              const VecStatsCollector& batch, const VecStatsCollector& online)
00098 {
00099     int len = batch.length();
00100     PLASSERT(len==online.length());
00101     
00102     Mat batch_stats(1, len, batch.getAllStats(stat));
00103     Mat online_stats(1, len, online.getAllStats(stat));
00104 
00105     if ( !batch_stats.isEqual(online_stats, 5e-6) )
00106     {
00107         cerr << "At time " << t << " " << stat << " differ: " << endl
00108              << "batch\n  " << batch_stats(0) << endl    
00109              << "online\n  " << online_stats(0) << endl << endl;
00110         return true;
00111     }
00112     return false;
00113 }
00114 
00115 bool RemoveObservationTest::
00116 compareCovariance(int t,
00117                   const VecStatsCollector& batch, const VecStatsCollector& online)
00118 {
00119     PLASSERT(batch.length()==online.length());
00120     
00121     batch.getCovariance(m_batch_cov);
00122     online.getCovariance(m_online_cov);
00123 
00124     if ( !m_batch_cov.isEqual(m_online_cov, 5e-6) )
00125     {
00126         cerr << "At time " << t << " covariance differ!" << endl
00127              << "batch\n  " << m_batch_cov(0) << endl    
00128              << "online\n  " << m_online_cov(0) << endl << endl;
00129         return true;
00130     }
00131     return false;
00132 }
00133 
00135 // perform //
00137 void RemoveObservationTest::perform()
00138 {
00139     int N = 8;
00140     int T = 2500;
00141     
00142     Vec obs(N);
00143     VecStatsCollector vsc;
00144     vsc.compute_covariance = true;
00145     vsc.no_removal_warnings = true;
00146     vsc.build();
00147 
00148     m_windowed_vsc.m_window = 100;
00149     m_windowed_vsc.compute_covariance = true;
00150     m_windowed_vsc.no_removal_warnings = true;
00151     m_windowed_vsc.build();
00152 
00153     for (int t=0; t<T; t++)
00154     {        
00155         int half_n = N/2;
00156 
00157         // Online window management
00158         for (int n=0; n<N; n++)
00159             obs[n] = (t+1) * pow(10.0, n-half_n);
00160         m_windowed_vsc.update(obs);
00161 
00162         // Batch window management
00163         vsc.forget();
00164         vsc.update(m_windowed_vsc.getObservations());
00165 
00166         bool stop =    compareStats(t, "N"          , vsc, m_windowed_vsc);
00167         stop = stop || compareStats(t, "NMISSING"   , vsc, m_windowed_vsc);
00168         stop = stop || compareStats(t, "NNONMISSING", vsc, m_windowed_vsc);
00169         stop = stop || compareStats(t, "E"          , vsc, m_windowed_vsc);
00170         stop = stop || compareStats(t, "V"          , vsc, m_windowed_vsc);
00171         stop = stop || compareStats(t, "STDDEV"     , vsc, m_windowed_vsc);
00172         stop = stop || compareStats(t, "STDERROR"   , vsc, m_windowed_vsc);
00173         stop = stop || compareStats(t, "SKEW"       , vsc, m_windowed_vsc);
00174         stop = stop || compareStats(t, "KURT"       , vsc, m_windowed_vsc);
00175 
00176         // Special covariance treatment
00177         stop = stop || compareCovariance(t, vsc, m_windowed_vsc);
00178         
00179         if( stop )
00180             break;
00181     }
00182         
00183     
00184     // cout << "E: " << vsc.getAllStats("E") << endl;
00185     // cout << "V: " << vsc.getAllStats("V") << endl;           
00186     // cout << "STDDEV: " << vsc.getAllStats("STDDEV") << endl;      
00187     // cout << "STDERROR: " << vsc.getAllStats("STDERROR") << endl;    
00188     // cout << "SKEW: " << vsc.getAllStats("SKEW") << endl;        
00189     // cout << "KURT: " << vsc.getAllStats("KURT") << endl;        
00190     // cout << "MIN: " << vsc.getAllStats("MIN") << endl;         
00191     // cout << "MAX: " << vsc.getAllStats("MAX") << endl;
00192     // cout << "RANGE: " << vsc.getAllStats("RANGE") << endl;       
00193     // cout << "SUM: " << vsc.getAllStats("SUM") << endl;         
00194     // cout << "SUMSQ: " << vsc.getAllStats("SUMSQ") << endl;       
00195     // cout << "FIRST: " << vsc.getAllStats("FIRST") << endl;       
00196     // cout << "LAST: " << vsc.getAllStats("LAST") << endl;        
00197     // cout << "N: " << vsc.getAllStats("N") << endl;           
00198     // cout << "NMISSING: " << vsc.getAllStats("NMISSING") << endl;    
00199     // cout << "NNONMISSING: " << vsc.getAllStats("NNONMISSING") << endl;        
00200 }
00201 
00202 } // end of namespace PLearn
00203 
00204 
00205 /*
00206   Local Variables:
00207   mode:c++
00208   c-basic-offset:4
00209   c-file-style:"stroustrup"
00210   c-file-offsets:((innamespace . 0)(inline-open . 0))
00211   indent-tabs-mode:nil
00212   fill-column:79
00213   End:
00214 */
00215 // 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