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))


Loaded: Example.xlsx

X	Y	s(Y)	weight
1	3.17	0	1
1	13.25	0	1
2	19.8	0	1
2	14.18	0	1
3	11.43	0	1
3	25.85	0	1
4	13.81	0	1
4	25.49	0	1
5	26.94	0	1
5	38.86	0	1

Linear equation fit


In [5]:
equation = Linear('Linear')
fsession = SingleFitSession(set0, equation)


	Fitting session for Set 1 initialised!

In [6]:
fsession.fit()
fsession.calculate_errors()
print(fsession.string_estimates())
fsession.calculate_errors()
print(fsession.string_liklimits())


Number of point fitted = 10
Number of parameters estimated = 2
Degrees of freedom = 8
Residual error SD = 7.034      (variance = 49.483)
Parameter 1: a  	= 3.666  	  Approx SD = 5.21687	  CV = 142.3
Parameter 2: b  	= 5.204  	  Approx SD = 1.57295	  CV = 30.2
Minimum SSD = 395.865; 
Max log-likelihood = -32.582
Correlation matrix = [!!!! PRINTOUT OF CORRELATION MATRIX NOT IMPLEMENTED YET. SORRY.

WARNING: SOME PARAMETERS ARE STRONGLY CORRELATED (coeff > 0.9); try different guesses
WARNING: SOME PARAMETERS POORLY DEFINED (CV > 33%); try different guesses

LIKELIHOOD INTERVALS
2-unit Likelihood Intervals  (equivalent SD for Gaussian- 2)
Lmax= -32.5818;   Lcrit= -34.5818
Parameter 1:   a	= 3.666	  LOWER limit not found	  UPPER = 14.008
Parameter 2:   b	= 5.204	  LOWER = 2.08269	  UPPER = 8.32224

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, '-');


Using scipy curve_fit


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))


p0: 3.6659999652079693 +/- 5.216871478818698 [-8.3641272368856  15.69612716730154]
p1: 5.2040000101664505 +/- 1.5729459161660968 [1.5767802233035781  8.831219797029323]

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 [ ]: