PLearn 0.1
WeightedQuadraticPolynomialKernel.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal
00006 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal
00007 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion
00008 
00009 // Redistribution and use in source and binary forms, with or without
00010 // modification, are permitted provided that the following conditions are met:
00011 // 
00012 //  1. Redistributions of source code must retain the above copyright
00013 //     notice, this list of conditions and the following disclaimer.
00014 // 
00015 //  2. Redistributions in binary form must reproduce the above copyright
00016 //     notice, this list of conditions and the following disclaimer in the
00017 //     documentation and/or other materials provided with the distribution.
00018 // 
00019 //  3. The name of the authors may not be used to endorse or promote
00020 //     products derived from this software without specific prior written
00021 //     permission.
00022 // 
00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 // 
00034 // This file is part of the PLearn library. For more information on the PLearn
00035 // library, go to the PLearn Web site at www.plearn.org
00036 
00037 
00038 /* *******************************************************      
00039  * $Id: WeightedQuadraticPolynomialKernel.cc 6802 2007-03-29 15:19:55Z tihocan $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "WeightedQuadraticPolynomialKernel.h"
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00048 
00049 
00050 PLEARN_IMPLEMENT_OBJECT(
00051     WeightedQuadraticPolynomialKernel,
00052     "Polynomial kernel of degree two, with coefficient correction.",
00053     "Computes K(x,y) = 0.5 * ( alpha (1 + <x,y>)^2 - alpha \n"
00054     "                          + 2*(1 - alpha) * \\sum_i x_i y_i\n"
00055     "                          + (2*beta - alpha) * \\sum_i x_i^2 y_i^2 )\n"
00056     "This implies the following features:\n"
00057     "  - the first degree features x_i\n"
00058     "  - the correlation features x_i x_j (i \neq j) weighted by alpha\n"
00059     "  - the second degree features x_i^2 weighted by beta\n"
00060 );
00061 
00063 // WeightedQuadraticPolynomialKernel //
00065 
00066 WeightedQuadraticPolynomialKernel::WeightedQuadraticPolynomialKernel(
00067     real the_alpha, real the_beta,
00068     bool call_build_):
00069     inherited(true, call_build_),
00070     alpha(the_alpha),
00071     beta(the_beta)
00072 {
00073     if (call_build_)
00074         build_();
00075 }
00076 
00078 // declareOptions //
00080 void WeightedQuadraticPolynomialKernel::declareOptions(OptionList &ol)
00081 {
00082     declareOption(ol, "alpha", &WeightedQuadraticPolynomialKernel::alpha, 
00083                   OptionBase::buildoption,
00084                   "Weight on correlation features.");
00085 
00086     declareOption(ol, "beta", &WeightedQuadraticPolynomialKernel::beta, 
00087                   OptionBase::buildoption,
00088                   "Weight on second degree features.");
00089 
00090     // Declare options inherited from parent class.
00091     inherited::declareOptions(ol);
00092 }
00093 
00094 
00096 // build //
00098 void WeightedQuadraticPolynomialKernel::build()
00099 {
00100     inherited::build();
00101     build_();
00102 }
00103 
00105 // build_ //
00107 void WeightedQuadraticPolynomialKernel::build_()
00108 {}
00109 
00111 // evaluate //
00113 real WeightedQuadraticPolynomialKernel::evaluate(const Vec& x1, const Vec& x2) const
00114 { 
00115 #ifdef BOUNDCHECK
00116     if(x1.length()!=x2.length())
00117         PLERROR("In WeightedQuadraticPolynomialKernel::evaluate(): "
00118             "x1 and x2 have different lengths.");
00119 #endif
00120     real res = 0;
00121     real corr = 0;
00122     if (x1.size() > 0 && x2.size() > 0) {
00123         real* v1 = x1.data();
00124         real* v2 = x2.data();
00125         real v1i = 0;
00126         real v2i = 0;
00127         for(int i=0; i<x1.length(); i++)
00128         {
00129             v1i = v1[i];
00130             v2i = v2[i];
00131             res += v1i*v2i;
00132             corr += v1i*v2i*v1i*v2i;
00133         }
00134     }
00135     
00136     //Computes K(x,y) = 0.5 * ( alpha (1 + <x,y>)^2 - alpha \n"
00137     //                          + 2*(1 - alpha) * \sum_i x_i y_i\n"
00138     //                          + (2*beta - alpha) * \sum_i x_i^2 y_i^2 )\n"
00139     return 0.5 * (alpha * ipow(res + real(1.0), 2) - alpha + 2*(1 - alpha)*res + 
00140                   (2*beta - alpha)*corr);
00141 }
00142 
00144 // makeDeepCopyFromShallowCopy //
00146 void WeightedQuadraticPolynomialKernel::makeDeepCopyFromShallowCopy(CopiesMap& copies) {
00147     inherited::makeDeepCopyFromShallowCopy(copies);
00148 }
00149 
00150 } // end of namespace PLearn
00151 
00152 
00153 /*
00154   Local Variables:
00155   mode:c++
00156   c-basic-offset:4
00157   c-file-style:"stroustrup"
00158   c-file-offsets:((innamespace . 0)(inline-open . 0))
00159   indent-tabs-mode:nil
00160   fill-column:79
00161   End:
00162 */
00163 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines