Skip to content

Commit 2381463

Browse files
Code clean up
1 parent 70f4ae6 commit 2381463

File tree

1 file changed

+58
-26
lines changed

1 file changed

+58
-26
lines changed

src/bootstrapping.f90

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
! - https://cran.r-project.org/web/packages/meboot/vignettes/meboot.pdf
99
! - https://gist.github.com/christianjauregui/314456688a3c2fead43a48be3a47dad6
1010

11+
interface compute_stats
12+
module procedure :: compute_stats_real64
13+
end interface
14+
1115
contains
1216
! ------------------------------------------------------------------------------
1317
module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
@@ -29,15 +33,14 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
2933

3034
! Parameters
3135
real(real64), parameter :: zero = 0.0d0
32-
real(real64), parameter :: half = 0.5d0
3336
real(real64), parameter :: p05 = 5.0d-2
37+
real(real64), parameter :: half = 5.0d-1
3438

3539
! Local Variables
36-
integer(int32) :: i, j, i1, i2, n, ns, nc, ncoeffs, flag
40+
integer(int32) :: i, j, n, ns, nc, ncoeffs, flag
3741
real(real64) :: eps, alph, ms
3842
real(real64), allocatable, dimension(:) :: fLocal, yLocal, rLocal
3943
real(real64), allocatable, dimension(:,:) :: allcoeffs
40-
type(t_distribution) :: dist
4144
class(errors), pointer :: errmgr
4245
type(errors), target :: deferr
4346

@@ -60,10 +63,7 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
6063
n = size(x)
6164
ncoeffs = order + 1
6265
nc = order
63-
i1 = floor(alph * ns, int32)
64-
i2 = ns - i1 + 1
6566
if (intercept) nc = nc + 1
66-
dist%dof = real(ns - nc)
6767

6868
! Compute the fit
6969
call linear_least_squares(order, intercept, x, y, coeffs, &
@@ -116,26 +116,7 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
116116

117117
! Perform statistics calculations, if needed
118118
if (present(stats)) then
119-
! Update the relevant statistical metrics for each coefficient based
120-
! upon the actual distribution
121-
j = 1
122-
if (intercept) j = 0
123-
do i = 1, nc
124-
j = j + 1
125-
ms = trimmed_mean(allcoeffs(j,:), p = half * alph)
126-
! As we have a distribution of mean values, the standard deviation
127-
! of this population yields the standard error estimate for the
128-
! overall problem
129-
stats(i)%standard_error = standard_deviation(allcoeffs(j,:))
130-
! As before, this is a distribution of mean values. The CI can
131-
! be directly estimated by considering the values of the bottom
132-
! alpha/2 and top alpha/2 terms.
133-
stats(i)%upper_confidence_interval = allcoeffs(j,i2)
134-
stats(i)%lower_confidence_interval = allcoeffs(j,i1)
135-
stats(i)%t_statistic = coeffs(i) / stats(i)%standard_error
136-
stats(i)%probability = regularized_beta(half * dist%dof, half, &
137-
dist%dof / (dist%dof + (stats(i)%t_statistic)**2))
138-
end do
119+
call compute_stats(coeffs, allcoeffs, alph, intercept, stats)
139120
end if
140121

141122
! Compute the bias for each parameter, if needed
@@ -154,5 +135,56 @@ module subroutine bs_linear_least_squares_real64(order, intercept, x, y, &
154135
end if
155136
end subroutine
156137

138+
! ------------------------------------------------------------------------------
139+
subroutine compute_stats_real64(mdl, coeffs, alpha, intercept, stats)
140+
! Arguments
141+
real(real64), intent(in), dimension(:) :: mdl
142+
real(real64), intent(inout), dimension(:,:) :: coeffs
143+
real(real64), intent(in) :: alpha
144+
logical, intent(in) :: intercept
145+
type(bootstrap_regression_statistics), intent(out), dimension(:) :: stats
146+
147+
! Parameters
148+
real(real64), parameter :: half = 0.5d0
149+
150+
! Local Variables
151+
integer(int32) :: i, j, i1, i2, ncoeffs, nc, nsamples
152+
real(real64) :: ms
153+
type(t_distribution) :: dist
154+
155+
! Initialization
156+
ncoeffs = size(coeffs, 1)
157+
nsamples = size(coeffs, 2)
158+
nc = ncoeffs
159+
if (.not.intercept) nc = ncoeffs - 1
160+
i1 = floor(half * alpha * nsamples, int32)
161+
i2 = nsamples - i1 + 1
162+
dist%dof = real(nsamples - nc)
163+
164+
! Process
165+
j = 1
166+
if (intercept) j = 0
167+
do i = 1, nc
168+
j = j + 1
169+
ms = trimmed_mean(coeffs(j,:), p = half * alpha)
170+
171+
! As we have a distribution of mean values, the standard deviation
172+
! of this population yields the standard error estimate for the
173+
! overall problem
174+
stats(i)%standard_error = standard_deviation(coeffs(j,:))
175+
176+
! As before, this is a distribution of mean values. The CI can
177+
! be directly estimated by considering the values of the bottom
178+
! alpha/2 and top alpha/2 terms.
179+
stats(i)%upper_confidence_interval = coeffs(j,i2)
180+
stats(i)%lower_confidence_interval = coeffs(j,i1)
181+
182+
! Compute the remaining parameters
183+
stats(i)%t_statistic = mdl(j) / stats(i)%standard_error
184+
stats(i)%probability = regularized_beta(half * dist%dof, half, &
185+
dist%dof / (dist%dof + (stats(i)%t_statistic)**2))
186+
end do
187+
end subroutine
188+
157189
! ------------------------------------------------------------------------------
158190
end submodule

0 commit comments

Comments
 (0)