Stack Exchange
Compiling function with Numba is not working
I'm a beginner with Numba, and I want to optimize this code returning two numpy arrays :

@jit(nopython=True)
def payoff_derivative(N_data, control=(False, 0)):
  if control == (False, 0):
    Z = np.random.randn(N_data, d)
  else:
    Z = control[1]

  W = np.dot(corr_Cholesky, Z.T).T
  S = S0*np.exp((r-0.5*sigma**2)*T + sigma*np.sqrt(T)*W)

  if type_product == "basket call":
    payoff = np.maximum((1/d)*np.sum(S, axis=1) - K, 0)
  elif type_product == "worst of put":
    payoff = np.maximum(K - np.min(S, axis=1), 0)
  elif type_product == "binary":
    payoff = G*((1/d)*np.sum(S, axis=1) >= K)

  if control == (False, 0):
    return Z, (payoff - np.mean(payoff))/np.std(payoff)
  else:
    return Z, payoff


And here is my error message when I give it a try :
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Cannot unify array(float64, 2d, C) and int64 for 'Z.2', defined at <ipython-input-15-15c401d7c3d7> (9)

File "<ipython-input-15-15c401d7c3d7>", line 9:
def payoff_derivative(N_data, control=(False, 0)):
    <source elided>

  W = (np.dot(corr_Cholesky, Z.T).T).astype('float64')
  ^

During: typing of assignment at <ipython-input-15-15c401d7c3d7> (9)

File "<ipython-input-15-15c401d7c3d7>", line 9:
def payoff_derivative(N_data, control=(False, 0)):
    <source elided>

  W = (np.dot(corr_Cholesky, Z.T).T)
  ^

It seems to be an error of types but I do not understand exactly where it comes from and why, as Numpy arrays are supported by Numba.
Can anyone explain me this error ? Thanks a lot for your help !

What is supposed to be corr_Cholesky and S0 as well as d? Please pose a minimal complete reproducible code.

Checking the docs for "Cannot unify" problems, you can translate
Cannot unify array(float64, 2d, C) and int64 for 'Z.2'...

As “Numba cannot find a type that can safely represent an array(float64, 2d, C) and an integer”.
That happens here:
if control == (False, 0):
    Z = np.random.randn(N_data, d)
else:
    Z = control[1]

In the first case you assign an array of floats and in the second one, you assign an integer.
Later on, when attempting to use Z, the JIT compiler doesn't know how to compile np.dot(corr_Cholesky, Z.T).T.
