Changeset 627


Ignore:
Timestamp:
Sep 5, 2006, 7:39:45 AM (15 years ago)
Author:
Peter
Message:

fixes #124 also extended interface of AveragerPairWeighted? supporting DataLookupWeighted1D

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/classifier/GaussianKernelFunction.cc

    r616 r627  
    55#include <c++_tools/classifier/KernelFunction.h>
    66#include <c++_tools/classifier/DataLookup1D.h>
     7#include <c++_tools/classifier/DataLookupWeighted1D.h>
    78
    89#include <math.h>
     
    2728    return exp(-d2/sigma2_);
    2829  }
     30
     31
     32  double GaussianKernelFunction::operator()(const DataLookup1D& x,
     33                                            const DataLookupWeighted1D& y) const
     34  {
     35    assert(x.size()==y.size());
     36    double d2 = 0;
     37    double normalization_factor = 0;
     38    for (size_t i=0; i<x.size(); i++) {
     39      // ignoring Nan with accompanied weight zero
     40      if (y.weight(i)){
     41        d2 += y.weight(i) * (x(i)-y.data(i)) * (x(i)-y.data(i));
     42        normalization_factor += y.weight(i);
     43      }
     44    }
     45    // to make it coherent with no weight case
     46    normalization_factor /= x.size();
     47    return exp(d2/normalization_factor/sigma2_);
     48  }
     49
     50
     51  double GaussianKernelFunction::operator()(const DataLookupWeighted1D& x,
     52                                            const DataLookupWeighted1D& y) const
     53  {
     54    assert(x.size()==y.size());
     55    double d2 = 0;
     56    double normalization_factor = 0;
     57    for (size_t i=0; i<x.size(); i++) {
     58      // ignoring Nan with accompanied weight zero
     59      if (x.weight(i) && y.weight(i)){
     60        d2 += x.weight(i) * y.weight(i) * (x.data(i)-y.data(i)) *
     61          (x.data(i)-y.data(i));
     62        normalization_factor += x.weight(i) * y.weight(i);
     63      }
     64    }
     65    // to make it coherent with no weight case
     66    normalization_factor /= x.size();
     67    return exp(d2/normalization_factor/sigma2_);
     68  }
     69
    2970
    3071  double GaussianKernelFunction::operator()(const DataLookup1D& a1,
  • trunk/c++_tools/classifier/GaussianKernelFunction.h

    r616 r627  
    2020   
    2121  public:
    22     ///
    23     ///Constructor taking the sigma_ , i.e. the width of the Gaussian,as
    24     ///input. Default is sigma_ = 1.
     22    ///
     23    /// Constructor taking the sigma_ , i.e. the width of the Gaussian,as
     24    /// input. Default is sigma_ = 1.
    2525    ///
    2626    GaussianKernelFunction(double = 1);
     
    3333    ///
    3434    /// returning the scalar product of two vectors in feature space using the
    35     /// Gaussian kernel. @return \f$ exp((x - y)^{2}/\sigma^2) \f$ \n
     35    /// Gaussian kernel. @return \f$ exp(-(x - y)^{2}/\sigma^2) \f$ \n
    3636    ///
    37     double operator()(const DataLookup1D& a1,
    38                       const DataLookup1D& a2) const;
     37    double operator()(const DataLookup1D& x,
     38                      const DataLookup1D& y) const;
     39
     40    /**
     41       \f$ \exp(-d^2/\sigma^2) \f$ where \f$ d^2 = \sum w_y(x_i-y_i)^2
     42       / \sum w_y * N \f$
     43     **/
     44    double operator()(const DataLookup1D& x,
     45                      const DataLookupWeighted1D& y) const;
     46
     47    /**
     48       \f$ \exp(-d^2/\sigma^2) \f$ where \f$ d^2 = \sum w_xw_y(x_i-y_i)^2
     49       / \sum w_xw_y * N \f$
     50     **/
     51    double operator()(const DataLookupWeighted1D& x,
     52                      const DataLookupWeighted1D& y) const;
    3953
    4054    ///
  • trunk/c++_tools/classifier/KernelFunction.h

    r608 r627  
    77namespace classifier {
    88  class DataLookup1D;
     9  class DataLookupWeighted1D;
    910
    1011  ///
     
    3435    /// @return scalar product of two vector in feature space.
    3536    ///
     37    virtual double operator()(const DataLookup1D&,
     38                              const DataLookupWeighted1D&) const = 0;
     39   
     40    ///
     41    /// @return scalar product of two vector in feature space.
     42    ///
     43    inline double operator()(const DataLookupWeighted1D& vec_w,
     44                             const DataLookup1D& vec) const
     45    { return this->operator()(vec,vec_w); }
     46   
     47    ///
     48    /// @return scalar product of two vector in feature space.
     49    ///
     50    virtual double operator()(const DataLookupWeighted1D&,
     51                              const DataLookupWeighted1D&) const = 0;
     52   
     53    ///
     54    /// @return scalar product of two vector in feature space.
     55    ///
    3656    virtual double operator()(const DataLookup1D&,
    3757                              const DataLookup1D&,
  • trunk/c++_tools/classifier/PolynomialKernelFunction.cc

    r616 r627  
    11// $Id$
    2 
    32
    43#include <c++_tools/classifier/PolynomialKernelFunction.h>
    54#include <c++_tools/classifier/DataLookup1D.h>
     5#include <c++_tools/classifier/DataLookupWeighted1D.h>
    66
    77#include <c++_tools/statistics/AveragerPairWeighted.h>
     
    1818}
    1919
     20
     21double PolynomialKernelFunction::operator()(const DataLookup1D& x,
     22                                            const DataLookup1D& y) const   
     23{
     24  assert(x.size()==y.size());
     25  return ((order_>1) ? pow(1+x*y,order_) : x*y);
     26}
     27
     28
     29double PolynomialKernelFunction::operator()(const DataLookup1D& x,
     30                                            const DataLookupWeighted1D& y) const
     31{
     32  assert(x.size()==y.size());
     33  statistics::AveragerPairWeighted averager;
     34  averager.add(x,y);
     35  if(order_>1)
     36    return pow(1+averager.sum_xy(),order_);
     37  return averager.sum_xy();
     38}
     39
     40
     41double PolynomialKernelFunction::operator()(const DataLookupWeighted1D& x,
     42                                            const DataLookupWeighted1D& y) const
     43{
     44  assert(x.size()==y.size());
     45  statistics::AveragerPairWeighted averager;
     46  averager.add(x,y);
     47  if(order_>1)
     48    return pow(1+averager.sum_xy(),order_);
     49  return averager.sum_xy();
     50}
     51
     52
    2053double PolynomialKernelFunction::operator()(const DataLookup1D& a1,
    2154                                            const DataLookup1D& a2,
     
    2760    averager.add(a1(i),a2(i),w1(i),w2(i));
    2861
    29   // a1.size() term to make it coherent with no weight case
    3062  if(order_>1)
    31     return pow(1+averager.sum_xy()/averager.sum_w()*a1.size(),order_);
    32   return averager.sum_xy()/averager.sum_w()*a1.size();
     63    return pow(1+averager.sum_xy(),order_);
     64  return averager.sum_xy();
    3365}
    3466
  • trunk/c++_tools/classifier/PolynomialKernelFunction.h

    r608 r627  
    1 // $Id$
    2 
    31#ifndef _theplu_classifier_polynomial_kernel_function_
    42#define _theplu_classifier_polynomial_kernel_function_
     3
     4// $Id$
    55
    66#include <c++_tools/classifier/KernelFunction.h>
     
    3535   
    3636    ///
    37     ///returning the scalar product of two vectors in feature space using the
    38     ///polynomial kernel. @return If order is larger than one: \f$ (1+x \cdot
    39     ///y)^{order} \f$ \n If order is one (linear): \f$ x \cdot y \f$
     37    /// returning the scalar product of two vectors in feature space using the
     38    /// polynomial kernel. @return If order is larger than one: \f$ (1+x \cdot
     39    /// y)^{order} \f$ \n If order is one (linear): \f$ x \cdot y \f$
    4040    ///   
    41     inline double operator()(const DataLookup1D& a1,
    42                              const DataLookup1D& a2) const
    43     { return ((order_>1) ? pow(1+a1*a2,order_) : a1*a2); }
     41    double operator()(const DataLookup1D& a1, const DataLookup1D& a2) const;
     42
     43    ///
     44    /// @return If order is larger than one: \f$ (1+x \cdot y)^{order}
     45    /// \f$ \n If order is one (linear): \f$ \frac{\sum w_yxy} \f$
     46    ///
     47    ///
     48    double operator()(const DataLookup1D& x,
     49                      const DataLookupWeighted1D& y) const;
     50    ///
     51    /// returning the scalar product of two vectors in feature space
     52    /// using the polynomial kernel with weights. Having all weights
     53    /// equal to unity yields the same as non-weighted version.
     54    ///
     55    /// @return If order is larger than one: \f$ (1+x \cdot y)^{order}
     56    /// \f$ \n If order is one (linear): \f$ \sum w_xw_yxy} \f$
     57    ///
     58    double operator()(const DataLookupWeighted1D& x,
     59                      const DataLookupWeighted1D& y) const;
    4460
    4561
     
    5066    ///
    5167    /// @return If order is larger than one: \f$ (1+x \cdot y)^{order}
    52     /// \f$ \n If order is one (linear): \f$ N\frac{\sum
    53     /// w_xw_yxy}{\sum w_xw_y} \f$
     68    /// \f$ \n If order is one (linear): \f$ \frac{\sum w_xw_yxy} \f$
    5469    ///
    5570    ///
  • trunk/c++_tools/statistics/AveragerPairWeighted.cc

    r582 r627  
    11// $Id$
    22
    3 #include <c++_tools/statistics/AveragerPairWeighted.h>
    4 #include <c++_tools/statistics/AveragerPair.h>
    5 #include <c++_tools/statistics/Averager.h>
     3#include "c++_tools/statistics/AveragerPairWeighted.h"
     4#include "c++_tools/statistics/AveragerPair.h"
     5#include "c++_tools/statistics/Averager.h"
     6#include "c++_tools/classifier/DataLookup1D.h"
     7#include "c++_tools/classifier/DataLookupWeighted1D.h"
    68
    79#include <cmath>
     
    2527  }
    2628
     29
     30  void AveragerPairWeighted::add(const classifier::DataLookup1D& x,
     31                                 const classifier::DataLookupWeighted1D& y)
     32  {
     33    assert(x.size()==y.size());
     34    for (size_t i=0; i<x.size(); ++i)
     35      add(x(i), y.data(i), 1.0, y.weight(i));
     36  }
     37
     38
     39  void AveragerPairWeighted::add(const classifier::DataLookupWeighted1D& x,
     40                                 const classifier::DataLookupWeighted1D& y)
     41  {
     42    assert(x.size()==y.size());
     43    for (size_t i=0; i<x.size(); ++i)
     44      add(x.data(i), y.data(i), x.weight(i), y.weight(i));
     45  }
     46
    2747}} // of namespace statistics and namespace theplu
  • trunk/c++_tools/statistics/AveragerPairWeighted.h

    r616 r627  
    1010
    1111namespace theplu{
     12namespace classifier{
     13  class DataLookup1D;
     14  class DataLookupWeighted1D;
     15}
    1216namespace statistics{
    1317  ///
     
    4246    void  add(const double x, const double y,
    4347              const double wx, const double wy);
     48
     49    ///
     50    ///
     51    ///
     52    void add(const classifier::DataLookup1D& x,
     53             const classifier::DataLookupWeighted1D& y);
     54
     55    ///
     56    ///
     57    ///
     58    inline void add(const classifier::DataLookupWeighted1D& x,
     59                    const classifier::DataLookup1D& y){ add(y,x); }
     60
     61    ///
     62    ///
     63    ///
     64    void add(const classifier::DataLookupWeighted1D& x,
     65             const classifier::DataLookupWeighted1D& y);
    4466
    4567    ///
  • trunk/doc/Statistics.tex

    r586 r627  
    275275\subsection{Polynomial Kernel}
    276276The polynomial kernel of degree $N$ is defined as $(1+<x,y>)^N$, where
    277 $<x,y>$ is the linear kenrel (usual scalar product). For weights we
    278 define the linear kernel to be $<x,y>=\frac{\sum w_xw_yxy}{\sum
    279 w_xw_y}$ and the polynomial kernel can be calculated as before
     277$<x,y>$ is the linear kernel (usual scalar product). For the weighted
     278case we define the linear kernel to be $<x,y>=\sum w_xw_yxy}$ and the
     279polynomial kernel can be calculated as before
    280280$(1+<x,y>)^N$. Is this kernel a proper kernel (always being semi
    281281positive definite). Yes, because $<x,y>$ is obviously a proper kernel
Note: See TracChangeset for help on using the changeset viewer.