forked from cericdahl/GenericHistFit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.py
86 lines (66 loc) · 2.81 KB
/
examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import GenericHistFit.GHF as ghf
import numpy as np
#%% Gaussian
x = np.random.randn((1000))*3 + 7
hist_out = np.histogram(x, bins=1000, range=(5,30))
thisfit = ghf.HistFit(*hist_out)
thisfit.addfun(ghf.GHF_Gaussian(), [100, 7, 3])
trueparams = np.float64([1000/(np.sqrt(2*np.pi)*3), 7, 3])
thisfit.fit()
sigma_off = (thisfit.bestfit - trueparams) / thisfit.bestfit_err
print('*** Gaussian Fit ***')
print('Success: ' + str(thisfit.fit_out.success))
print('Fit values: ' + str(thisfit.bestfit))
print('Fit uncertainty: ' + str(thisfit.bestfit_err))
print('Sigma deviation: ' + str(sigma_off))
print('Reduced Chi^2: ' + str(thisfit.chisq_nd))
#%% Exponential with flat background
x = -2*np.log(np.random.random((10000)))
y = 10*np.random.random((1000))
d = np.append(x,y)
hist_out = np.histogram(d, bins=1000, range=(0,10))
thisfit = ghf.HistFit(*hist_out)
thisfit.addfun(ghf.GHF_exp(), [10000, -1])
thisfit.addfun(ghf.GHF_flat(), 500)
trueparams = np.float64([5000, -2, 100])
thisfit.fit()
sigma_off = (thisfit.bestfit - trueparams) / thisfit.bestfit_err
print('*** Exponential with Background ***')
print('Success: ' + str(thisfit.fit_out.success))
print('Fit values: ' + str(thisfit.bestfit))
print('Fit uncertainty: ' + str(thisfit.bestfit_err))
print('Sigma deviation: ' + str(sigma_off))
print('Reduced Chi^2: ' + str(thisfit.chisq_nd))
#%% Gaussian with fixed mean
x = np.random.randn((1000)) * 75
hist_out = np.histogram(x, bins=100, range=(-50, 150))
thisfit = ghf.HistFit(*hist_out)
constrained_gaussian = ghf.GHF_Gaussian(mask=[1,0,1],
fixedparams = [0])
thisfit.addfun(constrained_gaussian, [1, 100])
trueparams = np.float64([1000/(np.sqrt(2*np.pi)*75), 75])
thisfit.fit()
sigma_off = (thisfit.bestfit - trueparams) / thisfit.bestfit_err
print('*** Constrained Gaussian ***')
print('Success: ' + str(thisfit.fit_out.success))
print('Fit values: ' + str(thisfit.bestfit))
print('Fit uncertainty: ' + str(thisfit.bestfit_err))
print('Sigma deviation: ' + str(sigma_off))
print('Reduced Chi^2: ' + str(thisfit.chisq_nd))
#%% Correlated uncertainties -- broad peak on questionable background
x = np.random.randn((1000))*5
y = 5*np.random.random((1000))-2.5
d = np.append(x,y)
hist_out = np.histogram(d, bins=1000, range=(-2,2))
thisfit = ghf.HistFit(*hist_out)
thisfit.addfun(ghf.GHF_Gaussian(), [80, 0, 5])
thisfit.addfun(ghf.GHF_flat(), 200)
trueparams = np.float64([200/np.sqrt(2*np.pi), 0, 5, 200])
thisfit.fit()
sigma_off = (thisfit.bestfit - trueparams) / thisfit.bestfit_err
print('*** Gaussian with Background, correlated uncertainties ***')
print('Success: ' + str(thisfit.fit_out.success))
print('Fit values: ' + str(thisfit.bestfit))
print('Fit uncertainty: ' + str(thisfit.bestfit_err))
print('Sigma deviation: ' + str(sigma_off))
print('Reduced Chi^2: ' + str(thisfit.chisq_nd))