home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 21 / IOPROG_21.ISO / SOFT / LIBMAT.ZIP / OLSQRH.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-16  |  3.9 KB  |  147 lines

  1. /**************************************************/
  2. /*        olsqrh.c source for olsQrh class        */
  3. /**************************************************/
  4.  
  5.  
  6. /**************************************************/
  7. /*            MatClass Source File                */
  8. /*       Copyright of C. R. Birchenhall           */
  9. /*       University of Manchester, UK.            */
  10. /*   MatClass is freeware. This file should be    */
  11. /* made freely available to users of any software */
  12. /* whose creation is wholly or partly dependent   */
  13. /*                on this file.                   */
  14. /**************************************************/
  15.  
  16. #include "olsqrh.hpp"
  17. #include "matqrh.hpp"
  18.  
  19. /*************************************************************/
  20. /*                      olsQrh methods                       */
  21. /*************************************************************/
  22.  
  23. void olsQrh::setNames( void )
  24. {
  25.    U.name( "olsU" ) ;
  26.    Q.name( " olsQ" ) ;
  27.    w.name( "olsW" ) ;
  28. } // olsprh setNames
  29.  
  30. #ifdef __cplusplus
  31. olsQrh::olsQrh( void ) : matOls( )
  32. #else
  33. olsQrh::olsQrh( void ) : ()
  34. #endif
  35. {
  36.    static char *mName = "olsQrh" ;
  37.    matFunc func( mName ) ; 
  38.    status = 0 ;
  39.    setNames() ;
  40.    func.trace(TRUE) ;
  41. } // olsQrh
  42.  
  43. #ifdef __cplusplus
  44. olsQrh::olsQrh( olsQrh& ols ) : matOls( ols )
  45. #else
  46. olsQrh::olsQrh( olsQrh& ols  ) : ( ols )
  47. #endif
  48. {} // olsQrh
  49.  
  50. void olsQrh::operator = ( olsQrh& ols )
  51. {
  52.       matOls::operator = ( ols ) ;
  53. } // olsQrh =
  54.  
  55. #ifdef __cplusplus
  56. olsQrh::olsQrh( const matrix& y, const matrix& x )
  57.      : matOls( y, x )
  58. #else
  59. olsQrh::olsQrh( const matrix& y, const matrix& x )
  60.      : ( y, x )
  61. #endif
  62. {
  63.    static char *mName = "olsQrh(y,x)" ;
  64.    matFunc func( mName ) ; 
  65.    setNames() ;
  66.    func.trace(TRUE) ;
  67. } // olsQrh( matrix& )
  68.  
  69. olsQrh::~olsQrh( void )
  70. {
  71.    static char *mName = "~olsQrh" ;
  72.    matFunc func( mName ) ; debugInfo( func ) ;
  73. } // ~olsQrh
  74.  
  75. void olsQrh::initial( void )
  76. {
  77.    matOls::initial() ;
  78.    U.reset( nObs, nDep ) ;
  79.    Q.reset( nObs, nVars ) ;
  80.    w.reset( nVars ) ;
  81. } // olsQrh::initial
  82.  
  83. void olsQrh::clear( void )
  84. {
  85.    matOls::clear() ;
  86.    U.clear() ;
  87.    Q.clear() ;
  88.    w.clear() ;
  89. } // olsQrh::clear
  90.  
  91. void olsQrh::decompose( void )
  92. {
  93.    static char *mName = "decomp" ;
  94.    matFunc func( mName ) ; debugInfo( func ) ;
  95.    matError error ;
  96.    if ( !( status & ASSIGNED ) )
  97.       errorExit( mName, UNASS ) ;
  98.    if ( !( status & DECOMPOSED ) ) {
  99.       U = Y ;
  100.       Q = X ;
  101.       // QR decomposition and formation of OLS coeffs
  102.       // leaving U = Q'Y, projection of Y onto X-space
  103.       if ( hProject( U, Q, R, w, beta, tol, error ) ) {
  104.          // generate R inverse
  105.          triuInv( R, Rinv, tol, error ) ;
  106.       } else
  107.          status |= SINGULAR ;
  108.       status |= DECOMPOSED ;
  109.    } // if
  110. } // olsQrh::decompose
  111.  
  112. outFile& olsQrh::info( outFile& f ) M_CONST
  113. {
  114.    return matOls::olsInfo( f, "olsQrh" ) ;
  115. } // olsQrh::info
  116.  
  117. REAL olsQrh::varAdd( const matrix& z, INDEX i )
  118. /***************************************************
  119.   Return modified RSS when vars z added to nth eq.
  120. ***************************************************/
  121. {
  122.    static char *mName = "addVar" ;
  123.    matFunc func( mName ) ; debugInfo( func ) ;
  124.    matError error ;
  125.    matrix Z, u, q, dg, b ;
  126.    INDEX nc = z.nCols() ;
  127.    REAL s ;
  128.    if ( nc >= dof )
  129.       errorExit( mName, GTDIM ) ;
  130.    // form Q'z
  131.    Z = z ;
  132.    if ( !qReflect( Z, Q, w, error ) )
  133.       errorExit( mName, error ) ;
  134.    // decompose lower Z into qr form
  135.    q = Z.sub( nVars+1, nObs, 1, nc ) ;
  136.    if ( !qrh( q, dg, b, tol, error ) )
  137.       errorExit( mName, error ) ;
  138.    // u = copy of original LUS residuals
  139.    u = U.sub( nVars+1, nObs, i, i ) ;
  140.    // project u onto residual space of new vars
  141.    if ( !qReflect( u, q, b, error ) )
  142.       errorExit( mName, error ) ;
  143.    // estimate new rss from new LUS residuals in u
  144.    s = u.smpl( nc + 1, u.nRows() ).sumsq() ;
  145.    return s ;
  146. } // olsQrh::varAdd
  147.