68 m_Log_Epsilon = 0.001;
69 m_Log_Difference = 1000.;
99 if( m_X.Get_NRows() == 0 )
101 m_X.Create(Predictors.
Get_N() + 1, 1);
103 else if( m_X.Get_NCols() == Predictors.
Get_N() + 1 )
112 m_w.Add_Row(Weight );
113 m_y.Add_Row(Dependent);
115 double *y = m_X[m_X.Get_NRows() - 1];
119 for(
int i=0; i<Predictors.
Get_N(); i++)
121 y[i + 1] = Predictors[i];
139 for(
int i=0; i<Weights.
Get_N(); i++)
153 sLong nSamples = m_w.Get_Size(), nPredictors = m_X.Get_NCols() - 1;
155 if( nSamples <= nPredictors || nSamples < 2 )
163 m_b = _Log_Get_Beta(m_X, m_y, m_w);
165 if( m_b.Get_N() == 0 )
174 for(
sLong i=0; i<nSamples; i++)
178 for(
sLong j=1; j<=nPredictors; j++)
180 YtW[j][i] = m_w[i] * m_X[i][j];
184 m_b = (YtW * m_X).Get_Inverse() * (YtW * m_y);
190 double rss = 0., tss = 0.;
192 for(
sLong i=0; i<nSamples; i++)
196 for(
sLong j=1; j<=nPredictors; j++)
198 yr += m_b[j] * m_X[i][j];
203 yr = 1. / (1. + exp(-yr));
211 if( tss > 0.0 && tss >= rss )
213 m_r2 = fabs(tss - rss) / tss;
239 m_Log_maxIter = maxIter;
252 m_Log_Epsilon = Epsilon;
263 if( Difference > 0. )
265 m_Log_Difference = Difference;
280 for(
int i=0; i<m_Log_maxIter; ++i)
282 CSG_Vector b_new = _Log_Get_Beta(b, X, y, w, p);
284 if( b_new.
Get_N() == 0 )
289 for(
int j=0; j<b_new.
Get_N(); ++j)
297 if( _Log_NoChange(b, b_new) )
302 if( _Log_OutOfControl(b, b_new) )
307 p = _Log_Get_Props(X, b_new);
319 CSG_Matrix M = Xt * _Log_Get_Xwp(p, X, w);
321 CSG_Vector v = N * _Log_Get_Ywp(p, y, w);
328 return( CSG_Vector() );
342 Xwp[i][j] = w[i] * p[i] * (1. - p[i]) * X[i][j];
353 CSG_Vector Ywp(y.
Get_N());
357 for(
int i=0; i<Ywp.Get_N(); i++)
359 Ywp[i] = w[i] * (y[i] - p[i]);
380 p[i] = 1. / (1. + exp(-z));
389 for(
int i=0; i<b_old.
Get_N(); ++i)
391 if( fabs(b_old[i] - b_new[i]) > m_Log_Epsilon )
401bool CSG_Regression_Weighted::_Log_OutOfControl(
const CSG_Vector &b_old,
const CSG_Vector &b_new)
403 for(
int i=0; i<b_old.
Get_N(); ++i)
405 if( b_old[i] == 0.0 )
410 if( fabs(b_old[i] - b_new[i]) / fabs(b_old[i]) > m_Log_Difference )
sLong Get_NRows(void) const
CSG_Matrix Get_Transpose(void) const
sLong Get_NCols(void) const
bool Create(const CSG_Matrix &Matrix)
CSG_Matrix Get_Inverse(bool bSilent=true, int nSubSquare=0) const
CSG_Vector Get_Row(sLong Row) const
virtual ~CSG_Regression_Weighted(void)
bool Get_CrossValidation(int nSubSamples=0)
bool Set_Log_maxIter(int maxIter)
bool Set_Log_Difference(double Difference)
CSG_Regression_Weighted(void)
bool Calculate(const CSG_Vector &Weights, const CSG_Vector &Dependents, const CSG_Matrix &Predictors, bool bLogistic=false)
bool Set_Log_Epsilon(double Epsilon)
bool Add_Sample(double Weight, double Dependent, const CSG_Vector &Predictors)