Skip to content

Instantly share code, notes, and snippets.

@geriskenderi
Created February 3, 2025 23:05
Show Gist options
  • Save geriskenderi/e0d654d120972be52c002934e2093e20 to your computer and use it in GitHub Desktop.
Save geriskenderi/e0d654d120972be52c002934e2093e20 to your computer and use it in GitHub Desktop.
python code to describe a generalized logistic function, useful for curve fitting and understanding when a transition happens
import numpy as np
from scipy.optimize import curve_fit
def logistic_fit(alpha, Q, b, v, alpha_N):
# Implements a more general logistic function (solution of Richards's DE with K=1)
return 1 / ((1 + Q * np.exp(-b*v*(alpha - alpha_N))) ** (1/v))
def generalized_logistic_threshold(alpha_N, b, Q, v):
# Calculate the threshold value of alpha_N given as the argument where the above generalized logistic function equals 0.5
correction_term = (np.log(2**v - 1) - np.log(Q)) / (-b*v)
return correction_term + alpha_N
# Example fit with scipy
popt, _ = curve_fit(logistic_fit, x, p_curve, p0=[1., 1., 1., 3.5], bounds=([0., -np.inf, 0., 3.], [np.inf, np.inf, np.inf, 6.]), maxfev=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment