[Numpy-discussion] hold parameters

Bruce Southey bsouthey@gmail....
Tue Aug 2 08:23:43 CDT 2011

On 08/01/2011 07:43 PM, Bevan Jenkins wrote:
> Hello,
> I have a function that I fitting to a curve via scipy.optimize.leastsq.  The
> function has 4 parameters and this is all working fine.
> For a site, I have a number of curves (n=10 in the example below).  I would
> like to some of the parameters to be the best fit across all curves (best fit
> for a site) while letting the other parameters vary for each curve.  I have
> this working as well.
> The issue I have is like to be able to vary this for a run.  That is do a run
> where parameter1 is best fit for entire site, whith the remaining three
> varying per curve. Then on the next run, have two parameters being held or
> fitted for all curves at one.  Or be able to do a run where all 4 parameters
> are fit for each individual curve.
It would really help to know what you mean by 'entire site' and 'run'.
If the runs are not independent then what you are doing is incorrect.

> Using my e.g. below, if I change the 'fix' dict, so that 'a','b', and 'c' are
> True, with 'd' False, then I will have to change the zip to
> for a,b,c in zip(a,b,c):
>      solve(a,b,c,d)
> I would prefer to find a way to do this via code.  I hope this example makes
> sense.  The code below is all within my objective function that is being
> called by scipy.optimize.leastsq.
> import numpy as np
> def solve(a,b,c,d):
>      print a,b,c,d
>      #return x*a*b*c*d
> fix = {"a":True,"b":True,"c":False,"d":False}
> n=10
> params = np.array([0,1,2,3]*n)
> params = params.reshape(-1,4)
> if fix["a"] is True:
>      a = params[0,0]
> else:
>      a = params[:,0]
> if fix["b"] is True:
>      b = params[0,1]
> else:
>      b = params[:,1]
> if fix["c"] is True:
>      c = params[0,2]
> else:
>      c = params[:,2]
> if fix["d"] is True:
>      d = params[0,3]
> else:
>      d = params[:,3]
> res=[]
> for c,d in zip(c,d):
>      res = solve(a,b,c,d)
>      #res = solve(a,b,c,d)-self.orig
> #return np.hstack(res)**2
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
Basically this code seems to be trying to do what an analysis of 
covariance would do.  Analysis of covariance type of approach provides a 
statistical framework where you have a 'global' parameter and condition 
specific parameters that modify  that parameter. Here is one example 
under SAS that fits a common slope but different intercepts due to drug 
That model can be extended allow for different slopes due different drug 
levels by fitting the interaction between both variables. You can do 
that easily in numpy/scipy by creating the correct 'design matrix'.

The real issue is that you need a statistical measure of the model fit 
as well as comparison between models (or restrictions). For linear 
models usually likelihood (or similar measure like Bayesian information 
criterion) and extra-sums of squares tests are used. But these measures 
get more interesting in nonlinear cases.


More information about the NumPy-Discussion mailing list