📄 lsq.f90
字号:
CALL INV(nreq, rinv)
pos = 1
start = 1
DO row = 1, nreq
pos2 = start
DO col = row, nreq
pos1 = start + col - row
IF (row == col) THEN
total = one / d(col)
ELSE
total = rinv(pos1-1) / d(col)
END IF
DO K = col+1, nreq
total = total + rinv(pos1) * rinv(pos2) / d(k)
pos1 = pos1 + 1
pos2 = pos2 + 1
END DO ! K = col+1, nreq
covmat(pos) = total * var
IF (row == col) sterr(row) = SQRT(covmat(pos))
pos = pos + 1
END DO ! col = row, nreq
start = start + nreq - row
END DO ! row = 1, nreq
DEALLOCATE(rinv)
RETURN
END SUBROUTINE cov
SUBROUTINE inv(nreq, rinv)
! ALGORITHM AS274 APPL. STATIST. (1992) VOL.41, NO. 2
! Invert first nreq rows and columns of Cholesky factorization
! produced by AS 75.1.
!
!--------------------------------------------------------------------------
INTEGER, INTENT(IN) :: nreq
REAL (dp), DIMENSION(:), INTENT(OUT) :: rinv
! Local variables.
INTEGER :: pos, row, col, start, k, pos1, pos2
REAL (dp) :: total
! Invert R ignoring row multipliers, from the bottom up.
pos = nreq * (nreq-1)/2
DO row = nreq-1, 1, -1
start = row_ptr(row)
DO col = nreq, row+1, -1
pos1 = start
pos2 = pos
total = zero
DO k = row+1, col-1
pos2 = pos2 + nreq - k
total = total - r(pos1) * rinv(pos2)
pos1 = pos1 + 1
END DO ! k = row+1, col-1
rinv(pos) = total - r(pos1)
pos = pos - 1
END DO ! col = nreq, row+1, -1
END DO ! row = nreq-1, 1, -1
RETURN
END SUBROUTINE inv
SUBROUTINE partial_corr(in, cormat, dimc, ycorr, ifault)
! Replaces subroutines PCORR and COR of:
! ALGORITHM AS274 APPL. STATIST. (1992) VOL.41, NO. 2
! Calculate partial correlations after the variables in rows
! 1, 2, ..., IN have been forced into the regression.
! If IN = 1, and the first row of R represents a constant in the
! model, then the usual simple correlations are returned.
! If IN = 0, the value returned in array CORMAT for the correlation
! of variables Xi & Xj is:
! sum ( Xi.Xj ) / Sqrt ( sum (Xi^2) . sum (Xj^2) )
! On return, array CORMAT contains the upper triangle of the matrix of
! partial correlations stored by rows, excluding the 1's on the diagonal.
! e.g. if IN = 2, the consecutive elements returned are:
! (3,4) (3,5) ... (3,ncol), (4,5) (4,6) ... (4,ncol), etc.
! Array YCORR stores the partial correlations with the Y-variable
! starting with YCORR(IN+1) = partial correlation with the variable in
! position (IN+1).
!
!--------------------------------------------------------------------------
INTEGER, INTENT(IN) :: in, dimc
INTEGER, INTENT(OUT) :: ifault
REAL (dp), DIMENSION(:), INTENT(OUT) :: cormat, ycorr
! Local variables.
INTEGER :: base_pos, pos, row, col, col1, col2, pos1, pos2
REAL (dp) :: rms(in+1:ncol), sumxx, sumxy, sumyy, work(in+1:ncol)
! Some checks.
ifault = 0
IF (in < 0 .OR. in > ncol-1) ifault = ifault + 4
IF (dimc < (ncol-in)*(ncol-in-1)/2) ifault = ifault + 8
IF (ifault /= 0) RETURN
! Base position for calculating positions of elements in row (IN+1) of R.
base_pos = in*ncol - (in+1)*(in+2)/2
! Calculate 1/RMS of elements in columns from IN to (ncol-1).
IF (d(in+1) > zero) rms(in+1) = one / SQRT(d(in+1))
DO col = in+2, ncol
pos = base_pos + col
sumxx = d(col)
DO row = in+1, col-1
sumxx = sumxx + d(row) * r(pos)**2
pos = pos + ncol - row - 1
END DO ! row = in+1, col-1
IF (sumxx > zero) THEN
rms(col) = one / SQRT(sumxx)
ELSE
rms(col) = zero
ifault = -col
END IF ! (sumxx > zero)
END DO ! col = in+1, ncol-1
! Calculate 1/RMS for the Y-variable
sumyy = sserr
DO row = in+1, ncol
sumyy = sumyy + d(row) * rhs(row)**2
END DO ! row = in+1, ncol
IF (sumyy > zero) sumyy = one / SQRT(sumyy)
! Calculate sums of cross-products.
! These are obtained by taking dot products of pairs of columns of R,
! but with the product for each row multiplied by the row multiplier
! in array D.
pos = 1
DO col1 = in+1, ncol
sumxy = zero
work(col1+1:ncol) = zero
pos1 = base_pos + col1
DO row = in+1, col1-1
pos2 = pos1 + 1
DO col2 = col1+1, ncol
work(col2) = work(col2) + d(row) * r(pos1) * r(pos2)
pos2 = pos2 + 1
END DO ! col2 = col1+1, ncol
sumxy = sumxy + d(row) * r(pos1) * rhs(row)
pos1 = pos1 + ncol - row - 1
END DO ! row = in+1, col1-1
! Row COL1 has an implicit 1 as its first element (in column COL1)
pos2 = pos1 + 1
DO col2 = col1+1, ncol
work(col2) = work(col2) + d(col1) * r(pos2)
pos2 = pos2 + 1
cormat(pos) = work(col2) * rms(col1) * rms(col2)
pos = pos + 1
END DO ! col2 = col1+1, ncol
sumxy = sumxy + d(col1) * rhs(col1)
ycorr(col1) = sumxy * rms(col1) * sumyy
END DO ! col1 = in+1, ncol-1
ycorr(1:in) = zero
RETURN
END SUBROUTINE partial_corr
SUBROUTINE vmove(from, to, ifault)
! ALGORITHM AS274 APPL. STATIST. (1992) VOL.41, NO. 2
! Move variable from position FROM to position TO in an
! orthogonal reduction produced by AS75.1.
!
!--------------------------------------------------------------------------
INTEGER, INTENT(IN) :: from, to
INTEGER, INTENT(OUT) :: ifault
! Local variables
REAL (dp) :: d1, d2, x, d1new, d2new, cbar, sbar, y
INTEGER :: m, first, last, inc, m1, m2, mp1, col, pos, row
! Check input parameters
ifault = 0
IF (from < 1 .OR. from > ncol) ifault = ifault + 4
IF (to < 1 .OR. to > ncol) ifault = ifault + 8
IF (ifault /= 0) RETURN
IF (from == to) RETURN
IF (.NOT. rss_set) CALL ss()
IF (from < to) THEN
first = from
last = to - 1
inc = 1
ELSE
first = from - 1
last = to
inc = -1
END IF
DO m = first, last, inc
! Find addresses of first elements of R in rows M and (M+1).
m1 = row_ptr(m)
m2 = row_ptr(m+1)
mp1 = m + 1
d1 = d(m)
d2 = d(mp1)
! Special cases.
IF (d1 < vsmall .AND. d2 < vsmall) GO TO 40
x = r(m1)
IF (ABS(x) * SQRT(d1) < tol(mp1)) THEN
x = zero
END IF
IF (d1 < vsmall .OR. ABS(x) < vsmall) THEN
d(m) = d2
d(mp1) = d1
r(m1) = zero
DO col = m+2, ncol
m1 = m1 + 1
x = r(m1)
r(m1) = r(m2)
r(m2) = x
m2 = m2 + 1
END DO ! col = m+2, ncol
x = rhs(m)
rhs(m) = rhs(mp1)
rhs(mp1) = x
GO TO 40
ELSE IF (d2 < vsmall) THEN
d(m) = d1 * x**2
r(m1) = one / x
r(m1+1:m1+ncol-m-1) = r(m1+1:m1+ncol-m-1) / x
rhs(m) = rhs(m) / x
GO TO 40
END IF
! Planar rotation in regular case.
d1new = d2 + d1*x**2
cbar = d2 / d1new
sbar = x * d1 / d1new
d2new = d1 * cbar
d(m) = d1new
d(mp1) = d2new
r(m1) = sbar
DO col = m+2, ncol
m1 = m1 + 1
y = r(m1)
r(m1) = cbar*r(m2) + sbar*y
r(m2) = y - x*r(m2)
m2 = m2 + 1
END DO ! col = m+2, ncol
y = rhs(m)
rhs(m) = cbar*rhs(mp1) + sbar*y
rhs(mp1) = y - x*rhs(mp1)
! Swap columns M and (M+1) down to row (M-1).
40 pos = m
DO row = 1, m-1
x = r(pos)
r(pos) = r(pos-1)
r(pos-1) = x
pos = pos + ncol - row - 1
END DO ! row = 1, m-1
! Adjust variable order (VORDER), the tolerances (TOL) and
! the vector of residual sums of squares (RSS).
m1 = vorder(m)
vorder(m) = vorder(mp1)
vorder(mp1) = m1
x = tol(m)
tol(m) = tol(mp1)
tol(mp1) = x
rss(m) = rss(mp1) + d(mp1) * rhs(mp1)**2
END DO
RETURN
END SUBROUTINE vmove
SUBROUTINE reordr(list, n, pos1, ifault)
! ALGORITHM AS274 APPL. STATIST. (1992) VOL.41, NO. 2
! Re-order the variables in an orthogonal reduction produced by
! AS75.1 so that the N variables in LIST start at position POS1,
! though will not necessarily be in the same order as in LIST.
! Any variables in VORDER before position POS1 are not moved.
! Auxiliary routine called: VMOVE
!
!--------------------------------------------------------------------------
INTEGER, INTENT(IN) :: n, pos1
INTEGER, DIMENSION(:), INTENT(IN) :: list
INTEGER, INTENT(OUT) :: ifault
! Local variables.
INTEGER :: next, i, l, j
! Check N.
ifault = 0
IF (n < 1 .OR. n > ncol+1-pos1) ifault = ifault + 4
IF (ifault /= 0) RETURN
! Work through VORDER finding variables which are in LIST.
next = pos1
i = pos1
10 l = vorder(i)
DO j = 1, n
IF (l == list(j)) GO TO 40
END DO
30 i = i + 1
IF (i <= ncol) GO TO 10
! If this point is reached, one or more variables in LIST has not
! been found.
ifault = 8
RETURN
! Variable L is in LIST; move it up to position NEXT if it is not
! already there.
40 IF (i > next) CALL vmove(i, next, ifault)
next = next + 1
IF (next < n+pos1) GO TO 30
RETURN
END SUBROUTINE reordr
SUBROUTINE hdiag(xrow, nreq, hii, ifault)
! ALGORITHM AS274 APPL. STATIST. (1992) VOL.41, NO. 2
!
! -1 -1
! The hat matrix H = x(X'X) x' = x(R'DR) x' = z'Dz
! -1
! where z = x'R
! Here we only calculate the diagonal element hii corresponding to one
! row (xrow). The variance of the i-th least-squares residual is (1 - hii).
!--------------------------------------------------------------------------
INTEGER, INTENT(IN) :: nreq
INTEGER, INTENT(OUT) :: ifault
REAL (dp), DIMENSION(:), INTENT(IN) :: xrow
REAL (dp), INTENT(OUT) :: hii
! Local variables
INTEGER :: col, row, pos
REAL (dp) :: total, wk(ncol)
! Some checks
ifault = 0
IF (nreq > ncol) ifault = ifault + 4
IF (ifault /= 0) RETURN
! The elements of xrow.inv(R).sqrt(D) are calculated and stored in WK.
hii = zero
DO col = 1, nreq
IF (SQRT(d(col)) <= tol(col)) THEN
wk(col) = zero
ELSE
pos = col - 1
total = xrow(col)
DO row = 1, col-1
total = total - wk(row)*r(pos)
pos = pos + ncol - row - 1
END DO ! row = 1, col-1
wk(col) = total
hii = hii + total**2 / d(col)
END IF
END DO ! col = 1, nreq
RETURN
END SUBROUTINE hdiag
FUNCTION varprd(x, nreq) RESULT(fn_val)
! Calculate the variance of x'b where b consists of the first nreq
! least-squares regression coefficients.
!
!--------------------------------------------------------------------------
INTEGER, INTENT(IN) :: nreq
REAL (dp), DIMENSION(:), INTENT(IN) :: x
REAL (dp) :: fn_val
! Local variables
INTEGER :: ifault, row
REAL (dp) :: var, wk(nreq)
! Check input parameter values
fn_val = zero
ifault = 0
IF (nreq < 1 .OR. nreq > ncol) ifault = ifault + 4
IF (nobs <= nreq) ifault = ifault + 8
IF (ifault /= 0) THEN
WRITE(*, '(1x, a, i4)') 'Error in function VARPRD: ifault =', ifault
RETURN
END IF
! Calculate the residual variance estimate.
var = sserr / (nobs - nreq)
! Variance of x'b = var.x'(inv R)(inv D)(inv R')x
! First call BKSUB2 to calculate (inv R')x by back-substitution.
CALL BKSUB2(x, wk, nreq)
DO row = 1, nreq
IF(d(row) > tol(row)) fn_val = fn_val + wk(row)**2 / d(row)
END DO
fn_val = fn_val * var
RETURN
END FUNCTION varprd
SUBROUTINE bksub2(x, b, nreq)
! Solve x = R'b for b given x, using only the first nreq rows and
! columns of R, and only the first nreq elements of R.
!
!--------------------------------------------------------------------------
INTEGER, INTENT(IN) :: nreq
REAL (dp), DIMENSION(:), INTENT(IN) :: x
REAL (dp), DIMENSION(:), INTENT(OUT) :: b
! Local variables
INTEGER :: pos, row, col
REAL (dp) :: temp
! Solve by back-substitution, starting from the top.
DO row = 1, nreq
pos = row - 1
temp = x(row)
DO col = 1, row-1
temp = temp - r(pos)*b(col)
pos = pos + ncol - col - 1
END DO
b(row) = temp
END DO
RETURN
END SUBROUTINE bksub2
END MODULE lsq
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -