Mathematical Modeling and Optimization for Curve Fitting with Gurobi in Python
Introduction:
Curve fitting plays a crucial role in understanding relationships between variables from observed data. In this blog, we’ll explore the process of curve fitting using the Gurobi optimization library in Python. Gurobi provides a robust platform for solving optimization problems, making it a valuable tool for finding optimal solutions to curve fitting models.
Model Setup:
We begin by creating a Gurobi model named ‘CurveFitting’ to represent our curve fitting problem. Our objective is to find the coefficients ‘a’ and ‘b’ for the linear curve \(f(x) = a \cdot x + b\) that best fits a given set of data points \((x[i], y[i])\).
# Import Gurobi library
from gurobipy import GRB, Model
# Sample data (replace with your actual data)
observations = range(5)
x = [1, 2, 3, 4, 5]
y = [2.5, 3.5, 4.0, 4.5, 5.0]
# Create a model
model = Model('CurveFitting')
# Create the variables
b = model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, name="b")
a = model.addVar(lb=-GRB.INFINITY, ub=GRB.INFINITY, vtype=GRB.CONTINUOUS, name="a")
u = model.addVars(observations, vtype=GRB.CONTINUOUS, name="u")
v = model.addVars(observations, vtype=GRB.CONTINUOUS, name="v")
# Deviation constraints
deviations = model.addConstrs((a * x[i] + b + u[i] - v[i] == y[i] for i in observations), name='deviations')
# Objective function to minimize total deviations
model.setObjective(u.sum('*') + v.sum('*'))
Optimization:
With the model set up and constraints defined, we proceed with the optimization process.
# Optimize the model
model.optimize()
Results:
Let’s print the optimal values for ‘a’ and ‘b’ obtained through the Gurobi optimization.
# Print results
print(f"Optimal 'a' value: {a.x}")
print(f"Optimal 'b' value: {b.x}")
Conclusion:
This blog has provided a practical demonstration of mathematical modeling for curve fitting using Gurobi in Python. By integrating optimization techniques, Gurobi efficiently determines the optimal coefficients for the linear curve, demonstrating the application of mathematical modeling to solve real-world curve fitting problems.
Appendix: Derivation of the Objective Function
The objective function in our curve fitting model aims to minimize the total positive and negative deviations between the observed data points and the predicted values from our linear curve. Let’s derive this objective function step by step.
The linear curve is represented by the function \(f(x) = a \cdot x + b\), where ‘a’ is the coefficient of the linear term and ‘b’ is the constant term.
For each observation \((x[i], y[i])\), we introduce two non-negative continuous variables, \(u[i]\) and \(v[i]\), to represent the positive and negative deviations, respectively, between the observed \(y[i]\) and the predicted \(f(x[i])\).
The deviation constraints are formulated as follows:
\[a \cdot x[i] + b + u[i] - v[i] = y[i]\]To derive the objective function, we aim to minimize the total positive deviations (\(u\)) and total negative deviations (\(v\)) across all observations:
\[\text{Minimize } \sum_{i} (u[i] + v[i])\]This objective function reflects our goal of finding the optimal values for ‘a’ and ‘b’ that result in the linear curve minimizing the overall deviations from the observed data points.
In the Gurobi Python code, this objective function is implemented using the model.setObjective(u.sum('*') + v.sum('*'))
line. The solver then seeks values for ‘a’ and ‘b’ that achieve the minimum total deviations, providing an optimal fit for the given dataset.