Cosan  1.0
Data Analytics Library
Cosan::CosanRidgeRegression< NumericType > Class Template Reference

#include <CosanRidgeRegression.h>

Inheritance diagram for Cosan::CosanRidgeRegression< NumericType >:
Cosan::CosanLinearRegression< NumericType > Cosan::CosanLinearModel< NumericType > Cosan::CosanModel Cosan::CosanBO

Public Member Functions

 CosanRidgeRegression (bool bias=false)
 
 CosanRidgeRegression (NumericType Lambda, bool bias=false)
 
 CosanRidgeRegression (CosanRawData< NumericType > &RD, bool bias=false)
 
 CosanRidgeRegression (CosanData< NumericType > &CD, bool bias=false)
 
template<class T , std::enable_if_t< std::is_same_v< std::decay_t< T >, CosanMatrix< NumericType >>, bool > = true>
 CosanRidgeRegression (T &&X, const CosanMatrix< NumericType > &Y, NumericType Lambda, bool Bias)
 
void SetParams (NumericType Lambda)
 
NumericType GetParams ()
 
EModelType GetModelType () override
 
const std::string GetName () const override
 Get the name of the objects. More...
 
template<class T , std::enable_if_t< std::is_same_v< std::decay_t< T >, CosanMatrix< NumericType >>, bool > = true>
void fit (T &&X, const CosanMatrix< NumericType > &Y)
 
- Public Member Functions inherited from Cosan::CosanLinearRegression< NumericType >
 CosanLinearRegression ()
 
 CosanLinearRegression (bool Bias)
 
template<class T , std::enable_if_t< std::is_same_v< std::decay_t< T >, CosanMatrix< NumericType >>, bool > = true>
 CosanLinearRegression (T &&X, const CosanMatrix< NumericType > &Y, bool Bias)
 
 CosanLinearRegression (CosanRawData< NumericType > &RD, bool Bias)
 
 CosanLinearRegression (CosanData< NumericType > &CD, bool Bias)
 
virtual EProblemType GetProblemType () override
 
template<class T , std::enable_if_t< std::is_same_v< std::decay_t< T >, CosanMatrix< NumericType >>, bool > = true>
void fit (T &&X, const CosanMatrix< NumericType > &Y)
 
virtual CosanMatrix< NumericTypepredict (const CosanMatrix< NumericType > &X) override
 
- Public Member Functions inherited from Cosan::CosanLinearModel< NumericType >
 CosanLinearModel ()=delete
 
 CosanLinearModel (bool Bias)
 
void SetBias (const bool NewBias)
 
virtual void SetBeta (CosanMatrix< NumericType > InitBeta)
 
const CosanMatrix< NumericType > & GetBeta () const
 
bool GetBias () const
 
- Public Member Functions inherited from Cosan::CosanModel
 CosanModel ()
 
virtual void SetMaxTrainTime (double t)
 
virtual double GetMaxTrainTime () const
 
virtual ESolverType GetSolverType () const
 
void SetSolverType (ESolverType sr)
 
- Public Member Functions inherited from Cosan::CosanBO
 CosanBO ()
 Default constructor. More...
 

Private Attributes

NumericType MLambda
 

Additional Inherited Members

- Protected Attributes inherited from Cosan::CosanLinearModel< NumericType >
CosanMatrix< NumericTypeMBeta
 
bool MBias
 
- Protected Attributes inherited from Cosan::CosanModel
double MaxTrainTime
 
ESolverType SolverType
 

Detailed Description

template<Numeric NumericType>
class Cosan::CosanRidgeRegression< NumericType >

y = a+bx minimize \sum_{i=1}^n (y_i-a-b^Tx_i)^2+|b|_2^2 -> find the optimal a and b

Template Parameters
NumericTypehttps://en.wikipedia.org/wiki/Ridge_regression

Definition at line 18 of file CosanRidgeRegression.h.

Constructor & Destructor Documentation

◆ CosanRidgeRegression() [1/5]

template<Numeric NumericType>
Cosan::CosanRidgeRegression< NumericType >::CosanRidgeRegression ( bool  bias = false)
inline

Definition at line 25 of file CosanRidgeRegression.h.

25 : CosanLinearRegression<NumericType>(bias){}

◆ CosanRidgeRegression() [2/5]

template<Numeric NumericType>
Cosan::CosanRidgeRegression< NumericType >::CosanRidgeRegression ( NumericType  Lambda,
bool  bias = false 
)
inline

Definition at line 26 of file CosanRidgeRegression.h.

26  : CosanLinearRegression<NumericType>(bias){
27  MLambda=Lambda;
28  }

◆ CosanRidgeRegression() [3/5]

template<Numeric NumericType>
Cosan::CosanRidgeRegression< NumericType >::CosanRidgeRegression ( CosanRawData< NumericType > &  RD,
bool  bias = false 
)
inline

Definition at line 33 of file CosanRidgeRegression.h.

33  : CosanLinearModel<NumericType>(bias){
34  fit(RD.GetInput(),RD.GetTarget());
35  }

◆ CosanRidgeRegression() [4/5]

template<Numeric NumericType>
Cosan::CosanRidgeRegression< NumericType >::CosanRidgeRegression ( CosanData< NumericType > &  CD,
bool  bias = false 
)
inline

