PLearn 0.1
BootstrapSplitter.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // BootstrapSplitter.cc
00004 //
00005 // Copyright (C) 2003,2007 Olivier Delalleau
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 /* *******************************************************
00036  * $Id: BootstrapSplitter.cc 6861 2007-04-09 19:04:15Z saintmlx $
00037  ******************************************************* */
00038 
00041 #include "BootstrapSplitter.h"
00042 #include "BootstrapVMatrix.h"
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00048 // BootstrapSplitter //
00050 BootstrapSplitter::BootstrapSplitter():
00051     frac(0.6667),
00052     n_splits(0),
00053     allow_repetitions(false),
00054     seed(1827),
00055     rgen(new PRandom())
00056 {}
00057 
00058 PLEARN_IMPLEMENT_OBJECT(BootstrapSplitter,
00059         "A splitter whose splits are bootstrap samples of the original dataset.",
00060         "Each split of this splitter contains a single set, which is a\n"
00061         "bootstrap sample of the original dataset. Note that, by default,\n"
00062         "samples are taken without repetition: in order to perform a 'real'\n"
00063         "bootstrap, the option 'allow_repetitions thus must be set to 1.\n"
00064 );
00065 
00067 // declareOptions //
00069 void BootstrapSplitter::declareOptions(OptionList& ol)
00070 {
00071     declareOption(ol, "n_splits", &BootstrapSplitter::n_splits, OptionBase::buildoption,
00072                   "Number of splits wanted.");
00073 
00074     declareOption(ol, "frac", &BootstrapSplitter::frac, OptionBase::buildoption,
00075                   "Fraction of elements to take in each bootstrap.");
00076 
00077     declareOption(ol, "allow_repetitions", &BootstrapSplitter::allow_repetitions, 
00078                   OptionBase::buildoption,
00079                   "Allows each row to appear more than once per split.");
00080 
00081     declareOption(ol, "seed", &BootstrapSplitter::seed, 
00082                   OptionBase::buildoption,
00083                   "Seed for the random number generator.");
00084 
00085     // Now call the parent class' declareOptions
00086     inherited::declareOptions(ol);
00087 }
00088 
00090 // build_ //
00092 void BootstrapSplitter::build_()
00093 {
00094     if (dataset) {
00095         rgen->manual_seed(seed);
00096         bootstrapped_sets.resize(0,0); // First clear the current sets.
00097         bootstrapped_sets.resize(n_splits,1);
00098         for (int i = 0; i < n_splits; i++) {
00099             // Construct a new bootstrap sample from the dataset.
00100             PP<PRandom> vmat_rgen= rgen->split();
00101             // Note: indices in the bootstrapped sets are sorted, so that
00102             // access may be faster (e.g. when reading large data from disk).
00103             bootstrapped_sets(i,0) = 
00104                 new BootstrapVMatrix(dataset,frac,vmat_rgen, 
00105                                      false, allow_repetitions);
00106         }
00107     } else {
00108         bootstrapped_sets.resize(0,0);
00109     }
00110 }
00111 
00113 // build //
00115 void BootstrapSplitter::build()
00116 {
00117     inherited::build();
00118     build_();
00119 }
00120 
00122 // makeDeepCopyFromShallowCopy //
00124 void BootstrapSplitter::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00125 {
00126     inherited::makeDeepCopyFromShallowCopy(copies);
00127     deepCopyField(bootstrapped_sets, copies);
00128     deepCopyField(rgen,              copies);
00129 }
00130 
00132 // nsplits //
00134 int BootstrapSplitter::nsplits() const
00135 {
00136     return n_splits;
00137 }
00138 
00140 // nSetsPerSplit //
00142 int BootstrapSplitter::nSetsPerSplit() const
00143 {
00144     // One single set per split.
00145     return 1;
00146 }
00147 
00148 TVec<VMat> BootstrapSplitter::getSplit(int k)
00149 {
00150     // ### Build and return the kth split
00151     if (k >= n_splits) {
00152         PLERROR("BootstrapSplitter::getSplit: k is too high");
00153     } else if (k >= bootstrapped_sets.length()) {
00154         PLERROR("BootstrapSplitter::getSplit: you asked for a split but they're not ready yet");
00155     }
00156     return bootstrapped_sets(k);
00157 }
00158 
00160 // setDataSet //
00162 void BootstrapSplitter::setDataSet(VMat the_dataset) {
00163     inherited::setDataSet(the_dataset);
00164     build(); // necessary to recompute the bootstrap samples.
00165 }
00166 
00167 } // end of namespace PLearn
00168 
00169 
00170 /*
00171   Local Variables:
00172   mode:c++
00173   c-basic-offset:4
00174   c-file-style:"stroustrup"
00175   c-file-offsets:((innamespace . 0)(inline-open . 0))
00176   indent-tabs-mode:nil
00177   fill-column:79
00178   End:
00179 */
00180 // 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