Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Module or Algorithm to produce same results as Mathematica LinearModelFit

First I don't really know Mathematica and I haven't done stats in a very long time.

I have been trying to find (Google and RTFM) a way to reproduce the results produced by the Mathematica LinearModelFit function using scipy.stats.linregress. It is now obvious that this is not the way to go except for the most simple cases.

LinearModelFit[ydata, 1/(2 n - x)^100, x]

produces 16.3766 + <<70>>/(2580 - x)^100

If someone could point me in the right direction I would appreciate it.

Thanks in advance.

data: http://pastebin.com/RTp5em0W

Screen shot of Mathematica Notebook: https://i.sstatic.net/lT43O.jpg

Note: I did not do the Mathematica work. ddd is the data that can be found at the pastebin link. The y in the denominator should be x.

like image 560
PlacidLush Avatar asked Oct 16 '25 02:10

PlacidLush


1 Answers

I don't know the python solution, but one way to handle this problem is to transform your x data according to the functional form you are supplying as the argument to LinearModelFit :

 n=1290
 LinearModelFit[ydata, 1/(2 n - x)^100, x]["BestFit"]

16.1504 + 1.471945513739138*10^315/(2580 - x)^100

is equivalent to:

 xtransform = 1/(2 n - #)^100  & /@ Range[Length[ydata]];
 LinearModelFit[Transpose[{xtransform, ydata}], x, x]["BestFit"]

16.1504 + 1.471945513739138*10^315 x

You should readily be able to do that transform and use standard linear regression in python. You might have precision issues due to the large exponent however.

like image 136
agentp Avatar answered Oct 18 '25 16:10

agentp