%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%22marimo%3E%3D0.23.9%22%2C%20%22numpy%3E%3D2.0%22%2C%20%22matplotlib%3E%3D3.9%22%2C%20%22pandas%3E%3D2.2%22%2C%20%22scipy%3E%3D1.14%22%2C%20%22ipython%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Curve%20fitting%0A%0A%20%20%20%20%3E%20Marcos%20Duarte%2C%20Renato%20Naville%20Watanabe%3Cbr%3E%0A%20%20%20%20%3E%20%5BLaboratory%20of%20Biomechanics%20and%20Motor%20Control%5D(https%3A%2F%2Fbmclab.pesquisa.ufabc.edu.br%2F)%3Cbr%3E%0A%20%20%20%20%3E%20Federal%20University%20of%20ABC%2C%20Brazil%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20to%20use%20this%20guide%0A%0A%20%20%20%20A%20curve%20through%20the%20points%20can%20summarize%20a%20measurement%20or%20test%20a%20physical%20model.%20Those%20are%20different%20ambitions.%20We%20will%20fit%20lines%20and%20a%20nonlinear%20peak%2C%20inspect%20residuals%2C%20and%20distinguish%20uncertainty%20in%20a%20fitted%20mean%20from%20uncertainty%20in%20a%20future%20observation.%20Algebra%20and%20basic%20arrays%20are%20enough%20to%20begin.%0A%0A%20%20%20%20Read%20the%20examples%20in%20order%2C%20predict%20each%20result%2C%20and%20then%20run%20the%20cell.%20Try%20the%20distributed%20Exercises%20before%20opening%20their%20worked%20solutions.%20Finish%20with%20the%20checkpoint%20questions%20and%20independent%20Problems%3B%20their%20panels%20contain%20final%20answers%20only.%0A%0A%20%20%20%20**Challenge%200.**%20Two%20fits%20both%20have%20%24R%5E2%3D0.99%24.%20One%20leaves%20a%20curved%20pattern%20in%20its%20residuals.%20Would%20you%20trust%20the%20two%20models%20equally%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Python%20setup%0A%0A%20%20%20%20NumPy%2C%20SciPy%2C%20pandas%2C%20and%20Matplotlib%20provide%20fitting%2C%20tables%2C%20and%20plots%3B%20IPython%20renders%20equations.%20Examples%20use%20simulated%20or%20explicitly%20constructed%20data%2C%20so%20no%20private%20experimental%20files%20are%20required.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Curve%20fitting%20is%20the%20process%20of%20fitting%20a%20model%2C%20expressed%20in%20terms%20of%20a%20mathematical%20function%2C%20that%20depends%20on%20adjustable%20parameters%20to%20a%20series%20of%20data%20points%20and%20once%20adjusted%2C%20that%20curve%20has%20the%20best%20fit%20to%20the%20data%20points.%0A%0A%20%20%20%20The%20model%20can%20be%20an%20arbitrary%20class%20of%20functions%20such%20as%20polynomials%20and%20the%20fit%20would%20determine%20the%20polynomial%20coefficients%20to%20simplily%20summarize%20the%20data%20or%20the%20model%20parameters%20can%20represent%20a%20underlying%20theory%20that%20the%20data%20are%20supposed%20to%20satisfy%20such%20as%20fitting%20an%20exponential%20function%20to%20data%20of%20a%20decay%20process%20to%20determine%20its%20decay%20rate%20or%20a%20parabola%20to%20the%20position%20data%20of%20an%20object%20falling%20to%20determine%20the%20gravity%20acceleration.%0A%0A%20%20%20%20The%20general%20approach%20to%20the%20fitting%20procedure%20involves%20the%20definition%20of%20a%20merit%20function%20that%20measures%20the%20agreement%20between%20data%20and%20model.%20The%20model%20parameters%20are%20then%20adjusted%20to%20yield%20the%20best-fit%20parameters%20as%20a%20problem%20of%20minimization.%20A%20fitting%20procedure%20should%20provide%20(i)%20parameters%2C%20(ii)%20error%20estimates%20on%20the%20parameters%2C%20and%20(iii)%20a%20statistical%20measure%20of%20goodness-of-fit.%20When%20the%20third%20item%20suggests%20that%20the%20model%20is%20an%20unlikely%20match%20to%20the%20data%2C%20then%20items%20(i)%20and%20(ii)%20are%20probably%20worthless.%3Cbr%3E%0A%20%20%20%20(Numerical%20Recipes%202007%2C%20Bevington%20and%20Robinson%202002)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Least%20squares%0A%0A%20%20%20%20Consider%20%24n%24%20data%20points%20%24(x_i%2C%20y_i)%2C%5C%3A%20i%3D1%2C%20%5Cdots%20%2C%20n%24%2C%20where%20%24x_i%24%20is%20the%20independent%20variable%20(or%20predictor)%20and%20%24y_i%24%20is%20the%20dependent%20variable%20(or%20response)%20to%20be%20fitted%20by%20a%20model%20function%20%24y%24%20with%20%24m%24%20adjustable%20parameters%20%24%5Cbeta_i%2C%5C%3A%20i%3D1%2C%20%5Cdots%20%2C%20m%24.%20The%20problem%20is%20to%20find%20the%20parameter%20values%20for%20the%20model%20which%20best%20fits%20the%20data.%20A%20classical%20solution%20is%20to%20find%20the%20best%20fit%20by%20minimizing%20the%20sum%20of%20the%20squared%20differences%20between%20data%20points%20and%20the%20model%20function%20(the%20sum%20of%20squared%20residuals%20as%20the%20merit%20function)%2C%20which%20is%20known%20as%20the%20least-squares%20fit%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cleft%5B%20y_i%20-%20y(x%20%7C%20%5Cbeta_1%20%5Cdots%20%5Cbeta_%7Bm%7D)%20%5Cright%5D%5E2%20%5C%3B%5C%3B%5C%3B%5C%3B%5C%3B%5C%3B%20%5Cmathrm%7Bminimize%5C%3B%20over%3A%7D%20%5C%3B%5C%3B%5C%3B%20%5Cbeta_1%20%5Cdots%20%5Cbeta_%7Bm%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**Chi-Square**%3Cbr%3E%0A%20%20%20%20If%20we%20consider%20that%20each%20response%20%24y_i%24%20has%20a%20measurement%20error%20or%20uncertainty%20described%20by%20a%20standard%20deviation%2C%20%24%5Csigma_i%24%2C%20the%20problem%20now%20is%20to%20minimize%20the%20following%20function%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cleft%5B%20%5Cfrac%7B%20y_i%20-%20y(x%20%7C%20%5Cbeta_1%20%5Cdots%20%5Cbeta_%7Bm%7D)%20%7D%7B%5Csigma_i%7D%20%5Cright%5D%5E2%20%3D%20%5Cchi%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Considering%20that%20the%20residuals%20are%20normally%20distributed%2C%20the%20sum%20of%20squared%20residuals%20divided%20by%20their%20variance%2C%20%24%5Csigma_i%5E2%24%2C%20by%20definition%20will%20have%20a%20%5Bchi-squared%20distribution%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FChi-squared_distribution)%2C%20%24%5Cchi%5E2%24.%20Once%20the%20best-fit%20parameters%20are%20found%2C%20the%20terms%20in%20the%20sum%20above%20are%20not%20all%20statistically%20independent%20and%20the%20probability%20distribution%20of%20%24%5Cchi%5E2%24%20will%20be%20the%20chi-squared%20distribution%20for%20%24n-m%24%20degrees%20of%20freedom.%0A%0A%20%20%20%20The%20uncertainty%20%24%5Csigma_i%24%20can%20be%20seen%20as%20the%20inverse%20of%20the%20weights%20in%20a%20weighted%20sum%20(because%20less%20certainty%20we%20have%20about%20this%20measure).%20Larger%20%24%5Csigma_i%24%2C%20smaller%20the%20weight%20of%20%24y_i%24%20in%20the%20sum.%20If%20uncertainties%20are%20unknown%20and%20all%20observations%20receive%20equal%20weight%2C%20set%20all%20%24%5Csigma_i%3D1%24%20as%20a%20weighting%20convention.%20This%20does%20not%20mean%20the%20measurements%20have%20no%20uncertainty.%0A%0A%20%20%20%20A%20rough%20estimate%20of%20the%20goodness%20of%20fit%20is%20the%20reduced%20chi-square%20statistic%2C%20%24%5Cchi%5E2_%7Bred%7D%24%3A%20the%20%24%5Cchi%5E2%24%20value%20divided%20by%20the%20number%20of%20degrees%20of%20freedom%20(%24n-m%24).%3Cbr%3E%0A%20%20%20%20For%20a%20correct%20model%20with%20independent%20normal%20errors%20and%20correctly%20specified%20standard%20deviations%2C%20reduced%20chi-square%20is%20expected%20near%20one%2C%20with%20sampling%20variability.%20A%20large%20value%20can%20reflect%20model%20error%20or%20underestimated%20uncertainties%3B%20a%20small%20value%20can%20reflect%20overestimated%20uncertainties.%20Rescaling%20errors%20to%20force%20a%20value%20of%20one%20is%20not%20model%20validation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%201.**%20Two%20residuals%20are%20both%202%20units%2C%20but%20their%20stated%20SDs%20are%201%20and%202%20units.%20What%20are%20their%20contributions%20to%20chi-square%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20They%20contribute%20%24(2%2F1)%5E2%3D4%24%20and%20%24(2%2F2)%5E2%3D1%24.%20The%20less%20precise%20observation%20receives%20less%20influence.%20In%20the%20squared-residual%20objective%20the%20weights%20are%20inverse%20variances.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Linear%20fit%0A%0A%20%20%20%20Let's%20derive%20the%20analytical%20expression%20for%20the%20least-square%20fit%20when%20the%20model%20function%20is%20a%20straight%20line%20(a.k.a.%20linear%20regression)%20and%20the%20dependent%20variable%20%24y_i%24%20has%20uncertainty%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20y(x)%20%3D%20y(x%20%7C%20a%2Cb)%20%3D%20a%20%2B%20bx%0A%20%20%20%20%24%24%0A%0A%20%20%20%20We%20want%20to%20find%20%24a%2C%20b%24%20such%20that%20minimizes%20the%20%24%5Cchi%5E2%24%20function%20defined%20above%20(a.k.a.%20%24%5Cchi%5E2%24%20fitting)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cchi%5E2(a%2Cb)%20%3D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cleft%5B%20%5Cfrac%7B%20y_i%20-%20(a%20%2B%20bx_i)%20%7D%7B%5Csigma_i%7D%20%5Cright%5D%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Using%20the%20property%20that%20at%20the%20minimum%20of%20%24%5Cchi%5E2%24%20its%20derivative%20is%20zero%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cfrac%7B%5Cpartial%20%5Cchi%5E2%7D%7B%5Cpartial%20a%7D%20%3D%20-2%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cfrac%7B%20y_i%20-%20a%20-%20bx_i%20%7D%7B%5Csigma_i%5E2%7D%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cfrac%7B%5Cpartial%20%5Cchi%5E2%7D%7B%5Cpartial%20b%7D%20%3D%20-2%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cfrac%7B%20x_i(y_i%20-%20a%20-%20bx_i)%20%7D%7B%5Csigma_i%5E2%7D%20%3D%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20To%20solve%20these%20two%20equations%2C%20let's%20define%20the%20sums%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20S%20%3D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cfrac%7B1%7D%7B%5Csigma_i%5E2%7D%20%5C%3B%5C%3B%5C%3B%20S_x%20%3D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cfrac%7Bx_i%7D%7B%5Csigma_i%5E2%7D%20%5C%3B%5C%3B%5C%3B%20S_y%20%3D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cfrac%7By_i%7D%7B%5Csigma_i%5E2%7D%20%5C%3B%5C%3B%5C%3B%20S_%7Bxx%7D%20%3D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cfrac%7Bx_i%5E2%7D%7B%5Csigma_i%5E2%7D%20%5C%3B%5C%3B%5C%3B%20S_%7Bxy%7D%20%3D%20%5Csum_%7Bi%3D1%7D%5E%7Bn%7D%20%5Cfrac%7Bx_i%20y_i%7D%7B%5Csigma_i%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Using%20these%20definitions%2C%20the%20former%20two%20equations%20become%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20S_y%20%5C%3A%5C%3A%20%3D%20aS%20%2B%20bS_x%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20S_%7Bxy%7D%20%3D%20aS_x%20%2B%20bS_%7Bxx%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20And%20solving%20these%20two%20equations%20for%20the%20two%20unknowns%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20a%20%3D%20%5Cfrac%7BS_%7Bxx%7DS_y%20-%20S_x%20S_%7Bxy%7D%7D%7B%5CDelta%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20b%20%3D%20%5Cfrac%7BS%20S_%7Bxy%7D%20-%20S_x%20S_y%7D%7B%5CDelta%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5CDelta%20%3D%20S%20S_%7Bxx%7D%20-%20S_x%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20With%20the%20parameters%20above%2C%20the%20straight%20line%20will%20be%20the%20best%20fit%20in%20the%20sense%20that%20the%20sum%20of%20the%20squared%20residuals%20are%20minimum.%0A%0A%20%20%20%20**Estimating%20the%20uncertainty%20of%20the%20parameters**%0A%0A%20%20%20%20The%20uncertainty%20of%20each%20parameter%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma_a%5E2%20%3D%20%5Cfrac%7BSxx%7D%7B%5CDelta%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma_b%5E2%20%3D%20%5Cfrac%7BS%7D%7B%5CDelta%7D%0A%20%20%20%20%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%202.**%20The%20points%20(0%2C1)%2C%20(1%2C3)%2C%20and%20(2%2C5)%20lie%20on%20a%20line.%20Find%20its%20intercept%20and%20slope%2C%20and%20state%20the%20units%20if%20x%20is%20seconds%20and%20y%20is%20metres.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20The%20change%20in%20y%20per%20unit%20x%20is%202%2C%20and%20y%20at%20x%3D0%20is%201.%20Thus%20%24y%3D1%2B2x%24%3B%20the%20intercept%20is%201%20m%20and%20the%20slope%20is%202%20m%2Fs.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Correlation%20coefficient%0A%0A%20%20%20%20The%20Pearson%20product-moment%20correlation%20coefficient%2C%20or%20simply%20the%20correlation%20coefficient%2C%20is%20a%20measure%20of%20the%20linear%20correlation%20between%20two%20variables%20%24x%24%20and%20%24y%24%2C%20with%20values%20varying%20from%20%2B1%20to%20%E2%88%921%2C%20where%201%20is%20total%20positive%20correlation%2C%200%20is%20no%20linear%20correlation%2C%20and%20%E2%88%921%20is%20total%20negative%20correlation.%3Cbr%3E%0A%20%20%20%20The%20correlation%20coefficient%20between%20populations%20of%20two%20random%20variables%20is%20the%20covariance%20of%20the%20two%20variables%20divided%20by%20the%20product%20of%20their%20standard%20deviations%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Crho_%7Bx%2C%20y%7D%20%3D%20%5Cfrac%7Bcov(x%2C%20y)%7D%7B%5Csigma_x%5Csigma_y%7D%20%3D%20%5Cfrac%7BE%5B(x-%5Cmu_x)(y-%5Cmu_y)%5D%7D%7B%5Csqrt%7BE%5B(x-%5Cmu_x)%5E2%5D%7D%5Csqrt%7BE%5B(y-%5Cmu_y)%5E2%5D%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24E%5B%5Ccdot%5D%24%20is%20the%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMoment_(mathematics)%22%3Eexpectation%20operator%3C%2Fa%3E.%0A%0A%20%20%20%20For%20samples%20of%20two%20random%20variables%2C%20the%20covariance%20and%20standard%20deviation%20are%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20cov(x%2C%20y)%20%3D%20%5Cfrac%7B1%7D%7Bn-1%7D%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(x_i-%5Cbar%7Bx%7D)(y_i-%5Cbar%7By%7D)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma_x%20%3D%20%5Csqrt%7B%5Cfrac%7B1%7D%7Bn-1%7D%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(x_i-%5Cbar%7Bx%7D)%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20So%2C%20the%20correlation%20coefficient%20for%20the%20samples%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20r_%7Bx%2C%20y%7D%20%3D%20%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(x_i-%5Cbar%7Bx%7D)(y_i-%5Cbar%7By%7D)%7D%7B%5Csqrt%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(x_i-%5Cbar%7Bx%7D)%5E2%7D%5Csqrt%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(y_i-%5Cbar%7By%7D)%5E2%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20ordinary%20unweighted%20simple%20linear%20regression%20**with%20an%20intercept**%2C%20the%20coefficient%20of%20determination%20equals%20the%20square%20of%20the%20sample%20Pearson%20correlation%2C%20and%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20R%5E2(y%2C%20%5Cwidehat%7By%7D)%20%3D%20%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(%5Cwidehat%7By%7D_i-%5Cbar%7By%7D)%5E2%7D%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(y_i-%5Cbar%7By%7D)%5E2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24%5Cwidehat%7By%7D_i%24%20are%20the%20fitted%20values%20from%20the%20linear%20fit.%0A%0A%20%20%20%20Examining%20the%20division%20above%2C%20it%20consists%20of%20the%20variance%20of%20the%20fitted%20values%20around%20the%20mean%20value%20of%20%24y%24%20divided%20by%20the%20variance%20of%20%24y_i%24.%20Because%20of%20that%2C%20it%20is%20said%20that%20the%20coefficient%20of%20determination%20is%20the%20proportion%20of%20variance%20in%20%24y%24%20explained%20by%20a%20linear%20function%20of%20%24x%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%203.**%20A%20relationship%20is%20U-shaped%20and%20symmetric%20about%20x%3D0.%20Can%20its%20Pearson%20correlation%20be%20zero%20even%20if%20y%20is%20determined%20by%20x%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20Yes.%20For%20symmetric%20x%20values%20and%20%24y%3Dx%5E2%24%2C%20positive%20and%20negative%20contributions%20to%20the%20covariance%20cancel.%20Zero%20correlation%20rules%20out%20a%20linear%20trend%20in%20this%20example%2C%20not%20a%20deterministic%20nonlinear%20relationship.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Computing%20the%20curve%20fitting%0A%0A%20%20%20%20Python%20and%20its%20ecosystem%20for%20scientific%20computing%20have%20plenty%20of%20functions%20ready%20available%20for%20curve%20fitting.%20Instead%20of%20writting%20our%20own%20code%20to%20implement%20the%20formula%20above%2C%20let's%20use%20the%20functions%20available%20which%20will%20cover%20many%20more%20cases%20(general%20polynomials%2C%20nonlinear%20functions%2C%20etc.).%0A%0A%20%20%20%20First%2C%20if%20we%20only%20want%20to%20fit%20polynomials%2C%20we%20can%20use%20the%20Numpy%20polyfit%20function%3A%0A%0A%20%20%20%20%20%20%20%20polyfit(x%2C%20y%2C%20deg%2C%20rcond%3DNone%2C%20full%3DFalse%2C%20w%3DNone%2C%20cov%3DFalse)%0A%20%20%20%20%20%20%20%20%20%20%20%20Least%20squares%20polynomial%20fit.%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20Fit%20a%20polynomial%20%60%60p(x)%20%3D%20p%5B0%5D%20*%20x**deg%20%2B%20...%20%2B%20p%5Bdeg%5D%60%60%20of%20degree%20%60deg%60%0A%20%20%20%20%20%20%20%20%20%20%20%20to%20points%20%60(x%2C%20y)%60.%20Returns%20a%20vector%20of%20coefficients%20%60p%60%20that%20minimises%0A%20%20%20%20%20%20%20%20%20%20%20%20the%20squared%20error.%0A%0A%20%20%20%20Let's%20demonstrate%20how%20polyfit%20works%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Import%20the%20necessary%20libraries%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20%23%20'%25matplotlib%20inline'%20command%20supported%20automatically%20in%20marimo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20scipy.stats%20as%20stats%0A%0A%20%20%20%20return%20np%2C%20plt%2C%20stats%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Generate%20some%20data%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20n%20%3D%2050%0A%20%20%20%20x%20%3D%20np.arange(1%2C%20n%2B1)%0A%20%20%20%20y%20%3D%20x%20%2B%2010*np.random.randn(n)%20%2B%2010%0A%20%20%20%20yerr%20%3D%20np.abs(10*np.random.randn(n))%20%2B%205%0A%20%20%20%20return%20n%2C%20x%2C%20y%2C%20yerr%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20First%2C%20let's%20plot%20the%20data%20and%20perform%20the%20curve%20fitting%20without%20considering%20the%20uncertainty%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(n%2C%20np%2C%20plt%2C%20x%2C%20y%2C%20yerr)%3A%0A%20%20%20%20(p%2C%20_cov)%20%3D%20np.polyfit(x%2C%20y%2C%201%2C%20cov%3DTrue)%0A%20%20%20%20yfit%20%3D%20np.polyval(p%2C%20x)%0A%20%20%20%20perr%20%3D%20np.sqrt(np.diag(_cov))%0A%20%20%20%20R2%20%3D%20np.corrcoef(x%2C%20y)%5B0%2C%201%5D%20**%202%0A%20%20%20%20resid%20%3D%20y%20-%20yfit%0A%20%20%20%20chi2red%20%3D%20np.sum((resid%20%2F%20yerr)%20**%202)%20%2F%20(y.size%20-%202)%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20plt.plot(x%2C%20y%2C%20'bo')%0A%20%20%20%20plt.plot(x%2C%20yfit%2C%20linewidth%3D3%2C%20color%3D%5B1%2C%200%2C%200%2C%200.5%5D)%0A%20%20%20%20plt.xlabel('x'%2C%20fontsize%3D16)%0A%20%20%20%20plt.ylabel('y'%2C%20fontsize%3D16)%0A%20%20%20%20plt.title('%24y%20%3D%20%25.2f%20%5C%5Cpm%20%25.2f%20%2B%20(%25.2f%20%5C%5Cpm%20%25.2f)x%20%5C%5C%3B%20%5BR%5E2%3D%25.2f%2C%5C%5C%2C%20%5C%5Cchi%5E2_%7Bred%7D%3D%25.1f%5D%24'%20%25%20(p%5B1%5D%2C%20perr%5B1%5D%2C%20p%5B0%5D%2C%20perr%5B0%5D%2C%20R2%2C%20chi2red)%2C%20fontsize%3D20%2C%20color%3D%5B0%2C%200%2C%200%5D)%0A%20%20%20%20plt.xlim((0%2C%20n%20%2B%201))%0A%20%20%20%20plt.ylim((-50%2C%20100))%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20curve%20fitting%20by%20a%20line%20considering%20the%20uncertainty%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20x%2C%20y%2C%20yerr)%3A%0A%20%20%20%20(p_1%2C%20_cov)%20%3D%20np.polyfit(x%2C%20y%2C%201%2C%20w%3D1%20%2F%20yerr%2C%20cov%3DTrue)%0A%20%20%20%20yfit_1%20%3D%20np.polyval(p_1%2C%20x)%0A%20%20%20%20perr_1%20%3D%20np.sqrt(np.diag(_cov))%0A%20%20%20%20R2_1%20%3D%20np.corrcoef(x%2C%20y)%5B0%2C%201%5D%20**%202%0A%20%20%20%20resid_1%20%3D%20y%20-%20yfit_1%0A%20%20%20%20chi2red_1%20%3D%20np.sum((resid_1%20%2F%20yerr)%20**%202)%20%2F%20(y.size%20-%202)%0A%20%20%20%20return%20R2_1%2C%20chi2red_1%2C%20p_1%2C%20perr_1%2C%20resid_1%2C%20yfit_1%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20And%20the%20plot%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(R2_1%2C%20chi2red_1%2C%20n%2C%20p_1%2C%20perr_1%2C%20plt%2C%20x%2C%20y%2C%20yerr%2C%20yfit_1)%3A%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20plt.errorbar(x%2C%20y%2C%20yerr%3Dyerr%2C%20fmt%3D'bo'%2C%20ecolor%3D'b'%2C%20capsize%3D0)%0A%20%20%20%20plt.plot(x%2C%20yfit_1%2C%20linewidth%3D3%2C%20color%3D%5B1%2C%200%2C%200%2C%200.5%5D)%0A%20%20%20%20plt.xlabel('x'%2C%20fontsize%3D16)%0A%20%20%20%20plt.ylabel('y'%2C%20fontsize%3D16)%0A%20%20%20%20plt.title('%24y%20%3D%20%25.2f%20%5C%5Cpm%20%25.2f%20%2B%20(%25.2f%20%5C%5Cpm%20%25.2f)x%20%5C%5C%3B%20%5BR%5E2%3D%25.2f%2C%5C%5C%2C%20%5C%5Cchi%5E2_%7Bred%7D%3D%25.1f%5D%24'%20%25%20(p_1%5B1%5D%2C%20perr_1%5B1%5D%2C%20p_1%5B0%5D%2C%20perr_1%5B0%5D%2C%20R2_1%2C%20chi2red_1)%2C%20fontsize%3D20%2C%20color%3D%5B0%2C%200%2C%200%5D)%0A%20%20%20%20plt.xlim((0%2C%20n%20%2B%201))%0A%20%20%20%20plt.ylim((-50%2C%20100))%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20According%20to%20our%20assumptions%2C%20the%20residuals%20should%20be%20normally%20distributed.%20%20Let's%20create%20a%20function%20to%20plot%20the%20residuals%20and%20a%20probability%20plot%20that%20will%20be%20useful%20to%20check%20if%20the%20residuals%20are%20normally%20distributed%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20stats)%3A%0A%20%20%20%20def%20plot_resid(x%2C%20resid)%3A%0A%20%20%20%20%20%20%20%20%22%22%22%20plot%20residuals%20and%20probability%20plot%20of%20residuals%20for%20a%20normal%20distribution.%22%22%22%0A%20%20%20%20%20%20%20%20(_fig%2C%20_ax)%20%3D%20plt.subplots(1%2C%202%2C%20figsize%3D(13%2C%205))%0A%20%20%20%20%20%20%20%20_ax%5B0%5D.plot(x%2C%20resid%2C%20'ro')%0A%20%20%20%20%20%20%20%20_ax%5B0%5D.plot(%5B0%2C%20x%5B-1%5D%5D%2C%20%5B0%2C%200%5D%2C%20'k')%0A%20%20%20%20%20%20%20%20_ax%5B0%5D.set_xlabel('x'%2C%20fontsize%3D12)%0A%20%20%20%20%20%20%20%20_ax%5B0%5D.set_ylabel('Residuals'%2C%20fontsize%3D12)%0A%20%20%20%20%20%20%20%20stats.probplot(resid%2C%20dist%3D'norm'%2C%20plot%3Dplt)%0A%20%20%20%20%20%20%20%20_ax%5B1%5D.set_xlabel('Quantiles'%2C%20fontsize%3D12)%0A%20%20%20%20%20%20%20%20_ax%5B1%5D.set_ylabel('Ordered%20values'%2C%20fontsize%3D12)%0A%20%20%20%20%20%20%20%20_ax%5B1%5D.set_title('Probability%20Plot%20of%20the%20residuals')%0A%20%20%20%20%20%20%20%20plt.show()%0A%0A%20%20%20%20return%20(plot_resid%2C)%0A%0A%0A%40app.cell%0Adef%20_(plot_resid%2C%20resid_1%2C%20x)%3A%0A%20%20%20%20plot_resid(x%2C%20resid_1)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20could%20apply%20a%20statistical%20test%20to%20verify%20if%20the%20data%20above%20is%20normallly%20distributed%2C%20but%20for%20now%2C%20by%20visual%20inspection%2C%20the%20residuals%20indeed%20seem%20to%20be%20normallly%20distributed.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20The%20effect%20of%20uncertainty%20on%20the%20curve%20fitting%0A%0A%20%20%20%20To%20demonstrate%20the%20effect%20of%20uncertainty%20on%20the%20curve%20fitting%2C%20let's%20plot%20the%20same%20(x%2C%20y)%20values%20but%20with%20different%20errors%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20x_1%20%3D%20np.array(%5B1%2C%202%2C%203%2C%204%2C%205%5D)%0A%20%20%20%20y_1%20%3D%20np.array(%5B1%2C%202%2C%203%2C%206%2C%204%5D)%0A%20%20%20%20yerr_1%20%3D%20np.array(%5B%5B0%2C%200%2C%200%2C%200%2C%200%5D%2C%20%5B1%2C%201%2C%201%2C%201%2C%201%5D%2C%20%5B1%2C%201%2C%201%2C%200.5%2C%202%5D%2C%20%5B1%2C%201%2C%201%2C%202%2C%200.5%5D%5D)%0A%20%20%20%20return%20x_1%2C%20y_1%2C%20yerr_1%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20def%20linearfit(x%2C%20y%2C%20yerr%3DNone)%3A%0A%20%20%20%20%20%20%20%20w%20%3D%20None%20if%20yerr%20is%20None%20or%20np.sum(yerr)%20%3D%3D%200%20else%201%20%2F%20yerr%0A%20%20%20%20%20%20%20%20(p%2C%20_cov)%20%3D%20np.polyfit(x%2C%20y%2C%201%2C%20w%3Dw%2C%20cov%3DTrue)%0A%20%20%20%20%20%20%20%20yfit%20%3D%20np.polyval(p%2C%20x)%0A%20%20%20%20%20%20%20%20perr%20%3D%20np.sqrt(np.diag(_cov))%0A%20%20%20%20%20%20%20%20R2%20%3D%20np.corrcoef(x%2C%20y)%5B0%2C%201%5D%20**%202%0A%20%20%20%20%20%20%20%20resid%20%3D%20y%20-%20yfit%0A%20%20%20%20%20%20%20%20chi2red%20%3D%20np.sum((resid%20%2F%20yerr)%20**%202)%20%2F%20(y.size%20-%202)%20if%20w%20is%20not%20None%20else%20np.nan%0A%20%20%20%20%20%20%20%20return%20(yfit%2C%20p%2C%20R2%2C%20chi2red%2C%20perr%2C%20resid)%0A%0A%20%20%20%20return%20(linearfit%2C)%0A%0A%0A%40app.cell%0Adef%20_(linearfit%2C%20plt%2C%20x_1%2C%20y_1%2C%20yerr_1)%3A%0A%20%20%20%20(_fig%2C%20_ax)%20%3D%20plt.subplots(2%2C%202%2C%20figsize%3D(10%2C%206)%2C%20sharex%3DTrue%2C%20sharey%3DTrue)%0A%20%20%20%20_axs%20%3D%20_ax.flatten()%0A%20%20%20%20for%20(_i%2C%20_ax)%20in%20enumerate(_axs)%3A%0A%20%20%20%20%20%20%20%20(_yf%2C%20p_2%2C%20R2_2%2C%20chi2red_2%2C%20perr_2%2C%20resid_2)%20%3D%20linearfit(x_1%2C%20y_1%2C%20yerr%3Dyerr_1%5B_i%2C%20%3A%5D)%0A%20%20%20%20%20%20%20%20_ax.errorbar(x_1%2C%20y_1%2C%20yerr%3Dyerr_1%5B_i%2C%20%3A%5D%2C%20fmt%3D'bo'%2C%20ecolor%3D'b'%2C%20capsize%3D0%2C%20elinewidth%3D2)%0A%20%20%20%20%20%20%20%20_ax.plot(x_1%2C%20_yf%2C%20linewidth%3D3%2C%20color%3D%5B1%2C%200%2C%200%2C%200.5%5D)%0A%20%20%20%20%20%20%20%20_ax.set_title('%24y%20%3D%20%25.2f%20%2B%20%25.2f%20x%20%5C%5C%2C%20%5BR%5E2%3D%25.2f%2C%5C%5Cchi%5E2_%7Bred%7D%3D%25.1f%5D%24'%20%25%20(p_2%5B1%5D%2C%20p_2%5B0%5D%2C%20R2_2%2C%20chi2red_2)%2C%20fontsize%3D18)%0A%20%20%20%20%20%20%20%20_ax.grid()%0A%20%20%20%20_ax.set_xlim(0.5%2C%205.5)%0A%20%20%20%20_fig.subplots_adjust(bottom%3D0.1%2C%20left%3D0.05%2C%20right%3D0.95%2C%20hspace%3D0.2%2C%20wspace%3D0.05)%0A%20%20%20%20plt.suptitle('The%20effect%20of%20uncertainty%20on%20the%20curve%20fitting%20(same%20data%2C%20different%20errors)'%2C%20fontsize%3D16%2C%20y%3D1.02)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20From%20figure%20above%2C%20if%20the%20errors%20(weights)%20are%20all%20equal%2C%20the%20fitting%20is%20the%20same%20as%20if%20we%20don't%20input%20any%20error%20(first%20line).%3Cbr%3E%0A%20%20%20%20When%20the%20errors%20are%20different%20across%20data%2C%20the%20uncertainty%20has%20a%20strong%20impact%20on%20the%20curve%20fitting%20(second%20line).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Curve%20fitting%20as%20a%20model%0A%0A%20%20%20%20We%20have%20to%20be%20carefull%20in%20interpreting%20the%20results%20of%20a%20curve%20fitting%20to%20determine%20whether%20the%20fitted%20model%20truely%20captures%20the%20relation%20between%20the%20independent%20variable%20(predictor)%20and%20the%20dependent%20variable%20(response).%0A%0A%20%20%20%20An%20illustrative%20example%20to%20demonstrate%20that%20the%20result%20of%20a%20curve%20fitting%20is%20not%20necessarily%20an%20indicator%20of%20the%20phenomenon%20being%20modelled%20is%20the%20%5BAnscombe's%20quartet%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAnscombe%2527s_quartet)%20data.%20These%20four%20sets%20of%20data%20have%20very%20similar%20basic%20statistical%20properties%20and%20linear%20fitting%20parameters%2C%20but%20are%20very%20different%20when%20visualized.%20Let's%20work%20with%20these%20data%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20x_2%20%3D%20np.array(%5B%5B10%2C%208%2C%2013%2C%209%2C%2011%2C%2014%2C%206%2C%204%2C%2012%2C%207%2C%205%5D%2C%20%5B10%2C%208%2C%2013%2C%209%2C%2011%2C%2014%2C%206%2C%204%2C%2012%2C%207%2C%205%5D%2C%20%5B10%2C%208%2C%2013%2C%209%2C%2011%2C%2014%2C%206%2C%204%2C%2012%2C%207%2C%205%5D%2C%20%5B8%2C%208%2C%208%2C%208%2C%208%2C%208%2C%208%2C%2019%2C%208%2C%208%2C%208%5D%5D)%0A%20%20%20%20y_2%20%3D%20np.array(%5B%5B8.04%2C%206.95%2C%207.58%2C%208.81%2C%208.33%2C%209.96%2C%207.24%2C%204.26%2C%2010.84%2C%204.82%2C%205.68%5D%2C%20%5B9.14%2C%208.14%2C%208.74%2C%208.77%2C%209.26%2C%208.1%2C%206.13%2C%203.1%2C%209.13%2C%207.26%2C%204.74%5D%2C%20%5B7.46%2C%206.77%2C%2012.74%2C%207.11%2C%207.81%2C%208.84%2C%206.08%2C%205.39%2C%208.15%2C%206.42%2C%205.73%5D%2C%20%5B6.58%2C%205.76%2C%207.71%2C%208.84%2C%208.47%2C%207.04%2C%205.25%2C%2012.5%2C%205.56%2C%207.91%2C%206.89%5D%5D)%0A%20%20%20%20print(%22The%20Anscombe's%20quartet%20data%20ha)ve%20the%20same%20basic%20statistical%20properties%3A%22)%0A%20%20%20%20print('Mean%20of%20x%20%20%20%20%3A'%2C%20np.mean(y_2%2C%20axis%3D1))%0A%20%20%20%20print('Variance%20of%20x%3A'%2C%20np.var(y_2%2C%20axis%3D1))%0A%20%20%20%20print('Mean%20of%20y%20%20%20%20%3A'%2C%20np.mean(y_2%2C%20axis%3D1))%0A%20%20%20%20print('Variance%20of%20y%3A'%2C%20np.var(y_2%2C%20axis%3D1))%0A%20%20%20%20return%20x_2%2C%20y_2%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Or%20we%20can%20use%20Pandas%20to%20describe%20the%20data%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20x_2%2C%20y_2)%3A%0A%20%20%20%20import%20pandas%20as%20pd%0A%20%20%20%20df%20%3D%20pd.DataFrame(np.vstack((x_2%2C%20y_2)).T%2C%20columns%3D%5B'X1'%2C%20'X2'%2C%20'X3'%2C%20'X4'%2C%20'Y1'%2C%20'Y2'%2C%20'Y3'%2C%20'Y4'%5D)%0A%20%20%20%20df.describe()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(linearfit%2C%20np%2C%20plt%2C%20x_2%2C%20y_2)%3A%0A%20%20%20%20(_fig%2C%20_ax)%20%3D%20plt.subplots(2%2C%202%2C%20figsize%3D(10%2C%206)%2C%20sharex%3DTrue%2C%20sharey%3DTrue)%0A%20%20%20%20_axs%20%3D%20_ax.flatten()%0A%20%20%20%20resid_3%20%3D%20np.empty_like(y_2)%0A%20%20%20%20for%20(_i%2C%20_ax)%20in%20enumerate(_axs)%3A%0A%20%20%20%20%20%20%20%20(_yf%2C%20p_3%2C%20R2_3%2C%20chi2red_3%2C%20perr_3%2C%20resid_3%5B_i%2C%20%3A%5D)%20%3D%20linearfit(x_2%5B_i%2C%20%3A%5D%2C%20y_2%5B_i%2C%20%3A%5D%2C%20yerr%3DNone)%0A%20%20%20%20%20%20%20%20_ax.plot(x_2%5B_i%2C%20%3A%5D%2C%20y_2%5B_i%2C%20%3A%5D%2C%20color%3D%5B0%2C%200.2%2C%201%2C%200.8%5D%2C%20marker%3D'o'%2C%20linestyle%3D''%2C%20markersize%3D8)%0A%20%20%20%20%20%20%20%20_ax.plot(x_2%5B_i%2C%20%3A%5D%2C%20_yf%2C%20linewidth%3D3%2C%20color%3D%5B1%2C%200%2C%200%2C%200.8%5D)%0A%20%20%20%20%20%20%20%20_ax.set_title('%24y%20%3D%20%25.2f%20%2B%20%25.2f%20x%20%5C%5C%2C%20%5BR%5E2%3D%25.2f%5D%24'%20%25%20(p_3%5B1%5D%2C%20p_3%5B0%5D%2C%20R2_3)%2C%20fontsize%3D18)%0A%20%20%20%20%20%20%20%20_ax.grid()%0A%20%20%20%20_ax.set_xlim(0%2C%2020)%0A%20%20%20%20_fig.subplots_adjust(bottom%3D0.1%2C%20left%3D0.05%2C%20right%3D0.95%2C%20hspace%3D0.2%2C%20wspace%3D0.05)%0A%20%20%20%20plt.suptitle(%22Linear%20fit%20of%20the%20Anscombe's%20quartet%20data%22%2C%20fontsize%3D18%2C%20y%3D1.02)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20(resid_3%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20And%20we%20can%20check%20whether%20the%20residuals%20of%20the%20linear%20fit%20are%20normally%20distributed%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20resid_3%2C%20stats)%3A%0A%20%20%20%20_fig%20%3D%20plt.figure(figsize%3D(10%2C%206))%0A%20%20%20%20for%20_i%20in%20range(4)%3A%0A%20%20%20%20%20%20%20%20_ax%20%3D%20plt.subplot(2%2C%202%2C%20_i%20%2B%201)%0A%20%20%20%20%20%20%20%20stats.probplot(resid_3%5B_i%2C%20%3A%5D%2C%20dist%3D'norm'%2C%20plot%3Dplt)%0A%20%20%20%20plt.suptitle('Probability%20plot%20of%20the%20residuals%20for%20a%20normal%20distribution'%2C%20fontsize%3D18%2C%20y%3D1.05)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Even%20the%20residuals%20don't%20look%20bad!%3Cbr%3E%0A%20%20%20%20Exactly%20the%20same%20model%20fits%20very%20different%20data.%3Cbr%3E%0A%20%20%20%20We%20should%20be%20very%20carefull%20in%20interpreting%20the%20result%20of%20a%20curve%20fitting%20as%20a%20description%20of%20a%20phenomenon.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%204.**%20Residuals%20from%20a%20straight-line%20fit%20are%20positive%20at%20both%20ends%20and%20negative%20in%20the%20middle.%20What%20should%20you%20investigate%20before%20reporting%20the%20slope%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%204%0A%20%20%20%20The%20residual%20curvature%20suggests%20a%20missing%20nonlinear%20term%20or%20a%20wrong%20physical%20model.%20A%20high%20%24R%5E2%24%20can%20coexist%20with%20that%20pattern.%20Inspect%20the%20measurement%20process%20and%20compare%20a%20scientifically%20justified%20curved%20model.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Confidence%20and%20prediction%20intervals%20for%20the%20linear%20fit%0A%0A%20%20%20%20Analog%20to%20the%20case%20for%20a%20random%20variable%20(see%20%5BConfidence%20and%20prediction%20intervals%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fconfidence_prediction_intervals.py))%2C%20we%20can%20estimate%20confidence%20and%20prediction%20intervals%20for%20the%20linear%20fit%20(for%20the%20deduction%20of%20these%20intervals%20see%20for%20example%20Montgomery%20(2013)).%0A%0A%20%20%20%20**Confidence%20interval**%0A%0A%20%20%20%20A%2095%25%20confidence%20interval%20for%20the%20linear%20fit%20gives%20the%2095%25%20probability%20that%20this%20interval%20around%20the%20linear%20fit%2C%20%24%5Chat%7B%5Cmu%7D_%7By%7Cx0%7D%24%2C%20contains%20the%20mean%20response%20of%20new%20values%2C%20%24%5Cmu_%7By%7Cx0%7D%24%2C%20at%20a%20specified%20value%2C%20%24x_0%24%2C%20and%20it%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%7C%20%5C%3A%20%5Chat%7B%5Cmu%7D_%7By%7Cx0%7D%20-%20%5Cmu_%7By%7Cx0%7D%20%5C%3A%20%5Cright%7C%20%5C%3B%20%5Cleq%20%5C%3B%20T_%7Bn-2%7D%5E%7B.975%7D%20%5C%3B%20%5Chat%7B%5Csigma%7D%20%5C%3B%20%5Csqrt%7B%5Cfrac%7B1%7D%7Bn%7D%2B%5Cfrac%7B(x_0-%5Cbar%7Bx%7D)%5E2%7D%7B%5Csum_%7Bi%3D1%7D%5En%7B(x_i-%5Cbar%7Bx%7D)%5E2%7D%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%3A%0A%0A%20%20%20%20%24%5Chat%7B%5Cmu%7D_%7By%7Cx0%7D%20%3D%20a%20%2B%20bx_0%24%20is%20computed%20from%20the%20lineat%20fit.%0A%0A%20%20%20%20%24T_%7Bn-2%7D%5E%7B.975%7D%24%20is%20the%20%2497.5%5E%7Bth%7D%24%20percentile%20of%20the%20Student's%20t-distribution%20with%20n%E2%88%922%20degrees%20of%20freedom.%0A%0A%20%20%20%20%24%5Chat%7B%5Csigma%7D%24%20is%20the%20standard%20deviation%20of%20the%20error%20term%20in%20the%20linear%20fit%20(residuals)%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Chat%7B%5Csigma%7D%20%3D%20%5Csqrt%7B%5Csum_%7Bi%3D1%7D%5En%7B%5Cfrac%7B(y_i-%5Chat%7By%7D)%5E2%7D%7Bn-2%7D%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**Prediction%20interval**%0A%0A%20%20%20%20A%2095%25%20prediction%20interval%20for%20the%20linear%20fit%20gives%20the%2095%25%20probability%20that%20this%20interval%20around%20the%20linear%20fit%2C%20%24%5Chat%7By%7D_0%24%2C%20contains%20a%20new%20observation%2C%20%24y_0%24%2C%20at%20a%20specified%20value%2C%20%24x_0%24%2C%20and%20it%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%7C%20%5C%3A%20%5Chat%7By%7D_0%20-%20y_0%20%5C%3A%20%5Cright%7C%20%5C%3B%20%5Cleq%20%5C%3B%20T_%7Bn-2%7D%5E%7B.975%7D%20%5C%3B%20%5Chat%7B%5Csigma%7D%20%5C%3B%20%5Csqrt%7B1%20%2B%20%5Cfrac%7B1%7D%7Bn%7D%2B%5Cfrac%7B(x_0-%5Cbar%7Bx%7D)%5E2%7D%7B%5Csum_%7Bi%3D1%7D%5En%7B(x_i-%5Cbar%7Bx%7D)%5E2%7D%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24%5Chat%7By%7D_0%20%3D%20a%20%2B%20bx_0%24%20is%20computed%20from%20the%20lineat%20fit.%0A%0A%20%20%20%20**Implementation%20in%20Python**%0A%0A%20%20%20%20Here%20is%20an%20implementation%20of%20the%20linear%20fit%20with%20the%20confidence%20and%20prediction%20intervals%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt%2C%20stats)%3A%0A%20%20%20%20def%20linearfit_1(x%2C%20y%2C%20yerr)%3A%0A%20%20%20%20%20%20%20%20%22%22%22Linear%20fit%20of%20x%20and%20y%20with%20uncertainty%20and%20plots%20results.%22%22%22%0A%20%20%20%20%20%20%20%20(x%2C%20y)%20%3D%20(np.asarray(x)%2C%20np.asarray(y))%0A%20%20%20%20%20%20%20%20n%20%3D%20y.size%0A%20%20%20%20%20%20%20%20(p%2C%20_cov)%20%3D%20np.polyfit(x%2C%20y%2C%201%2C%20w%3D1%20%2F%20yerr%2C%20cov%3DTrue)%0A%20%20%20%20%20%20%20%20yfit%20%3D%20np.polyval(p%2C%20x)%0A%20%20%20%20%20%20%20%20perr%20%3D%20np.sqrt(np.diag(_cov))%0A%20%20%20%20%20%20%20%20R2%20%3D%20np.corrcoef(x%2C%20y)%5B0%2C%201%5D%20**%202%0A%20%20%20%20%20%20%20%20resid%20%3D%20y%20-%20yfit%0A%20%20%20%20%20%20%20%20chi2red%20%3D%20np.sum((resid%20%2F%20yerr)%20**%202)%20%2F%20(n%20-%202)%0A%20%20%20%20%20%20%20%20s_err%20%3D%20np.sqrt(np.sum(resid%20**%202)%20%2F%20(n%20-%202))%0A%20%20%20%20%20%20%20%20t%20%3D%20stats.t.ppf(0.975%2C%20n%20-%202)%0A%20%20%20%20%20%20%20%20ci%20%3D%20t%20*%20s_err%20*%20np.sqrt(1%20%2F%20n%20%2B%20(x%20-%20np.mean(x))%20**%202%20%2F%20np.sum((x%20-%20np.mean(x))%20**%202))%0A%20%20%20%20%20%20%20%20pi%20%3D%20t%20*%20s_err%20*%20np.sqrt(1%20%2B%201%20%2F%20n%20%2B%20(x%20-%20np.mean(x))%20**%202%20%2F%20np.sum((x%20-%20np.mean(x))%20**%202))%0A%20%20%20%20%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20%20%20%20%20plt.fill_between(x%2C%20yfit%20%2B%20pi%2C%20yfit%20-%20pi%2C%20color%3D%5B1%2C%200%2C%200%2C%200.1%5D%2C%20edgecolor%3DNone)%0A%20%20%20%20%20%20%20%20plt.fill_between(x%2C%20yfit%20%2B%20ci%2C%20yfit%20-%20ci%2C%20color%3D%5B1%2C%200%2C%200%2C%200.15%5D%2C%20edgecolor%3DNone)%0A%20%20%20%20%20%20%20%20plt.errorbar(x%2C%20y%2C%20yerr%3Dyerr%2C%20fmt%3D'bo'%2C%20ecolor%3D'b'%2C%20capsize%3D0)%0A%20%20%20%20%20%20%20%20plt.plot(x%2C%20yfit%2C%20linewidth%3D3%2C%20color%3D%5B1%2C%200%2C%200%2C%200.8%5D)%0A%20%20%20%20%20%20%20%20plt.xlabel('x'%2C%20fontsize%3D16)%0A%20%20%20%20%20%20%20%20plt.ylabel('y'%2C%20fontsize%3D16)%0A%20%20%20%20%20%20%20%20plt.title('%24y%20%3D%20%25.2f%20%5C%5Cpm%20%25.2f%20%2B%20(%25.2f%20%5C%5Cpm%20%25.2f)x%20%5C%5C%3B%20%5BR%5E2%3D%25.2f%2C%5C%5C%2C%20%5C%5Cchi%5E2_%7Bred%7D%3D%25.1f%5D%24'%20%25%20(p%5B1%5D%2C%20perr%5B1%5D%2C%20p%5B0%5D%2C%20perr%5B0%5D%2C%20R2%2C%20chi2red)%2C%20fontsize%3D20%2C%20color%3D%5B0%2C%200%2C%200%5D)%0A%20%20%20%20%20%20%20%20plt.xlim((0%2C%20n%20%2B%201))%0A%20%20%20%20%20%20%20%20plt.show()%0A%0A%20%20%20%20return%20(linearfit_1%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20And%20an%20example%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(linearfit_1%2C%20np)%3A%0A%20%20%20%20n_1%20%3D%2020%0A%20%20%20%20x_3%20%3D%20np.arange(1%2C%20n_1%20%2B%201)%0A%20%20%20%20y_3%20%3D%20x_3%20%2B%205%20*%20np.random.randn(n_1)%0A%20%20%20%20yerr_2%20%3D%20np.abs(4%20*%20np.random.randn(n_1))%20%2B%202%0A%20%20%20%20linearfit_1(x_3%2C%20y_3%2C%20yerr_2)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%205.**%20At%20the%20same%20x%20and%20confidence%20level%2C%20which%20is%20wider%3A%20a%20confidence%20interval%20for%20the%20fitted%20mean%20or%20a%20prediction%20interval%20for%20a%20new%20observation%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%205%0A%20%20%20%20The%20prediction%20interval%20includes%20the%20mean-estimation%20variance%20**plus**%20the%20new%20observation%E2%80%99s%20variance%2C%20so%20it%20is%20wider.%20Near%20the%20average%20x%20the%20fitted%20mean%20is%20usually%20best%20constrained.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Non-linear%20curve%20fitting%0A%0A%20%20%20%20A%20more%20general%20curve%20fitting%20function%20is%20the%20%60scipy.optimize.curve_fit%60%3A%0A%0A%20%20%20%20%20%20%20%20scipy.optimize.curve_fit(f%2C%20xdata%2C%20ydata%2C%20p0%3DNone%2C%20sigma%3DNone%2C%20**kw)%5Bsource%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20Use%20non-linear%20least%20squares%20to%20fit%20a%20function%2C%20f%2C%20to%20data.%0A%0A%20%20%20%20For%20the%20%60curve_fit%60%20funcion%2C%20we%20need%20to%20define%20a%20model%20(e.g.%2C%20a%20mathematical%20expression)%20for%20the%20fit%3A%0A%0A%20%20%20%20%20%20%20%20f%20%3A%20callable%0A%20%20%20%20%20%20%20%20The%20model%20function%2C%20f(x%2C%20...).%20It%20must%20take%20the%20independent%20variable%20as%20the%20first%20argument%20and%20the%20parameters%20to%20fit%20as%20separate%20remaining%20arguments.%0A%0A%20%20%20%20Let's%20create%20a%20gaussian%20curve%20as%20model%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Import%20the%20necessary%20libraries%0A%20%20%20%20from%20scipy.optimize%20import%20curve_fit%0A%20%20%20%20from%20IPython.display%20import%20display%2C%20Math%0A%20%20%20%20%23%20'%25matplotlib%20inline'%20command%20supported%20automatically%20in%20marimo%0A%20%20%20%20return%20Math%2C%20curve_fit%2C%20display%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Define%20the%20function%20for%20curve%20fitting%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Math%2C%20display%2C%20np)%3A%0A%20%20%20%20def%20func(x%2C%20a%2C%20b%2C%20c%2C%20d)%3A%0A%20%20%20%20%20%20%20%20%23%20Gauss%20function%0A%20%20%20%20%20%20%20%20return%20a*np.exp(-(x-b)**2%2F(2*c**2))%20%2B%20d%0A%0A%20%20%20%20display(Math(%20r'y%20%3D%20a%20*%20exp%5Cleft(-%5Cfrac%7B(x-b)%5E2%7D%7B2c%5E2%7D%5Cright)%20%2B%20d'%20))%0A%20%20%20%20return%20(func%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Generate%20numeric%20data%20to%20be%20fitted%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(func%2C%20np)%3A%0A%20%20%20%20x_4%20%3D%20np.linspace(0%2C%208%2C%20101)%0A%20%20%20%20noise%20%3D%20np.random.randn(len(x_4))%20%2B%201%0A%20%20%20%20y_4%20%3D%20func(x_4%2C%2010%2C%204%2C%201%2C%20np.mean(noise))%20%2B%20noise%0A%20%20%20%20yerr_3%20%3D%20np.abs(np.random.randn(len(x_4)))%20%2B%201%0A%20%20%20%20return%20x_4%2C%20y_4%2C%20yerr_3%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Perform%20the%20curve%20fitting%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Math%2C%20curve_fit%2C%20display%2C%20func%2C%20np%2C%20x_4%2C%20y_4%2C%20yerr_3)%3A%0A%20%20%20%20(p_4%2C%20_cov)%20%3D%20curve_fit(func%2C%20x_4%2C%20y_4%2C%20sigma%3Dyerr_3)%0A%20%20%20%20yfit_2%20%3D%20func(x_4%2C%20p_4%5B0%5D%2C%20p_4%5B1%5D%2C%20p_4%5B2%5D%2C%20p_4%5B3%5D)%0A%20%20%20%20perr_4%20%3D%20np.sqrt(np.diag(_cov))%0A%20%20%20%20resid_4%20%3D%20y_4%20-%20yfit_2%0A%20%20%20%20chi2red_4%20%3D%20np.sum((resid_4%20%2F%20yerr_3)%20**%202)%20%2F%20(y_4.size%20-%204)%0A%20%20%20%20print('Fitted%20parameters%3A')%0A%20%20%20%20display(Math('a%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_4%5B0%5D%2C%20perr_4%5B0%5D)))%0A%20%20%20%20display(Math('b%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_4%5B1%5D%2C%20perr_4%5B1%5D)))%0A%20%20%20%20display(Math('c%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_4%5B2%5D%2C%20perr_4%5B2%5D)))%0A%20%20%20%20display(Math('d%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_4%5B3%5D%2C%20perr_4%5B3%5D)))%0A%20%20%20%20display(Math('%5C%5Cchi%5E2_%7Bred%7D%3D%25.2f'%20%25%20chi2red_4))%0A%20%20%20%20return%20(yfit_2%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Plot%20data%20and%20fitted%20curve%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plt%2C%20x_4%2C%20y_4%2C%20yerr_3%2C%20yfit_2)%3A%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20plt.errorbar(x_4%2C%20y_4%2C%20yerr%3Dyerr_3%2C%20fmt%3D'bo'%2C%20ecolor%3D'b'%2C%20capsize%3D0)%0A%20%20%20%20plt.plot(x_4%2C%20yfit_2%2C%20linewidth%3D3%2C%20color%3D%5B1%2C%200%2C%200%2C%200.5%5D)%0A%20%20%20%20plt.xlabel('x'%2C%20fontsize%3D16)%0A%20%20%20%20plt.ylabel('y'%2C%20fontsize%3D16)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Evidently%20this%20is%20not%20correct.%0A%0A%20%20%20%20We%20need%20to%20enter%20an%20initial%20guess%20for%20the%20parameters%20(%60p0%60)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Math%2C%20curve_fit%2C%20display%2C%20func%2C%20np%2C%20plt%2C%20x_4%2C%20y_4%2C%20yerr_3)%3A%0A%20%20%20%20(p_5%2C%20_cov)%20%3D%20curve_fit(func%2C%20x_4%2C%20y_4%2C%20p0%3D%5B1%2C%204%2C%201%2C%201%5D%2C%20sigma%3Dyerr_3)%0A%20%20%20%20yfit_3%20%3D%20func(x_4%2C%20p_5%5B0%5D%2C%20p_5%5B1%5D%2C%20p_5%5B2%5D%2C%20p_5%5B3%5D)%0A%20%20%20%20perr_5%20%3D%20np.sqrt(np.diag(_cov))%0A%20%20%20%20resid_5%20%3D%20y_4%20-%20yfit_3%0A%20%20%20%20chi2red_5%20%3D%20np.sum((resid_5%20%2F%20yerr_3)%20**%202)%20%2F%20(y_4.size%20-%203)%0A%20%20%20%20print('Fitted%20parameters%3A')%0A%20%20%20%20display(Math('a%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_5%5B0%5D%2C%20perr_5%5B0%5D)))%0A%20%20%20%20display(Math('b%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_5%5B1%5D%2C%20perr_5%5B1%5D)))%0A%20%20%20%20display(Math('c%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_5%5B2%5D%2C%20perr_5%5B2%5D)))%0A%20%20%20%20display(Math('d%3D%25.2f%20%5C%5Cpm%20%25.2f'%20%25%20(p_5%5B3%5D%2C%20perr_5%5B3%5D)))%0A%20%20%20%20display(Math('%5C%5Cchi%5E2_%7Bred%7D%3D%25.2f'%20%25%20chi2red_5))%0A%20%20%20%20plt.figure(figsize%3D(10%2C%205))%0A%20%20%20%20plt.errorbar(x_4%2C%20y_4%2C%20yerr%3Dyerr_3%2C%20fmt%3D'bo'%2C%20ecolor%3D'b'%2C%20capsize%3D0)%0A%20%20%20%20plt.plot(x_4%2C%20yfit_3%2C%20linewidth%3D3%2C%20color%3D%5B1%2C%200%2C%200%2C%200.5%5D)%0A%20%20%20%20plt.xlabel('x'%2C%20fontsize%3D16)%0A%20%20%20%20plt.ylabel('y'%2C%20fontsize%3D16)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%20(resid_5%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20For%20the%20interval%20interpretation%20used%20here%2C%20inspect%20whether%20the%20residuals%20are%20consistent%20with%20independent%20errors%20of%20the%20assumed%20spread%20and%20an%20approximately%20normal%20distribution.%20Least-squares%20coefficients%20can%20be%20computed%20without%20normality%3B%20exact%20normal-theory%20intervals%20need%20stronger%20assumptions%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(plot_resid%2C%20resid_5%2C%20x_4)%3A%0A%20%20%20%20plot_resid(x_4%2C%20resid_5)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%206.**%20Why%20can%20changing%20the%20initial%20guess%20rescue%20the%20Gaussian%20fit%3F%20Does%20convergence%20prove%20the%20model%20is%20correct%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%206%0A%20%20%20%20A%20nonlinear%20solver%20searches%20from%20its%20starting%20parameters%20and%20can%20reach%20a%20poor%20local%20region%20or%20a%20nearly%20flat%20direction.%20A%20plausible%20initial%20center%20and%20width%20help.%20Convergence%20only%20reports%20a%20numerical%20stopping%20condition%3B%20inspect%20residuals%2C%20parameter%20plausibility%2C%20and%20sensitivity%20to%20starting%20values.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20What%20does%20least%20squares%20minimize%3F%0A%20%20%20%202.%20Does%20setting%20unknown%20SDs%20to%20one%20make%20them%20measured%20uncertainties%3F%0A%20%20%20%203.%20Does%20high%20%24R%5E2%24%20prove%20a%20causal%20model%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20A%20sum%20of%20squared%20residuals%2C%20optionally%20standardized%20by%20uncertainties.%0A%20%20%20%202.%20No.%0A%20%20%20%203.%20No.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20Fit%20the%20exact%20points%20(0%2C2)%2C%20(1%2C5)%2C%20(2%2C8).%0A%20%20%20%202.%20A%20fit%20with%2020%20data%20points%20and%20three%20fitted%20parameters%20has%20chi-square%2034.%20Give%20the%20reduced%20chi-square.%0A%20%20%20%203.%20State%20two%20checks%20beyond%20the%20optimizer%E2%80%99s%20success%20flag.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%20%24y%3D2%2B3x%24.%0A%20%20%20%202.%202.%0A%20%20%20%203.%20Residual%20structure%20and%20parameter%20plausibility%3B%20sensitivity%20to%20starting%20guesses%20is%20another%20valid%20check.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20%5BPolynomial%20fitting%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fpolynomial_fitting.py)%20extends%20the%20line%20to%20more%20powers%20of%20x.%20%5BSciPy%20curve_fit%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.optimize.curve_fit.html)%20explains%20%60sigma%60%2C%20covariance%2C%20and%20%60absolute_sigma%60.%0A%0A%20%20%20%20%23%23%23%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20%5BMIT%20Statistics%20for%20Applications%20lectures%5D(https%3A%2F%2Focw.mit.edu%2Fcourses%2F18-650-statistics-for-applications-fall-2016%2Fvideo_galleries%2Flecture-videos%2F)%20%E2%80%94%20the%20estimation%20and%20regression%20lectures%20explain%20uncertainty%20in%20fitted%20parameters%20and%20predictions.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20References%0A%0A%20%20%20%20-%20%5BBevington%20and%20Robinson%20(2002)%20Data%20Reduction%20and%20Error%20Analysis%20for%20the%20Physical%20Science.%20McGraw-Hill%20Science%2FEngineering%2FMath%3B%203rd%20edition%5D(https%3A%2F%2Fwww.mcgraw-hill.co.uk%2Fhtml%2F0071199268.html).%0A%0A%20%20%20%20-%20%5BPress%20et%20al.%20(2007)%20Numerical%20Recipes%203rd%20Edition%3A%20The%20Art%20of%20Scientific%20Computing.%20Cambridge%20University%20Press%5D(http%3A%2F%2Fwww.nr.com%2F).%0A%0A%20%20%20%20-%20%5BMontgomery%20(2013)%20Applied%20Statistics%20and%20Probability%20for%20Engineers.%20John%20Wiley%20%26%20Sons%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3D_f4KrEcNAfEC).%0A%0A%20%20%20%20-%20%5BNIST%2FSEMATECH%20e-Handbook%20of%20Statistical%20Methods%5D(http%3A%2F%2Fwww.itl.nist.gov%2Fdiv898%2Fhandbook%2Fpri%2Fsection2%2Fpri24.htm)%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
62da8d9e3c382c31e93c8bcd7bfe3340