MLpp
LogisticRegression.hpp
1 #pragma once
2 /* (C) 2021 Roman Werpachowski */
3 #include <Eigen/Core>
4 #include "dll.hpp"
5 
6 namespace ml
7 {
23  {
24  public:
28  struct Result
29  {
30  Eigen::VectorXd w;
31  unsigned int steps_taken;
32  bool converged;
41  DLL_DECLSPEC void predict(Eigen::Ref<const Eigen::MatrixXd> X, Eigen::Ref<Eigen::VectorXd> y) const;
42 
49  Eigen::VectorXd predict(Eigen::Ref<const Eigen::MatrixXd> X) const
50  {
51  Eigen::VectorXd y(X.cols());
52  predict(X, y);
53  return y;
54  }
55 
62  DLL_DECLSPEC double predict_single(Eigen::Ref<const Eigen::VectorXd> x) const;
63 
65  DLL_DECLSPEC std::string to_string() const;
66  };
67 
71  DLL_DECLSPEC virtual ~LogisticRegression();
72 
83  DLL_DECLSPEC virtual Result fit(Eigen::Ref<const Eigen::MatrixXd> X, Eigen::Ref<const Eigen::VectorXd> y) const = 0;
84 
92  DLL_DECLSPEC static double probability(Eigen::Ref<const Eigen::VectorXd> x, double y, Eigen::Ref<const Eigen::VectorXd> w);
93 
104  DLL_DECLSPEC static double log_likelihood(Eigen::Ref<const Eigen::MatrixXd> X, Eigen::Ref<const Eigen::VectorXd> y, Eigen::Ref<const Eigen::VectorXd> w, double lam);
105 
116  DLL_DECLSPEC static void grad_log_likelihood(Eigen::Ref<const Eigen::MatrixXd> X, Eigen::Ref<const Eigen::VectorXd> y, Eigen::Ref<const Eigen::VectorXd> w, double lam, Eigen::Ref<Eigen::VectorXd> g);
117 
128  DLL_DECLSPEC static void hessian_log_likelihood(Eigen::Ref<const Eigen::MatrixXd> X, Eigen::Ref<const Eigen::VectorXd> y, Eigen::Ref<const Eigen::VectorXd> w, double lam, Eigen::Ref<Eigen::MatrixXd> H);
129  };
130 
137  {
138  public:
142  DLL_DECLSPEC AbstractLogisticRegression();
143 
148  double lam() const
149  {
150  return lam_;
151  }
152 
156  double absolute_tolerance() const
157  {
158  return absolute_tolerance_;
159  }
160 
164  double relative_tolerance() const
165  {
166  return relative_tolerance_;
167  }
168 
172  unsigned int maximum_steps() const
173  {
174  return maximum_steps_;
175  }
176 
182  DLL_DECLSPEC void set_lam(double lam);
183 
189  DLL_DECLSPEC void set_absolute_tolerance(double absolute_tolerance);
190 
196  DLL_DECLSPEC void set_relative_tolerance(double relative_tolerance);
197 
201  DLL_DECLSPEC void set_maximum_steps(unsigned int maximum_steps);
202  protected:
209  bool weights_converged(Eigen::Ref<const Eigen::VectorXd> old_weights, Eigen::Ref<const Eigen::VectorXd> new_weights) const;
210  private:
211  double lam_;
212  double relative_tolerance_;
213  double absolute_tolerance_;
214  unsigned int maximum_steps_;
215  };
216 
221  {
222  public:
223  DLL_DECLSPEC Result fit(Eigen::Ref<const Eigen::MatrixXd> X, Eigen::Ref<const Eigen::VectorXd> y) const override;
224  };
225 }
ml::AbstractLogisticRegression::set_absolute_tolerance
void set_absolute_tolerance(double absolute_tolerance)
Sets absolute tolerance for weight convergence.
ml::LogisticRegression::Result::predict
Eigen::VectorXd predict(Eigen::Ref< const Eigen::MatrixXd > X) const
Predicts labels for features X given w. Version which returns a new vector.
Definition: LogisticRegression.hpp:49
ml::LogisticRegression::grad_log_likelihood
static void grad_log_likelihood(Eigen::Ref< const Eigen::MatrixXd > X, Eigen::Ref< const Eigen::VectorXd > y, Eigen::Ref< const Eigen::VectorXd > w, double lam, Eigen::Ref< Eigen::VectorXd > g)
Calculates the gradient of the posterior log-likelihood of data given model weights,...
ml::ConjugateGradientLogisticRegression::fit
Result fit(Eigen::Ref< const Eigen::MatrixXd > X, Eigen::Ref< const Eigen::VectorXd > y) const override
Fits the model and returns the result.
ml
Definition: BallTree.hpp:10
ml::AbstractLogisticRegression::set_relative_tolerance
void set_relative_tolerance(double relative_tolerance)
Sets relative tolerance for weight convergence.
ml::AbstractLogisticRegression::set_maximum_steps
void set_maximum_steps(unsigned int maximum_steps)
Sets maximum number of steps.
dll.hpp
ml::AbstractLogisticRegression::AbstractLogisticRegression
AbstractLogisticRegression()
Constructor setting default parameter values.
ml::LogisticRegression::Result::predict_single
double predict_single(Eigen::Ref< const Eigen::VectorXd > x) const
Predicts label for feature x given w.
ml::AbstractLogisticRegression
Abstract implementation, sharing the common parameters and stopping criterion.
Definition: LogisticRegression.hpp:136
ml::AbstractLogisticRegression::set_lam
void set_lam(double lam)
Sets the regularisation parameter.
ml::LogisticRegression::Result::w
Eigen::VectorXd w
Definition: LogisticRegression.hpp:30
ml::LogisticRegression::Result::steps_taken
unsigned int steps_taken
Definition: LogisticRegression.hpp:31
ml::AbstractLogisticRegression::weights_converged
bool weights_converged(Eigen::Ref< const Eigen::VectorXd > old_weights, Eigen::Ref< const Eigen::VectorXd > new_weights) const
Check if weight fitting converged.
ml::LogisticRegression::hessian_log_likelihood
static void hessian_log_likelihood(Eigen::Ref< const Eigen::MatrixXd > X, Eigen::Ref< const Eigen::VectorXd > y, Eigen::Ref< const Eigen::VectorXd > w, double lam, Eigen::Ref< Eigen::MatrixXd > H)
Calculates the Hessian of the posterior log-likelihood of data given model weights,...
ml::AbstractLogisticRegression::absolute_tolerance
double absolute_tolerance() const
Returns absolute tolerance for fitted weights.
Definition: LogisticRegression.hpp:156
ml::LogisticRegression::Result::predict
void predict(Eigen::Ref< const Eigen::MatrixXd > X, Eigen::Ref< Eigen::VectorXd > y) const
Predicts labels for features X given w.
ml::ConjugateGradientLogisticRegression
Conjugate gradient logistic regression, as described in Sec. 4 of Thomas P. Minka,...
Definition: LogisticRegression.hpp:220
ml::LogisticRegression
Binomial logistic regression algorithm.
Definition: LogisticRegression.hpp:22
ml::AbstractLogisticRegression::maximum_steps
unsigned int maximum_steps() const
Returns maximum number of steps allowed.
Definition: LogisticRegression.hpp:172
ml::AbstractLogisticRegression::lam
double lam() const
Returns the regularisation parameter.
Definition: LogisticRegression.hpp:148
ml::LogisticRegression::log_likelihood
static double log_likelihood(Eigen::Ref< const Eigen::MatrixXd > X, Eigen::Ref< const Eigen::VectorXd > y, Eigen::Ref< const Eigen::VectorXd > w, double lam)
Calculates the posterior log-likelihood of data given model weights.
ml::LogisticRegression::Result::to_string
std::string to_string() const
Formats the result as string.
ml::LogisticRegression::fit
virtual Result fit(Eigen::Ref< const Eigen::MatrixXd > X, Eigen::Ref< const Eigen::VectorXd > y) const =0
Fits the model and returns the result.
ml::LogisticRegression::probability
static double probability(Eigen::Ref< const Eigen::VectorXd > x, double y, Eigen::Ref< const Eigen::VectorXd > w)
Calculates the probability of label given model weights and feature vector.
ml::AbstractLogisticRegression::relative_tolerance
double relative_tolerance() const
Returns relative tolerance for fitted weights.
Definition: LogisticRegression.hpp:164
ml::LogisticRegression::~LogisticRegression
virtual ~LogisticRegression()
Virtual destructor.
ml::LogisticRegression::Result::converged
bool converged
Definition: LogisticRegression.hpp:32
ml::LogisticRegression::Result
Result of binomial logistic regression.
Definition: LogisticRegression.hpp:28