Exercise

Optimization with Scipy

It is possible to write a numpy implementation of the analytic solution to find the minimal RSS value. But for more complex models, finding analytic formulae is not possible, and so we turn to other methods.

In this exercise you will use scipy.optimize to employ a more general approach to solve the same optimization problem.

In so doing, you will see additional return values from the method that tell answer us "how good is best". Here we will use the same measured data and parameters as seen in the last exercise for ease of comparison of the new scipy approach.

Instructions

100 XP
  • Define a function model_func(x, a0, a1) that, for a given array x returns a0 + a1*x.
  • Use the scipy function optimize.curve_fit() to compute optimal values for a0 and a1.
  • Unpack the param_opt so as to store the model parameters as a0 = param_opt[0] and a1 = param_opt[1].
  • Use the predefined function compute_rss_and_plot_fit to test and verify your answer.