1

I would like to define a python function that has three parameters: C, psi, and m but it actually internally has an integral that is running based on another variable called eta.

def func(x: np.float64, C: np.float64, psi: np.float64, m: np.float64) -> np.float64:
    return C * (1-psi) * quad(np.power(1-eta, psi) * np.power(m/x, C/eta+1)/eta, 0, np.inf, args=(C, psi, m, x))[0]

Since I would like to fit this function to a dataset, I cannot choose eta to be a free parameter for fitting and hence it should not appear as a parameter in the function signature. But, python doesn't know the internal eta as a defined variable.

File "c:\Users\username\Desktop\Project\my_script.py", line 277, in func
    np.power(1 - eta, psi) * np.power(m / x, C/eta + 1) / eta, 0, np.inf,
NameError: name 'eta' is not defined

How can I let the function know that it is not expecting to be fed with a parameter but the inside integral takes care of that through integration.

---Thanks,

Roosha
  • 89
  • 3
  • 10
  • 1
    You could try a framework that allows variables to be fixed. The `lmfit` module (while a bit more complicated than some of the standard scipy functions) does a nice job of this. – alexpiers Apr 09 '22 at 23:42
  • 1
    Just change the signature to `def func(x, C, psi, m, eta)` and wrap the function by `func2 = lambda x, *args: func(x, *args, your_eta_value)`. Then you can fit `func2`. – joni Apr 10 '22 at 07:19

0 Answers0