Cosan  1.0
Data Analytics Library
CosanPCRRidge.h
Go to the documentation of this file.
1 //
2 // Created by Xinyu Zhang on 4/7/21.
3 //
4 
5 #ifndef COSAN_COSANPCRRIDGE_H
6 #define COSAN_COSANPCRRIDGE_H
7 
10 #include <numeric>
11 namespace Cosan
12 {
13 
14  /** principal component regression with L2 square penaly term on coefficient beta.
15  * @tparam NumericType
16  * @details https://en.wikipedia.org/wiki/Principal_component_regression
17  */
18  template<Numeric NumericType>
19  class CosanPCRRidge: public CosanPrincipalComponentRegression<NumericType> {
20  public:
21  // Initialization
23  CosanPCRRidge(std::vector<NumericType> params,bool bias=false ): CosanPrincipalComponentRegression<NumericType>(params[0]){
24  MLambda = params[1];}
25 
26  CosanPCRRidge(CosanRawData<NumericType>& RD,std::vector<NumericType> params ,bool bias = false ): CosanPrincipalComponentRegression<NumericType>(params[0]){
27  MLambda = params[1];
28  fit(RD.GetInput(),RD.GetTarget());
29  }
30  CosanPCRRidge(CosanData<NumericType>& CD,std::vector<NumericType> params ,bool bias = false ): CosanPrincipalComponentRegression<NumericType>(params[0]){
31  MLambda = params[1];
32  fit(CD.GetInput(),CD.GetTarget());
33  }
34  template<class T,
35  std::enable_if_t<std::is_same_v<std::decay_t<T>,CosanMatrix<NumericType>>,bool> = true >
36  CosanPCRRidge(T&& X,const CosanMatrix<NumericType>& Y,std::vector<NumericType> params,bool bias = false): CosanPrincipalComponentRegression<NumericType>(params[0]){
37  MLambda = params[1];
38  this->PC = PrincipalComponentAnalysis(X,this->__ncomp).GetPC();
39  this->DerivatedCovariate = X*(this->PC);
40  CosanMatrix<NumericType> Identity = MLambda*CosanMatrix<NumericType>::Identity((this->PC).cols(),(this->PC).cols());
41  this->MBeta = ((this->DerivatedCovariate).transpose()*(this->DerivatedCovariate)+Identity).ldlt().solve((this->DerivatedCovariate).transpose()*Y);
42  }
43 
44  virtual EModelType GetModelType() override {
46  };
47 
49  return PdUnivariateRegression;}
50  virtual const std::string GetName() const override {
51  return "Principal Component Regression with least square and ridge regularization";}
52 
53  template<class T,
54  std::enable_if_t<std::is_same_v<std::decay_t<T>,CosanMatrix<NumericType>>,bool> =true
55  >
56  void fit(T&& X,const CosanMatrix<NumericType>& Y) {
57  this->PC = PrincipalComponentAnalysis(X,this->__ncomp).GetPC();
58  this->DerivatedCovariate = X*(this->PC);
59  CosanMatrix<NumericType> Identity = MLambda*CosanMatrix<NumericType>::Identity((this->PC).cols(),(this->PC).cols());
60  this->MBeta = ((this->DerivatedCovariate).transpose()*(this->DerivatedCovariate)+Identity).ldlt().solve((this->DerivatedCovariate).transpose()*Y);
61  }
62  void SetParams(std::vector<NumericType> params) {
63  this->__ncomp = params[0];
64  MLambda = params[1]; };
65  NumericType GetParams() {return {this->__ncomp ,MLambda};}
66  CosanMatrix<NumericType> &GetPC() {return this->PC;}
67  private:
69  };
70 }
71 
72 
73 #endif //COSAN_COSANPCRRIDGE_H
Cosan
Definition: CosanBO.h:29
Cosan::CosanRawData::GetInput
CosanMatrix< NumericType > GetInput()
Get a copy of CosanMatrix<NumericType> X.
Definition: CosanData.h:141
Cosan::CosanRawData::GetTarget
CosanMatrix< NumericType > GetTarget()
Get a copy of CosanMatrix<NumericType> Y.
Definition: CosanData.h:147
Cosan::CosanPCRRidge::CosanPCRRidge
CosanPCRRidge(CosanRawData< NumericType > &RD, std::vector< NumericType > params, bool bias=false)
Definition: CosanPCRRidge.h:26
Cosan::CosanPCRRidge::fit
void fit(T &&X, const CosanMatrix< NumericType > &Y)
Definition: CosanPCRRidge.h:56
Cosan::CosanPCRRidge::CosanPCRRidge
CosanPCRRidge(T &&X, const CosanMatrix< NumericType > &Y, std::vector< NumericType > params, bool bias=false)
Definition: CosanPCRRidge.h:36
NumericType
double NumericType
Definition: onehotencodingTest.cpp:20
Cosan::CosanPCRRidge::GetProblemType
virtual EProblemType GetProblemType()
Definition: CosanPCRRidge.h:48
Cosan::CosanPrincipalComponentRegression
Definition: CosanPrincipalComponentRegression.h:20
Cosan::EModelType
EModelType
Definition: CosanModel.h:21
Cosan::CosanPCRRidge::GetPC
CosanMatrix< NumericType > & GetPC()
Definition: CosanPCRRidge.h:66
Cosan::CosanPCRRidge::GetParams
NumericType GetParams()
Definition: CosanPCRRidge.h:65
Cosan::CosanPCRRidge::SetParams
void SetParams(std::vector< NumericType > params)
Definition: CosanPCRRidge.h:62
Cosan::CosanMatrix
Eigen::Matrix< NumericType, Eigen::Dynamic, Eigen::Dynamic > CosanMatrix
Definition: CosanBO.h:37
Cosan::CosanLinearModel::MBeta
CosanMatrix< NumericType > MBeta
Definition: CosanLinearModel.h:56
Cosan::CosanPrincipalComponentRegression::__ncomp
NumericType __ncomp
Definition: CosanPrincipalComponentRegression.h:72
Cosan::MdRidgePrincipalComponentRegression
@ MdRidgePrincipalComponentRegression
Definition: CosanModel.h:26
Cosan::CosanData
Data container.
Definition: CosanData.h:546
Cosan::EProblemType
EProblemType
Definition: CosanModel.h:13
principalcomponentanalysis.h
Cosan::CosanPrincipalComponentRegression::PC
CosanMatrix< NumericType > PC
Definition: CosanPrincipalComponentRegression.h:73
Cosan::PrincipalComponentAnalysis::GetPC
CosanMatrix< NumericType > & GetPC()
Definition: principalcomponentanalysis.h:34
Cosan::PrincipalComponentAnalysis
Definition: principalcomponentanalysis.h:16
Cosan::CosanRawData
Raw Data container.
Definition: CosanData.h:36
Cosan::CosanPCRRidge::CosanPCRRidge
CosanPCRRidge(std::vector< NumericType > params, bool bias=false)
Definition: CosanPCRRidge.h:23
Cosan::CosanPCRRidge::CosanPCRRidge
CosanPCRRidge(CosanData< NumericType > &CD, std::vector< NumericType > params, bool bias=false)
Definition: CosanPCRRidge.h:30
CosanPrincipalComponentRegression.h
Cosan::PdUnivariateRegression
@ PdUnivariateRegression
Definition: CosanModel.h:16
Cosan::CosanPrincipalComponentRegression::DerivatedCovariate
CosanMatrix< NumericType > DerivatedCovariate
Definition: CosanPrincipalComponentRegression.h:74
Cosan::CosanPCRRidge::GetModelType
virtual EModelType GetModelType() override
Definition: CosanPCRRidge.h:44
Cosan::CosanPCRRidge
Definition: CosanPCRRidge.h:19
Cosan::CosanPCRRidge::CosanPCRRidge
CosanPCRRidge()
Definition: CosanPCRRidge.h:22
Cosan::CosanPCRRidge::GetName
virtual const std::string GetName() const override
Get the name of the objects.
Definition: CosanPCRRidge.h:50
Cosan::CosanPCRRidge::MLambda
NumericType MLambda
Definition: CosanPCRRidge.h:68