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))
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?