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

StatsIterator.cc

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.cc,v 1.7 2004/05/19 17:23:00 tihocan Exp $ 00039 * This file is part of the PLearn library. 00040 ******************************************************* */ 00041 00042 #include "StatsIterator.h" 00043 #include "TMat_maths.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 // ******************* 00049 // ** StatsIterator ** 00050 // ******************* 00051 00052 PLEARN_IMPLEMENT_ABSTRACT_OBJECT(StatsIterator, "ONE LINE DESCR", "NO HELP"); 00053 void StatsIterator::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00054 { 00055 inherited::makeDeepCopyFromShallowCopy(copies); 00056 deepCopyField(result, copies); 00057 } 00058 00059 bool StatsIterator::requiresMultiplePasses() { return false; } 00060 00061 Vec StatsIterator::getResult() { return result; } 00062 00063 void StatsIterator::declareOptions(OptionList& ol) 00064 { 00065 declareOption(ol, "result", &StatsIterator::result, OptionBase::learntoption, 00066 " result\n"); 00067 00068 inherited::declareOptions(ol); 00069 } 00070 00071 void StatsIterator::oldwrite(ostream& out) const 00072 { 00073 writeHeader(out,"StatsIterator"); 00074 //inherited::write(out); 00075 writeField(out,"result",result); 00076 writeFooter(out,"StatsIterator"); 00077 } 00078 00079 void StatsIterator::oldread(istream& in) 00080 { 00081 readHeader(in,"StatsIterator"); 00082 //inherited::read(int); 00083 readField(in,"result",result); 00084 readFooter(in,"StatsIterator"); 00085 } 00086 00087 // *********************** 00088 // ** MeanStatsIterator ** 00089 // *********************** 00090 00091 PLEARN_IMPLEMENT_OBJECT(MeanStatsIterator, "ONE LINE DESCR", "NO HELP"); 00092 00093 void MeanStatsIterator::init(int inputsize) 00094 { 00095 // We do not use resize on purpose, so 00096 // that the previous result Vec does not get overwritten 00097 result = Vec(inputsize); 00098 nsamples.resize(inputsize); 00099 nsamples.clear(); 00100 } 00101 00102 void MeanStatsIterator::update(const Vec& input) 00103 { 00104 addIfNonMissing(input,nsamples,result); 00105 } 00106 00107 bool MeanStatsIterator::finish() 00108 { 00109 for (int i=0;i<result.length();i++) 00110 result[i] /= nsamples[i]; 00111 return true; 00112 } 00113 00114 void MeanStatsIterator::declareOptions(OptionList& ol) 00115 { 00116 declareOption(ol, "nsamples", &MeanStatsIterator::nsamples, OptionBase::learntoption, 00117 " nsamples\n"); 00118 00119 inherited::declareOptions(ol); 00120 } 00121 00122 00123 void MeanStatsIterator::oldwrite(ostream& out) const 00124 { 00125 writeHeader(out,"MeanStatsIterator"); 00126 inherited::write(out); 00127 writeField(out,"nsamples",nsamples); 00128 writeFooter(out,"MeanStatsIterator"); 00129 } 00130 00131 void MeanStatsIterator::oldread(istream& in) 00132 { 00133 readHeader(in,"MeanStatsIterator"); 00134 inherited::oldread(in); 00135 readField(in,"nsamples",nsamples); 00136 readFooter(in,"MeanStatsIterator"); 00137 } 00138 00139 // *********************** 00140 // ** ExpMeanStatsIterator ** 00141 // *********************** 00142 00143 PLEARN_IMPLEMENT_OBJECT(ExpMeanStatsIterator, "ONE LINE DESCR", "NO HELP"); 00144 00145 void ExpMeanStatsIterator::init(int inputsize) 00146 { 00147 // We do not use resize on purpose, so 00148 // that the previous result Vec does not get overwritten 00149 result = Vec(inputsize); 00150 nsamples.resize(inputsize); 00151 nsamples.clear(); 00152 } 00153 00154 void ExpMeanStatsIterator::update(const Vec& input) 00155 { 00156 addIfNonMissing(input,nsamples,result); 00157 } 00158 00159 bool ExpMeanStatsIterator::finish() 00160 { 00161 for (int i=0;i<result.length();i++) 00162 result[i] = exp(result[i]/nsamples[i]); 00163 return true; 00164 } 00165 00166 void ExpMeanStatsIterator::declareOptions(OptionList& ol) 00167 { 00168 declareOption(ol, "nsamples", &ExpMeanStatsIterator::nsamples, OptionBase::learntoption, 00169 " nsamples\n"); 00170 00171 inherited::declareOptions(ol); 00172 } 00173 00174 void ExpMeanStatsIterator::oldwrite(ostream& out) const 00175 { 00176 writeHeader(out,"ExpMeanStatsIterator"); 00177 inherited::write(out); 00178 writeField(out,"nsamples",nsamples); 00179 writeFooter(out,"ExpMeanStatsIterator"); 00180 } 00181 00182 void ExpMeanStatsIterator::oldread(istream& in) 00183 { 00184 readHeader(in,"ExpMeanStatsIterator"); 00185 inherited::oldread(in); 00186 readField(in,"nsamples",nsamples); 00187 readFooter(in,"ExpMeanStatsIterator"); 00188 } 00189 00190 // ************************* 00191 // ** StddevStatsIterator ** 00192 // ************************* 00193 00194 PLEARN_IMPLEMENT_OBJECT(StddevStatsIterator, "ONE LINE DESCR", "NO HELP"); 00195 00196 void StddevStatsIterator::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00197 { 00198 inherited::makeDeepCopyFromShallowCopy(copies); 00199 deepCopyField(mean, copies); 00200 deepCopyField(meansquared, copies); 00201 } 00202 00203 void StddevStatsIterator::init(int inputsize) 00204 { 00205 // We do not use resize on purpose, so 00206 // that the previous result Vec does not get overwritten 00207 meansquared = Vec(inputsize); 00208 mean = Vec(inputsize); 00209 nsamples.resize(inputsize); 00210 nsamples.clear(); 00211 } 00212 00213 void StddevStatsIterator::update(const Vec& input) 00214 { 00215 addXandX2IfNonMissing(input,nsamples,mean,meansquared); 00216 } 00217 00218 bool StddevStatsIterator::finish() 00219 { 00220 Vec square_mean(mean.length()); 00221 for (int i=0;i<mean.length();i++) 00222 { 00223 //mean[i] /= nsamples[i]; 00224 real n = nsamples[i]; 00225 square_mean[i] = mean[i]*mean[i]/(n*(n-1.0)); 00226 meansquared[i] /= n-1.0; 00227 } 00228 //squareSubtract(meansquared, mean); 00229 meansquared -= square_mean; 00230 result = sqrt(meansquared); 00231 return true; 00232 } 00233 00234 void StddevStatsIterator::declareOptions(OptionList& ol) 00235 { 00236 declareOption(ol, "mean", &StddevStatsIterator::mean, OptionBase::learntoption, 00237 " mean\n"); 00238 00239 declareOption(ol, "meansquared", &StddevStatsIterator::meansquared, OptionBase::learntoption, 00240 " meansquared\n"); 00241 00242 declareOption(ol, "nsamples", &StddevStatsIterator::nsamples, OptionBase::learntoption, 00243 " nsamples\n"); 00244 00245 inherited::declareOptions(ol); 00246 } 00247 00248 void StddevStatsIterator::oldwrite(ostream& out) const 00249 { 00250 writeHeader(out,"StddevStatsIterator"); 00251 inherited::write(out); 00252 writeField(out,"mean",mean); 00253 writeField(out,"meansquared",meansquared); 00254 writeField(out,"nsamples",nsamples); 00255 writeFooter(out,"StddevStatsIterator"); 00256 } 00257 00258 void StddevStatsIterator::oldread(istream& in) 00259 { 00260 readHeader(in,"StddevStatsIterator"); 00261 inherited::oldread(in); 00262 readField(in,"mean",mean); 00263 readField(in,"meansquared",meansquared); 00264 readField(in,"nsamples",nsamples); 00265 readFooter(in,"StddevStatsIterator"); 00266 } 00267 00268 // ************************* 00269 // ** StderrStatsIterator ** 00270 // ************************* 00271 00272 PLEARN_IMPLEMENT_OBJECT(StderrStatsIterator, "ONE LINE DESCR", "NO HELP"); 00273 00274 void StderrStatsIterator::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00275 { 00276 inherited::makeDeepCopyFromShallowCopy(copies); 00277 deepCopyField(mean, copies); 00278 deepCopyField(meansquared, copies); 00279 } 00280 00281 void StderrStatsIterator::init(int inputsize) 00282 { 00283 // We do not use resize on purpose, so 00284 // that the previous result Vec does not get overwritten 00285 meansquared = Vec(inputsize); 00286 mean = Vec(inputsize); 00287 nsamples.resize(inputsize); 00288 nsamples.clear(); 00289 } 00290 00291 void StderrStatsIterator::update(const Vec& input) 00292 { 00293 addXandX2IfNonMissing(input,nsamples,mean,meansquared); 00294 } 00295 00296 bool StderrStatsIterator::finish() 00297 { 00298 for (int i=0;i<mean.length();i++) 00299 { 00300 mean[i] /= nsamples[i]; 00301 meansquared[i] /= nsamples[i]-1; 00302 } 00303 squareSubtract(meansquared, mean); 00304 for (int i = 0; i < mean.length(); i++) { 00305 result[i] = sqrt(meansquared[i] / real(nsamples[i])); 00306 } 00307 return true; 00308 } 00309 00310 void StderrStatsIterator::declareOptions(OptionList& ol) 00311 { 00312 declareOption(ol, "mean", &StderrStatsIterator::mean, OptionBase::learntoption, 00313 " mean\n"); 00314 00315 declareOption(ol, "meansquared", &StderrStatsIterator::meansquared, OptionBase::learntoption, 00316 " meansquared\n"); 00317 00318 declareOption(ol, "nsamples", &StderrStatsIterator::nsamples, OptionBase::learntoption, 00319 " nsamples\n"); 00320 00321 inherited::declareOptions(ol); 00322 } 00323 00324 void StderrStatsIterator::oldwrite(ostream& out) const 00325 { 00326 writeHeader(out,"StderrStatsIterator"); 00327 inherited::write(out); 00328 writeField(out,"mean",mean); 00329 writeField(out,"meansquared",meansquared); 00330 writeField(out,"nsamples",nsamples); 00331 writeFooter(out,"StderrStatsIterator"); 00332 } 00333 00334 void StderrStatsIterator::oldread(istream& in) 00335 { 00336 readHeader(in,"StderrStatsIterator"); 00337 inherited::oldread(in); 00338 readField(in,"mean",mean); 00339 readField(in,"meansquared",meansquared); 00340 readField(in,"nsamples",nsamples); 00341 readFooter(in,"StderrStatsIterator"); 00342 } 00343 00344 // ************************* 00345 // ** SharpeRatioStatsIterator ** 00346 // ************************* 00347 00348 PLEARN_IMPLEMENT_OBJECT(SharpeRatioStatsIterator, "ONE LINE DESCR", "NO HELP"); 00349 00350 void SharpeRatioStatsIterator::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00351 { 00352 inherited::makeDeepCopyFromShallowCopy(copies); 00353 deepCopyField(mean, copies); 00354 deepCopyField(meansquared, copies); 00355 } 00356 00357 void SharpeRatioStatsIterator::init(int inputsize) 00358 { 00359 // We do not use resize on purpose, so 00360 // that the previous result Vec does not get overwritten 00361 meansquared = Vec(inputsize); 00362 mean = Vec(inputsize); 00363 nnonzero = Vec(inputsize); 00364 } 00365 00366 void SharpeRatioStatsIterator::update(const Vec& input) 00367 { 00368 int n=input.length(); 00369 for (int i=0;i<n;i++) 00370 { 00371 real in=input[i]; 00372 if (in!=0) nnonzero[i]++; 00373 mean[i] += in; 00374 meansquared[i] += in*in; 00375 } 00376 } 00377 00378 bool SharpeRatioStatsIterator::finish() 00379 { 00380 mean /= nnonzero; 00381 meansquared /= nnonzero; 00382 squareSubtract(meansquared, mean); 00383 result = mean/sqrt(meansquared); 00384 return true; 00385 } 00386 00387 void SharpeRatioStatsIterator::declareOptions(OptionList& ol) 00388 { 00389 declareOption(ol, "mean", &SharpeRatioStatsIterator::mean, OptionBase::learntoption, 00390 " mean\n"); 00391 00392 declareOption(ol, "meansquared", &SharpeRatioStatsIterator::meansquared, OptionBase::learntoption, 00393 " meansquared\n"); 00394 00395 declareOption(ol, "nnonzero", &SharpeRatioStatsIterator::nnonzero, OptionBase::learntoption, 00396 " nnonzero\n"); 00397 00398 inherited::declareOptions(ol); 00399 } 00400 00401 void SharpeRatioStatsIterator::oldwrite(ostream& out) const 00402 { 00403 writeHeader(out,"SharpeRatioStatsIterator"); 00404 inherited::write(out); 00405 writeField(out,"mean",mean); 00406 writeField(out,"meansquared",meansquared); 00407 writeField(out,"nnonzero",nnonzero); 00408 writeFooter(out,"SharpeRatioStatsIterator"); 00409 } 00410 00411 void SharpeRatioStatsIterator::oldread(istream& in) 00412 { 00413 readHeader(in,"SharpeRatioStatsIterator"); 00414 inherited::oldread(in); 00415 readField(in,"mean",mean); 00416 readField(in,"meansquared",meansquared); 00417 readField(in,"nnonzero",nnonzero); 00418 readFooter(in,"SharpeRatioStatsIterator"); 00419 } 00420 00421 // *********************** 00422 // ** MinStatsIterator ** 00423 // *********************** 00424 00425 PLEARN_IMPLEMENT_OBJECT(MinStatsIterator, "ONE LINE DESCR", "NO HELP"); 00426 00427 void MinStatsIterator::init(int inputsize) 00428 { 00429 result = Vec(inputsize,FLT_MAX); 00430 } 00431 00432 void MinStatsIterator::update(const Vec& input) 00433 { 00434 real* inputdata = input.data(); 00435 real* resultdata = result.data(); 00436 for(int i=0; i<input.length(); i++) 00437 if(inputdata[i]<resultdata[i]) 00438 resultdata[i] = inputdata[i]; 00439 } 00440 00441 bool MinStatsIterator::finish() 00442 { return true; } 00443 00444 void MinStatsIterator::declareOptions(OptionList& ol) 00445 { 00446 inherited::declareOptions(ol); 00447 } 00448 00449 void MinStatsIterator::oldwrite(ostream& out) const 00450 { 00451 writeHeader(out,"MinStatsIterator"); 00452 inherited::write(out); 00453 writeFooter(out,"MinStatsIterator"); 00454 } 00455 00456 void MinStatsIterator::oldread(istream& in) 00457 { 00458 readHeader(in,"MinStatsIterator"); 00459 inherited::oldread(in); 00460 readFooter(in,"MinStatsIterator"); 00461 } 00462 00463 // *********************** 00464 // ** MaxStatsIterator ** 00465 // *********************** 00466 00467 PLEARN_IMPLEMENT_OBJECT(MaxStatsIterator, "ONE LINE DESCR", "NO HELP"); 00468 00469 void MaxStatsIterator::init(int inputsize) 00470 { 00471 // We do not use resize on purpose, so 00472 // that the previous result Vec does not get overwritten 00473 result = Vec(inputsize,-FLT_MAX); 00474 } 00475 00476 void MaxStatsIterator::update(const Vec& input) 00477 { 00478 real* inputdata = input.data(); 00479 real* resultdata = result.data(); 00480 for(int i=0; i<input.length(); i++) 00481 if(inputdata[i]>resultdata[i]) 00482 resultdata[i] = inputdata[i]; 00483 } 00484 00485 bool MaxStatsIterator::finish() 00486 { return true; } 00487 00488 void MaxStatsIterator::declareOptions(OptionList& ol) 00489 { 00490 inherited::declareOptions(ol); 00491 } 00492 00493 void MaxStatsIterator::oldwrite(ostream& out) const 00494 { 00495 writeHeader(out,"MaxStatsIterator"); 00496 inherited::write(out); 00497 writeFooter(out,"MaxStatsIterator"); 00498 } 00499 00500 void MaxStatsIterator::oldread(istream& in) 00501 { 00502 readHeader(in,"MaxStatsIterator"); 00503 inherited::oldread(in); 00504 readFooter(in,"MaxStatsIterator"); 00505 } 00506 00507 00508 // *********************** 00509 // ** LiftStatsIterator ** 00510 // *********************** 00511 00512 PLEARN_IMPLEMENT_OBJECT(LiftStatsIterator, "ONE LINE DESCR", "NO HELP"); 00513 void LiftStatsIterator::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00514 { 00515 StatsIterator::makeDeepCopyFromShallowCopy(copies); 00516 deepCopyField(output_and_pos, copies); 00517 deepCopyField(targets, copies); 00518 } 00519 00520 LiftStatsIterator::LiftStatsIterator(int the_index, real the_fraction) 00521 : lift_index(the_index), lift_fraction(the_fraction) 00522 {} 00523 00524 void LiftStatsIterator::init(int inputsize) 00525 { 00526 // We do not use resize on purpose, so 00527 // that the previous result Vec does not get overwritten 00528 result = Vec(2); 00529 00530 const int initial_length = 1000; 00531 output_and_pos.resize(initial_length, 2); // 1 output + 1 pos 00532 targets.resize(initial_length); 00533 nsamples = 0; 00534 } 00535 00536 void LiftStatsIterator::update(const Vec& input) 00537 { 00538 if (nsamples == output_and_pos.length()) 00539 { 00540 output_and_pos.resize(10*output_and_pos.length(), 2); 00541 targets.resize(10*output_and_pos.length()); 00542 } 00543 00544 output_and_pos(nsamples, 0) = FABS(input[lift_index]); 00545 output_and_pos(nsamples, 1) = nsamples; 00546 targets[nsamples] = (input[lift_index]>0) ? 1 : 0; 00547 nsamples++; 00548 } 00549 00550 bool LiftStatsIterator::finish() 00551 { 00552 output_and_pos.resize(nsamples,2); 00553 targets.resize(nsamples); 00554 00555 const int n_first_samples = int(lift_fraction*nsamples); 00556 const int n_last_samples = nsamples - n_first_samples; 00557 selectAndOrder(output_and_pos, n_last_samples); 00558 /* 00559 Vec first_samples_index = 00560 output_and_pos.subMat(n_last_samples,1,n_first_samples,1).toVecCopy(); 00561 */ 00562 TVec<int> first_samples_index(n_first_samples); 00563 first_samples_index << output_and_pos.subMat(n_last_samples,1,n_first_samples,1); 00564 00565 Vec first_samples_targets = targets(first_samples_index); 00566 real first_samples_perf = sum(first_samples_targets)/n_first_samples; 00567 real targets_perf = sum(targets)/nsamples; 00568 real lift = first_samples_perf/targets_perf*100.0; 00569 result[0] = lift; 00570 real nones = sum(targets); 00571 real max_first_samples_perf = MIN(nones,(real)n_first_samples)/n_first_samples; 00572 real max_lift = max_first_samples_perf/targets_perf*100.0; 00573 result[1] = lift/max_lift; 00574 00575 return true; 00576 } 00577 00578 void LiftStatsIterator::declareOptions(OptionList& ol) 00579 { 00580 declareOption(ol, "nsamples", &LiftStatsIterator::nsamples, OptionBase::learntoption, 00581 " nsamples\n"); 00582 00583 declareOption(ol, "lift_index", &LiftStatsIterator::lift_index, OptionBase::buildoption, 00584 " lift_index\n"); 00585 00586 declareOption(ol, "lift_fraction", &LiftStatsIterator::lift_fraction, OptionBase::buildoption, 00587 " lift_fraction\n"); 00588 00589 declareOption(ol, "output_and_pos", &LiftStatsIterator::output_and_pos, OptionBase::learntoption, 00590 " output_and_pos\n"); 00591 00592 declareOption(ol, "targets", &LiftStatsIterator::targets, OptionBase::learntoption, 00593 " targets\n"); 00594 00595 inherited::declareOptions(ol); 00596 } 00597 00598 void LiftStatsIterator::oldwrite(ostream& out) const 00599 { 00600 writeHeader(out,"LiftStatsIterator"); 00601 inherited::write(out); 00602 writeField(out,"nsamples",nsamples); 00603 writeField(out,"lift_index",lift_index); 00604 writeField(out,"lift_fraction",lift_fraction); 00605 writeField(out,"output_and_pos",output_and_pos); 00606 writeField(out,"targets",targets); 00607 writeFooter(out,"LiftStatsIterator"); 00608 } 00609 00610 void LiftStatsIterator::oldread(istream& in) 00611 { 00612 readHeader(in,"LiftStatsIterator"); 00613 inherited::oldread(in); 00614 readField(in,"nsamples",nsamples); 00615 readField(in,"lift_index",lift_index); 00616 readField(in,"lift_fraction",lift_fraction); 00617 readField(in,"output_and_pos",output_and_pos); 00618 readField(in,"targets",targets); 00619 readFooter(in,"LiftStatsIterator"); 00620 } 00621 00622 // **************************** 00623 // ** QuantilesStatsIterator ** 00624 // **************************** 00625 00626 PLEARN_IMPLEMENT_OBJECT(QuantilesStatsIterator, "ONE LINE DESCR", "NO HELP"); 00627 00628 void QuantilesStatsIterator::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00629 { 00630 StatsIterator::makeDeepCopyFromShallowCopy(copies); 00631 deepCopyField(quantiles, copies); 00632 deepCopyField(data, copies); 00633 } 00634 00635 QuantilesStatsIterator::QuantilesStatsIterator(Vec quantiles_,int n_data) 00636 : nsamples(n_data), quantiles(quantiles_) 00637 { 00638 } 00639 00640 void QuantilesStatsIterator::init(int inputsize) 00641 { 00642 data.resize(inputsize); 00643 for (int i=0;i<inputsize;i++) 00644 { 00645 data[i].resize(nsamples); 00646 data[i].resize(0); 00647 } 00648 nsamples=0; 00649 // We do not use resize on purpose, so 00650 // that the previous result Vec does not get overwritten 00651 result = Vec(quantiles.length()*data.length()); 00652 } 00653 00654 void QuantilesStatsIterator::update(const Vec& input) 00655 { 00656 if (nsamples == data[0].length()) 00657 { 00658 for (int i=0;i<data.length();i++) 00659 { 00660 data[i].resize(10*(nsamples+1)); 00661 data[i].resize(nsamples); 00662 } 00663 } 00664 00665 for (int i=0;i<input.length();i++) 00666 data[i].push_back(input[i]); 00667 nsamples++; 00668 } 00669 00670 bool QuantilesStatsIterator::finish() 00671 { 00672 for (int i=0;i<data.length();i++) 00673 sortElements(data[i]); 00674 real dq =real(1.0)/nsamples; 00675 real hdq = dq*real(0.5); 00676 Mat results = result.toMat(data.length(),quantiles.length()); 00677 results.fill(MISSING_VALUE); 00678 for (int i=0;i<data.length();i++) // loop over "variables" 00679 { 00680 real q=0; 00681 for (int t=0;t<nsamples;t++,q+=dq) 00682 { 00683 for (int j=0;j<quantiles.length();j++) 00684 if (quantiles[j]>q-hdq && quantiles[j]<=q+hdq) 00685 results(i,j) = data[i][t]; 00686 } 00687 } 00688 return true; 00689 } 00690 00691 void QuantilesStatsIterator::declareOptions(OptionList& ol) 00692 { 00693 declareOption(ol, "nsamples", &QuantilesStatsIterator::nsamples, OptionBase::buildoption, 00694 " nsamples\n"); 00695 00696 declareOption(ol, "quantiles", &QuantilesStatsIterator::quantiles, OptionBase::buildoption, 00697 " quantiles\n"); 00698 00699 inherited::declareOptions(ol); 00700 } 00701 00702 void QuantilesStatsIterator::oldwrite(ostream& out) const 00703 { 00704 writeHeader(out,"QuantilesStatsIterator"); 00705 inherited::write(out); 00706 writeField(out,"nsamples",nsamples); 00707 writeField(out,"quantiles",quantiles); 00708 writeFooter(out,"QuantilesStatsIterator"); 00709 } 00710 00711 void QuantilesStatsIterator::oldread(istream& in) 00712 { 00713 readHeader(in,"QuantilesStatsIterator"); 00714 inherited::oldread(in); 00715 readField(in,"nsamples",nsamples); 00716 readField(in,"quantiles",quantiles); 00717 readFooter(in,"QuantilesStatsIterator"); 00718 } 00719 00720 // ****************** 00721 // ** StatsItArray ** 00722 // ****************** 00723 00724 StatsItArray::StatsItArray() 00725 : Array<StatsIt>(0,5) 00726 {} 00727 00728 StatsItArray::StatsItArray(const StatsIt& statsit) 00729 : Array<StatsIt>(1,5) 00730 { (*this)[0] = statsit; } 00731 00732 StatsItArray::StatsItArray(const StatsIt& statsit1, const StatsIt& statsit2) 00733 : Array<StatsIt>(2,5) 00734 { 00735 (*this)[0] = statsit1; 00736 (*this)[1] = statsit2; 00737 } 00738 00739 void StatsItArray::init(int inputsize) 00740 { 00741 for(int k=0; k<size(); k++) 00742 (*this)[k]->init(inputsize); 00743 } 00744 00745 void StatsItArray::update(const Vec& input) 00746 { 00747 for(int k=0; k<size(); k++) 00748 (*this)[k]->update(input); 00749 } 00750 void StatsItArray::update(const Mat& inputs) 00751 { 00752 for (int i=0;i<inputs.length();i++) 00753 { 00754 Vec input = inputs(i); 00755 update(input); 00756 } 00757 } 00758 00759 bool StatsItArray::requiresMultiplePasses() 00760 { 00761 for(int k=0; k<size(); k++) 00762 if ( (*this)[k]->requiresMultiplePasses() ) 00763 return true; 00764 return false; 00765 } 00766 00767 // returns an array of those that are not yet finished 00768 StatsItArray StatsItArray::finish() 00769 { 00770 StatsItArray unfinished; 00771 for(int k=0; k<size(); k++) 00772 if ( ! (*this)[k]->finish() ) 00773 unfinished.append((*this)[k]); 00774 return unfinished; 00775 } 00776 00777 Array<Vec> StatsItArray::getResults() 00778 { 00779 Array<Vec> results(size()); 00780 for(int k=0; k<size(); k++) 00781 results[k] = (*this)[k]->getResult(); 00782 return results; 00783 } 00784 00785 Array<Vec> StatsItArray::computeStats(VMat data) 00786 { 00787 int inputsize = data.width(); 00788 init(inputsize); 00789 00790 Vec input(inputsize); 00791 StatsItArray unfinished = *this; 00792 00793 while(unfinished.size()>0) 00794 { 00795 for(int i=0; i<data.length(); i++) 00796 { 00797 data->getRow(i,input); 00798 unfinished.update(input); 00799 } 00800 unfinished = finish(); 00801 } 00802 00803 return getResults(); 00804 } 00805 00806 } // end of namespace PLearn

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