[SciPy-User] scipy.optimize.leastsq not varying fit parameters

208 views
Skip to first unread message

Kevin Gullikson

unread,
Dec 3, 2011, 9:53:59 PM12/3/11
to scipy...@scipy.org
I am trying to do a nonlinear least squares fit using scipy.optimize.leastsq. I have done this several times pretty easily for fitting some analytical function. However, now I am fitting the parameters of a numerical model to my data. As far as I can tell, the only thing that should change is that the fitting function gets much more complicated (since I have to generate one of these models at every iteration). 

My problem is this: leastsq is not varying the parameters, so I keep getting the exact same model and eventually it tells me that 'The cosine of the angle between func(x) and any column of the\n  Jacobian is at most 0.000000 in absolute value'

I have played with the epsfcn keyword, increasing it to 0.1 and it still never changes the parameters. What am I doing wrong? I will include some code, but not all since it is quite long:

The function call:
    fitout = leastsq(ErrorFunction, pars, args=(chips[i], const_pars), full_output=True, epsfcn = 0.1)


The Error Function:
def ErrorFunction(pars, chip, const_pars):
  all_pars = const_pars
  all_pars.extend(pars)
  print "Pars: ", pars
  model = FitFunction(chip, all_pars)
  return chip.y - model.y



The Fit Function:
def FitFunction(chip, pars):
  print pars
  temperature = pars[0]
  pressure = pars[1]
  co2 = pars[2]
  co = pars[3]
  o3 = pars[4]
  wavenum_start = pars[5]
  wavenum_end = pars[6]
  ch4 = pars[7]
  humidity = pars[8]
  resolution = pars[9]
  angle = pars[10]
  
  #Generate the model:
  model = MakeModel.Main(pressure, temperature, humidity, wavenum_start, wavenum_end, angle, co2, o3, ch4, co, chip.x, resolution)
  if "FullSpectrum.freq" in os.listdir(TelluricModelDir):
    cmd = "rm "+ TelluricModelDir + "FullSpectrum.freq"
    command = subprocess.check_call(cmd, shell=True)
    
  return model

Kevin Gullikson

josef...@gmail.com

unread,
Dec 3, 2011, 10:46:57 PM12/3/11
to SciPy Users List
On Sat, Dec 3, 2011 at 9:53 PM, Kevin Gullikson
<kevin.g...@gmail.com> wrote:
> I am trying to do a nonlinear least squares fit using
> scipy.optimize.leastsq. I have done this several times pretty easily for
> fitting some analytical function. However, now I am fitting the parameters
> of a numerical model to my data. As far as I can tell, the only thing that
> should change is that the fitting function gets much more complicated (since
> I have to generate one of these models at every iteration).
>
> My problem is this: leastsq is not varying the parameters, so I keep getting
> the exact same model and eventually it tells me that 'The cosine of the
> angle between func(x) and any column of the\n  Jacobian is at most 0.000000
> in absolute value'
>
> I have played with the epsfcn keyword, increasing it to 0.1 and it still
> never changes the parameters. What am I doing wrong? I will include some
> code, but not all since it is quite long:
>
> The function call:
>     fitout = leastsq(ErrorFunction, pars, args=(chips[i], const_pars),
> full_output=True, epsfcn = 0.1)
>
>
> The Error Function:
> def ErrorFunction(pars, chip, const_pars):
>   all_pars = const_pars
>   all_pars.extend(pars)

can you check all_pars here? To me this looks like const_par is
mutable and constantly extended

Josef

> _______________________________________________
> SciPy-User mailing list
> SciPy...@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

Kevin Gullikson

unread,
Dec 3, 2011, 11:05:19 PM12/3/11
to SciPy Users List
can you check all_pars here? To me this looks like const_par is
mutable and constantly extended

Ah, yes that was the problem. I just made my Fit Function take both lists separately and it seems to work now. Thanks for the speedy reply!

Kevin
Reply all
Reply to author
Forward
0 new messages