Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

gnn_msre.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *  @file gnn_msre.h
00003  *  @brief Mean Square Relative Error Criterion.
00004  *
00005  *  @date   : 27-09-03 21:08
00006  *  @author : Pedro Ortega C. <peortega@dcc.uchile.cl>
00007  *  Copyright  2003  Pedro Ortega C.
00008  ****************************************************************************/
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024 
00025 /**
00026  * @brief Mean Square Relative Error function.
00027  * @defgroup gnn_msre_doc gnn_msre : Mean-Square-Relative-Error Criterion.
00028  * @ingroup gnn_criterion_doc
00029  *
00030  * This datatype implements the MSRE criterion, given by
00031  * \f[ E(y,t) = \frac{1}{m} \sum_i^m \frac{(y_i - t_i)^2}{t_i^2} \f]
00032  * This function resembles the MSE criterion, but measures the relative
00033  * error with respect to the target value. Minimizing this function is
00034  * equivalent to minimize
00035  *
00036  * \f[ E(y,t) = \frac{1}{m} \sum_i^m \left | \frac{y_i - t_i}{t_i} \right | \f]
00037  *
00038  * Please note that zero target values do undefine the function. This criterion
00039  * isn't appropiate for small target values.
00040  *
00041  * In a batch training mode, where all patterns are used to determine the
00042  * gradient \f$\frac{\partial E}{\partial w}\f$, then the training optimizes
00043  * the MAPE criterion.
00044  */
00045 
00046 
00047 
00048 /******************************************/
00049 /* Include Files                          */
00050 /******************************************/
00051 
00052 #include "gnn_msre.h"
00053 
00054 
00055 
00056 /******************************************/
00057 /* Static Declaration                     */
00058 /******************************************/
00059 
00060 typedef gnn_criterion gnn_msre;
00061 
00062 double
00063 gnn_msre_e (gnn_criterion *crit,
00064             const gsl_vector *y,
00065             const gsl_vector *t);
00066 
00067 int
00068 gnn_msre_dy (gnn_criterion *crit,
00069              const gsl_vector *y,
00070              const gsl_vector *t,
00071              gsl_vector * dy);
00072 
00073 
00074 
00075 /******************************************/
00076 /* Static Implementation                  */
00077 /******************************************/
00078 
00079 /**
00080  * @brief The evaluation function.
00081  * @ingroup gnn_msre_doc
00082  *
00083  * This function corresponds to the evaluation of the MSRE criterion.
00084  *
00085  * @param  crit A pointer to a \ref gnn_msre criterion.
00086  * @param  y    A pointer to an estimation vector \f$y\f$.
00087  * @param  t    A pointer to the desired target vector \f$t\f$.
00088  * @return A real number corresponding to the value of the criterion.
00089  */
00090 double
00091 gnn_msre_e (gnn_criterion *crit,
00092             const gsl_vector *y,
00093             const gsl_vector *t)
00094 {
00095     int i;
00096     double E;
00097 
00098     assert (crit != NULL);
00099     assert (y != NULL);
00100     assert (t != NULL);
00101 
00102     /* check sizes */
00103     if (y->size != t->size)
00104         GSL_ERROR_VAL ("vector sizes should be the same", GSL_EINVAL, 0.0);
00105 
00106     /* compute error */
00107     E = 0.0;
00108     for (i=0; i<y->size; ++i)
00109     {
00110         double yi;
00111         double ti;
00112 
00113         yi = gsl_vector_get (y, i);
00114         ti = gsl_vector_get (t, i);
00115 
00116         E += (yi - ti) * (yi - ti) / (ti * ti);
00117     }
00118     E /= y->size;
00119 
00120     return E;
00121 }
00122 
00123 /**
00124  * @brief The gradient evaluation function.
00125  * @ingroup gnn_msre_doc
00126  *
00127  * This function implements the \ref gnn_msre criterion's gradient evaluation
00128  * function given by
00129  *
00130  * \f[ \frac{\partial E}{\partial y_i} = \frac{2}{m t_i^2} (y_i - t_i) \f]
00131  *
00132  * @param  crit A pointer to a \ref gnn_msre criterion.
00133  * @param  y    A pointer to an estimation vector \f$y\f$.
00134  * @param  t    A pointer to the desired target vector \f$t\f$.
00135  * @param  dy   A pointer to a buffer vector where the result should be placed.
00136  * @return Returns 0 if succeeded.
00137  */
00138 int
00139 gnn_msre_dy (gnn_criterion *crit,
00140              const gsl_vector *y,
00141              const gsl_vector *t,
00142              gsl_vector * dy)
00143 {
00144     int i;
00145 
00146     assert (crit != NULL);
00147     assert (y != NULL);
00148     assert (t != NULL);
00149     assert (dy != NULL);
00150 
00151     /* check sizes */
00152     if (y->size != t->size || y->size != dy->size)
00153         GSL_ERROR ("vector sizes should be the same", GSL_EINVAL);
00154 
00155     /* compute dy */
00156     for (i=0; i<y->size; ++i)
00157     {
00158         double yi;
00159         double ti;
00160         double dyi;
00161 
00162         yi  = gsl_vector_get (y, i);
00163         ti  = gsl_vector_get (t, i);
00164         dyi = gsl_vector_get (dy, i);
00165 
00166         dyi += 2 * (yi - ti) / (y->size * ti * ti);
00167 
00168         gsl_vector_set (dy, i, dyi);
00169     }
00170 
00171     return 0;
00172 }
00173 
00174 
00175 /******************************************/
00176 /* Public Interface                       */
00177 /******************************************/
00178 
00179 /**
00180  * @brief Creates a new \ref gnn_msre criterion.
00181  * @ingroup gnn_msre_doc
00182  *
00183  * This function creates a new \ref gnn_msre of the given size.
00184  *
00185  * @param  size    The size of the estimation and the target vector \f$y\f$
00186  *                 and \f$t\f$.
00187  * @return Returns a pointer to a new \ref gnn_msre or NULL if failed.
00188  */
00189 gnn_criterion *
00190 gnn_msre_new (size_t size)
00191 {
00192     int status;
00193     gnn_criterion *crit;
00194 
00195     /* check size */
00196     if (size < 1)
00197         GSL_ERROR_VAL ("size should be strictly positive", GSL_EINVAL, NULL);
00198 
00199     /* alloc memory */
00200     crit = (gnn_criterion *) malloc (sizeof (*crit));
00201     if (crit == NULL)
00202         GSL_ERROR_VAL ("couldn't alloc memory for gnn_msre", GSL_ENOMEM, NULL);
00203 
00204     /* initialize */
00205     status = gnn_criterion_init  (crit, "gnn_msre", size,
00206                                   gnn_msre_e, gnn_msre_dy, NULL);
00207     if (status)
00208     {
00209         gnn_criterion_destroy (crit);
00210         GSL_ERROR_VAL ("couldn't initialize gnn_msre", GSL_EFAILED, NULL);
00211     }
00212 
00213     return crit;
00214 }
00215 
00216 
00217 

Generated on Sun Jun 13 20:50:12 2004 for libgnn Gradient Retropropagation Machine Library by doxygen1.2.18