%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%22scipy%3E%3D1.14%22%2C%20%22urllib3%3E%3D2.2%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%20Prediction%20ellipse%20and%20prediction%20ellipsoid%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%20Two%20coordinates%20can%20vary%20together%3A%20a%20cloud%20that%20is%20long%20and%20tilted%20is%20not%20well%20described%20by%20two%20independent%20error%20bars.%20This%20guide%20builds%20prediction%20regions%20from%20the%20covariance%20matrix%2C%20checks%20their%20finite-sample%20scale%2C%20and%20extends%20the%20same%20geometry%20from%20an%20ellipse%20to%20an%20ellipsoid.%20Read%20the%20univariate%20interval%20guide%20first.%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.**%20A%20cloud%20of%20points%20lies%20close%20to%20the%20line%20y%3Dx.%20Would%20a%20circular%20boundary%20or%20a%20tilted%20ellipse%20describe%20its%20dispersion%20more%20efficiently%3F%20What%20information%20determines%20the%20tilt%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%20and%20SciPy%20calculate%20covariance%20and%20probability%20quantiles%3B%20Matplotlib%20draws%20the%20two-%20and%20three-dimensional%20examples.%20The%20repository%20helper%20%60hyperellipsoid.py%60%20supplies%20the%20geometry.%20Samples%20are%20simulated.%20Optional%20animation%20encoding%20needs%20external%20software%20only%20if%20you%20explicitly%20save%20a%20movie%3B%20calculations%20and%20static%20figures%20do%20not.%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%20To%20describe%20the%20dispersion%20of%20data%20or%20to%20make%20a%20prediction%20for%20a%20sample%2C%20we%20can%20calculate%20a%20%5Bprediction%20interval%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPrediction_interval)%20or%20%5Btolerance%20interval%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FTolerance_interval).%20For%20the%20specific%20case%20of%20a%20univariate%20random%20variable%2C%20see%20%5BConfidence%20and%20prediction%20intervals%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fconfidence_prediction_intervals.py).%20For%20a%20multivariate%20random%20variable%2C%20we%20need%20to%20calculate%20a%20prediction%20hyperellipsoid%20(%5BChew%2C%201966%5D(http%3A%2F%2Fwww.jstor.org%2Fstable%2F2282774))%3A%0A%0A%20%20%20%20%3E%20The%2095%25%20prediction%20hyperellipsoid%20is%20a%20prediction%20interval%20for%20a%20sample%20of%20a%20multivariate%20random%20variable%20such%20that%20there%20is%2095%25%20of%20probability%20that%20a%20new%20observation%20will%20lie%20inside%20the%20hyperellipsoid.%0A%0A%20%20%20%20The%2095%25%20prediction%20interval%20(a%20p-dimensional%20hyperellipsoid)%20for%20a%20sample%20of%20a%20multivariate%20random%20variable%20with%20%24p%24%20dimensions%20and%20size%20%24n%24%20from%20a%20population%20with%20normal%20distribution%20is%20given%20by%20the%20following%20probabilistic%20bounds%20which%20holds%2095%25%20of%20the%20time%20(Chew%2C%201966)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)'%5C%3A%20S%5E%7B-1%7D%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)%20%5Cleq%20%5Cfrac%7BF_%7B(0.95%2C%5C%3A%20p%2C%5C%3A%20n-p)%7D(n-1)%5C%3Ap%5C%3A(n%2B1)%7D%7Bn%5C%3A(n-p)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24x_%7Bn%2B1%7D%24%20(with%20dimension%20%24p%24)%20is%20the%20new%20observation%20for%20which%20we%20want%20to%20calculate%20the%20prediction%20interval%3B%20%24F%24%20is%20the%20%5BF-distribution%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FF-distribution)%3B%20%24%5Cbar%7Bx%7D%24%20is%20the%20sample%20mean%20(with%20dimension%20%24p%24)%2C%20and%20%24S%24%20is%20the%20%5Bsample%20covariance%20matrix%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSample_mean_and_sample_covariance)%20defined%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20S_%7Bj%2Ck%7D%20%3D%20%5Cfrac%7B1%7D%7Bn-1%7D%5Csum_%7Bi%3D1%7D%5E%7Bn%7D(x_%7Bij%7D-%5Cbar%7Bx%7D_j)(x_%7Bik%7D-%5Cbar%7Bx%7D_k)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20is%20the%20formula%20for%20the%20prediction%20of%20the%20next%20single%20observation%20where%20the%20population%20mean%20and%20variance%20are%20unknown%20(we%20only%20have%20estimates%20of%20these%20parameters%20from%20the%20sample).%20This%20formula%20is%20also%20equivalent%20to%20the%20formula%20for%20calculation%20of%20a%20tolerance%20region%20(called%20Type%202%20tolerance%20region%20by%20%5BChew%20(1966)%5D(http%3A%2F%2Fwww.jstor.org%2Fstable%2F2282774))%3A%0A%0A%20%20%20%20%3E%20The%2095%25%20tolerance%20hyperellipsoid%20is%20a%20tolerance%20region%20for%20a%20sample%20of%20a%20multivariate%20random%20variable%20such%20that%20the%20average%20or%20expected%20value%20of%20the%20proportion%20of%20the%20population%20contained%20in%20the%20hyperellipsoid%20is%2095%25.%0A%0A%20%20%20%20In%20this%20context%2C%20we%20can%20refer%20to%20the%20calculated%20hyperellipsoid%20as%20a%20prediction%20or%20tolerance%20hyperellipsoid%20(but%20prediction%20and%20tolerance%20intervals%20are%20in%20general%20different%20concepts%20in%20statistics).%0A%0A%20%20%20%20The%20function%20%60hyperellipsoid.py%60%20(code%20at%20the%20end%20of%20this%20text)%20calculates%20the%20prediction%20or%20tolerance%20hyperellipsoid%2C%20some%20related%20parameters%2C%20and%20plots%20the%20results%20for%20a%20given%20multivariate%20random%20variable.%20The%20function%20signature%20is%3A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20hypervol%2C%20saxes%2C%20angles%2C%20p0%2C%20R%20%3D%20hyperellipsoid(P%2C%20y%3DNone%2C%20z%3DNone%2C%20pvalue%3D.95%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20units%3DNone%2C%20show%3DTrue%2C%20ax%3DNone)%3A%0A%20%20%20%20%60%60%60%0A%20%20%20%20Before%20showing%20how%20to%20use%20this%20code%2C%20let's%20look%20a%20bit%20more%20at%20the%20statistical%20principles%20behind%20the%20estimation%20of%20a%20prediction%20or%20tolerance%20interval.%0A%0A%20%20%20%20%3C!--%20TEASER_END%20--%3E%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%20What%20the%20region%20predicts%0A%0A%20%20%20%20The%20stated%20finite-sample%20F%20formula%20assumes%20independent%20observations%20from%20one%20multivariate%20normal%20population%2C%20with%20%24n%3Ep%24%20and%20a%20nonsingular%20sample%20covariance.%20It%20concerns%20a%20future%20independent%20observation%2C%20not%20a%20confidence%20region%20for%20the%20population%20mean.%20A%2095%25%20prediction%20procedure%20has%2095%25%20joint%20coverage%20over%20repeated%20training%20samples%20and%20future%20observations%20under%20this%20model%3B%20the%20fraction%20of%20the%20training%20points%20inside%20is%20not%20forced%20to%20be%2095%25.%0A%0A%20%20%20%20A%20covariance%20matrix%20from%20collinear%20points%20is%20singular.%20That%20is%20a%20geometric%20issue%2C%20not%20something%20fixed%20by%20asking%20an%20inverse%20routine%20to%20try%20harder.%20Reduce%20the%20model%20dimension%20or%20investigate%20the%20measurement%20design%20before%20applying%20the%20full-dimensional%20formula.%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%20Univariate%20variable%0A%0A%20%20%20%20For%20%24p%24%20%3D%201%20(univariate%20case)%20the%2095%25%20prediction%20interval%20formula%20above%20reduces%20to%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%7C%5Cfrac%7Bx_%7Bn%2B1%7D-%5Cbar%7Bx%7D%7D%7Bs%7D%5Cright%7C%20%5Cleq%20%5Csqrt%7BF_%7B(0.95%2C%5C%3A%201%2C%5C%3A%20n-1)%7D(1%2B1%2Fn)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24s%24%20is%20the%20square%20root%20of%20the%20sample%20covariance%2C%20the%20sample%20standard%20deviation.%0A%0A%20%20%20%20And%20considering%20that%20%24F_%7B(0.95%2C%5C%3A%201%2C%5C%3A%20n-1)%7D%24%20is%20equal%20to%20%24T%5E2_%7B(0.975%2C%5C%3A%20n-1)%7D%24%2C%20the%20square%20of%20the%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FStudent's_t-distribution%22%3EStudent's%20t-distribution%3C%2Fa%3E%2C%20finally%20we%20have%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%7Cx_%7Bn%2B1%7D-%5Cbar%7Bx%7D%5Cright%7C%20%5Cleq%20s%5C%3AT_%7B(0.975%2C%5C%3A%20n-1)%7D%5Csqrt%7B1%2B1%2Fn%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Indeed%2C%20this%20results%20in%20the%2095%25%20prediction%20interval%20used%20for%20a%20sample%20of%20a%20univariate%20variable%2C%20see%20%5BConfidence%20and%20prediction%20intervals%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fconfidence_prediction_intervals.py).%0A%0A%20%20%20%20For%20large%20%24n%24%2C%20the%20relevant%20reference%20distributions%20have%20the%20limits%20%24T(%5Cinfty)%3DN%24%2C%20where%20%24N%24%20is%20the%20%5Bnormal%20distribution%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FNormal_distribution)%2C%20and%20the%20above%20statement%20can%20be%20simplified%20to%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%7Cx_%7Bn%2B1%7D-%5Cbar%7Bx%7D%5Cright%7C%20%5Cleq%20s%5C%3AN_%7B0.975%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24N_%7B0.975%7D%24%20is%20equal%20to%201.96%2C%20a%20commonly%20used%20value%20for%20calculating%2095%25-probability%20intervals.%3Cbr%3E%0A%20%20%20%20Let's%20calculate%20the%20error%20of%20this%20approximation%20for%20different%20sample%20sizes.%3Cbr%3E%0A%20%20%20%20First%20let's%20import%20the%20necessary%20Python%20libraries%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%20from%20scipy%20import%20stats%0A%0A%20%20%20%20return%20np%2C%20stats%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20stats)%3A%0A%20%20%20%20F%20%3D%20lambda%20p%2C%20n%3A%20np.sqrt(stats.f.ppf(0.95%2C%20p%2C%20n-p)*(n-1)*p*(n%2B1)%2Fn%2F(n-p))%20%20%23%20F%20distribution%0A%20%20%20%20N%20%3D%20stats.norm.ppf(0.975)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20Normal%20distribution%0A%20%20%20%20for%20i%20in%20%5B1000%2C%20100%2C%2030%2C%2010%5D%3A%0A%20%20%20%20%20%20%20%20print('%5CnApproximation%20error%20for%20n%20%3D%20%25d'%20%25i)%0A%20%20%20%20%20%20%20%20print('Using%20Normal%20distribution%3A%20%25.1f%25%25'%20%25%20(100*(N-F(1%2C%20i))%2FF(1%2C%20i)))%0A%20%20%20%20return%20(F%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%20n%3D1000%2C%20the%20approximation%20is%20probably%20good%20enough%2C%20for%20n%3D10%20it%20is%20bad%2C%20and%20it%20always%20underestimates.%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.**%20Why%20does%20the%20one-dimensional%20prediction%20interval%20contain%20%24%5Csqrt%7B1%2B1%2Fn%7D%24%20instead%20of%20only%20%241%2F%5Csqrt%20n%24%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20The%20future%20observation%20has%20its%20own%20variability%20in%20addition%20to%20uncertainty%20in%20the%20estimated%20mean.%20The%20extra%201%20represents%20that%20observation%20variance.%20Without%20it%20the%20interval%20would%20target%20the%20mean.%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%20Bivariate%20variable%0A%0A%20%20%20%20For%20%24p%24%20%3D%202%20(bivariate%20case)%20the%2095%25%20prediction%20interval%20formula%20above%20reduces%20to%20(again%2C%20these%20probabilistic%20bounds%20hold%2095%25%20of%20the%20time%20(%5BChew%2C%201966%5D(http%3A%2F%2Fwww.jstor.org%2Fstable%2F2282774))%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)'%5C%3A%20S%5E%7B-1%7D%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)%20%5Cleq%20%5Cfrac%7B2(n-1)(n%2B1)%7D%7Bn%5C%3A(n-2)%7DF_%7B(0.95%2C%5C%3A%202%2C%5C%3A%20n-2)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20formula%20is%20also%20equivalent%20to%20the%20formula%20for%20calculation%20of%20a%20tolerance%20region%20such%20that%20the%20average%20or%20expected%20value%20of%20the%20proportion%20of%20the%20population%20contained%20in%20this%20region%20is%20exactly%2095%25%20(called%20Type%202%20tolerance%20region%20by%20%5BChew%20(1966)%5D(http%3A%2F%2Fwww.jstor.org%2Fstable%2F2282774)).%0A%0A%20%20%20%20For%20large%20%24n%24%2C%20the%20relevant%20reference%20distributions%20have%20the%20limits%20%242F(2%2C%5Cinfty)%3D%5Cchi%5E2(2)%24%2C%20where%20%24%5Cchi%5E2%24%20is%20the%20%5Bchi-square%20distribution%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FChi-squared_distribution)%2C%20and%20the%20formula%20above%20for%20the%2095%25%20prediction%20interval%20for%20the%20bivariate%20case%20can%20be%20simplified%20to%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)'%5C%3A%20S%5E%7B-1%7D%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)%20%5Cleq%20%5Cchi%5E2_%7B0.95%2C%5C%3A%202%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Or%2C%20equivalently%2C%20we%20can%20use%20the%20fact%20that%20%24%5Csqrt%7B2F(2%2C%5Cinfty)%7D%3DR%24%2C%20where%20%24R%24%20is%20the%20%5BRayleigh%20distribution%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRayleigh_distribution)%2C%20and%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)'%5C%3A%20S%5E%7B-1%7D%20(x_%7Bn%2B1%7D-%5Cbar%7Bx%7D)%20%5Cleq%20(R_%7B0.95%7D)%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Let's%20calulate%20the%20error%20of%20this%20approximation%20for%20different%20sample%20sizes%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(F%2C%20np%2C%20stats)%3A%0A%20%20%20%20C%20%3D%20lambda%20p%3A%20np.sqrt(stats.chi2.ppf(0.95%2C%20p))%0A%20%20%20%20R%20%3D%20stats.rayleigh.ppf(0.95)%0A%20%20%20%20for%20i_1%20in%20%5B1000%2C%20100%2C%2030%2C%2010%5D%3A%0A%20%20%20%20%20%20%20%20print('%5CnApproximation%20error%20for%20n%20%3D%20%25d'%20%25%20i_1)%0A%20%20%20%20%20%20%20%20print('Using%20Chi-square%20distribution%3A%20%25.1f%25%25'%20%25%20(100%20*%20(C(2)%20-%20F(2%2C%20i_1))%20%2F%20F(2%2C%20i_1)))%0A%20%20%20%20%20%20%20%20print('Using%20Rayleigh%20distribution%3A%20%25.1f%25%25'%20%25%20(100%20*%20(R%20-%20F(2%2C%20i_1))%20%2F%20F(2%2C%20i_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%20For%20n%3D1000%2C%20the%20approximation%20is%20probably%20good%20enough%2C%20for%20n%3D10%20it%20is%20bad%2C%20and%20it%20always%20underestimates.%3Cbr%3E%0A%20%20%20%20To%20read%20more%20about%20multivariate%20prediction%20and%20confidence%20intervals%2C%20see%20%5BChew%20(1966)%5D(http%3A%2F%2Fwww.jstor.org%2Fstable%2F2282774)%2C%20and%20about%20the%20use%20of%20prediction%20ellipse%20in%20the%20field%20of%20posturography%2C%20see%20%5BSchubert%20and%20Kirchner%20(2014)%5D(http%3A%2F%2Fwww.sciencedirect.com%2Fscience%2Farticle%2Fpii%2FS0966636213005961).%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.**%20For%20covariance%20%24S%3D%5Coperatorname%7Bdiag%7D(4%2C1)%24%20and%20threshold%20%24c%3D9%24%2C%20find%20the%20prediction%20ellipse%E2%80%99s%20semiaxes%20and%20area.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20The%20eigenvectors%20are%20the%20coordinate%20axes.%20The%20semiaxes%20are%20%24%5Csqrt%7B9(4)%7D%3D6%24%20and%20%24%5Csqrt%7B9(1)%7D%3D3%24.%20The%20area%20is%20%24%5Cpi(6)(3)%3D18%5Cpi%24.%20The%20threshold%20is%20stipulated%20here%3B%20it%20is%20not%20claimed%20to%20be%20a%2095%25%20threshold.%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%20A%20Python%20code%20to%20compute%20the%20hyperellipsoid%0A%0A%20%20%20%20We%20can%20write%20a%20code%20to%20calculate%20the%20ellipse%2C%20ellipsoid%2C%20and%20in%20a%20more%20general%20case%2C%20the%20hyperellipsoid%20using%20the%20formula%20for%20the%20prediction%20hyperellipsoid%20(this%20code%20is%20published%20in%20Duarte%20(2015)).%20The%20directions%20and%20lengths%20of%20the%20hyperellipsoid%20semi-axes%20are%20found%2C%20respectively%2C%20as%20the%20eigenvectors%20and%20square%20roots%20of%20the%20scaled%20eigenvalues%20of%20the%20covariance%20matrix%20of%20the%20data%20using%20the%20concept%20of%20%5Bprincipal%20components%20analysis%20(PCA)%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPrincipal_component_analysis)%20or%20%5Bsingular%20value%20decomposition%20(SVD)%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSingular_value_decomposition)%20and%20the%20length%20of%20the%20semi-axes%20are%20adjusted%20to%20account%20for%20the%20necessary%20prediction%20probability.%20The%20volume%20of%20the%20hyperellipsoid%20is%20calculated%20with%20the%20same%20equation%20for%20the%20%5Bvolume%20of%20a%20n-dimensional%20ball%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FVolume_of_an_n-ball)%20with%20the%20radius%20replaced%20by%20the%20semi-axes%20of%20the%20hyperellipsoid.%3Cbr%3E%0A%20%20%20%20In%20Python%2C%20such%20code%20to%20compute%20the%20hypervolume%20of%20the%2095%25%20prediction%20hyperellipsoid%20is%3A%0A%20%20%20%20%60%60%60Python%0A%20%20%20%20import%20numpy%20as%20np%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20import%20Numpy%20package%0A%20%20%20%20from%20scipy.stats%20import%20f%20as%20F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20import%20F%20distribution%0A%20%20%20%20from%20scipy.special%20import%20gamma%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20import%20Gamma%20function%0A%20%20%20%20n%2C%20p%20%3D%20np.asarray(data).shape%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%202-D%20array%20dimensions%0A%20%20%20%20cov%20%3D%20np.cov(data%2C%20rowvar%3D0)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20covariance%20matrix%20of%20data%0A%20%20%20%20U%2C%20s%2C%20Vt%20%3D%20np.linalg.svd(cov)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20singular%20value%20decomposition%0A%20%20%20%20f95%20%3D%20F.ppf(.95%2Cp%2Cn-p)*(n-1)*p*(n%2B1)%2Fn%2F(n-p)%20%20%23%20F%2095%20percent%20point%20function%0A%20%20%20%20saxes%20%3D%20np.sqrt(s*f95)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20semi-axes%20lengths%0A%20%20%20%20hypervolume%20%3D%20np.pi**(p%2F2)%2Fgamma(p%2F2%2B1)*np.prod(saxes)%0A%20%20%20%20hypervolume%0A%20%20%20%20%60%60%60%0A%0A%20%20%20%20For%20instance%2C%20if%20%60data%60%20has%20two%20columns%2C%20%60hyperellipsoid%60%20will%20contain%20the%20area%20of%20the%2095%25%20prediction%20ellipse.%0A%0A%20%20%20%20%23%23%23%20Equivalent%20code%20for%20a%20Matlab-like%20environment%0A%0A%20%20%20%20The%20equivalent%20code%20for%20a%20Matlab-like%20environment%20to%20compute%20the%20hypervolume%20of%20the%2095%25%20prediction%20hyperellipsoid%20is%3A%0A%20%20%20%20%60%60%60Matlab%0A%20%20%20%20%5Bn%2C%20p%5D%20%3D%20size(data)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%25%202-D%20array%20dimensions%0A%20%20%20%20covar%20%3D%20cov(data)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%25%20covariance%20matrix%20of%20data%0A%20%20%20%20%5BU%2C%20S%2C%20V%5D%20%3D%20svd(covar)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%25%20singular%20value%20decomposition%0A%20%20%20%20f95%20%3D%20finv(.95%2Cp%2Cn-p)*(n-1)*p*(n%2B1)%2Fn%2F(n-p)%3B%20%20%25%20F%2095%20percent%20point%20function%0A%20%20%20%20saxes%20%3D%20sqrt(diag(S)*f95)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%25%20semi-axes%20lengths%0A%20%20%20%20hypervolume%20%3D%20pi%5E(p%2F2)%2Fgamma(p%2F2%2B1)*prod(saxes)%3B%0A%20%20%20%20hypervolume%0A%20%20%20%20%60%60%60%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%20Python%20function%20hyperellipsoid.py%0A%0A%20%20%20%20Let's%20see%20now%20a%20more%20complete%20function%2C%20%60hyperellipsoid.py%60%20(code%20at%20the%20end%20of%20this%20text)%2C%20to%20compute%20the%20prediction%20hyperellipsoid%20and%20related%20variables%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20try%3A%0A%20%20%20%20%20%20%20%20from%20notebook_resources%20import%20repo_file%0A%20%20%20%20except%20ModuleNotFoundError%20as%20_error%3A%0A%20%20%20%20%20%20%20%20if%20_error.name%20!%3D%20%22notebook_resources%22%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%0A%20%20%20%20%20%20%20%20raise%20ModuleNotFoundError(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22This%20notebook%20needs%20notebook_resources.py.%20Download%20it%20from%3A%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22https%3A%2F%2Fraw.githubusercontent.com%2FBMClab%2FBMC%2Fmaster%2Ffunctions%2Fnotebook_resources.py%5Cn%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Save%20it%20in%20the%20same%20folder%20as%20this%20notebook%2C%20then%20rerun%20this%20cell.%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22notebook_resources%22%2C%0A%20%20%20%20%20%20%20%20)%20from%20None%0A%20%20%20%20return%20(repo_file%2C)%0A%0A%0A%40app.cell%0Adef%20_(repo_file)%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20%23%20'%25matplotlib%20inline'%20command%20supported%20automatically%20in%20marimo%0A%20%20%20%20import%20sys%0A%20%20%20%20sys.path.insert(0%2C%20str(repo_file('functions%2Fhyperellipsoid.py').parent))%20%20%23%20add%20to%20pythonpath%0A%20%20%20%20from%20hyperellipsoid%20import%20hyperellipsoid%0A%0A%20%20%20%20return%20hyperellipsoid%2C%20plt%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%23%20Prediction%20ellipse%0A%0A%20%20%20%20For%20a%20bivariate%20random%20data%20with%20normal%20distribution%20with%20mean%200%20and%20variance%201%2C%20the%20prediction%20ellipse%20should%20be%20a%20circle%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hyperellipsoid%2C%20np)%3A%0A%20%20%20%20P%20%3D%20np.random.randn(10000%2C%202)%0A%20%20%20%20(area%2C%20axes%2C%20angles%2C%20center%2C%20R_1)%20%3D%20hyperellipsoid(P%2C%20units%3D'cm'%2C%20show%3DTrue)%0A%20%20%20%20print('Area%20%3D'%2C%20area)%0A%20%20%20%20print('Semi-axes%20%3D'%2C%20axes)%0A%20%20%20%20print('Angles%20%3D'%2C%20angles)%0A%20%20%20%20print('Center%20%3D'%2C%20center)%0A%20%20%20%20print('Rotation%20matrix%20%3D%5Cn'%2C%20R_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%20For%20other%20data%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hyperellipsoid%2C%20np)%3A%0A%20%20%20%20y%20%3D%20np.cumsum(np.random.randn(3000))%20%2F%2050%0A%20%20%20%20x%20%3D%20np.cumsum(np.random.randn(3000))%20%2F%20100%0A%20%20%20%20(area_1%2C%20axes_1%2C%20angles_1%2C%20center_1%2C%20R_2)%20%3D%20hyperellipsoid(x%2C%20y%2C%20units%3D'cm'%2C%20show%3DTrue)%0A%20%20%20%20print('Area%20%3D'%2C%20area_1)%0A%20%20%20%20print('Semi-axes%20%3D'%2C%20axes_1)%0A%20%20%20%20print('Angles%20%3D'%2C%20angles_1)%0A%20%20%20%20print('Center%20%3D'%2C%20center_1)%0A%20%20%20%20print('Rotation%20matrix%20%3D%5Cn'%2C%20R_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%23%23%23%20Find%20points%20outside%20the%20ellipse%0A%0A%20%20%20%20The%20**outside_hyperellipsoid**%20function%20can%20be%20used%20to%20determine%20whether%20specific%20points%20lie%20outside%20the%20ellipse%20as%20demonstrated%20below.%0A%0A%20%20%20%20Note%20that%20the%20proportion%20of%20points%20outside%20a%2095%25%20ellipse%20is%20expected%20to%20tend%20toward%200.05%20as%20n%20increases.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hyperellipsoid%2C%20np%2C%20plt)%3A%0A%20%20%20%20from%20hyperellipsoid%20import%20outside_hyperellipsoid%0A%20%20%20%20np.random.seed(6)%0A%20%20%20%20P_1%20%3D%20np.random.randn(100%2C%202)%0A%20%20%20%20(volume%2C%20axes_2%2C%20angles_2%2C%20center_2%2C%20R_3)%20%3D%20hyperellipsoid(P_1%2C%20pvalue%3D0.95%2C%20show%3DFalse)%0A%20%20%20%20b%20%3D%20outside_hyperellipsoid(axes_2%2C%20angles_2%2C%20center_2%2C%20R_3%2C%20P_1)%0A%20%20%20%20p%20%3D%20P_1%5Bb%5D%0A%20%20%20%20fig%20%3D%20plt.figure(figsize%3D(4%2C%204))%0A%20%20%20%20ax%20%3D%20fig.add_axes(%5B0%2C%200%2C%201%2C%201%5D)%0A%20%20%20%20ax.plot(p%5B%3A%2C%200%5D%2C%20p%5B%3A%2C%201%5D%2C%20'ro'%2C%20ms%3D8)%0A%20%20%20%20(volume%2C%20axes_2%2C%20angles_2%2C%20center_2%2C%20R_3)%20%3D%20hyperellipsoid(P_1%2C%20pvalue%3D0.95%2C%20show%3DTrue%2C%20ax%3Dax)%0A%20%20%20%20print('Number%20of%20points%20outside%20ellipsoid%3A%20%25d'%20%25%20b.sum())%0A%20%20%20%20print('Proportion%20of%20points%20outside%20ellipsoid%3A%20%25.3f'%20%25%20b.mean())%0A%20%20%20%20return%20(outside_hyperellipsoid%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%20**Exercise%203.**%20For%20the%20ellipse%20in%20Exercise%202%2C%20is%20the%20point%20(3%2C3)%20inside%20when%20the%20center%20is%20(0%2C0)%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20Its%20squared%20Mahalanobis%20distance%20is%20%243%5E2%2F4%2B3%5E2%2F1%3D11.25%24%2C%20greater%20than%209%2C%20so%20it%20is%20outside.%20Checking%20each%20coordinate%20against%20its%20semiaxis%20separately%20would%20incorrectly%20accept%20it.%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%23%20Prediction%20ellipsoid%0A%0A%20%20%20%20For%20a%20trivariate%20random%20data%20with%20normal%20distribution%20with%20mean%200%20and%20variance%201%2C%20the%20prediction%20ellipsoid%20should%20be%20a%20sphere%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hyperellipsoid%2C%20np)%3A%0A%20%20%20%20P_2%20%3D%20np.random.randn(10000%2C%203)%0A%20%20%20%20(volume_1%2C%20axes_3%2C%20angles_3%2C%20center_3%2C%20R_4)%20%3D%20hyperellipsoid(P_2%2C%20units%3D'cm'%2C%20show%3DTrue)%0A%20%20%20%20print('Volume%20%3D'%2C%20volume_1)%0A%20%20%20%20print('Semi-axes%20%3D'%2C%20axes_3)%0A%20%20%20%20print('Angles%20%3D'%2C%20angles_3)%0A%20%20%20%20print('Center%20%3D'%2C%20center_3)%0A%20%20%20%20print('Rotation%20matrix%20%3D%5Cn'%2C%20R_4)%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%20For%20other%20data%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hyperellipsoid%2C%20np)%3A%0A%20%20%20%20P_3%20%3D%20np.random.randn(1000%2C%203)%0A%20%20%20%20P_3%5B%3A%2C%202%5D%20%3D%20P_3%5B%3A%2C%202%5D%20%2B%20P_3%5B%3A%2C%201%5D%20*%200.5%0A%20%20%20%20P_3%5B%3A%2C%201%5D%20%3D%20P_3%5B%3A%2C%201%5D%20%2B%20P_3%5B%3A%2C%200%5D%20*%200.5%0A%20%20%20%20(volume_2%2C%20axes_4%2C%20angles_4%2C%20center_4%2C%20R_5)%20%3D%20hyperellipsoid(P_3%2C%20units%3D'cm'%2C%20show%3DTrue)%0A%20%20%20%20print('Volume%20%3D'%2C%20volume_2)%0A%20%20%20%20print('Semi-axes%20%3D'%2C%20axes_4)%0A%20%20%20%20print('Angles%20%3D'%2C%20angles_4)%0A%20%20%20%20print('Center%20%3D'%2C%20center_4)%0A%20%20%20%20print('Rotation%20matrix%20%3D%5Cn'%2C%20R_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%23%23%23%20Find%20points%20outside%20the%20(hyper)ellipsoid%0A%0A%20%20%20%20The%20**outside_hyperellipsoid**%20function%20can%20be%20used%20to%20determine%20whether%20specific%20points%20lie%20outside%20the%20ellipsoid%20as%20demonstrated%20below.%0A%0A%20%20%20%20Note%20that%20the%20proportion%20of%20points%20outside%20a%2095%25%20ellipsoid%20is%20expected%20to%20tend%20toward%200.05%20as%20n%20increases.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(hyperellipsoid%2C%20np%2C%20outside_hyperellipsoid%2C%20plt)%3A%0A%20%20%20%20from%20mpl_toolkits.mplot3d%20import%20Axes3D%0A%20%20%20%20import%20matplotlib.animation%20as%20animation%0A%20%20%20%20from%20hyperellipsoid%20import%20_plot%0A%20%20%20%20np.random.seed(6)%0A%20%20%20%20P_4%20%3D%20np.random.randn(100%2C%203)%0A%20%20%20%20(volume_3%2C%20axes_5%2C%20angles_5%2C%20center_5%2C%20R_6)%20%3D%20hyperellipsoid(P_4%2C%20pvalue%3D0.95%2C%20show%3DFalse)%0A%20%20%20%20b_1%20%3D%20outside_hyperellipsoid(axes_5%2C%20angles_5%2C%20center_5%2C%20R_6%2C%20P_4)%0A%20%20%20%20p_1%20%3D%20P_4%5Bb_1%5D%0A%20%20%20%20fig_1%20%3D%20plt.figure(figsize%3D(7%2C%207))%0A%20%20%20%20ax_1%20%3D%20fig_1.add_axes(%5B0%2C%200%2C%201%2C%201%5D%2C%20projection%3D'3d')%0A%20%20%20%20ax_1.plot(p_1%5B%3A%2C%200%5D%2C%20p_1%5B%3A%2C%201%5D%2C%20p_1%5B%3A%2C%202%5D%2C%20'ro'%2C%20ms%3D8)%0A%20%20%20%20(volume_3%2C%20axes_5%2C%20angles_5%2C%20center_5%2C%20R_6)%20%3D%20hyperellipsoid(P_4%2C%20pvalue%3D0.95%2C%20show%3DTrue%2C%20ax%3Dax_1)%0A%20%20%20%20print('Number%20of%20points%20outside%20ellipsoid%3A%20%25d'%20%25%20b_1.sum())%0A%20%20%20%20print('Proportion%20of%20points%20outside%20ellipsoid%3A%20%25.3f'%20%25%20b_1.mean())%0A%20%20%20%20return%20P_4%2C%20animation%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%20Static%20Matplotlib%20output%20shows%20one%20view.%20The%20animation%20below%20changes%20the%20view%20without%20changing%20the%20data.%20Desktop%20interactive%20backends%20can%20also%20rotate%20a%20figure%2C%20but%20a%20Qt%20window%20is%20not%20required%20and%20is%20unavailable%20in%20a%20browser-only%20runtime%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(P_4%2C%20hyperellipsoid)%3A%0A%20%20%20%20hyperellipsoid(P_4%2C%20units%3D'cm'%2C%20show%3DTrue)%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%20Let's%20generate%20an%20animation%20where%20the%20ellipsoid%20is%20rotated%20to%20better%20visualize%20it%20in%20this%20notebook%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(P_4%2C%20animation%2C%20hyperellipsoid%2C%20plt)%3A%0A%20%20%20%20from%20hyperellipsoid%20import%20_plot%0A%20%20%20%20fig_2%20%3D%20plt.figure(figsize%3D(7%2C%207))%0A%20%20%20%20ax_2%20%3D%20fig_2.add_axes(%5B0%2C%200%2C%201%2C%201%5D%2C%20projection%3D'3d')%0A%20%20%20%20pts%20%3D%20ax_2.plot(%5B%5D%2C%20%5B%5D%2C%20%5B%5D%2C%20'o')%0A%20%20%20%20(volume_4%2C%20axes_6%2C%20angles_6%2C%20center_6%2C%20R_7)%20%3D%20hyperellipsoid(P_4%2C%20units%3D'cm'%2C%20show%3DFalse%2C%20ax%3DNone)%0A%20%20%20%20_plot(P_4%2C%20volume_4%2C%20axes_6%2C%20center_6%2C%20R_7%2C%200.95%2C%20'cm'%2C%20ax_2)%0A%0A%20%20%20%20def%20animate(i)%3A%0A%20%20%20%20%20%20%20%20ax_2.view_init(20%2C%203%20*%20i)%0A%20%20%20%20%20%20%20%20fig_2.canvas.draw()%0A%20%20%20%20%20%20%20%20return%20pts%0A%20%20%20%20anim%20%3D%20animation.FuncAnimation(fig_2%2C%20func%3Danimate%2C%20frames%3D120%2C%20interval%3D30%2C%20blit%3DTrue)%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%20This%20is%20the%20animation%20you%20see%20in%20a%20separate%20window%20when%20you%20run%20the%20code%20above%3A%0A%0A%20%20%20%20%3Cdiv%20class%3D'center-align'%3E%3Cfigure%3E%3Cimg%20src%3D'https%3A%2F%2Fgithub.com%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fimages%2Fellipsoid.gif%3Fraw%3D1'%20width%3D450%20alt%3D'ellipsoid'%2F%3E%3C%2Ffigure%3E%3C%2Fdiv%3E%3Cbr%3E%0A%0A%20%20%20%20To%20save%20the%20animation%20generated%20above%20as%20a%20video%20file%20(you%20need%20to%20have%20%5BFFmpeg%5D(http%3A%2F%2Fwww.ffmpeg.org%2F)%20or%20%5BImageMagick%5D(http%3A%2F%2Fwww.imagemagick.org%2Fscript%2Findex.php)%20installed)%3A%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.**%20If%20you%20rotate%20every%20observation%20by%20an%20orthogonal%20matrix%2C%20should%20the%20ellipse%E2%80%99s%20area%20change%3F%20What%20if%20you%20rescale%20one%20coordinate%20by%20two%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%204%0A%20%20%20%20An%20orthogonal%20rotation%20preserves%20eigenvalues%20and%20area%3B%20it%20changes%20orientation.%20Scaling%20one%20coordinate%20by%20two%20multiplies%20the%20two-dimensional%20area%20by%20two%20and%20changes%20its%20units%2Fshape.%20Report%20coordinate%20units%20alongside%20area.%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.%20Why%20must%20%24n%3Ep%24%20in%20the%20stated%20finite-sample%20formula%3F%0A%20%20%20%202.%20Does%20a%2095%25%20ellipse%20guarantee%20exactly%205%25%20of%20fitted%20observations%20lie%20outside%3F%0A%20%20%20%203.%20What%20do%20the%20covariance%20eigenvectors%20specify%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20Positive%20denominator%20degrees%20of%20freedom%20are%20required%2C%20and%20covariance%20must%20also%20have%20full%20rank.%0A%20%20%20%202.%20No.%0A%20%20%20%203.%20Principal%20directions%20of%20the%20ellipsoid.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20Give%20the%20area%20of%20an%20ellipse%20with%20semiaxes%202%20cm%20and%205%20cm.%0A%20%20%20%202.%20Give%20the%20volume%20of%20an%20ellipsoid%20with%20semiaxes%201%2C%202%2C%20and%203%20cm.%0A%20%20%20%203.%20Can%20the%20stated%20full-dimensional%20formula%20be%20used%20for%20three%20variables%20and%20only%20three%20observations%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%20%2410%5Cpi%24%20cm%C2%B2%2C%20approximately%2031.42%20cm%C2%B2.%0A%20%20%20%202.%20%248%5Cpi%24%20cm%C2%B3%2C%20approximately%2025.13%20cm%C2%B3.%0A%20%20%20%203.%20No.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20%5BConfidence%20and%20prediction%20intervals%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fconfidence_prediction_intervals.py)%20%E2%80%94%20establish%20the%20one-dimensional%20target%20first.%20The%20Chew%20paper%20and%20the%20posturography%20exchange%20below%20explain%20why%20prediction%20and%20confidence%20regions%20should%20not%20share%20a%20label.%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%20multivariate%20regression%20and%20covariance%20connect%20statistical%20regions%20with%20linear%20algebra.%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-%20Chew%20V%20(1966)%20%5BConfidence%2C%20Prediction%2C%20and%20Tolerance%20Regions%20for%20the%20Multivariate%20Normal%20Distribution%5D(http%3A%2F%2Fwww.jstor.org%2Fstable%2F2282774).%20Journal%20of%20the%20American%20Statistical%20Association%2C%2061%2C%20315%2C%20pp.%20605-617.%0A%0A%20%20%20%20-%20Duarte%20M%20(2015)%20%5BComments%20on%20%E2%80%9CEllipse%20area%20calculations%20and%20their%20applicability%20in%20posturography%E2%80%9D%20(Schubert%20and%20Kirchner%2C%20vol.39%2C%20pages%20518-522%2C%202014)%5D(http%3A%2F%2Fdemotu.org%2Fpubs%2FGP15.pdf).%20Gait%20%26%20Posture%2C%2041%2C%2044-45.%20%3Ca%20href%3D%22https%3A%2F%2Fpubmed.ncbi.nlm.nih.gov%2F25194690%22%20target%3D%22_blank%22%3EPubMed%3C%2Fa%3E.%0A%0A%20%20%20%20-%20Schubert%20P%2C%20Kirchner%20M%20(2014)%20%5BEllipse%20area%20calculations%20and%20their%20applicability%20in%20posturography%5D(http%3A%2F%2Fwww.sciencedirect.com%2Fscience%2Farticle%2Fpii%2FS0966636213005961).%20Gait%20%26%20Posture%2C%2039%2C%20518%E2%80%93522.%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%20Function%20hyperellipsoid.py%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20repo_file)%3A%0A%20%20%20%20mo.ui.code_editor(repo_file(%22functions%2Fhyperellipsoid.py%22).read_text()%2C%20language%3D%22python%22%2C%20disabled%3DTrue)%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
770a10e64839c2497be3a3047393b790