I am trying to generate 1000 sets of data points using fake_exp_y_values
generator. All I am trying to do is to calculate the chi2
values for all 1000 iterations with different fitting models. But the way I code it right now only gives me back one chi2
value. I don’t understand what is wrong.
Here is my code:
JavaScript
x
27
27
1
true_mean = 5 #Assume that we know the perfect fitting model is gaussian
2
#with mean value 5. And use try_mean with different mean values to see how
3
#does the chi2 behave
4
true_amp = 100
5
true_wid = 2
6
true_offset =10
7
x_values = np.array([i for i in np.arange(0,10,0.4)])
8
exact_y_values = np.array([true_offset + true_amp*
9
np.exp(-((i-true_mean)/true_wid)**2)
10
for i in x_values])
11
12
13
def func (x_values,offset,amp,mean,wid):
14
return (offset + amp*np.exp(-((i-mean)/wid)**2))
15
16
try_mean=np.linspace(2,8,25)
17
y_values=func(x_values,true_offset,true_amp,try_mean,true_wid)
18
19
for i in range (1000):
20
chi2=np.zeros(1000)
21
fake_exp_y_values = np.array([np.random.poisson(y)
22
for y in exact_y_values])
23
residuals=fake_exp_y_values-y_values
24
y_err=np.clip(np.sqrt(fake_exp_y_values),1,9999)
25
pulls=residuals/y_err
26
chi2[i]=np.sum(pulls**2)
27
Yet the returned chi2
list is:
Advertisement
Answer
This code 1000 times creates an array with 1000 zeros and inserts one value in each of them, but only keeps the last one:
JavaScript151for i in range (1000):
2chi2=np.zeros(1000)
3# [...]
4chi2[i]=np.sum(pulls**2)
5
You only want to create one array and use it in every one of the 1000 iterations:
JavaScript
1
5
1
chi2 = np.zeros(1000)
2
for i in range(1000):
3
# [...]
4
chi2[i] = np.sum(pulls**2)
5