#include <agm.h>
List of all members.
Public Member Functions |
| TLogRegFit () |
| ~TLogRegFit () |
PLogRegPredict | CalcLogRegGradient (const TVec< TFltV > &XPt, const TFltV &yPt, const TStr &PlotNm=TStr(), const double &ChangeEps=0.01, const int &MaxStep=200, const bool InterceptPt=false) |
PLogRegPredict | CalcLogRegNewton (const TVec< TFltV > &XPt, const TFltV &yPt, const TStr &PlotNm=TStr(), const double &ChangeEps=0.01, const int &MaxStep=200, const bool InterceptPt=false) |
int | MLEGradient (const double &ChangeEps, const int &MaxStep, const TStr PlotNm) |
int | MLENewton (const double &ChangeEps, const int &MaxStep, const TStr PlotNm) |
double | GetStepSizeByLineSearch (const TFltV &DeltaV, const TFltV &GradV, const double &Alpha, const double &Beta) |
double | Likelihood (const TFltV &NewTheta) |
double | Likelihood () |
void | Gradient (TFltV &GradV) |
void | Hessian (TFltVV &HVV) |
void | GetNewtonStep (TFltVV &HVV, const TFltV &GradV, TFltV &DeltaLV) |
Private Attributes |
TVec< TFltV > | X |
TFltV | Y |
TFltV | Theta |
int | M |
Detailed Description
Definition at line 142 of file agm.h.
Constructor & Destructor Documentation
Member Function Documentation
Definition at line 698 of file agm.cpp.
{
bool HSingular = false;
for (int i = 0; i < HVV.GetXDim(); i++) {
if (HVV(i,i) == 0.0) {
HVV(i,i) = 0.001;
HSingular = true;
}
DeltaLV[i] = GradV[i] / HVV(i, i);
}
if (! HSingular) {
if (HVV(0, 0) < 0) {
for (int r = 0; r < Theta.Len(); r++) {
for (int c = 0; c < Theta.Len(); c++) {
HVV(r, c) = - HVV(r, c);
}
}
TNumericalStuff::SolveSymetricSystem(HVV, GradV, DeltaLV);
}
else {
TNumericalStuff::SolveSymetricSystem(HVV, GradV, DeltaLV);
for (int i = 0; i < DeltaLV.Len(); i++) {
DeltaLV[i] = - DeltaLV[i];
}
}
}
}
Definition at line 817 of file agm.cpp.
{
double StepSize = 1.0;
double InitLikelihood = Likelihood();
IAssert(Theta.Len() == DeltaV.Len());
TFltV NewThetaV(Theta.Len());
double MinVal = -1e10, MaxVal = 1e10;
for(int iter = 0; ; iter++) {
for (int i = 0; i < Theta.Len(); i++){
NewThetaV[i] = Theta[i] + StepSize * DeltaV[i];
if (NewThetaV[i] < MinVal) { NewThetaV[i] = MinVal; }
if (NewThetaV[i] > MaxVal) { NewThetaV[i] = MaxVal; }
}
if (Likelihood(NewThetaV) < InitLikelihood + Alpha * StepSize * TLinAlg::DotProduct(GradV, DeltaV)) {
StepSize *= Beta;
} else {
break;
}
}
return StepSize;
}
Definition at line 838 of file agm.cpp.
{
TFltV OutV;
TLogRegPredict::GetCfy(X, OutV, NewTheta);
double L = 0;
for (int r = 0; r < OutV.Len(); r++) {
L += Y[r] * log(OutV[r]);
L += (1 - Y[r]) * log(1 - OutV[r]);
}
return L;
}
Definition at line 777 of file agm.cpp.
{
TExeTm ExeTm;
TFltV GradV(Theta.Len());
int iter = 0;
TIntFltPrV IterLV, IterGradNormV;
double MinVal = -1e10, MaxVal = 1e10;
double GradCutOff = 100000;
for(iter = 0; iter < MaxStep; iter++) {
Gradient(GradV);
for(int i = 0; i < Theta.Len(); i++) {
if (GradV[i] < -GradCutOff) { GradV[i] = -GradCutOff; }
if (GradV[i] > GradCutOff) { GradV[i] = GradCutOff; }
if (Theta[i] <= MinVal && GradV[i] < 0) { GradV[i] = 0.0; }
if (Theta[i] >= MaxVal && GradV[i] > 0) { GradV[i] = 0.0; }
}
double Alpha = 0.15, Beta = 0.9;
double LearnRate = GetStepSizeByLineSearch(GradV, GradV, Alpha, Beta);
if (TLinAlg::Norm(GradV) < ChangeEps) { break; }
for(int i = 0; i < Theta.Len(); i++) {
double Change = LearnRate * GradV[i];
Theta[i] += Change;
if(Theta[i] < MinVal) { Theta[i] = MinVal;}
if(Theta[i] > MaxVal) { Theta[i] = MaxVal;}
}
if (! PlotNm.Empty()) {
double L = Likelihood();
IterLV.Add(TIntFltPr(iter, L));
IterGradNormV.Add(TIntFltPr(iter, TLinAlg::Norm(GradV)));
}
}
if (! PlotNm.Empty()) {
TGnuPlot::PlotValV(IterLV, PlotNm + ".likelihood_Q");
TGnuPlot::PlotValV(IterGradNormV, PlotNm + ".gradnorm_Q");
printf("MLE for Lambda completed with %d iterations(%s)\n",iter,ExeTm.GetTmStr());
}
return iter;
}
Definition at line 750 of file agm.cpp.
{
TExeTm ExeTm;
TFltV GradV(Theta.Len()), DeltaLV(Theta.Len());
TFltVV HVV(Theta.Len(), Theta.Len());
int iter = 0;
double MinVal = -1e10, MaxVal = 1e10;
for(iter = 0; iter < MaxStep; iter++) {
Gradient(GradV);
Hessian(HVV);
GetNewtonStep(HVV, GradV, DeltaLV);
double Increment = TLinAlg::DotProduct(GradV, DeltaLV);
if (Increment <= ChangeEps) {break;}
double LearnRate = GetStepSizeByLineSearch(DeltaLV, GradV, 0.15, 0.5);
for(int i = 0; i < Theta.Len(); i++) {
double Change = LearnRate * DeltaLV[i];
Theta[i] += Change;
if(Theta[i] < MinVal) { Theta[i] = MinVal;}
if(Theta[i] > MaxVal) { Theta[i] = MaxVal;}
}
}
if (! PlotNm.Empty()) {
printf("MLE with Newton method completed with %d iterations(%s)\n",iter,ExeTm.GetTmStr());
}
return iter;
}
Member Data Documentation
The documentation for this class was generated from the following files: