In [0]:
import numpy as np
In [0]:
import pandas as pd

העלאת קובץ האקסל הרצוי

In [0]:
df = pd.read_excel("/content/lab_2_chi2_tut.xlsx")
In [23]:
df
Out[23]:
X y y_err
0 0 0.3 0.13
1 1 2.2 0.13
2 2 3.9 0.13
3 3 6.1 0.13
4 4 8.0 0.13
5 5 9.7 0.13
In [0]:
X = df["X"].values
In [0]:
y = df["y"].values
In [0]:
y_err = df["y_err"].values

הגדרת הפונקציות שאני צריך להתאמה

In [0]:
def lin_fun(X,a,b):
  return a * X + b
In [0]:
exp_fun = lambda X,a,b: a * np.exp(b * X)

probfit התקנה חבילת

In [33]:
!pip install -q matplotlib-venn probfit
     |████████████████████████████████| 1.2MB 4.9MB/s 
     |████████████████████████████████| 4.2MB 45.6MB/s 
  Building wheel for probfit (setup.py) ... done
In [0]:
from probfit import Chi2Regression 
In [0]:
reg = Chi2Regression(lin_fun,X,y, error=y_err)

iminuit התקנת חבילת

In [0]:
!pip install -q matplotlib-venn iminuit
In [0]:
from iminuit import Minuit

הגדרת הנתונים לאופטימזציה

In [40]:
opt = Minuit(reg)
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: InitialParamWarning: Parameter a does not have initial value. Assume 0.
  """Entry point for launching an IPython kernel.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: InitialParamWarning: Parameter a is floating but does not have initial step size. Assume 1.
  """Entry point for launching an IPython kernel.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: InitialParamWarning: Parameter b does not have initial value. Assume 0.
  """Entry point for launching an IPython kernel.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:1: InitialParamWarning: Parameter b is floating but does not have initial step size. Assume 1.
  """Entry point for launching an IPython kernel.

ביצוע האופטימזציה

In [41]:
opt.migrad()
Out[41]:
FCN = 4.035 Ncalls = 34 (34 total)
EDM = 9.52E-22 (Goal: 1E-05) up = 1.0
Valid Min. Valid Param. Above EDM Reached call limit
True True False False
Hesse failed Has cov. Accurate Pos. def. Forced
False True True True False
Name Value Hesse Error Minos Error- Minos Error+ Limit- Limit+ Fixed
0 a 1.903 0.031
1 b 0.28 0.09
In [0]:
chi2 = opt.fval
In [0]:
chi2_ndof = chi2/(6-2)
In [45]:
chi2_ndof
Out[45]:
1.0087348548887007
In [0]:
import matplotlib.pyplot as plt
In [48]:
plt.scatter(X,y)
Out[48]:
<matplotlib.collections.PathCollection at 0x7f2f86ef4240>

הצגת הגרף הסופי עם הפרמטרים

In [64]:
plt.rc("font", family="Times New Roman", size=18)
plt.figure(figsize=(5, 5))
plt.style.use("classic")
plt.xlabel("X_data", fontdict={"size":20, "fontname": "Times New Roman"})
plt.ylabel("Y_data", fontdict={"size":20, "fontname": "Times New Roman"})
plt.errorbar(x=X, y=y, y_err=y_err, elinewidth=10, fmt='none', ecolor="blue")
reg.show(opt)
Out[64]:
((array([0., 1., 2., 3., 4., 5.]), array([0.3, 2.2, 3.9, 6.1, 8. , 9.7])),
 None,
 (array([0., 1., 2., 3., 4., 5.]),
  array([0.27619048, 2.17904762, 4.08190476, 5.9847619 , 7.88761905,
         9.79047619])),
 [])
In [0]:
 
In [67]:
X = df["X"].values
y = df["y"].values
y_err = df["y_err"].values

exp_fun = lambda X,a,b: a * np.exp(b * X)
reg = Chi2Regression(exp_fun,X,y, error=y_err)
opt = Minuit(reg)
opt.migrad()

plt.rc("font", family="Times New Roman", size=18)
plt.figure(figsize=(5, 5))
plt.style.use("classic")
plt.xlabel("X_data", fontdict={"size":20, "fontname": "Times New Roman"})
plt.ylabel("Y_data", fontdict={"size":20, "fontname": "Times New Roman"})
plt.errorbar(x=X, y=y, y_err=y_err, elinewidth=10, fmt='none', ecolor="blue")
reg.show(opt)
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: InitialParamWarning: Parameter a does not have initial value. Assume 0.
  import sys
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: InitialParamWarning: Parameter a is floating but does not have initial step size. Assume 1.
  import sys
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: InitialParamWarning: Parameter b does not have initial value. Assume 0.
  import sys
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:7: InitialParamWarning: Parameter b is floating but does not have initial step size. Assume 1.
  import sys
Out[67]:
((array([0., 1., 2., 3., 4., 5.]), array([0.3, 2.2, 3.9, 6.1, 8. , 9.7])),
 None,
 (array([0., 1., 2., 3., 4., 5.]),
  array([ 1.81124625,  2.56818096,  3.64144492,  5.16323472,  7.32099301,
         10.38049624])),
 [])
In [0]: