add: print all model values after solving simple linear regression
This commit is contained in:
@@ -9,6 +9,9 @@ from src import centrality
|
||||
from src import plot
|
||||
from src import fitting
|
||||
|
||||
# TODO: implement this AIC:
|
||||
# https://www.statlect.com/fundamentals-of-statistics/linear-regression-model-selection-criteria
|
||||
|
||||
|
||||
def merfish():
|
||||
"""
|
||||
|
||||
@@ -21,7 +21,7 @@ def fit_piece_wise_linear(d, C, M=1000):
|
||||
"""
|
||||
n = len(d)
|
||||
|
||||
model = gp.Model()
|
||||
model = gp.Model("Piece-wise Linear Model")
|
||||
m = model.addVar(vtype=GRB.CONTINUOUS, name="m") # Slope before breakpoint
|
||||
|
||||
c0 = model.addVar(vtype=GRB.CONTINUOUS, name="c0") # y-intercept
|
||||
@@ -64,13 +64,17 @@ def fit_linear_regression(d, C):
|
||||
tuple: Optimized slope (beta) and intercept (alpha).
|
||||
"""
|
||||
n = len(d)
|
||||
model = gp.Model()
|
||||
model = gp.Model("Simple Linear Regression")
|
||||
|
||||
alpha = model.addVar(vtype=GRB.CONTINUOUS, name="alpha")
|
||||
beta = model.addVar(vtype=GRB.CONTINUOUS, name="beta")
|
||||
|
||||
model.setObjective(gp.quicksum((C[i] - alpha - beta * d[i])**2 for i in range(n)), GRB.MINIMIZE)
|
||||
model.optimize()
|
||||
|
||||
for v in model.getVars():
|
||||
print(f"{v.VarName} {v.X:g}")
|
||||
|
||||
return beta.X, alpha.X
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user