lecture 10 - regression part 2, least squares, and numeric integration
Overview
The lesson explains how to assess regression with residual error and R², then shows how NumPy least squares solves ill-posed linear systems that ordinary solvers cannot handle. Examples include estimating NO₂ and ozone concentrations from five UV absorbance measurements and integrating continuous or discrete data with SciPy's `quad` and NumPy's trapezoidal rule.
Key takeaways
- R² for ordinary linear regression measures improvement over predicting every observation with the sample mean, using 1 − residual sum of squares divided by total sum of squares.
- For custom nonlinear regression, report residual-based standard error rather than assuming R² retains its usual interpretation; use n − k degrees of freedom when estimating variance.
- `numpy.linalg.lstsq` handles overdetermined, underdetermined, and rank-deficient systems by finding a least-squares solution and can also return rank and singular-value diagnostics.
- In the UV spectrometry example, five measurements at wavelengths from 430 to 450 nm constrain two gas concentrations, yielding estimated NO₂ and ozone concentrations of about 0.016 and 0.015.
- Use `scipy.integrate.quad` for continuous functions and `numpy.trapezoid(y, x)` for sampled data; the CO₂ example estimates about 232.9 million metric tons across 1960 to 2016.
Chapters
- The project applies the principle of maximum entropy to a combustion problem, building on a cross-disciplinary business and engineering application.
- The instructor recommends rereading the assignment to become familiar with its mathematical symbols and ideas before planning an approach.
- The project requires a nonlinear system solver and numerical integration, with a solution expected to take no more than about 50 lines of code.
- The project is due on the Monday after fall break, with presentations scheduled during that week.
- Deliverables include a Jupyter notebook with discussion and a slide presentation.
- The rubric assigns about 60% to technical content, 20% to slides, and the remainder to presentation delivery.
- `numpy.polyfit` handles standard polynomials, including straight lines and quadratics, when coefficients enter linearly.
- `scipy.optimize.curve_fit` supports custom models, including nonlinear models, and solves the fitting problem internally.
- Regression quality requires comparing how closely observations cluster around the fitted curve, especially when several observations share an x-value.
- Standard deviation measures the spread of observations around their mean; a larger value indicates more dispersion.
- Using the mean as the baseline prediction, the sum of squared deviations from the mean represents error before regression.
- The regression residual sum of squares measures error after fitting by summing squared differences between each observation and its fitted value.
- For the standard linear-regression setting, R² compares residual error after fitting with the mean-based error before fitting: R² = 1 − residual sum of squares / total sum of squares.
- An R² near 1 indicates that regression reduces error substantially relative to the mean; an R² near 0 indicates little improvement.
- For nonlinear regression, the instructor advises against relying on R² and recommends reporting standard error instead, accounting for the number of fitted parameters with an n − k denominator.
- Least squares is useful when a linear system is singular, overdetermined, underdetermined, or rank deficient.
- When the equations do not determine a unique exact solution, least squares adds an optimization criterion by minimizing residual error.
- The method can be understood as selecting a solution through residual minimization or a pseudoinverse.
- For an m × n matrix with m greater than n and full column rank, the system is overdetermined; when m is less than n, it is underdetermined.
- `numpy.linalg.lstsq(A, b, ...)` returns a solution and additional diagnostics, including residuals, matrix rank, and singular values.
- For very large sparse matrices, the instructor points to SciPy's sparse least-squares routine, `lsqr`.
- The Beer-Lambert model relates absorbance to optical path length, molar absorptivity, and gas concentration.
- Five UV wavelength measurements, from 430 to 450 nm, produce five equations for two unknown concentrations, making the system overdetermined.
- With a 500 m optical path, `numpy.linalg.lstsq` estimates NO₂ concentration at about 0.016 and ozone concentration at about 0.015.
- `scipy.integrate.quad` integrates a continuous function over specified bounds and provides an estimated integration error.
- `numpy.trapezoid` integrates discrete samples from arrays of y-values and corresponding x-values.
- The project may require numerical integration to calculate moments from discrete data, in addition to using `quad` for continuous functions.
- The example defines a function involving e^(−x²) and cosine, then uses `quad` to integrate it over a bounded interval.
- The minimum inputs are the function and lower and upper bounds; extra function parameters can be supplied through `args`.
- The routine returns the integral estimate and an estimate of absolute error, and it can also accept infinite integration bounds.
- The example loads `CO2 world emissions.txt`, with years in the first column and annual worldwide emissions in the second.
- The columns are sliced into x-values for years and y-values for emissions, then passed to `numpy.trapezoid` in that order.
- The computed total from 1960 to 2016 is about 232.9 million metric tons; alternative numerical methods are welcome if users can explain and justify them.
Transcript excerpt
0:01 Let's begin. I uh released the project yesterday. Uh I might take one more iteration at it to fine-tune the uh a couple of hints, but I will let you know. Uh and you should be able to get started at least reading it, absorbing it. My advice to you is read it the first time, be like, "Oh, holy, oh my god, this is too much." Right? Just do that and let it go. um look at it the a second time and be like okay this looks weird I don't know where to start but just look at it the reason is the secret the secret to math and that's what my math uh mentor taught me during my PhD he said uh 90% of ma he was Russian had very thick accent Boris Coopermitt um he
0:49 he passed away a few years ago um but he was a great mentor and he would tell me uh uh 90% of the of the math is just the symbols. Get familiar with the symbols and you'll be fine. To get familiar with the symbols, the integrals, the strange symbols, mu and whatever, just look at them. Look at them. Get your brains to be familiar with just the shock of looking at weird math before you even understand them. Just get familiar with that a couple of times. Okay? So, look at the project couple of times every day. Open it, look at it. Don't have to get started. Maybe read it then and then read it another time to just get the ideas to be familiar with the ideas before you um you make a plan of attack. Um you have all the tools you need to solve a problem. It's an engineering
1:37 problem. Um a typical problem you will
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Saad Explains.