GURLS++  2.0.00
C++ Implementation of GURLS Matlab Toolbox
normzscore.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_NORMZSCORE_H_
00044 #define _GURLS_NORMZSCORE_H_
00045 
00046 
00047 #include "gurls++/norm.h"
00048 #include "gurls++/gmath.h"
00049 #include "gurls++/optmatrix.h"
00050 
00051 #include <string>
00052 
00053 namespace gurls {
00054 
00060 template <typename T>
00061 class NormZScore: public Norm<T>
00062 {
00063 public:
00072     GurlsOptionsList* execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& opt)  throw(gException);
00073 
00074 protected:
00075     void centerRescale(gMat2D<T> &M, T *stdDevs, const T *means);
00076 };
00077 
00078 template<typename T>
00079 GurlsOptionsList* NormZScore<T>::execute(const gMat2D<T>& X, const gMat2D<T>& Y, const GurlsOptionsList& /*opt*/) throw(gException)
00080 {
00081 //    [n,d] = size(X);
00082     const unsigned long n = X.rows();
00083     const unsigned long d = X.cols();
00084     const unsigned long t = Y.cols();
00085 
00086 //    meanX = mean(X);
00087 
00088     gMat2D<T> *v_meanX = new gMat2D<T>(1, d);
00089     mean(X.getData(), v_meanX->getData(), n, d, d);
00090 
00091     gMat2D<T> *v_meanY = new gMat2D<T>(1, t);
00092     mean(Y.getData(), v_meanY->getData(), n, t, t);
00093 
00094 //    stdX = std(X) + eps;
00095 //    X = X - repmat(meanX, n, 1);
00096 //    X = X./repmat(stdX, n, 1);
00097 
00098     gMat2D<T> *v_stdX = new gMat2D<T>(1, d);
00099     gMat2D<T> *v_stdY = new gMat2D<T>(1, t);
00100 
00101     gMat2D<T> *retX = new gMat2D<T>(n, d);
00102     copy(retX->getData(), X.getData(), retX->getSize());
00103 
00104     gMat2D<T> *retY = new gMat2D<T>(n, t);
00105     copy(retY->getData(), Y.getData(), retY->getSize());
00106 
00107     centerRescale(*retX, v_stdX->getData(), v_meanX->getData());
00108     centerRescale(*retY, v_stdY->getData(), v_meanY->getData());
00109 
00110     GurlsOptionsList* norm = new GurlsOptionsList("norm");
00111     norm->addOpt("X", new OptMatrix<gMat2D<T> >(*retX));
00112     norm->addOpt("meanX", new OptMatrix<gMat2D<T> >(*v_meanX));
00113     norm->addOpt("stdX", new OptMatrix<gMat2D<T> >(*v_stdX));
00114 
00115     norm->addOpt("Y", new OptMatrix<gMat2D<T> >(*retY));
00116     norm->addOpt("meanY", new OptMatrix<gMat2D<T> >(*v_meanY));
00117     norm->addOpt("stdY", new OptMatrix<gMat2D<T> >(*v_stdY));
00118 
00119     return norm;
00120 }
00121 
00122 template<typename T>
00123 void NormZScore<T>::centerRescale(gMat2D<T> &M, T *stdDevs, const T *means)
00124 {
00125     const unsigned long n = M.rows();
00126     const unsigned long d = M.cols();
00127 
00128     const T epsilon = std::numeric_limits<T>::epsilon();
00129 
00130     T* column = M.getData();
00131     T* std_it = stdDevs;
00132     const T* mean_it = means;
00133     for(unsigned long i=0; i<d; ++i, column+=n, ++std_it, ++mean_it)
00134     {
00135         axpy(n, (T)-1.0, mean_it, 0, column, 1);
00136 
00137         T norm = nrm2(n, column, 1);
00138         T stdDev = sqrt( (norm*norm) / (n-1)) + epsilon;
00139 
00140         *std_it = stdDev;
00141         scal(n, (T)1.0/stdDev, column, 1);
00142     }
00143 }
00144 
00145 }
00146 
00147 #endif //_GURLS_NORMZSCORE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends