GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
rmse.h
00001 /*
00002  * The GURLS Package in C++
00003  *
00004  * Copyright (C) 2011-1013, IIT@MIT Lab
00005  * All rights reserved.
00006  *
00007  * authors:  M. Santoro
00008  * email:   msantoro@mit.edu
00009  * website: http://cbcl.mit.edu/IIT@MIT/IIT@MIT.html
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  *
00015  *     * Redistributions of source code must retain the above
00016  *       copyright notice, this list of conditions and the following
00017  *       disclaimer.
00018  *     * Redistributions in binary form must reproduce the above
00019  *       copyright notice, this list of conditions and the following
00020  *       disclaimer in the documentation and/or other materials
00021  *       provided with the distribution.
00022  *     * Neither the name(s) of the copyright holders nor the names
00023  *       of its contributors or of the Massacusetts Institute of
00024  *       Technology or of the Italian Institute of Technology may be
00025  *       used to endorse or promote products derived from this software
00026  *       without specific prior written permission.
00027  *
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00031  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00032  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00033  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00034  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00038  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039  * POSSIBILITY OF SUCH DAMAGE.
00040  */
00041 
00042 
00043 #ifndef _GURLS_RMSE_H_
00044 #define _GURLS_RMSE_H_
00045 
00046 #include "gurls++/perf.h"
00047 
00048 #include "gurls++/utils.h"
00049 #include "gurls++/gvec.h"
00050 #include "gurls++/optmatrix.h"
00051 
00052 #include "float.h"
00053 
00054 namespace gurls {
00055 
00061 template <typename T>
00062 class PerfRmse: public Performance<T>{
00063 
00064 public:
00076     GurlsOptionsList* execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt) throw(gException);
00077 };
00078 
00079 template<typename T>
00080 GurlsOptionsList* PerfRmse<T>::execute(const gMat2D<T>& /*X*/, const gMat2D<T>& Y, const GurlsOptionsList &opt) throw(gException)
00081 {
00082     const unsigned long rows = Y.rows();
00083     const unsigned long cols = Y.cols();
00084 
00085     const T* y_true = Y.getData();
00086 
00087 //    if isfield (opt,'perf')
00088 //        p = opt.perf; % lets not overwrite existing performance measures.
00089 //                  % unless they have the same name
00090 //    end
00091 
00092     GurlsOptionsList* perf;
00093 
00094     if(opt.hasOpt("perf"))
00095     {
00096         GurlsOptionsList* tmp_opt = new GurlsOptionsList("tmp");
00097         tmp_opt->copyOpt("perf", opt);
00098 
00099         perf = GurlsOptionsList::dynacast(tmp_opt->getOpt("perf"));
00100         tmp_opt->removeOpt("perf", false);
00101         delete tmp_opt;
00102 
00103         perf->removeOpt("rmse");
00104         perf->removeOpt("forho");
00105 //        perf->removeOpt("forplot");
00106     }
00107     else
00108         perf = new GurlsOptionsList("perf");
00109 
00110 
00111     const gMat2D<T> &pred = opt.getOptValue<OptMatrix<gMat2D<T> > >("pred");
00112 
00113     T *pred_t = new T[pred.getSize()];
00114     copy(pred_t, pred.getData(), pred.getSize());
00115 
00116     //pred = rows*cols
00117 
00118 //     n    = size(X,1);
00119     const T n = static_cast<T>(rows);
00120 
00121 //     diff     = opt.pred - y;
00122     axpy(rows*cols, (T)-1.0, y_true, 1, pred_t, 1);
00123 
00124 //  p.rmse = norm(diff,'fro') / sqrt(n);
00125     T rmse = nrm2<T>(rows*cols, pred_t, 1)/sqrt(n);
00126 
00127     delete [] pred_t;
00128 
00129     perf->addOpt("rmse", new OptNumber(rmse));
00130 
00131 //    p.forho   = -p.rmse;
00132     perf->addOpt("forho", new OptNumber(-rmse));
00133 
00134 //    p.forplot     = p.rmse;
00135 //    perf->addOpt("forplot", new OptNumber(rmse));
00136 
00137     return perf;
00138 }
00139 
00140 }
00141 
00142 #endif //_GURLS_RMSE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends