Search code examples
pythonnumpyscipyleast-squaresarray-broadcasting

Multiple Parameter Estimation. Problems with broadcasting


I need to get the parameters(kf, beta1, beta2, gamma) with a nonlinear least squares regression. The error message is: "ValueError: operands could not be broadcast together with shapes (4,7) (0,)"

I did 4 experiments with the next data collected:

  • flujo_ms(x-axis Data): an array with 7 positions
  • "fri":each "fri" is an array of 7 positions.
  • brfv: takes four constant values, one constant for each experiment.

I would like to minimize the error for the parameter estimation using the four experiments together.

import numpy as np
from scipy.optimize import leastsq

flux = [flujo_ms, flujo_ms, flujo_ms, flujo_ms]
brfv = [[0.00694] * 7, [0.00972] * 7, [0.0139] * 7, [0.0208]*7]
fr = [fr1, fr2, fr3, fr4]


def foulingRate(parameters, flux, brfv, mlts=8.22):
    kf, beta1, beta2, gamma = parameters
    FR = kf * np.exp(flux * (beta1 * brfv + beta2 * mlts + gamma))
    return FR


def objective(pars, yData, xData, brfv):
    # it will minimize this function
    err = yData - foulingRate(pars, xData, brfv)
    return err

x0 = [5.6 * 10 ** -4, -2.48 * 10 ** 8, 5.1 * 10 ** 4,
      2.81 * 10 ** 6]  # initial values for the parameters

plsq = leastsq(objective, x0, args=(fr, flux, brfv))
print("Fitted parameters = {0}".format(plsq[0]))

Solution

  • I've found a solution. I used the module lmfit: cars9.uchicago.edu/software/python/lmfit