Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Filtering files
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcantin committed Jan 13, 2018
1 parent 6ed8282 commit 6a2ca4d
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
92 changes: 92 additions & 0 deletions include/robust_filtering.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/** \file robust_filtering.hpp \brief Robust filtering for outliers */
/*
-------------------------------------------------------------------------
This file is part of BayesOpt, an efficient C++ library for
Bayesian optimization.
Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
BayesOpt is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BayesOpt 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
*/


#ifndef _ROBUST_FILTERING_HPP_
#define _ROBUST_FILTERING_HPP_

#include <boost/scoped_ptr.hpp>
#include "bayesopt/parameters.hpp"
#include "specialtypes.hpp"
#include "dataset.hpp"
#include "posteriormodel.hpp"


/**
* Namespace of the library interface
*/
namespace bayesopt {


//Forward declaration
class PosteriorModel;
class Dataset;

/** \addtogroup BayesOpt
* \brief Filtering for outliers
*/
/*@{*/

class RobustFiltering
{
public:
/**
* Constructor
* @param params set of parameters (see parameters.hpp)
*/
RobustFiltering(size_t dim, Parameters parameters, randEngine& eng);

/**
* Default destructor
*/
virtual ~RobustFiltering();

const Dataset* filterPoints();

void setSamples(const matrixd &x, const vectord &y)
{
mRobustModel->setSamples(x,y);
};

void addSample(const vectord &x, double y)
{
mRobustModel->addSample(x,y);
};

private:
boost::scoped_ptr<PosteriorModel> mRobustModel;
boost::scoped_ptr<Dataset> mFilteredData;

double up_margin, low_margin;

RobustFiltering();
};

/**@}*/



} //namespace bayesopt


#endif
60 changes: 60 additions & 0 deletions src/robust_filtering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
-------------------------------------------------------------------------
This file is part of BayesOpt, an efficient C++ library for
Bayesian optimization.
Copyright (C) 2011-2015 Ruben Martinez-Cantin <rmcantin@unizar.es>
BayesOpt is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BayesOpt 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with BayesOpt. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
*/

#include "robust_filtering.hpp"

namespace bayesopt
{
RobustFiltering::RobustFiltering(size_t dim, Parameters parameters, randEngine& eng)
{
Parameters par2 = parameters;
up_margin = (100.0 - parameters.up_margin) / 100.0;
low_margin = parameters.low_margin / 100.0;
par2.surr_name = "sStudentTProcessNIG";
mRobustModel.reset(PosteriorModel::create(dim,par2,eng));
}

RobustFiltering::~RobustFiltering(){}

const Dataset* RobustFiltering::filterPoints()
{
mFilteredData.reset();
vecOfvec XX = mRobustModel->getData()->mX;
vectord YY = mRobustModel->getData()->mY;

size_t n_points = mRobustModel->getData()->getNSamples();

for(size_t i = 0; i < n_points; ++i)
{
ProbabilityDistribution* pd = mRobustModel->getPrediction(XX[i]);
double f_up = pd->quantile(up_margin);
double f_low = pd->quantile(low_margin);
if ((YY[i] < f_up) && (YY[i] > f_low))
{
mFilteredData->mX.push_back(XX[i]);
utils::append(mFilteredData->mY, YY[i]);
}
}
return mFilteredData.get();
}

}

0 comments on commit 6a2ca4d

Please sign in to comment.