Skip to content

Instantly share code, notes, and snippets.

@danieljfarrell
Created February 19, 2016 12:26
Show Gist options
  • Save danieljfarrell/81809e6f53c07a18cd12 to your computer and use it in GitHub Desktop.
Save danieljfarrell/81809e6f53c07a18cd12 to your computer and use it in GitHub Desktop.
Python code implementing some of MATLAB's nlparci functionality (Nonlinear regression parameter confidence intervals)
def non_linear_parameters_95_percent_confidence_interval(fvec, jac):
"""Returns the 95% confidence interval on parameters from
non-linear fit results."""
# residual sum of squares
rss = np.sum(fvec**2)
# number of data points and parameters
n, p = jac.shape
# the statistical degrees of freedom
nmp = n - p
# mean residual error
ssq = rss / nmp
# the Jacobian
J = np.matrix(jac)
# covariance matrix
c = inv(J.T*J)
# variance-covariance matrix.
pcov = c * ssq
# Diagonal terms provide error estimate based on uncorrelated parameters.
# The sqrt convert from variance to std. dev. units.
err = np.sqrt(np.diag(np.abs(pcov))) * 1.96 # std. dev. x 1.96 -> 95% conf
# Here err is the full 95% area under the normal distribution curve. This
# means that the plus-minus error is the half of this value
return err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment