8 #ifndef LAR_SUPPORT_VECTOR_MACHINE_H 9 #define LAR_SUPPORT_VECTOR_MACHINE_H 1 15 #include "Helpers/XmlHelper.h" 16 #include "Pandora/StatusCodes.h" 33 typedef std::function<double(const LArMvaHelper::MvaFeatureVector &, const LArMvaHelper::MvaFeatureVector &, const double)>
KernelFunction;
141 FeatureInfo(
const double muValue,
const double sigmaValue);
155 double StandardizeParameter(
const double parameter)
const;
199 pandora::StatusCode
ReadComponent(pandora::TiXmlElement *pCurrentXmlElement);
208 pandora::StatusCode
ReadMachine(
const pandora::TiXmlHandle ¤tHandle);
217 pandora::StatusCode
ReadFeatures(
const pandora::TiXmlHandle ¤tHandle);
226 pandora::StatusCode
ReadSupportVector(
const pandora::TiXmlHandle ¤tHandle);
306 std::cout <<
"LArSupportVectorMachine: cannot calculate probabilities for this SVM" <<
std::endl;
307 throw pandora::STATUS_CODE_NOT_INITIALIZED;
314 return 1. / (1. + std::exp(scaledScore));
343 const double denominator(scaleFactor * scaleFactor);
344 if (denominator < std::numeric_limits<double>::epsilon())
345 throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
348 for (
unsigned int i = 0; i < features.size(); ++i)
349 total += supportVector.at(i).Get() * features.at(i).Get();
351 return total / denominator;
359 const double denominator(scaleFactor * scaleFactor);
360 if (denominator < std::numeric_limits<double>::epsilon())
361 throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
364 for (
unsigned int i = 0; i < features.size(); ++i)
365 total += supportVector.at(i).Get() * features.at(i).Get();
367 total = total / denominator + 1.;
368 return total *
total;
376 const double denominator(scaleFactor * scaleFactor);
377 if (denominator < std::numeric_limits<double>::epsilon())
378 throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
381 for (
unsigned int i = 0; i < features.size(); ++i)
382 total += supportVector.at(i).Get() * features.at(i).Get();
384 total = total / denominator + 1.;
385 return total * total *
total;
394 for (
unsigned int i = 0; i < features.size(); ++i)
395 total += (supportVector.at(i).Get() - features.at(i).Get()) * (supportVector.at(i).Get() - features.at(i).Get());
397 return std::exp(-scaleFactor * total);
412 m_sigmaValue(sigmaValue)
426 if (
m_sigmaValue < std::numeric_limits<double>::epsilon())
427 throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
434 #endif // #ifndef LAR_SUPPORT_VECTOR_MACHINE_H
static double CubicKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
An inhomogeneous cubic kernel.
SupportVectorMachine class.
bool Classify(const LArMvaHelper::MvaFeatureVector &features) const
Make a classification for a set of input features, based on the trained model.
pandora::StatusCode ReadComponent(pandora::TiXmlElement *pCurrentXmlElement)
Read the component at the current xml element.
pandora::StatusCode ReadSupportVector(const pandora::TiXmlHandle ¤tHandle)
Read the support vector component at the current xml handle.
MvaTypes::MvaFeatureVector MvaFeatureVector
std::vector< SupportVectorInfo > SVInfoList
bool m_enableProbability
Whether to enable probability calculations.
double m_scaleFactor
The kernel scale factor.
FeatureInfoVector m_featureInfoList
The list of FeatureInfo objects.
double CalculateClassificationScore(const LArMvaHelper::MvaFeatureVector &features) const
Calculate the classification score for a set of input features, based on the trained model...
std::vector< FeatureInfo > FeatureInfoVector
SVInfoList m_svInfoList
The list of SupportVectorInfo objects.
bool IsInitialized() const
Query whether this svm is initialized.
static Argument * parameter
static double QuadraticKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
An inhomogeneous quadratic kernel.
void SetKernelFunction(KernelFunction kernelFunction)
Set the kernel function to use.
static double LinearKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
A linear kernel.
pandora::StatusCode ReadMachine(const pandora::TiXmlHandle ¤tHandle)
Read the machine component at the current xml handle.
double m_muValue
The average value of this feature.
bool m_standardizeFeatures
Whether to standardize the features.
LArMvaHelper::MvaFeatureVector m_supportVector
The support vector.
double m_bias
The bias term.
void ReadXmlFile(const std::string &svmFileName, const std::string &svmName)
Read the svm parameters from an xml file.
KernelMap m_kernelMap
Map from the kernel types to the kernel functions.
KernelFunction m_kernelFunction
The kernel function.
double StandardizeParameter(const double parameter) const
Standardize a parameter corresponding to this feature.
FeatureInfo()
Default constructor to allow default-construction of (uninitialized) svms.
KernelType m_kernelType
The kernel type.
pandora::StatusCode Initialize(const std::string ¶meterLocation, const std::string &svmName)
Initialize the svm using a serialized model.
double m_probAParameter
The first-order score coefficient for mapping to a probability using the logistic function...
unsigned int m_nFeatures
The number of features.
double m_yAlpha
The alpha-value multiplied by the y-value for the support vector.
std::function< double(const LArMvaHelper::MvaFeatureVector &, const LArMvaHelper::MvaFeatureVector &, const double)> KernelFunction
std::map< KernelType, KernelFunction > KernelMap
double m_sigmaValue
The stddev of this feature.
double CalculateProbability(const LArMvaHelper::MvaFeatureVector &features) const
Calculate the classification probability for a set of input features, based on the trained model...
static double GaussianRbfKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
A gaussian RBF kernel.
double CalculateClassificationScoreImpl(const LArMvaHelper::MvaFeatureVector &features) const
Implementation method for calculating the classification score using the trained model.
SupportVectorMachine()
Default constructor.
bool m_isInitialized
Whether this svm has been initialized.
KernelType
KernelType enum.
double m_probBParameter
The score offset parameter for mapping to a probability using the logistic function.
pandora::StatusCode ReadFeatures(const pandora::TiXmlHandle ¤tHandle)
Read the feature component at the current xml handle.
SupportVectorInfo(const double yAlpha, LArMvaHelper::MvaFeatureVector supportVector)
Constructor.
unsigned int GetNFeatures() const
Get the number of features.
Header file for the lar multivariate analysis interface class.
QTextStream & endl(QTextStream &s)