add: top cut model initial implementation

This commit is contained in:
2026-01-26 14:12:47 +01:00
parent 64844e860c
commit 2b2247a758

View File

@@ -58,6 +58,36 @@ def fit_piece_wise_linear(d, C, M=1000):
return m.X, c0.X, b.X, aic
def fit_cut(d, C):
"""
Fits a simple linear regression model to the given data.
However the top 10% of points are used to derive a constant which is added as a constraint to the optimization problem.
This results in a piece-wise linear function of two parts, where the first part is a linear function while the second function is a constant.
Parameters:
d (array-like): Distance values.
C (array-like): Corresponding centrality values.
Returns:
tuple: Optimize slope (m), intercept (t) and breaking point (b)
"""
n = len(d)
cut = math.floor(n * 0.1)
b = sum(C[n - cut..n]) / cut
model = gp.Model("Top Cut")
m = model.addVar(vtype=GRB.CONTINUOUS, name="m")
t = model.addVar(vtype=GRB.CONTINUOUS, name="t")
model.setObjective(gp.quicksum((C[i] - t - m * d[i])**2 for i in range(n - cut, n)), GRB.MINIMIZE)
for i in range(n - cut, n):
model.addConstr(b >= m * d[i] + t)
model.optimize()
return m.X, t.X, b
def fit_linear_regression(d, C):
"""
Fits a simple linear regression model to the given data using optimization.