1
$\begingroup$

I am trying to do a simple linear regression. I have only two data points with errors. How do I estimate the y-intercept with errors?

Currently I have:

#! /usr/bin/env python

import sys,os
from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt

xdata = [25, 33]
ydata = [-279.430059,-279.450271]
yerr = [0.0021, 0.0019]

def linear(x, a,b):
        y = a*x + b
        return y

## Pol fit
initial=[0.1, min(ydata)]
popt, pcov = curve_fit(linear, xdata, ydata, sigma=yerr,p0=initial)
print("Params", popt, np.sqrt(np.diag(pcov)))

x=np.linspace(xdata[0],xdata[-1],200)

fig, ax = plt.subplots()
plt.plot(xdata, ydata, 'o')
plt.plot(x, linear(x, *popt), '--', lw=1, label="Linear Func")
plt.show()
OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)
('Params', array([-2.52650000e-03, -2.79366897e+02]), array([inf, inf]))

Preferably looking for something that will generalize to more data points.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.