-
-
Save jdegenstein/27efab475d630b4b66e1c3d55aa1f7e9 to your computer and use it in GitHub Desktop.
Python code implementing some of MATLAB's nlparci functionality (Nonlinear regression parameter confidence intervals)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = np.linalg.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