Python Curve Fitting

ECON 3209 · Week 14, Lecture 3 · Kerala Agricultural University

Department of Development Economics, KAU

Autumn 2026

Learning Outcomes

By the end of this lecture, you will be able to:

  1. fit polynomial curves with numpy.polyfit
  2. fit nonlinear functions with scipy.optimize.curve_fit
  3. compare model fit using SSE, AIC, and BIC
  4. visualize competing fitted curves
  5. distinguish flexible fitting from economically meaningful modeling

Three Common Curve-Fitting Tools

statsmodels

  • regression-style estimation
  • inference and summaries
  • best for linear-in-parameters models

polyfit / curve_fit

  • direct numerical fitting
  • flexible curves
  • useful for prediction and visualization

Model Comparison Metrics

\[AIC = n \ln\left(\frac{SSE}{n}\right) + 2k\] \[BIC = n \ln\left(\frac{SSE}{n}\right) + k\ln(n)\]

where \(SSE\) is the sum of squared errors and \(k\) is the number of estimated parameters.

Example Curves

We compare: - a linear fit - a quadratic fit - a nonlinear saturation curve

This is useful when modelling diminishing returns to fertilizer or technology adoption.

Nonlinear Saturation Function

A common nonlinear form is

\[Y = \frac{aX}{b + X} + c\]

  • rises quickly at low \(X\)
  • flattens as \(X\) gets large
  • captures saturation or diminishing returns

Python Demo: Generate Nonlinear Data

Python Demo: Linear and Quadratic with polyfit

Python Demo: Nonlinear Fit with curve_fit

Python Demo: Compute SSE, AIC, and BIC

Python Demo: Plot Competing Fits

Overfitting Warning

  • A more flexible curve often fits the sample better.
  • But a better in-sample fit may not mean better economic interpretation or future prediction.
  • Use theory, simplicity, and diagnostics together.

When to Use Which Tool

  • Use statsmodels when you need regression tables and inference.
  • Use numpy.polyfit for quick polynomial approximation.
  • Use curve_fit for nonlinear functions that are not linear in parameters.

The best model is not the fanciest one; it is the one that balances fit, interpretation, and economic plausibility.

🏋️ Exercise

  1. Simulate a nonlinear fertilizer-yield relationship.
  2. Fit a linear, quadratic, and saturation model.
  3. Compute SSE, AIC, and BIC for all three fits.
  4. Plot the competing curves and choose the most convincing model.

Summary

✅ Python offers several complementary approaches to curve fitting.

numpy.polyfit is convenient for linear and polynomial approximations.

curve_fit handles genuinely nonlinear parameterizations.

✅ AIC and BIC help compare fit while penalizing extra complexity.

✅ Model choice should reflect both the data and the economics of the problem.

Next Lecture

Course Next Step

We will cover: - review Weeks 10–14 as a connected block - see how these tools modify the basic OLS framework - prepare for later topics such as qualitative response and specification tests - practice interpreting models, not just running code