Skip to content

Commit c2de9fb

Browse files
committed
handle zero variance fast linear fit
1 parent 6d46784 commit c2de9fb

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

pymc_bart/pgbart.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,16 @@ def fast_linear_fit(
523523
xbar = np.sum(x) / n
524524
ybar = np.sum(y) / n
525525

526-
b = ((x - xbar) @ (y - ybar).T) / ((x - xbar) @ (x - xbar).T)
526+
x_diff = x - xbar
527+
y_diff = y - ybar
528+
529+
x_var = x_diff @ x_diff.T
530+
531+
if x_var == 0:
532+
b = np.zeros(1)
533+
else:
534+
b = (x_diff @ y_diff.T) / x_var
535+
527536
a = ybar - b * xbar
528537

529538
y_fit = a + b * x

0 commit comments

Comments
 (0)