<commit_before>/***************************************************************************
 *  The Kernel-Machine Library                                             *
 *  Copyright (C) 2004--2006 by <NAME>                         *
 *                                                                         *
 *  This library is free software; you can redistribute it and/or          *
 *  modify it under the terms of the GNU Lesser General Public             *
 *  License as published by the Free Software Foundation; either           *
 *  version 2.1 of the License, or (at your option) any later version.     *
 *                                                                         *
 *  This library is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
 *  Lesser General Public License for more details.                        *
 *                                                                         *
 *  You should have received a copy of the GNU Lesser General Public       *
 *  License along with this library; if not, write to the Free Software    *
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  *
 ***************************************************************************/

#ifndef KRLS_HPP
#define KRLS_HPP

#include <boost/numeric/bindings/traits/ublas_matrix.hpp>
#include <boost/numeric/bindings/traits/ublas_symmetric.hpp>
#include <boost/numeric/bindings/traits/ublas_vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/bindings/atlas/cblas.hpp>
#include <kml/kernel_machine.hpp>
#include <kml/regression.hpp>
#include <kml/symmetric_view.hpp>


namespace atlas = boost::numeric::bindings::atlas;


namespace kml {


/*!
\brief Kernel Recursive Least Squares
   
A preprocessing step is taken to absorb the bias term into the weight vector w, by
redefining w as (w^T, b)^T and \f$\phi\f$ as (phi^T,1)^T. For details, see Engel et al,
Sparse Online Greedy Support Vector Regression, below on page 3:
\f$k(x,x')=k(x,x') + \lambda^2\f$, where \f$\lambda\f$ is a small, positive constant.
 
\section bibliography References
-# Engel et al., 2003. Kernel Recursive Least Squares. 
*/


template< typename Problem, typename Kernel, typename PropertyMap, class Enable = void>
class krls: public kernel_machine< Problem, Kernel, PropertyMap > {}
;



template< typename Problem, typename Kernel, typename PropertyMap >
class krls< Problem, Kernel, PropertyMap, typename boost::enable_if< is_regression<Problem> >::type>:
    public kernel_machine< Problem, Kernel, PropertyMap > {

    typedef kernel_machine< Problem, Kernel, PropertyMap > base_type;
    typedef typename base_type::kernel_type kernel_type;
    typedef double scalar_type;

    typedef symmetric_view< ublas::matrix<double> > symmetric_type;

    typedef ublas::matrix<double> matrix_type;
    typedef ublas::vector<double> vector_type;

    typedef typename Problem::input_type input_type;
    typedef typename Problem::output_type output_type;

    typedef typename boost::property_traits<PropertyMap>::key_type key_type;
    typedef typename boost::property_traits<PropertyMap>::value_type object_type;

public:
    krls( scalar_type n, scalar_type l,
          typename boost::call_traits<kernel_type>::param_type k,
          typename boost::call_traits<PropertyMap>::param_type map ):
    base_type(k,map), nu(n), lambda_squared(l*l) {}

    template< typename TokenIterator >
    krls( TokenIterator const begin, TokenIterator const end,
          typename boost::call_traits<kernel_type>::param_type k,
          typename boost::call_traits<PropertyMap>::param_type map ):
    base_type(k,map) {
        nu = 1e-1;						// default value
        scalar_type lambda=1e-3;		// default value
        TokenIterator token(begin);
        if ( token != end ) {
            nu = boost::lexical_cast<double>( *token++ );
            if ( token != end )
                lambda = boost::lexical_cast<double>( *token );
        }
        lambda_squared=lambda*lambda;
    }


    output_type operator()( input_type const &x ) {

        vector_type temp_K( base_type::key_lookup.size() );
        base_type::fill_kernel( x, base_type::key_lookup.begin(), base_type::key_lookup.end(), temp_K.begin() );
        for( int i=0; i < temp_K.size(); ++i )
            temp_K[i] += lambda_squared;
        return atlas::dot( base_type::weight, temp_K );

    }


    /*! learn the entire range of keys indicated by this range */
    template<typename KeyIterator>
    void learn( KeyIterator begin, KeyIterator end ) {
        KeyIterator key_iterator(begin);
        while( key_iterator != end ) {
            increment( *key_iterator );
            ++key_iterator;
        }
    }

<commit_msg>fixed a warning about signedness<commit_after>/***************************************************************************
 *  The Kernel-Machine Library                                             *
 *  Copyright (C) 2004--2006 by <NAME>                         *
 *                                                                         *
 *  This library is free software; you can redistribute it and/or          *
 *  modify it under the terms of the GNU Lesser General Public             *
 *  License as published by the Free Software Foundation; either           *
 *  version 2.1 of the License, or (at your option) any later version.     *
 *                                                                         *
 *  This library is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
 *  Lesser General Public License for more details.                        *
 *                                                                         *
 *  You should have received a copy of the GNU Lesser General Public       *
 *  License along with this library; if not, write to the Free Software    *
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  *
 ***************************************************************************/

#ifndef KRLS_HPP
#define KRLS_HPP

#include <boost/numeric/bindings/traits/ublas_matrix.hpp>
#include <boost/numeric/bindings/traits/ublas_symmetric.hpp>
#include <boost/numeric/bindings/traits/ublas_vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/bindings/atlas/cblas.hpp>
#include <kml/kernel_machine.hpp>
#include <kml/regression.hpp>
#include <kml/symmetric_view.hpp>


namespace atlas = boost::numeric::bindings::atlas;


namespace kml {


/*!
\brief Kernel Recursive Least Squares
   
A preprocessing step is taken to absorb the bias term into the weight vector w, by
redefining w as (w^T, b)^T and \f$\phi\f$ as (phi^T,1)^T. For details, see Engel et al,
Sparse Online Greedy Support Vector Regression, below on page 3:
\f$k(x,x')=k(x,x') + \lambda^2\f$, where \f$\lambda\f$ is a small, positive constant.
 
\section bibliography References
-# Engel et al., 2003. Kernel Recursive Least Squares. 
*/


template< typename Problem, typename Kernel, typename PropertyMap, class Enable = void>
class krls: public kernel_machine< Problem, Kernel, PropertyMap > {}
;



template< typename Problem, typename Kernel, typename PropertyMap >
class krls< Problem, Kernel, PropertyMap, typename boost::enable_if< is_regression<Problem> >::type>:
    public kernel_machine< Problem, Kernel, PropertyMap > {

    typedef kernel_machine< Problem, Kernel, PropertyMap > base_type;
    typedef typename base_type::kernel_type kernel_type;
    typedef double scalar_type;

    typedef symmetric_view< ublas::matrix<double> > symmetric_type;

    typedef ublas::matrix<double> matrix_type;
    typedef ublas::vector<double> vector_type;

    typedef typename Problem::input_type input_type;
    typedef typename Problem::output_type output_type;

    typedef typename boost::property_traits<PropertyMap>::key_type key_type;
    typedef typename boost::property_traits<PropertyMap>::value_type object_type;

public:
    krls( scalar_type n, scalar_type l,
          typename boost::call_traits<kernel_type>::param_type k,
          typename boost::call_traits<PropertyMap>::param_type map ):
    base_type(k,map), nu(n), lambda_squared(l*l) {}

    template< typename TokenIterator >
    krls( TokenIterator const begin, TokenIterator const end,
          typename boost::call_traits<kernel_type>::param_type k,
          typename boost::call_traits<PropertyMap>::param_type map ):
    base_type(k,map) {
        nu = 1e-1;						// default value
        scalar_type lambda=1e-3;		// default value
        TokenIterator token(begin);
        if ( token != end ) {
            nu = boost::lexical_cast<double>( *token++ );
            if ( token != end )
                lambda = boost::lexical_cast<double>( *token );
        }
        lambda_squared=lambda*lambda;
    }


    output_type operator()( input_type const &x ) {

        vector_type temp_K( base_type::key_lookup.size() );
        base_type::fill_kernel( x, base_type::key_lookup.begin(), base_type::key_lookup.end(), temp_K.begin() );
        for( unsigned int i=0; i < temp_K.size(); ++i )
            temp_K[i] += lambda_squared;
        return atlas::dot( base_type::weight, temp_K );

    }


    /*! learn the entire range of keys indicated by this range */
    template<typename KeyIterator>
    void learn( KeyIterator begin, KeyIterator end ) {
        KeyIterator key_iterator(begin);
        while( key_iterator != end ) {
            increment( *key_iterator );
            ++key_iterator;
        }
    }


{'fraction_non_alphanumeric': 0.12451599755451397, 'fraction_numerical': 0.006725086610963929, 'mean_word_length': 2.7206216830932526, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2299030', 'n_tokens_mistral': 2817, 'n_tokens_neox': 2619, 'n_words': 801}