Search code examples
pythonmathnonlinear-optimizationnonlinear-functions

How to fit a custom non-linear model in python


Suppose, i want to find those parameters w, that minimize MAE or MSE of model y(x,z):

y = (w_1 * x_1 + ... + w_n * x_n) / (w_1 * z_1 + ... + w_n * z_n)

How can i do it in python?


Solution

  • You can use scipy.optimize.curve_fit. If you have n variables x and n variables z, you can let the independent variable X have 2n rows and the columns should be the observations for each variable.

    In this way, X is a 2-D array of rows 2n and columns M. Also the parameters we want to predict are in an array w of length n. Thus way, the function by OP is expressed as:

    y = w@x/w@z = w@X[:n, :]/w@X[n:, :]
    

    where @ is matrix multiplication in numpy. Here is the full code:

    def my_model(x, *args):
      N, _ = x.shape
      w = np.array(args)
      # assuming x and z have same number of variables = N/2
      y = w@x[:N//2, :] / (w@x[N//2:, :])
      return y
    
    popts, pcov = curve_fit(my_model, x, y)
    

    where popts contains all the optimized w variables.