1
$\begingroup$

I've fitted a data with following sigmoidal function with MATLAB using Curve Fitting Tool and with Python with scipy.curve_fit() for fitting and sklearn.metrics.r2_score for evaluation, and obtained strange results: $R^2$ = 0.582 for Python and 0.9609 for MATLAB. Data is identical for both Python and MATLAB as well as boundaries. Trust-Region was chosen as an algorithm.

Sigmoid: $\sigma(x) = \frac{scale}{1+exp^{-slope*(x+xshift)}} + yshift$.

Here is code for Python:

params,pconv = curve_fit(sigmoid, cD,cT, bounds=([-10,0,-500,5], [10,500,500,50]), maxfev=50000, loss = 'huber', f_scale = 0.5)

And here are MATLAB parameters: enter image description here

However, fitted functions look similar:

Python:enter image description here

MATLAB:enter image description here

I think there should be a difference, but not so significant. It can be worse on the similar data(there are few datasets). Why does it happen?

UPD: After constraining data with new boundaries for cD $R^2$ increased (and that's what should be expected), but it is still lower than one, computed in MATLAB.

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ In the Python plot there is no cD (X) data with a value greater than 45 or less than 5, but this is not true for the MATLAB plot. This is similarly true for plotted maximum values of the cT (Y) data. The plots indicate that these are two different data sets, is this correct? $\endgroup$ Sep 23, 2019 at 16:12
  • $\begingroup$ Agree with James Phillips. The data is not the same, so why should the model metrics be? $\endgroup$
    – mkt
    Sep 24, 2019 at 5:17
  • $\begingroup$ My bad, edited the post, but issue remains the same. According to metrics on the last picture, R2 value seems to be computed correclty, at least in Python. $\endgroup$ Sep 24, 2019 at 7:06
  • $\begingroup$ In Python you use a "Huber" loss. Why, then, is R-squared (which is appropriate for quadratic loss) even relevant? Indeed, no conceivable loss would be appropriate for these data given their extreme heteroscedasticity: both procedures performed admirably by finding good fits, but summarizing them by giving an overall measure of the residual sizes would be meaningless. $\endgroup$
    – whuber
    Sep 24, 2019 at 11:41

1 Answer 1

1
$\begingroup$

As whuber pointed out, I've used a wrong metric for given nonlinear regression and selected loss function. However, I think there was another reason for such dissimilarity - MATLAB applies weights to the points while computing metrics when "Robust" option is selected. In Python, I haven't applied any weights, so this gave me different result.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.