I have a staircase/step function $n(E)$. I know the points $\{E_i\}$ at which each "step" occurs and all steps are of constant height 1. I need to fit a line $a + bE$ to this function and find the least-squares deviation. In particular, I have to calculate the quantity
$$ \Delta = \min_{a,b} \int_{E_i}^{E_f} [n(E) - a - bE]^2 dE $$
In Python/NumPy, I could try and recreate the function $n(E)$ with np.heaviside
and then try to fit a line to it, but that feels inefficient. Is there a better way to fit a line to a staircase function?
One approach might be to break up the integral into parts between each "step" and then optimize the resultant expression wrt $a,b$. But I'd like to know if there is a cleaner, more efficient way to fit a line to a step function numerically.