Definition at line 36 of file CosanRidgeRegression.h.

36  : CosanLinearModel<NumericType>(bias){
37  fit(CD.GetInput(),CD.GetTarget());
38  }

◆ CosanRidgeRegression() [5/5]

template<Numeric NumericType>
template<class T , std::enable_if_t< std::is_same_v< std::decay_t< T >, CosanMatrix< NumericType >>, bool > = true>
Cosan::CosanRidgeRegression< NumericType >::CosanRidgeRegression ( T &&  X,
const CosanMatrix< NumericType > &  Y,
NumericType  Lambda,
bool  Bias 
)
inline

Definition at line 42 of file CosanRidgeRegression.h.

43  : CosanLinearRegression<NumericType>(Bias){
44  MLambda=Lambda;
45  fit(X,Y);
46  }

Member Function Documentation

◆ fit()

template<Numeric NumericType>
template<class T , std::enable_if_t< std::is_same_v< std::decay_t< T >, CosanMatrix< NumericType >>, bool > = true>
void Cosan::CosanRidgeRegression< NumericType >::fit ( T &&  X,
const CosanMatrix< NumericType > &  Y 
)
inline

Definition at line 57 of file CosanRidgeRegression.h.

57  {
58  CosanMatrix<NumericType> Identity = MLambda*CosanMatrix<NumericType>::Identity(X.cols(),X.cols());
59  if (this->MBias==true){
60  X.conservativeResize(X.rows(), X.cols()+1);
61  X.col(X.cols()-1) = CosanMatrix<NumericType>::Ones(X.rows(),1);
62  Identity.conservativeResize(Identity.rows()+1, Identity.cols()+1);
63  Identity.col(Identity.cols()-1) = CosanMatrix<NumericType>::Zero(Identity.rows(),1);
64  Identity.row(Identity.rows()-1) = CosanMatrix<NumericType>::Zero(1,Identity.cols());
65  }
66 // const auto& svd = X.jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV);
67 // const auto& s = svd.singularValues();
68 // const auto r = s.rows();
69 // const auto& D = s.cwiseQuotient((s.array().square() + MLambda).matrix()).asDiagonal();
70 // MBeta = svd.matrixV().leftCols(r) * D * svd.matrixU().transpose().topRows(r) * Y;
71  this->MBeta = (X.transpose()*X+Identity).ldlt().solve(X.transpose()*Y);
72 // MBeta = (X.transpose()*X+CosanMatrix(MLambda*Eigen::MatrixXd::Identity(X.cols(),X.cols()))).bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(X.transpose()*Y);
73 // MBeta = (X.transpose()*X+CosanMatrix(MLambda*Eigen::MatrixXd::Identity(X.cols(),X.cols()))).colPivHouseholderQr().solve(X.transpose()*Y);
74  if (this->MBias==true){
75  removeColumn(X,X.cols()-1);
76  }
77  }

◆ GetModelType()

template<Numeric NumericType>
EModelType Cosan::CosanRidgeRegression< NumericType >::GetModelType ( )
inlineoverridevirtual

Reimplemented from Cosan::CosanLinearRegression< NumericType >.

Definition at line 52 of file CosanRidgeRegression.h.

52 {return MdRidgeRegression;};

◆ GetName()

template<Numeric NumericType>
const std::string Cosan::CosanRidgeRegression< NumericType >::GetName ( ) const
inlineoverridevirtual

Get the name of the objects.

Description: Get the name of the object. It should return "Abstract Object"

Reimplemented from Cosan::CosanLinearRegression< NumericType >.

Definition at line 53 of file CosanRidgeRegression.h.

53 { return "Linear Ridge Regression";}

◆ GetParams()

template<Numeric NumericType>
NumericType Cosan::CosanRidgeRegression< NumericType >::GetParams ( )
inline

Definition at line 49 of file CosanRidgeRegression.h.

49 {return MLambda;}

◆ SetParams()

template<Numeric NumericType>
void Cosan::CosanRidgeRegression< NumericType >::SetParams ( NumericType  Lambda)
inline

Definition at line 48 of file CosanRidgeRegression.h.

48 {MLambda = Lambda; };

Member Data Documentation

◆ MLambda

template<Numeric NumericType>
NumericType Cosan::CosanRidgeRegression< NumericType >::MLambda
private

Definition at line 80 of file CosanRidgeRegression.h.


The documentation for this class was generated from the following file:
Cosan::CosanLinearModel::MBias
bool MBias
Definition: CosanLinearModel.h:57
Cosan::MdRidgeRegression
@ MdRidgeRegression
Definition: CosanModel.h:24
Cosan::CosanRidgeRegression::MLambda
NumericType MLambda
Definition: CosanRidgeRegression.h:80
Cosan::CosanLinearModel::MBeta
CosanMatrix< NumericType > MBeta
Definition: CosanLinearModel.h:56
removeColumn
void removeColumn(Matrix &matrix, gsl::index colToRemove)
Definition: utils.h:22
Cosan::CosanRidgeRegression::fit
void fit(T &&X, const CosanMatrix< NumericType > &Y)
Definition: CosanRidgeRegression.h:57