  This paper derives the optimal fit to a pixel's count rate in the case of an
ideal detector read out nondestructively in the presence of both read and
photon noise. The approach is general for any readout scheme, provides
closed-form expressions for all quantities, and has a computational cost that
is linear in the number of resultants (groups of reads). I also derive the bias
of the fit from estimating the covariance matrix and show how to remove it to
first order. The ramp-fitting algorithm I describe provides the $\chi^2$ value
of the fit of a line to the accumulated counts, which can be interpreted as a
goodness-of-fit metric. I provide and describe a pure Python implementation of
these algorithms that can process a 10-resultant ramp on a $4096 \times 4096$
detector in $\approx$8 seconds with bias removal on a single core of a 2020
Macbook Air. This Python implementation, together with tests and a tutorial
notebook, are available at \url{https://github.com/t-brandt/fitramp}. A
companion paper describes a jump detection algorithm based on hypothesis
testing of ramp fits and demonstrates all algorithms on data from JWST.
