0

I have a scatter plot and only my y values have uncertainty.

I obtained a linear fit to my data (using curve_fit from scipy.optimize).

enter image description here

I also have the 1 sigma uncertainty for the gradient and the intercept of the linear fit:

Gradient: -4.304e-06 +- 3.908e-06 Intercept: 0.068 +- 0.005

But I want to put some kind of uncertainty shading on my linear fit so i can visualize the uncertainty on the linear fit?

To do this, I think I need to considered the covariance between the gradient and intercept parameters, but I'm not certain. The top answer on this post gives a formula for the interdependence of gradient and intercept:

enter image description here

To get my visualization, would I just sample from this ring and plot many linear fits on my plot to shade an uncertainty region?

user1551817
  • 1,203
  • 1
  • Are you after an interval for the position of the line across the x's, or are you fixing some point (e.g. taking $(\bar{x},\bar{y})$ as a given) so you can focus purely on the gradient (i.e. specifically on the slope parameter) given the fixed point? I'd normally assume the former for an interval around a fitted line in a picture like yours, but you took the trouble to specify gradient in your question, so it's best to be doubly sure) 2. If you're after a pointwise interval*, this is a pretty standard weighted-regression problem and there's an algebraic formula, no need to sample.
  • – Glen_b Oct 27 '23 at 00:39
  • 1
    * by this I mean that the coverage properties would be given pointwise, the coverage statement applying at any specific $x$ value. – Glen_b Oct 27 '23 at 00:40
  • Thanks Glen. Yes I meant the former. I will edit my question to say "uncertainty on the linear fit" instead of "uncertainty on the gradient". – user1551817 Oct 27 '23 at 07:52
  • 2
    Assuming you're after a pointwise interval, isn't the CI for the weighted line (available as a simple algebraic formula, though possibly adjusted to z's rather than t's for known error variance if you want to rely on the specific values rather than their relative sizes) doing what you seek? – Glen_b Oct 28 '23 at 00:38
  • Thank you Glen. Yes perhaps I am over complicating this. The confidence interval for the weighted line is indeed what I am looking for. I perhaps can't see the wood for the trees, but which simple algebraic formula do I need? – user1551817 Oct 30 '23 at 11:48
  • For additional discussion and generalizations, please search our site for regression confidence band. – whuber Nov 01 '23 at 22:10