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