Course 1.3Lesson 3 of 3
A function can describe real data. We plot the data and choose a family: a line, a parabola or a reciprocal. Then the computer finds the best numbers with numpy's polyfit, which minimises the sum of the squares of the errors. We judge the model by its typical error, and we trust it only inside the range of the data.
Try the code and the simulations beside the lesson.
A scientist measures and collects numbers. A function turns the numbers into a rule, and the rule can then answer questions that nobody measured: what happens at , when we measured only and ?
A function that describes a real situation is called a model. Three families are enough for many problems.
| Family | Rule | Example |
|---|---|---|
| Line | A taxi charges and for each kilometre: fare | |
| Parabola | The area of a circle, , and the stopping distance of a car | |
| Reciprocal | The time for a journey of at speed : |
Suppose we choose a parabola . Which , and are best? For each measurement, the model makes a small error, the difference between the model and the measurement. The best fit makes the sum of the squares of the errors as small as possible. This is called the method of least squares. We square the errors so that a positive error and a negative error cannot cancel each other.
You do not need to do the calculation by hand. In Python, numpy.polyfit(x, y, degree) does it. Degree gives a line, and degree gives a parabola.
The picture shows seven measurements of the stopping distance of a car at different speeds. The data are made up for this lesson, but they look like real ones. The best line misses the points at the two ends. The best parabola passes close to all of them. The reason is physical: the kinetic energy grows with the square of the speed, and a car must lose all of it.
Try and . The bars almost vanish and the sum of squares drops near its smallest value. Then move a little: the sum grows again.
Measure the typical error: the square root of the mean of the squared errors. For the data above, the best line is typically wrong by , and the best parabola by . The smaller error says that the parabola is the better model.
A warning about too much freedom. With points, a polynomial of degree passes through every point, and the error is zero. But it wiggles wildly between the points, and it is useless for prediction. A good model is simple, and it follows the pattern of the data and not each small error.
A warning about the range. A model is built from data in some range, here to . Inside this range it is trustworthy. Outside it, nobody knows: at the car would not even be on the road. Do not trust a model far outside the data.
This program fits a line and a parabola to the data, prints the typical error of each, and predicts the stopping distance at .
numpy.polyfit(x, y, degree) finds the best polynomial by least squares.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.