2

I am struggling with a problem that involves calculating the equation of a curve fit for the edge of a scatterplot, shown below. It is a clearly defined edge, but I need a precise fit.

I based my initial attempts on this script...

Fit a curve to the boundary of a scatterplot

...but I'm not familiar enough with Python to know how this code is composed by reading it, and can't fully adapt it to my situation.

Here's what I have tried from that example. The goal here is to model the curve as an exponential growth with a limit, and then shift the curve downward towards the points iteratively. However, neither this nor the scipy.optimize curve_fit seems to give me a precise fit.

from math import e
model = lambda x, a, b: (a * (1 - (e ** (-b * x)))

def get_flipped(y_data, y_model):
    flipped = y_model - y_data
    flipped[flipped > 0] = 0
    return flipped

def flipped_resid(pars, x, y):
    y_model = model(x, *pars)
    flipped = get_flipped(y, y_model)
    resid = np.square(y + flipped - y_model)
    return np.nan_to_num(resid)

from scipy.optimize import leastsq
guesses =[100, 5]
fit_pars, flag = leastsq(func = flipped_resid, x0 = guesses,
                     args = (x_data, y_data))

Curve to be fit

I am relatively new to Python (R native), so there may be some obvious mistakes here, but how can I improve the fit so that the line sits right at the edge?

Dave2e
  • 22,192
  • 18
  • 42
  • 50

1 Answers1

0

I extracted data points along the edge, and in order to fit them I had to add an offset to your equation - making it "a * (1 - (exp(-b * X))) + offset", which gave a good fit to the edge with parameters a = 9.9214258118219334E+01, b = 4.2835994748526849E+00, and offset = -1.4428475065933015E+00. It is possible that this lack of an offset is causing your difficulty.

plot

James Phillips
  • 4,526
  • 3
  • 13
  • 11
  • 1
    Thanks very much! This definitely helped solve my issue getting the right fit. –  Jul 03 '19 at 16:50