0

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'

Zophikel
  • 123
  • 6
  • 1
    You shouldn't do `gauss_observed_values = [], gauss_expected_values = []` in a function declaration. It does not do what you think it does. Default them to `None` than assign them to empty lists within the function. (Though this probably isn't the current error it will definitely cause an error in the future). – Error - Syntactical Remorse Jun 19 '19 at 19:09
  • what does `make_gauss()` return? – Reblochon Masque Jun 19 '19 at 19:19
  • @ReblochonMasque I added the code for make_gauss() to make things a bit more clearer – Zophikel Jun 19 '19 at 19:54
  • @Error-SyntacticalRemorse do you know of a good source that explains why we shouldn't default function inputs to empty lists? I am 99% certain I have made this mistake in the past.. – Hoog Jun 19 '19 at 20:01
  • 1
    ok, that's what your problem is: `make_gauss()` returns a function, not a list of values - this is what causes this error: `TypeError: unsupported operand type(s) for -: 'function' and 'function'` - make sure that `make_gauss()` returns values in a list ot a numpy array. – Reblochon Masque Jun 19 '19 at 20:25
  • 1
    @Hoog See [“Least Astonishment” and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – Error - Syntactical Remorse Jun 19 '19 at 22:17

0 Answers0