From bcfb28480237031227c50a288cc0711a9cbf94a0 Mon Sep 17 00:00:00 2001 From: Jason Christopherson Date: Mon, 15 Apr 2024 08:31:43 -0500 Subject: [PATCH] Update design_matrix --- src/fstats.f90 | 2 +- src/fstats_regression.f90 | 20 +++++++++++--------- tests/fstats_regression_tests.f90 | 14 +++++++------- tests/fstats_tests.f90 | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/fstats.f90 b/src/fstats.f90 index 17ddec2..1c040be 100644 --- a/src/fstats.f90 +++ b/src/fstats.f90 @@ -49,7 +49,7 @@ module fstats public :: digamma public :: incomplete_gamma_upper public :: incomplete_gamma_lower - public :: coefficient_matrix + public :: design_matrix public :: covariance_matrix public :: linear_least_squares public :: regression_statistics diff --git a/src/fstats_regression.f90 b/src/fstats_regression.f90 index 9e05a76..5b79337 100644 --- a/src/fstats_regression.f90 +++ b/src/fstats_regression.f90 @@ -19,7 +19,7 @@ module fstats_regression public :: r_squared public :: adjusted_r_squared public :: correlation - public :: coefficient_matrix + public :: design_matrix public :: covariance_matrix public :: linear_least_squares public :: calculate_regression_statistics @@ -293,16 +293,18 @@ pure function correlation(x, y) result(rst) end function ! ------------------------------------------------------------------------------ -subroutine coefficient_matrix(order, intercept, x, c, err) - !! Computes the coefficient matrix \( X \) to the linear +subroutine design_matrix(order, intercept, x, c, err) + !! Computes the design matrix \( X \) for the linear !! least-squares regression problem of \( X \beta = y \), where - !! \( X \) is the coefficient matrix computed here, \( \beta \) is + !! \( X \) is the matrix computed here, \( \beta \) is !! the vector of coefficients to be determined, and \( y \) is the !! vector of measured dependent variables. !! !! See Also !! !! - [Wikipedia](https://en.wikipedia.org/wiki/Linear_regression) + !! - [Wikipedia](https://en.wikipedia.org/wiki/Vandermonde_matrix) + !! - [Wikipedia](https://en.wikipedia.org/wiki/Design_matrix) integer(int32), intent(in) :: order !! The order of the equation to fit. This value must be !! at least one (linear equation), but can be higher as desired. @@ -343,12 +345,12 @@ subroutine coefficient_matrix(order, intercept, x, c, err) ! Input Check if (order < 1) then - call errmgr%report_error("coefficient_matrix", & + call errmgr%report_error("design_matrix", & "The model order must be at least one.", FS_INVALID_INPUT_ERROR) return end if if (size(c, 1) /= npts .or. size(c, 2) /= ncols) then - call report_matrix_size_error(errmgr, "coefficient_matrix", & + call report_matrix_size_error(errmgr, "design_matrix", & "c", npts, ncols, size(c, 1), size(c, 2)) return end if @@ -372,7 +374,7 @@ subroutine coefficient_matrix(order, intercept, x, c, err) subroutine covariance_matrix(x, c, err) !! Computes the covariance matrix \( C \) where !! \( C = \left( X^{T} X \right)^{-1} \) and \( X \) is computed - !! by coefficient_matrix. + !! by design_matrix. !! !! See Also !! @@ -380,7 +382,7 @@ subroutine covariance_matrix(x, c, err) !! - [Wikipedia - Regression](https://en.wikipedia.org/wiki/Linear_regression) real(real64), intent(in) :: x(:,:) !! An M-by-N matrix containing the formatted independent data - !! matrix \( X \) as computed by coefficient_matrix. + !! matrix \( X \) as computed by design_matrix. real(real64), intent(out) :: c(:,:) !! The N-by-N covariance matrix. class(errors), intent(inout), optional, target :: err @@ -551,7 +553,7 @@ subroutine linear_least_squares(order, intercept, x, y, coeffs, & end if ! Compute the coefficient matrix - call coefficient_matrix(order, intercept, x, a, errmgr) + call design_matrix(order, intercept, x, a, errmgr) if (errmgr%has_error_occurred()) return ! Compute the covariance matrix diff --git a/tests/fstats_regression_tests.f90 b/tests/fstats_regression_tests.f90 index 3837239..15ed15b 100644 --- a/tests/fstats_regression_tests.f90 +++ b/tests/fstats_regression_tests.f90 @@ -5,7 +5,7 @@ module fstats_regression_tests implicit none contains ! ------------------------------------------------------------------------------ - function coefficient_matrix_test_1() result(rst) + function design_matrix_test_1() result(rst) ! Arguments logical :: rst @@ -37,24 +37,24 @@ function coefficient_matrix_test_1() result(rst) ans3(:,5) = x**4 ! Test 1 - linear w/ intercept - call coefficient_matrix(order1, .true., x, c1) + call design_matrix(order1, .true., x, c1) if (.not.is_equal(c1, ans1)) then rst = .false. - print '(A)', "TEST FAILED: Coefficient Matrix Test 1 - 1" + print '(A)', "TEST FAILED: Design Matrix Test 1 - 1" end if ! Test 2 - linear w/o intercept - call coefficient_matrix(order1, .false., x, c2) + call design_matrix(order1, .false., x, c2) if (.not.is_equal(c2, ans2)) then rst = .false. - print '(A)', "TEST FAILED: Coefficient Matrix Test 1 - 2" + print '(A)', "TEST FAILED: Design Matrix Test 1 - 2" end if ! Test 3 - 4th order w/ intercept - call coefficient_matrix(order2, .true., x, c3) + call design_matrix(order2, .true., x, c3) if (.not.is_equal(c3, ans3)) then rst = .false. - print '(A)', "TEST FAILED: Coefficient Matrix Test 1 - 3" + print '(A)', "TEST FAILED: Design Matrix Test 1 - 3" end if end function diff --git a/tests/fstats_tests.f90 b/tests/fstats_tests.f90 index 1d3a19a..ef5da95 100644 --- a/tests/fstats_tests.f90 +++ b/tests/fstats_tests.f90 @@ -73,7 +73,7 @@ program tests local = incomplete_gamma_test_1() if (.not.local) overall = .false. - local = coefficient_matrix_test_1() + local = design_matrix_test_1() if (.not.local) overall = .false. local = regression_test_1()