//------------------------------------------------------------------------------
//    module lossfunc.h                                                       //
//                                                                            //
//    Tracks some loss function results like mean absolute error (MAE) or     //
//    relative number of misclassifications (MCE).                            //
//    See source or http://www.newty.de/pnc2/sdocu.html for more information. //
//                                                                            //
//    copyright (c) 2000-2003 by Lars Haendel                                 //
//    home: www.newty.de                                                      //
//                                                                            //
//    This program is free software; you can redistribute it and/or modify    //
//    it under the terms of the GNU General Public License as published by    //
//    the Free Software Foundation as version 2 of the License.               //
//                                                                            //
//    This program is distributed in the hope that it will be useful,         //
//    but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//    GNU General Public License for more details.                            //
//                                                                            //
//    You should have received a copy of the GNU General Public License       //
//    along with this program; if not, write to the Free Software             //
//    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.               //
//                                                                            //
//------------------------------------------------------------------------------


#ifndef __LOSSFUNC_H
#define __LOSSFUNC_H

#include <fstream>      // due to:  ofstream

#ifdef BUILDER          // necessary if compiled with C++ builder
using namespace std;
#endif


//----------------------------------------------------------------------------------------------------------------------
#define PREC_LOSS_PERCENTAGE  (int) 2              // precision to display/write percentage loss values
#define PREC_LOSS             (int) 4              // precision to display/write floating point loss values


//----------------------------------------------------------------------------------------------------------------------
// encapsulate loss function results for various error criterions
class TLossFunction
{
public:

   // constructor
   TLossFunction(const float& _y_mean, const int& _nTup=-1);
   ~TLossFunction();

   // calculate actual loss
   float Mae();          // mean absolute error
   float MaeIns();
   float MaeOut();
   float Mse();          // mean square error
   float MseIns();
   float MseOut();
   float Mce();          // mean classification error
   float MceIns();
   float MceOut();
   float nErr();         // # miss classifications
   float InsideRatio();  // rate of inside predictions
   float NormMae();      // mean abs. error normalized by mean abs. error of baseline prediction (always predict mean)
   float NormMse();      // mean square error normalized by variance
   float MaeBase();
   float MseBase();


   const float*& Y_Err(){ return _y_e; };                                      // return error vector
   const float*& Y_p()  { return _y_p; };                                      // return predicted values

   inline const int& GetCounter() { return c; };                               // # predcitions made so far
   inline const int& nTup() { return _nTup; };                                 // # predcitions expected to be made

   // evaluate predicted and real value and tack loss
   void  EvPrediction(const float& prediction, const float& realValue, const float nIns=0);

   // write results to file
   void  WriteResults(ofstream& file, const bool& f_Symbolic, const bool& f_IsWithOutput=true);


private:
   int c, nIns, nOut;                           // counter for # predictions made

   float mae, mse, mce;                         // loss-function sums
   float mae_n, mse_n;
   float mae_ins, mse_ins, mce_ins, mae_out, mse_out, mce_out;

   float y_mean;                                // (learn data) output mean used for baseline prediction

   // variables used to store predictions
   int _nTup;                                   // # tuples which will be predicted
   float *_y, *_y_p, *_y_e, *__nIns;            // real value, predicted value, error, # cuboids covering the tuple
};
#endif