In [2]:
from scipy.optimize import curve_fit
import pandas as pd
import numpy as np
from uncertainties import ufloat
from bokeh.plotting import figure, show, output_file, save
In [4]:
data = pd.read_csv('./data_b.txt', sep=" ", header=None)
data.columns = ['x','y']
In [6]:
data['weights']=data['y'].apply(lambda elem: 0.01*elem)
In [7]:
data.dtypes
Out[7]:
x            int64
y          float64
weights    float64
dtype: object
In [8]:
data
Out[8]:
x y weights
0 0 0.366037 0.003660
1 5 4.758470 0.047585
2 10 11.042100 0.110421
3 15 17.813800 0.178138
4 20 24.646600 0.246466
5 25 32.333400 0.323334
6 30 41.667100 0.416671
7 35 50.817800 0.508178
8 40 65.885900 0.658859
9 45 77.842900 0.778429
10 50 85.956500 0.859565
11 55 88.945700 0.889457
12 60 86.017500 0.860175
13 65 84.248300 0.842483
14 70 84.736400 0.847364
15 75 83.455300 0.834553
16 80 79.246000 0.792460
17 85 69.729200 0.697292
18 90 66.313000 0.663130
19 95 55.759200 0.557592
20 100 45.205400 0.452054
21 105 29.771100 0.297711
22 110 20.437100 0.204371
23 115 15.556600 0.155566
In [10]:
def fit_data(_func,_data,p0=None,bounds=(-np.inf,np.inf)):
    _x=_data['x']
    _y=_data['y']
    _weights=_data['weights']

    popt, pcov = curve_fit(_func, _x, _y,sigma=_weights,p0=p0,bounds=bounds)
   

    #
    #lin=ufloat(popt[1],np.sqrt(pcov[1,1]))
    #const=ufloat(popt[0],np.sqrt(pcov[0,0]))
    #
    return popt, pcov
In [11]:
def _func(x, N, w,e):
    return np.exp(-w*(x/e + e/x))*N

popt, pcov = fit_data(_func, data,p0=[1,1,60])
In [12]:
x = np.linspace(5, 120, 120)
y = _func(x,popt[0],popt[1],popt[2])
y = _func(x,popt[0],popt[1],popt[2])

TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select"

p1 = figure(title="Plot Example", tools=TOOLS)
p1.circle(data['x'], data['y'], legend="Data",color="orange")
p1.line(x, y, legend="function(x)")

output_file("legend.html", title="legend.py example")

save(p1)  # open a browser
Out[12]:
'/Users/roberto/cernbox/Working/Teaching/Monte Carlo/Lezioni/legend.html'
In [175]:
def _func2(x, N, w,e,N1, w1,e1):
    return np.exp(-w*(x/e + e/x))*N + np.exp(-w1*(x/e1 + e1/x))*N1

popt, pcov = fit_data(_func2, data,p0=[1,1,40,1,1,67],bounds=(0,np.inf) )
+0.05(8) ( +3(6)e+01  )* t/h
In [177]:
x = np.linspace(5, 120, 120)
y = _func2(x,popt[0],popt[1],popt[2],popt[3],popt[4],popt[5])


TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select"

p1 = figure(title="Plot Example", tools=TOOLS)
p1.circle(data['x'], data['y'], legend="Data",color="orange")
p1.line(x, y, legend="sin(x)")

output_file("legend2.html", title="legend.py example")

save(p1)  # open a browser
Out[177]:
'/Users/roberto/Dropbox/Projects/Sentiment Analysis/legend2.html'
In [179]:
popt
Out[179]:
array([  5.25809962e-02,   5.70577479e-01,   2.28190149e+01,
         7.30755630e+01,   3.47908362e+00,   5.84910518e+01])
In [178]:
pcov
Out[178]:
array([[  5.85354353e-03,   7.06193105e-02,  -1.99748387e+00,
         -1.68031296e+01,  -1.27781971e-01,   8.52044496e-02],
       [  7.06193105e-02,   8.75316564e-01,  -2.52795923e+01,
         -2.16615829e+02,  -1.64466737e+00,   8.88380236e-01],
       [ -1.99748387e+00,  -2.52795923e+01,   7.47813392e+02,
          6.45687043e+03,   4.89779409e+01,  -2.22459415e+01],
       [ -1.68031296e+01,  -2.16615829e+02,   6.45687043e+03,
          6.46481361e+04,   4.80434465e+02,  -6.34962338e+01],
       [ -1.27781971e-01,  -1.64466737e+00,   4.89779409e+01,
          4.80434465e+02,   3.58215265e+00,  -5.77494305e-01],
       [  8.52044496e-02,   8.88380236e-01,  -2.22459415e+01,
         -6.34962338e+01,  -5.77494305e-01,   6.70322622e+00]])