Scaling.h

00001 /*          
00002  *             Copyright (C) 2005 Maarten Keijzer
00003  *
00004  *          This program is free software; you can redistribute it and/or modify
00005  *          it under the terms of version 2 of the GNU General Public License as 
00006  *          published by the Free Software Foundation. 
00007  *
00008  *          This program is distributed in the hope that it will be useful,
00009  *          but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *          GNU General Public License for more details.
00012  *
00013  *          You should have received a copy of the GNU General Public License
00014  *          along with this program; if not, write to the Free Software
00015  *          Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016  */
00017 
00018 #ifndef SCALING_H_
00019 #define SCALING_H_
00020 
00021 #include "shared_ptr.h"
00022 
00023 #include <valarray>
00024 #include <iostream>
00025 #include <string>
00026 
00027 class TargetInfo;
00028 
00029 class ScalingBase {
00030     public:
00031     
00032     virtual ~ScalingBase() {}
00033         
00034     std::valarray<double> apply(const std::valarray<double>& x) { 
00035         std::valarray<double> xtmp = x;
00036         transform(xtmp);
00037         return xtmp;
00038     }
00039         
00040     virtual double transform(double input) const = 0;
00041     virtual void transform(std::valarray<double>& inputs) const = 0;
00042     virtual std::ostream& print(std::ostream& os, std::string str) const = 0;
00043     virtual std::valarray<double> transform(const std::valarray<double>& inputs) const = 0;
00044 };
00045 
00046 typedef shared_ptr<ScalingBase> Scaling;
00047 
00048 class LinearScaling : public ScalingBase {
00049     
00050     double a,b;
00051     
00052     public:
00053     LinearScaling() : a(0.0), b(1.0) {}
00054     LinearScaling(double _a, double _b) : a(_a), b(_b) {}
00055 
00056     double transform(double input) const { input *=b; input += a; return input; }
00057     void transform(std::valarray<double>& inputs) const { inputs *= b; inputs += a; }
00058     std::valarray<double> transform(const std::valarray<double>& inputs) const { 
00059         std::valarray<double> y = a + b * inputs;
00060         return y;
00061     }
00062     
00063     double intercept() const { return a; }
00064     double slope()     const { return b; }
00065     
00066     std::ostream& print(std::ostream& os, std::string str) const {
00067         os.precision(16);
00068         os << a << " + " << b << " * " << str;
00069         return os;
00070     }
00071 };
00072 
00073 class NoScaling : public ScalingBase{
00074     void transform(std::valarray<double>&) const {}
00075     double transform(double input) const { return input; }
00076     std::valarray<double> transform(const std::valarray<double>& inputs) const { return inputs; }
00077     std::ostream& print(std::ostream& os, std::string str) const { return os << str; }
00078 };
00079 
00080 extern Scaling slope(const std::valarray<double>& inputs, const TargetInfo& targets); // slope only
00081 extern Scaling ols(const std::valarray<double>& inputs, const TargetInfo& targets);
00082 extern Scaling wls(const std::valarray<double>& inputs, const TargetInfo& targets);
00083 extern Scaling med(const std::valarray<double>& inputs, const TargetInfo& targets);
00084 
00085 extern Scaling ols(const std::valarray<double>& inputs, const std::valarray<double>& outputs);
00086 
00087 extern double mse(const std::valarray<double>& y, const TargetInfo& t);
00088 extern double rms(const std::valarray<double>& y, const TargetInfo& t);
00089 extern double mae(const std::valarray<double>& y, const TargetInfo& t);
00090 
00091 // Todo Logistic Scaling
00092 
00093 #endif
00094 
00095 

Generated on Thu Oct 19 05:06:42 2006 for EO by  doxygen 1.3.9.1