Calculate confidence band of least-square fit

后端 未结 2 945
悲&欢浪女
悲&欢浪女 2021-02-04 17:22

I got a question that I fight around for days with now.

How do I calculate the (95%) confidence band of a fit?

Fitt

2条回答
  •  野性不改
    2021-02-04 18:00

    kmpfit's confidence_band() calculates the confidence band for non-linear least squares. Here for your saturation curve:

    from pylab import *
    from kapteyn import kmpfit
    
    def model(p, x):
      a, b = p
      return a*(1-np.exp(b*x))
    
    x = np.linspace(0, 10, 100)
    y = .1*np.random.randn(x.size) + model([1, -.4], x)
    
    fit = kmpfit.simplefit(model, [.1, -.1], x, y)
    a, b = fit.params
    dfdp = [1-np.exp(b*x), -a*x*np.exp(b*x)]
    yhat, upper, lower = fit.confidence_band(x, dfdp, 0.95, model)
    
    scatter(x, y, marker='.', color='#0000ba')
    for i, l in enumerate((upper, lower, yhat)):
      plot(x, l, c='g' if i == 2 else 'r', lw=2)
    savefig('kmpfit confidence bands.png', bbox_inches='tight')
    

    The dfdp are the partial derivatives ∂f/∂p of the model f = a*(1-e^(b*x)) with respect to each parameter p (i.e., a and b), see my answer to a similar question for background links. And here the output:

提交回复
热议问题