I've been trying to write code to compute the Chi-squared distribution for a Gaussian function and I've run into a pesky type error that I don't know how to get past. May I have a solution for addressing the type error ? I've tried to avoid by accessing each element of the array and converting those elements to a different data type in question but that doesn't seem to help, also I've tried using non-numpy array's to do the job.
def make_gauss(N = random.random(), sigma = random.random(), mu = random.random()):
return (lambda x: N/(sigma * (2*numpy.pi)**.5) *
numpy.e ** (-(x-mu)**2/(2 * sigma**2)))
def gauss_chi2_generate(gauss_observed_values = [], gauss_expected_values = []):
""" Computes the Gaussian for Chi2"""
############################################################################
t_s = 0
t_g_s = 0
gauss_observed_values.append(make_gauss())
gauss_expected_values.append(make_gauss())
g_o_v = np.array(gauss_observed_values)
g_e_v = np.array(gauss_expected_values)
z_e_o_v = zip(g_o_v, g_e_v)
#############################################################################
for i in range(0,12):
""" Equation for Chi-Squared calculation"""
t_g_s+=((g_o_v[i])-(g_e_v[i]))**2/ g_e_v[i]
print("Observed Values ", g_o_v)
print("Expected Values" , g_e_v)
df=len(g_o_v)-1
print("Our goodness of fit for our linear function", stats.chi2.cdf(t_g_s,df))
return t_g_s
Output:
Traceback (most recent call last):
File "ChiSquaredFixed.py", line 82, in <module>
gauss_chi2_generate()
File "ChiSquaredFixed.py", line 67, in gauss_chi2_generate
t_g_s+=((g_o_v[i])-(g_e_v[i]))**2/ g_e_v[i]
TypeError: unsupported operand type(s) for -: 'function' and 'function'