In [1]:
%matplotlib inline
from pylab import*
In [2]:
import os
In [3]:
from cvfit import data
from cvfit.equations import Linear
from cvfit.fitting import SingleFitSession
In [4]:
filename = "./Example/Example.xlsx"
set0 = data.read_sets_from_Excel(filename, 2, 0, 2)[0]
print("Loaded: " + os.path.split(str(filename))[1])
print (str(set0))
In [5]:
equation = Linear('Linear')
fsession = SingleFitSession(set0, equation)
In [6]:
fsession.fit()
fsession.calculate_errors()
print(fsession.string_estimates())
fsession.calculate_errors()
print(fsession.string_liklimits())
In [7]:
X, Y = fsession.eq.calculate_plot(fsession.data.X, fsession.eq.pars)
plot(fsession.data.X, fsession.data.Y, 'o')
plot(X, Y, '-');
In [11]:
from scipy.optimize import curve_fit
from scipy.stats.distributions import t
In [8]:
def linear(x, a, b):
'''
The linear equation.
'''
return a + b * x
In [9]:
X = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
Y = [3.17, 13.25, 19.8, 14.18, 11.43, 25.85, 13.81, 25.49, 26.94, 38.86]
In [12]:
theta = [2, 6]
popt, pcov = curve_fit(linear, X, Y, p0=theta, bounds=(0, [100., 100.]))
In [13]:
############
n = len(X) # number of data points
p = 2 # number of parameters
dof = max(0, n - p) # number of degrees of freedom
# student-t value for the dof and confidence level
alpha = 0.05 # 95% confidence interval = 100*(1-alpha)
tval = t.ppf(1.0-alpha/2., dof)
In [14]:
for i, p, var in zip(range(p), popt, np.diag(pcov)):
sigma = var**0.5
print('p{0}: {1} +/- {2} [{3} {4}]'.format(i, p, sigma,
p - sigma*tval, p + sigma*tval))
In [18]:
plt.plot(X,Y,'bo ')
xfit = np.linspace(0,6)
yfit = linear(xfit, popt[0], popt[1])
plt.plot(xfit,yfit,'k-')
plt.legend(['data','fit'],loc='best');
#plt.savefig('images/nonlin-curve-fit-ci.png')
In [ ]: