Optimize piecewise linear function
Non-linear least square
Ordinary least square is used to approximate parameters for a linear model. However, the assumption will not always be linear and more complex.



The goal of non-linear least square is the same as OLS, minimizing the sum of square error. By setting the partial derivative to the parameter beta equal to zero, we are finding the local minima for the S equation.
Advantages of non-linear least squares
- Broad range of functions that fits
- Good estimates with relatively small amounts of data
- Well-developed theory computing confidence, prediction, and intervals
Disadvantages
- Iterative optimization procedures for estimating parameters
- User provided starting values
- Bad starting values may converge to local minima instead of global minima
- String sensitivity to outliers
How would you optimize a piecewise linear function in python?

Difficult part is generating a function which will constraint each piece to be continuous. The y intercept value for the third piece is not a parameter. y intercept value for the second and third piece are shared which allows continuity. However, full continuity is not reached.

Ordinary Least Square Iteration
Another approach for full continuity is using OLS with constraints for the y-intercept. The method would start with one interval and find a linear model. Find linear models for adjacent intervals which are continuous. At the end we will have n different models for n intervals. Decide on the best model with the smallest sum of squared error.
(to be continued)