//--------------------------------------------------------- bool CGW_Regression_Grid::Get_Regression(int x, int y) { int nPoints = Set_Variables(x, y); if( nPoints < m_nPoints_Min ) { return( false ); } //----------------------------------------------------- int i; double zMean, rss, tss; CSG_Vector b, z; CSG_Matrix Y, YtW; //----------------------------------------------------- z .Create(nPoints); Y .Create(2, nPoints); YtW.Create(nPoints, 2); for(i=0, zMean=0.0; i<nPoints; i++) { Y [i][0] = 1.0; Y [i][1] = m_y[i]; YtW[0][i] = m_w[i]; YtW[1][i] = m_w[i] * m_y[i]; zMean += (z[i] = m_z[i]); } //----------------------------------------------------- b = (YtW * Y).Get_Inverse() * (YtW * z); zMean /= nPoints; for(i=0, rss=0.0, tss=0.0; i<nPoints; i++) { rss += m_w[i] * SG_Get_Square(m_z[i] - (b[0] + b[1] * m_y[i])); tss += m_w[i] * SG_Get_Square(m_z[i] - zMean); } GRID_SET_VALUE(m_pRegression, x, y, b[0] + b[1] * m_pPredictor->asDouble(x, y)); GRID_SET_VALUE(m_pIntercept , x, y, b[0]); GRID_SET_VALUE(m_pSlope , x, y, b[1]); GRID_SET_VALUE(m_pQuality , x, y, (tss - rss) / tss); //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGW_Multi_Regression::Get_Regression(int x, int y) { int nPoints = Set_Variables(x, y); if( nPoints < m_nPoints_Min ) { return( false ); } //----------------------------------------------------- int i; double zMean, rss, tss; CSG_Vector b, z; CSG_Matrix Y, YtW; //----------------------------------------------------- z .Create(nPoints); Y .Create(1 + m_nPredictors, nPoints); YtW.Create(nPoints, 1 + m_nPredictors); for(i=0, zMean=0.0; i<nPoints; i++) { Y [i][0] = 1.0; YtW[0][i] = m_w[i]; for(int j=0; j<m_nPredictors; j++) { Y [i][j + 1] = m_y[i][j]; YtW[j + 1][i] = m_y[i][j] * m_w[i]; } zMean += (z[i] = m_z[i]); } //----------------------------------------------------- b = (YtW * Y).Get_Inverse() * (YtW * z); zMean /= nPoints; for(i=0, rss=0.0, tss=0.0; i<nPoints; i++) { double zr = b[0]; for(int j=0; j<m_nPredictors; j++) { zr += b[j + 1] * m_y[i][j]; } rss += m_w[i] * SG_Get_Square(m_z[i] - zr); tss += m_w[i] * SG_Get_Square(m_z[i] - zMean); } m_pQuality ->Set_Value(x, y, tss > 0.0 ? (tss - rss) / tss : 0.0); m_pIntercept->Set_Value(x, y, b[0]); for(i=0; i<m_nPredictors; i++) { m_pSlopes[i]->Set_Value(x, y, b[i + 1]); } //----------------------------------------------------- return( true ); }