several copy edits to brush up the code
- fix copyright header - rename probabilityDensityFunction -> density_function - use double-precision fp literal to indicate intent - remove redundant enum class start value - replace C-style comments with C++ style - make functions const
This commit is contained in:
parent
133e382aae
commit
31cae8f05f
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
Copyright (c) 2015, Project OSRM, Dennis Luxen, others
|
Copyright (c) 2015, Project OSRM contributors
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification,
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
@ -28,72 +28,75 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#ifndef BAYES_CLASSIFIER_HPP
|
#ifndef BAYES_CLASSIFIER_HPP
|
||||||
#define BAYES_CLASSIFIER_HPP
|
#define BAYES_CLASSIFIER_HPP
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
struct NormalDistribution
|
struct NormalDistribution
|
||||||
{
|
{
|
||||||
NormalDistribution(const double mean, const double standard_deviation)
|
NormalDistribution(const double mean, const double standard_deviation)
|
||||||
: mean(mean)
|
: mean(mean), standard_deviation(standard_deviation)
|
||||||
, standard_deviation(standard_deviation)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME implement log-probability version since its faster
|
// FIXME implement log-probability version since its faster
|
||||||
double probabilityDensityFunction(const double val)
|
double density_function(const double val) const
|
||||||
{
|
{
|
||||||
const double x = val - mean;
|
const double x = val - mean;
|
||||||
return 1.0 / (std::sqrt(2*M_PI) * standard_deviation) * std::exp(-x*x / (standard_deviation * standard_deviation));
|
return 1.0 / (std::sqrt(2. * M_PI) * standard_deviation) *
|
||||||
|
std::exp(-x * x / (standard_deviation * standard_deviation));
|
||||||
}
|
}
|
||||||
|
|
||||||
double mean;
|
double mean;
|
||||||
double standard_deviation;
|
double standard_deviation;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct LaplaceDistribution
|
struct LaplaceDistribution
|
||||||
{
|
{
|
||||||
LaplaceDistribution(const double location, const double scale)
|
LaplaceDistribution(const double location, const double scale)
|
||||||
: location(location)
|
: location(location), scale(scale)
|
||||||
, scale(scale)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME implement log-probability version since its faster
|
// FIXME implement log-probability version since its faster
|
||||||
double probabilityDensityFunction(const double val) const
|
double density_function(const double val) const
|
||||||
{
|
{
|
||||||
const double x = std::abs(val - location);
|
const double x = std::abs(val - location);
|
||||||
return 1.0 / (2*scale) * std::exp(-x / scale);
|
return 1.0 / (2. * scale) * std::exp(-x / scale);
|
||||||
}
|
}
|
||||||
|
|
||||||
double location;
|
double location;
|
||||||
double scale;
|
double scale;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename PositiveDistributionT, typename NegativeDistributionT, typename ValueT>
|
template <typename PositiveDistributionT, typename NegativeDistributionT, typename ValueT>
|
||||||
class BayesClassifier
|
class BayesClassifier
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum class ClassLabel : unsigned {NEGATIVE = 0, POSITIVE};
|
enum class ClassLabel : unsigned
|
||||||
|
{
|
||||||
|
NEGATIVE,
|
||||||
|
POSITIVE
|
||||||
|
};
|
||||||
using ClassificationT = std::pair<ClassLabel, double>;
|
using ClassificationT = std::pair<ClassLabel, double>;
|
||||||
|
|
||||||
BayesClassifier(const PositiveDistributionT& positive_distribution,
|
BayesClassifier(const PositiveDistributionT &positive_distribution,
|
||||||
const NegativeDistributionT& negative_distribution,
|
const NegativeDistributionT &negative_distribution,
|
||||||
const double positive_apriori_probability)
|
const double positive_apriori_probability)
|
||||||
: positive_distribution(positive_distribution)
|
: positive_distribution(positive_distribution),
|
||||||
, negative_distribution(negative_distribution)
|
negative_distribution(negative_distribution),
|
||||||
, positive_apriori_probability(positive_apriori_probability)
|
positive_apriori_probability(positive_apriori_probability),
|
||||||
, negative_apriori_probability(1 - positive_apriori_probability)
|
negative_apriori_probability(1. - positive_apriori_probability)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
// Returns label and the probability of the label.
|
||||||
* Returns label and the probability of the label.
|
ClassificationT classify(const ValueT &v) const
|
||||||
*/
|
|
||||||
ClassificationT classify(const ValueT& v) const
|
|
||||||
{
|
{
|
||||||
const double positive_postpriori = positive_apriori_probability * positive_distribution.probabilityDensityFunction(v);
|
const double positive_postpriori =
|
||||||
const double negative_postpriori = negative_apriori_probability * negative_distribution.probabilityDensityFunction(v);
|
positive_apriori_probability * positive_distribution.density_function(v);
|
||||||
|
const double negative_postpriori =
|
||||||
|
negative_apriori_probability * negative_distribution.density_function(v);
|
||||||
const double norm = positive_postpriori + negative_postpriori;
|
const double norm = positive_postpriori + negative_postpriori;
|
||||||
|
|
||||||
if (positive_postpriori > negative_postpriori)
|
if (positive_postpriori > negative_postpriori)
|
||||||
@ -104,11 +107,11 @@ public:
|
|||||||
return std::make_pair(ClassLabel::NEGATIVE, negative_postpriori / norm);
|
return std::make_pair(ClassLabel::NEGATIVE, negative_postpriori / norm);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PositiveDistributionT positive_distribution;
|
PositiveDistributionT positive_distribution;
|
||||||
NegativeDistributionT negative_distribution;
|
NegativeDistributionT negative_distribution;
|
||||||
double positive_apriori_probability;
|
double positive_apriori_probability;
|
||||||
double negative_apriori_probability;
|
double negative_apriori_probability;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* BAYES_CLASSIFIER_HPP */
|
#endif // BAYES_CLASSIFIER_HPP
|
||||||
|
Loading…
Reference in New Issue
Block a user