[SciPy-User] ODR fitting several equations to the same parameters

josef.pktd@gmai... josef.pktd@gmai...
Fri Nov 13 10:00:49 CST 2009


On Fri, Nov 13, 2009 at 10:28 AM, ms <devicerandom@gmail.com> wrote:
> josef.pktd@gmail.com ha scritto:
>> On Thu, Nov 12, 2009 at 10:04 AM, ms <devicerandom@gmail.com> wrote:
>>> josef.pktd@gmail.com ha scritto:
>> an example
>> (quickly written and not optimized, there are parts I don't remember
>> about curve_fit, fixed parameters could be better handled by a class)
>
> Hmm, it seems I don't have curve_fit -I am constrained to use
> scipy-0.6.0 and there's no chance to change that (it's an external server).

You can just copy the function (plus 2 helper functions) from the
current trunk. You would need to add the imports. Alternatively you
can just use optimize.leastsq directly, using curve_fit as a recipe.

Josef

http://projects.scipy.org/scipy/browser/trunk/scipy/optimize/minpack.py#L338

338	def _general_function(params, xdata, ydata, function):
339	    return function(xdata, *params) - ydata
340	
341	def _weighted_general_function(params, xdata, ydata, function, weights):
342	    return weights * (function(xdata, *params) - ydata)
343	
344	def curve_fit(f, xdata, ydata, p0=None, sigma=None, **kw):



>
> I am going to have a good look at what's doable with your approach
> anyway, but I am happy if someone gives me old-school alternatives :)
>
> cheers,
> m.
>
>> ####################
>> """stack equations with different known parameters
>>
>> I didn't get curve_fit to work with only 1 parameter to estimate
>>
>> Created on Thu Nov 12 11:17:21 2009
>> Author: josef-pktd
>> """
>> import numpy as np
>> from scipy import optimize
>>
>>
>> def fsingle(a,c,b,x):
>>     return b*x**a + c
>>
>> atrue = 1.
>> ctrue = 10.
>> b = np.array([[1.]*10, [2.]*10, [3.]*10])
>> b = np.array([1.,2.,3.])
>> x = np.random.uniform(size=(3,10))
>> y = np.hstack([fsingle(atrue, ctrue, b[i], x[i]) for i in range(x.shape[0])])
>> y += 0.1*np.random.normal(size=y.shape)
>>
>> def fun(x,a,c):
>>     #b is taken from enclosing scope
>>     #print x.shape
>>     xx=x.reshape((3,10))
>>     return np.hstack([fsingle(a, c, b[i], xx[i]) for i in range(xx.shape[0])])
>>
>> res = optimize.curve_fit(fun,x.ravel(),y, p0=np.array([2.,1.]))
>>
>> print 'true parameters   ', atrue, ctrue
>> print 'parameter estimate', res[0]
>> print 'standard deviation', np.sqrt(np.diag(res[1]))
>> ####################
>>
>>
>>
>>
>>> sorry,
>>> m.
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>


More information about the SciPy-User mailing list