[SciPy-User] scipy.optimize leastsq with numpy longdouble?

161 views
Skip to first unread message

Joan Smith

unread,
Nov 17, 2010, 12:13:56 PM11/17/10
to scipy...@scipy.org
Hi,

I'm fitting a maxwell-botlzmann distribution, using SciPy leastsq. Because of the data I'm fitting, I need high precision (on both the high and low ends), so I'm using numpy.longdouble for my types. Is there a way to use this type with leastsq? As it stands, I'm getting this error:

v = leastsq(self.chi_squared, self.v_0, args=self.args, full_output=1)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.py", line 281, in leastsq
maxfev, epsfcn, factor, diag)
TypeError: array cannot be safely cast to required type

I've been using leastsq for a while, and haven't seen this error before, so I suspect it has to do with using an unusual type.

Thanks in advance,
Joan
_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

Charles R Harris

unread,
Nov 17, 2010, 12:20:59 PM11/17/10
to SciPy Users List
On Wed, Nov 17, 2010 at 10:13 AM, Joan Smith <jo...@mit.edu> wrote:
Hi,

I'm fitting a maxwell-botlzmann distribution, using SciPy leastsq. Because of the data I'm fitting, I need high precision (on both the high and low ends), so I'm using numpy.longdouble for my types. Is there a way to use this type with leastsq? As it stands, I'm getting this error:

   v = leastsq(self.chi_squared, self.v_0,  args=self.args, full_output=1)
 File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy/optimize/minpack.py", line 281, in leastsq
   maxfev, epsfcn, factor, diag)
TypeError: array cannot be safely cast to required type

I've been using leastsq for a while, and haven't seen this error before, so I suspect it has to do with using an unusual type.


Yep. But where is your data coming from? And how are you trying to do the fit? I suspect that a change of method it the proper way to go here. Josef may have something in the stats model package.

Chuck

Joan Smith

unread,
Nov 17, 2010, 12:25:32 PM11/17/10
to SciPy Users List
Hi,

What do you mean where is the data coming from? It's from an experiment..
function I'm fitting is:
maxwell_boltzmann = lambda v,x: v[0]*(x-C0)**(-4)*np.exp(-(L/v[1])**2*(x-C0)**(-2)) + v[2]

and the data is in an array of longdoubles.
Does that answer your question?
Joan

josef...@gmail.com

unread,
Nov 17, 2010, 12:49:41 PM11/17/10
to SciPy Users List
On Wed, Nov 17, 2010 at 12:25 PM, Joan Smith <jo...@mit.edu> wrote:
> Hi,
> What do you mean where is the data coming from? It's from an experiment..
> function I'm fitting is:
> maxwell_boltzmann = lambda v,x:
> v[0]*(x-C0)**(-4)*np.exp(-(L/v[1])**2*(x-C0)**(-2)) + v[2]
> and the data is in an array of longdoubles.

I don't know if any of the optimization routines can handle
long-double, maybe one of the pure python optimizers is less picky
about the type.

some guesses:
Maybe taking exp(log(...)) of the first part increases the numerical
precision of the calculation enough to get results, or maybe a two
step method with v[2] estimated in an outer optimization and log in
the inner loop.

(statsmodels doesn't have anything that would help, as far as I can see)

Josef

Charles R Harris

unread,
Nov 17, 2010, 12:50:24 PM11/17/10
to SciPy Users List
On Wed, Nov 17, 2010 at 10:25 AM, Joan Smith <jo...@mit.edu> wrote:
Hi,

What do you mean where is the data coming from? It's from an experiment..
function I'm fitting is:
maxwell_boltzmann = lambda v,x: v[0]*(x-C0)**(-4)*np.exp(-(L/v[1])**2*(x-C0)**(-2)) + v[2]

and the data is in an array of longdoubles.
Does that answer your question?
Joan


That's a bit vague. Is the data of actual longdouble precision? 19 digits would be unusual accuracy for experimental data. Does the data have outliers? How much data do you have? The pdf doesn't look like the usual Maxwell-Boltzmann, where does it come from? What are L, C0, v, and x in the function?

Is there any reason you can't convert the experimental data to ordinary doubles?

Joan Smith

unread,
Nov 17, 2010, 2:10:06 PM11/17/10
to SciPy Users List
Thank you, with those suggestions it worked!

Joan

Charles R Harris

unread,
Nov 17, 2010, 2:21:57 PM11/17/10
to SciPy Users List
On Wed, Nov 17, 2010 at 10:49 AM, <josef...@gmail.com> wrote:
On Wed, Nov 17, 2010 at 12:25 PM, Joan Smith <jo...@mit.edu> wrote:
> Hi,
> What do you mean where is the data coming from? It's from an experiment..
> function I'm fitting is:
> maxwell_boltzmann = lambda v,x:
> v[0]*(x-C0)**(-4)*np.exp(-(L/v[1])**2*(x-C0)**(-2)) + v[2]
> and the data is in an array of longdoubles.

I don't know if any of the optimization routines can handle
long-double, maybe one of the pure python optimizers is less picky
about the type.

some guesses:
Maybe taking exp(log(...)) of the first part increases the numerical
precision of the calculation enough to get results, or maybe a two
step method with v[2] estimated in an outer optimization and log in
the inner loop.

(statsmodels doesn't have anything that would help, as far as I can see)


It can also be reduced to a one variable problem in v[1] for leastsq since given that variable the problem reduces to linear least squares which can be done inside the called function.

Chuck

Reply all
Reply to author
Forward
0 new messages