Skip to content

Commit 1633ba8

Browse files
Add linear fit to correlation plots
1 parent fda61c1 commit 1633ba8

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

src/fplot_stats_plots.f90

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ subroutine cp_init(this, x, labels, term, width, height, err)
8282

8383
! Local Variables
8484
integer(int32) :: i, j, k, t, n, flag
85+
real(real64) :: m, b
86+
real(real64), allocatable, dimension(:) :: mdl
8587
class(errors), pointer :: errmgr
8688
type(errors), target :: deferr
8789
type(plot_2d), allocatable, dimension(:) :: plts
88-
type(plot_data_2d) :: pdata
90+
type(plot_data_2d) :: pdata, mdata
8991
type(plot_data_histogram) :: hdata
9092
class(plot_axis), pointer :: xAxis, yAxis
9193

@@ -121,6 +123,8 @@ subroutine cp_init(this, x, labels, term, width, height, err)
121123
call pdata%set_draw_markers(.true.)
122124
call pdata%set_marker_style(MARKER_FILLED_CIRCLE)
123125
call pdata%set_marker_scaling(0.5)
126+
call mdata%set_line_width(2.0)
127+
call mdata%set_line_color(CLR_BLACK)
124128
if (errmgr%has_error_occurred()) return
125129
do j = 1, n
126130
do i = 1, n
@@ -137,6 +141,15 @@ subroutine cp_init(this, x, labels, term, width, height, err)
137141
call pdata%define_data(x(:,j), x(:,i), err = errmgr)
138142
if (errmgr%has_error_occurred()) return
139143
call plts(k)%push(pdata)
144+
145+
! Fit a line to the data
146+
call compute_linear_fit(x(:,j), x(:,i), m, b)
147+
mdl = m * x(:,j) + b
148+
149+
! Plot the fitted line
150+
call mdata%define_data(x(:,j), mdl, err = err)
151+
if (errmgr%has_error_occurred()) return
152+
call plts(k)%push(mdata)
140153
end if
141154

142155
! Deal with axis labels
@@ -301,5 +314,44 @@ subroutine cp_set_font_size(this, x)
301314
call this%m_plt%set_font_size(x)
302315
end subroutine
303316

317+
! ******************************************************************************
318+
! PRIVATE HELPER ROUTINES
319+
! ------------------------------------------------------------------------------
320+
subroutine compute_linear_fit(x, y, m, b)
321+
!! Computes the coefficients of a linear equation (y = m * x + b) using a
322+
!! least-squares approach.
323+
real(real64), intent(in), dimension(:) :: x
324+
!! The x-coordinate data.
325+
real(real64), intent(in), dimension(:) :: y
326+
!! The y-coordinate data.
327+
real(real64), intent(out) :: m
328+
!! The slope term.
329+
real(real64), intent(out) :: b
330+
!! The intercept term.
331+
332+
! Local Variables
333+
integer(int32) :: i, n
334+
real(real64) :: sumX, sumY, sumX2, sumY2, sumXY
335+
336+
! Initialization
337+
n = size(x)
338+
sumX = 0.0d0
339+
sumY = 0.0d0
340+
sumX2 = 0.0d0
341+
sumY2 = 0.0d0
342+
sumXY = 0.0d0
343+
344+
! Process
345+
do i = 1, n
346+
sumX = sumX + x(i)
347+
sumY = sumY + y(i)
348+
sumXY = sumXY + x(i) * y(i)
349+
sumX2 = sumX2 + (x(i))**2
350+
sumY2 = sumY2 + (y(i))**2
351+
end do
352+
m = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX**2)
353+
b = (sumY * sumX2 - sumX * sumXY) / (n * sumX2 - sumX**2)
354+
end subroutine
355+
304356
! ------------------------------------------------------------------------------
305357
end module

0 commit comments

Comments
 (0)