%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%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%20Confidence%20and%20prediction%20intervals%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%20You%20can%20estimate%20a%20group%E2%80%99s%20mean%20precisely%20while%20still%20being%20unable%20to%20predict%20the%20next%20person%20precisely.%20This%20guide%20puts%20those%20two%20intervals%20side%20by%20side%2C%20starting%20with%20a%20population%20whose%20parameters%20are%20known%20and%20then%20estimating%20them%20from%20a%20finite%20sample.%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.**%20Collecting%20four%20times%20as%20many%20independent%20observations%20roughly%20halves%20the%20standard%20error.%20Does%20it%20also%20halve%20the%20spread%20of%20the%20next%20observation%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%20generates%20simulated%20samples%2C%20SciPy%20supplies%20probability%20distributions%2C%20and%20Matplotlib%20draws%20them.%20The%20small%20%60pdf_norm_plot%60%20drawing%20helper%20is%20loaded%20from%20this%20repository.%20All%20samples%20are%20simulated%3B%20there%20is%20no%20experimental%20dataset%20to%20download.%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%20For%20a%20finite%20population%20of%20equally%20weighted%20observations%2C%20the%20mean%20%24%5Cmu%24%20(a%20measure%20of%20central%20tendency)%20and%20variance%20%24%5Csigma%5E2%24%20(a%20measure%20of%20dispersion)%20of%20a%20population%20are%20the%20well%20known%20formulas%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmu%20%3D%20%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bi%3D1%7D%5E%7BN%7D%20x_i%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma%5E2%20%3D%20%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bi%3D1%7D%5E%7BN%7D%20(x_i%20-%20%5Cmu)%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20For%20a%20more%20general%20case%2C%20a%20continuous%20univariate%20random%20variable%20%24x%24%20with%20%5Bprobability%20density%20function%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FProbability_density_function)%20(PDF)%2C%20%24f(x)%24%2C%20the%20mean%20and%20variance%20of%20a%20population%20are%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmu%20%3D%20%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%20x%20f(x)%5C%3A%20dx%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma%5E2%20%3D%20%5Cint_%7B-%5Cinfty%7D%5E%7B%5Cinfty%7D%20(x-%5Cmu)%5E2%20f(x)%5C%3A%20dx%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20PDF%20is%20a%20function%20that%20describes%20the%20relative%20likelihood%20for%20the%20random%20variable%20to%20take%20on%20a%20given%20value.%3Cbr%3E%0A%20%20%20%20The%20mean%20is%20the%20first%20raw%20moment%20and%20the%20variance%20is%20the%20second%20central%20moment%2C%20provided%20these%20moments%20exist.%20The%20standard%20deviation%20%24%5Csigma%24%20of%20the%20population%20is%20the%20square%20root%20of%20the%20variance.%0A%0A%20%20%20%20The%20normal%20distribution%20is%20useful%20partly%20because%20of%20the%20central%20limit%20theorem%3A%20for%20independent%2C%20identically%20distributed%20observations%20with%20finite%20variance%2C%20the%20standardized%20sample%20mean%20approaches%20a%20normal%20distribution%20as%20the%20number%20of%20observations%20**within%20each%20sample**%20grows.%20Repeating%20small%20samples%20many%20times%20reveals%20their%20sampling%20distribution%3B%20it%20does%20not%20itself%20make%20that%20distribution%20normal.%0A%0A%20%20%20%20The%20probability%20density%20function%20of%20a%20univariate%20normal%20(or%20Gaussian)%20distribution%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20f(x)%20%3D%20%5Cfrac%7B1%7D%7B%5Csqrt%7B2%5Cpi%5Csigma%5E2%7D%7D%20%5Cexp%5CBigl(-%5Cfrac%7B(x-%5Cmu)%5E2%7D%7B2%5Csigma%5E2%7D%5CBigr)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20only%20parameters%20that%20define%20the%20normal%20distribution%20are%20the%20mean%20%24%5Cmu%24%20and%20the%20variance%20%24%5Csigma%5E2%24%2C%20because%20of%20that%20a%20normal%20distribution%20is%20usually%20described%20as%20%24N(%5Cmu%2C%5C%3A%5Csigma%5E2)%24.%0A%0A%20%20%20%20Here%20is%20a%20plot%20of%20the%20PDF%20for%20the%20normal%20distribution%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%20%23%20import%20the%20necessary%20libraries%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20%23%20'%25matplotlib%20notebook'%20command%20supported%20automatically%20in%20marimo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20IPython.display%20import%20display%2C%20Latex%0A%20%20%20%20from%20scipy%20import%20stats%0A%20%20%20%20import%20sys%0A%20%20%20%20sys.path.insert(0%2C%20str(repo_file('functions%2Fpdf_norm_plot.py').parent))%20%20%23%20add%20to%20pythonpath%0A%20%20%20%20return%20Latex%2C%20display%2C%20np%2C%20plt%2C%20stats%0A%0A%0A%40app.cell%0Adef%20_(plt)%3A%0A%20%20%20%20from%20pdf_norm_plot%20import%20pdf_norm_plot%0A%20%20%20%20pdf_norm_plot()%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%20horizontal%20axis%20above%20is%20shown%20in%20terms%20of%20the%20number%20of%20standard%20deviations%20in%20relation%20to%20the%20mean%2C%20which%20is%20known%20as%20standard%20score%20or%20%24z%24%20score%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20z%20%3D%20%5Cfrac%7Bx%20-%20%5Cmu%7D%7B%5Csigma%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20So%2C%20instead%20of%20specifying%20raw%20values%20in%20the%20distribution%2C%20we%20define%20the%20PDF%20in%20terms%20of%20%24z%24%20scores%3B%20this%20conversion%20process%20is%20called%20standardizing%20the%20distribution%20(and%20the%20result%20is%20known%20as%20standard%20normal%20distribution).%20If%20%24x%24%20is%20normal%20with%20mean%20%24%5Cmu%24%20and%20SD%20%24%5Csigma%24%2C%20its%20standardized%20value%20%24z%24%20is%20standard%20normal%2C%20with%20mean%20zero%20and%20variance%20one.%0A%0A%20%20%20%20The%20percentage%20numbers%20in%20the%20plot%20are%20the%20probability%20(area%20under%20the%20curve)%20for%20each%20interval%20shown%20in%20the%20horizontal%20label.%3Cbr%3E%0A%20%20%20%20An%20interval%20in%20terms%20of%20z%20score%20is%20specified%20as%3A%20%24%5B%5Cmu-z%5Csigma%2C%5C%3B%5Cmu%2Bz%5Csigma%5D%24.%3Cbr%3E%0A%20%20%20%20The%20interval%20%24%5B%5Cmu-1%5Csigma%2C%5C%3B%5Cmu%2B1%5Csigma%5D%24%20contains%2068.3%25%20of%20the%20population%20and%20the%20interval%20%24%5B%5Cmu-2%5Csigma%2C%5C%3B%5Cmu%2B2%5Csigma%5D%24%20contains%2095.4%25%20of%20the%20population.%3Cbr%3E%0A%20%20%20%20These%20numbers%20can%20be%20calculated%20using%20the%20function%20%60stats.norm.cdf()%60%2C%20the%20%5Bcumulative%20distribution%20function%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCumulative_distribution_function)%20(CDF)%20of%20the%20normal%20distribution%20at%20a%20given%20value%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Latex%2C%20display%2C%20stats)%3A%0A%20%20%20%20print('Cumulative%20distribution%20function%20(cdf)%20of%20the%20normal%20distribution%3A')%0A%20%20%20%20for%20_i%20in%20range(-3%2C%204)%3A%0A%20%20%20%20%20%20%20%20display(Latex('%25d%24%5C%5Csigma%3A%5C%5C%3B%24%25.2f'%20%25%20(_i%2C%20stats.norm.cdf(_i%2C%20loc%3D0%2C%20scale%3D1)%20*%20100)%20%2B%20'%20%25'))%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.**%20A%20normal%20variable%20has%20mean%2010%20and%20SD%202.%20What%20z-score%20corresponds%20to%2014%2C%20and%20which%20%60scale%60%20should%20SciPy%20receive%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20The%20z-score%20is%20%24(14-10)%2F2%3D2%24.%20Use%20%60scale%3D2%60%2C%20not%20variance%204.%20A%20density%20height%20is%20not%20a%20point%20probability%3B%20interval%20probability%20is%20area%20under%20the%20density.%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%20The%20parameters%20%60loc%60%20and%20%60scale%60%20are%20optionals%20and%20represent%20mean%20and%20standard%20deviation%20of%20the%20distribution.%20The%20default%20is%20%60loc%3D0%60%20and%20%60scale%3D1%60.%3Cbr%3E%0A%20%20%20%20A%20commonly%20used%20proportion%20is%2095%25.%20The%20value%20that%20results%20is%20this%20proportion%20can%20be%20found%20using%20the%20function%20%60stats.norm.ppf()%60.%20If%20we%20want%20to%20find%20the%20%24%5Cpm%24%20value%20for%20the%20interval%20that%20will%20result%20in%2095%25%20of%20the%20population%20inside%2C%20we%20have%20to%20consider%20that%202.5%25%20of%20the%20population%20will%20stay%20out%20of%20the%20interval%20in%20each%20tail%20of%20the%20distribution.%20Because%20of%20that%2C%20the%20number%20we%20have%20to%20use%20with%20the%20%60stats.norm.ppf()%60%20is%200.975%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Latex%2C%20display%2C%20stats)%3A%0A%20%20%20%20print('Percent%20point%20function%20(inverse%20of%20CDF)%20of%20the%20normal%20distribution%3A')%0A%20%20%20%20display(Latex(r'ppf(.975)%20%3D%20%25.2f'%20%25%20stats.norm.ppf(.975%2C%20loc%3D0%2C%20scale%3D1)))%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%20Or%20we%20can%20use%20the%20function%20%60stats.norm.interval%60%20which%20already%20gives%20the%20interval%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(stats)%3A%0A%20%20%20%20print('Confidence%20interval%20around%20the%20mean%3A')%0A%20%20%20%20stats.norm.interval(confidence%3D0.95%2C%20loc%3D0%2C%20scale%3D1)%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%20So%2C%20the%20interval%20%24%5B%5Cmu-1.96%5Csigma%2C%5C%3B%5Cmu%2B1.96%5Csigma%5D%24%20contains%2095%25%20of%20the%20population.%0A%0A%20%20%20%20Now%20that%20we%20know%20how%20the%20probability%20density%20function%20of%20a%20normal%20distribution%20looks%20like%2C%20let's%20demonstrate%20the%20central%20limit%20theorem%20for%20a%20uniform%20distribution.%20For%20that%2C%20we%20will%20generate%20samples%20of%20a%20uniform%20distribution%2C%20calculate%20the%20mean%20across%20samples%2C%20and%20plot%20the%20histogram%20of%20the%20mean%20%20across%20samples%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20_rc%20%3D%20%7B'axes.labelsize'%3A%2012%2C%20'font.size'%3A%2012%2C%20'legend.fontsize'%3A%2012%2C%20'axes.titlesize'%3A%2012%7D%0A%20%20%20%20plt.rcParams.update(**_rc)%0A%20%20%20%20(_fig%2C%20_ax)%20%3D%20plt.subplots(1%2C%204%2C%20sharey%3DTrue%2C%20squeeze%3DTrue%2C%20figsize%3D(9%2C%203))%0A%20%20%20%20_x%20%3D%20np.linspace(0%2C%201%2C%20100)%0A%20%20%20%20for%20(_i%2C%20n)%20in%20enumerate(%5B1%2C%202%2C%203%2C%2010%5D)%3A%0A%20%20%20%20%20%20%20%20_f%20%3D%20np.mean(np.random.random((1000%2C%20n))%2C%201)%0A%20%20%20%20%20%20%20%20(m%2C%20s)%20%3D%20(np.mean(_f)%2C%20np.std(_f%2C%20ddof%3D1))%0A%20%20%20%20%20%20%20%20_fn%20%3D%201%20%2F%20(s%20*%20np.sqrt(2%20*%20np.pi))%20*%20np.exp(-(_x%20-%20m)%20**%202%20%2F%20(2%20*%20s%20**%202))%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.hist(_f%2C%2020%2C%20density%3DTrue%2C%20color%3D%5B0%2C%200.2%2C%200.8%2C%200.6%5D%2C%20edgecolor%3D'black')%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.set_title('n%3D%25d'%20%25%20n%2C%20fontsize%3D12)%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.plot(_x%2C%20_fn%2C%20color%3D%5B1%2C%200%2C%200%2C%200.6%5D%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.xaxis.label.set_size(12)%0A%20%20%20%20plt.suptitle('Demonstration%20of%20the%20central%20limit%20theorem%20for%20a%20uniform%20distribution'%2C%20fontsize%3D12%2C%20y%3D1.0)%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%20**Exercise%202.**%20Does%20taking%2010%2C000%20samples%20of%20size%202%20make%20the%20mean%20of%20two%20uniform%20observations%20exactly%20normal%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20No.%20More%20repetitions%20reveal%20the%20sampling%20distribution%20more%20clearly.%20The%20central%20limit%20theorem%20concerns%20increasing%20the%20number%20of%20independent%20observations%20per%20mean%2C%20under%20its%20assumptions.%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**Statistics%20for%20a%20sample%20of%20the%20population**%0A%0A%20%20%20%20Parameters%20(such%20as%20mean%20and%20variance)%20are%20characteristics%20of%20a%20population%3B%20statistics%20are%20the%20equivalent%20for%20a%20sample.%20A%20normal%20population%20is%20specified%20by%20its%20mean%20and%20variance.%20Two%20sample%20summaries%20do%20not%20completely%20describe%20an%20observed%20dataset%3B%20inspect%20its%20distribution%20too.%0A%0A%20%20%20%20The%20difference%20between%20sample%20and%20population%20is%20fundamental%20for%20the%20understanding%20of%20probability%20and%20statistics.%3Cbr%3E%0A%20%20%20%20In%20Statistics%2C%20a%20sample%20is%20a%20set%20of%20data%20collected%20from%20a%20population.%20A%20population%20is%20usually%20very%20large%20and%20can't%20be%20accessed%20completely%3B%20all%20we%20have%20access%20is%20a%20sample%20(a%20smaller%20set)%20of%20the%20population.%0A%0A%20%20%20%20If%20we%20have%20only%20a%20sample%20of%20a%20finite%20univariate%20random%20variable%20with%20a%20normal%20distribution%2C%20both%20mean%20and%20variance%20of%20the%20population%20are%20unknown%20and%20they%20have%20to%20be%20estimated%20from%20the%20sample%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbar%7Bx%7D%20%3D%20%5Cfrac%7B1%7D%7BN%7D%5Csum_%7Bi%3D1%7D%5E%7BN%7D%20x_i%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20s%5E2%20%3D%20%5Cfrac%7B1%7D%7BN-1%7D%5Csum_%7Bi%3D1%7D%5E%7BN%7D%20(x_i%20-%20%5Cbar%7Bx%7D)%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20sample%20%24%5Cbar%7Bx%7D%24%20and%20%24s%5E2%24%20are%20only%20estimations%20of%20the%20unknown%20true%20mean%20and%20variance%20of%20the%20population%2C%20but%20because%20of%20the%20%5Blaw%20of%20large%20numbers%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FLaw_of_large_numbers)%2C%20as%20the%20size%20of%20the%20sample%20increases%2C%20the%20sample%20mean%20and%20variance%20have%20an%20increased%20probability%20of%20being%20close%20to%20the%20population%20mean%20and%20variance.%0A%0A%20%20%20%20**Prediction%20interval%20around%20the%20mean**%0A%0A%20%20%20%20For%20a%20sample%20of%20a%20univariate%20random%20variable%2C%20the%20area%20in%20an%20interval%20of%20the%20probability%20density%20function%20can't%20be%20interpreted%20anymore%20as%20the%20proportion%20of%20the%20sample%20lying%20inside%20the%20interval.%20Rather%2C%20that%20area%20in%20the%20interval%20is%20a%20prediction%20of%20the%20probability%20that%20a%20new%20value%20from%20the%20population%20added%20to%20the%20sample%20will%20be%20inside%20the%20interval.%20This%20is%20called%20a%20%5Bprediction%20interval%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPrediction_interval).%20However%2C%20there%20is%20one%20more%20thing%20to%20correct.%20We%20have%20to%20adjust%20the%20interval%20limits%20for%20the%20fact%20that%20now%20we%20have%20only%20a%20sample%20of%20the%20population%20and%20the%20parameters%20%24%5Cmu%24%20and%20%24%5Csigma%24%20are%20unknown%20and%20have%20to%20be%20estimated.%20This%20correction%20will%20increase%20the%20interval%20for%20the%20same%20probability%20value%20of%20the%20interval%20because%20we%20are%20not%20so%20certain%20about%20the%20distribution%20of%20the%20population.%3Cbr%3E%0A%20%20%20%20To%20calculate%20the%20interval%20given%20a%20desired%20probability%2C%20we%20have%20to%20determine%20the%20distribution%20of%20the%20z-score%20equivalent%20for%20the%20case%20of%20a%20sample%20with%20unknown%20mean%20and%20variance%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cfrac%7Bx_%7Bn%2Bi%7D-%5Cbar%7Bx%7D%7D%7Bs%5Csqrt%7B1%2B1%2Fn%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24x_%7Bn%2Bi%7D%24%20is%20the%20new%20observation%20for%20which%20we%20want%20to%20calculate%20the%20prediction%20interval.%3Cbr%3E%0A%20%20%20%20The%20distribution%20of%20the%20ratio%20above%20is%20called%20%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FStudent's_t-distribution%22%3EStudent's%20t-distribution%3C%2Fa%3E%20or%20simply%20%24T%24%20distribution%2C%20with%20%24n-1%24%20degrees%20of%20freedom.%20A%20%24T%24%20distribution%20is%20symmetric%20and%20its%20pdf%20tends%20to%20that%20of%20the%0A%20%20%20%20standard%20normal%20as%20%24n%24%20tends%20to%20infinity.%0A%0A%20%20%20%20Then%2C%20the%20prediction%20interval%20around%20the%20sample%20mean%20for%20a%20new%20observation%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%5B%5Cbar%7Bx%7D%20-%20T_%7Bn-1%7D%5C%3As%5C%3A%5Csqrt%7B1%2B1%2Fn%7D%2C%5Cquad%20%5Cbar%7Bx%7D%20%2B%20T_%7Bn-1%7D%5C%3As%5C%3A%5Csqrt%7B1%2B1%2Fn%7D%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24T_%7Bn-1%7D%24%20is%20the%20%24100((1%2Bp)%2F2)%5E%7Bth%7D%24%20percentile%20of%20the%20Student's%20t-distribution%20with%20n%E2%88%921%20degrees%20of%20freedom.%0A%0A%20%20%20%20For%20instance%2C%20the%20prediction%20interval%20with%2095%25%20of%20probability%20for%20a%20sample%20(%24%5Cbar%7Bx%7D%3D0%2C%5C%3Bs%5E2%3D1%24)%20with%20size%20equals%20to%2010%20is%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20stats)%3A%0A%20%20%20%20np.asarray(stats.t.interval(confidence%3D0.95%2C%20df%3D25-1%2C%20loc%3D0%2C%20scale%3D1))%20*%20np.sqrt(1%2B1%2F10)%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%20large%20degrees%20of%20freedom%20(e.g.%2C%209999)%2C%20the%20t%20quantile%20approaches%20the%20standard-normal%20quantile%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20stats)%3A%0A%20%20%20%20np.asarray(stats.t.interval(confidence%3D0.95%2C%20df%3D10000-1%2C%20loc%3D0%2C%20scale%3D1))%20*%20np.sqrt(1%2B1%2F10000)%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%20Here%20is%20a%20plot%20of%20the%20pdf%20for%20the%20normal%20distribution%20and%20the%20pdf%20for%20the%20Student's%20t-distribution%20with%20different%20number%20of%20degrees%20of%20freedom%20(n-1)%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%20(_fig%2C%20_ax)%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(8%2C%204))%0A%20%20%20%20_x%20%3D%20np.linspace(-4%2C%204%2C%201000)%0A%20%20%20%20_f%20%3D%20stats.norm.pdf(_x%2C%20loc%3D0%2C%20scale%3D1)%0A%20%20%20%20t2%20%3D%20stats.t.pdf(_x%2C%20df%3D2%20-%201)%0A%20%20%20%20t10%20%3D%20stats.t.pdf(_x%2C%20df%3D10%20-%201)%0A%20%20%20%20t100%20%3D%20stats.t.pdf(_x%2C%20df%3D100%20-%201)%0A%20%20%20%20_ax.plot(_x%2C%20_f%2C%20color%3D'k'%2C%20linestyle%3D'--'%2C%20lw%3D4%2C%20label%3D'Normal')%0A%20%20%20%20_ax.plot(_x%2C%20t2%2C%20color%3D'r'%2C%20lw%3D2%2C%20label%3D'T%20(1)')%0A%20%20%20%20_ax.plot(_x%2C%20t10%2C%20color%3D'g'%2C%20lw%3D2%2C%20label%3D'T%20(9)')%0A%20%20%20%20_ax.plot(_x%2C%20t100%2C%20color%3D'b'%2C%20lw%3D2%2C%20label%3D'T%20(99)')%0A%20%20%20%20_ax.legend(title%3D'Distribution'%2C%20fontsize%3D14)%0A%20%20%20%20_ax.set_title(%22Normal%20and%20Student's%20t%20distributions%22%2C%20fontsize%3D12)%0A%20%20%20%20_ax.set_xticks(np.linspace(-4%2C%204%2C%209))%0A%20%20%20%20xtl%20%3D%20%5B'%25%2Bd%24%5C%5Csigma%24'%20%25%20_i%20for%20_i%20in%20range(-4%2C%205%2C%201)%5D%0A%20%20%20%20xtl%5B4%5D%20%3D%20'%24%5C%5Cmu%24'%0A%20%20%20%20_ax.set_xticklabels(xtl)%0A%20%20%20%20_ax.set_ylim(-0.01%2C%200.41)%0A%20%20%20%20plt.grid()%0A%20%20%20%20plt.rc('font'%2C%20size%3D12)%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%20It's%20common%20to%20use%201.96%20as%20value%20for%20the%2095%25%20prediction%20interval%20even%20when%20dealing%20with%20a%20sample%3B%20let's%20quantify%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_(np%2C%20stats)%3A%0A%20%20%20%20_T%20%3D%20lambda%20n%3A%20stats.t.ppf(0.975%2C%20n%20-%201)%20*%20np.sqrt(1%20%2B%201%20%2F%20n)%0A%20%20%20%20_N%20%3D%20stats.norm.ppf(0.975)%0A%20%20%20%20for%20n_1%20in%20%5B1000%2C%20100%2C%2010%5D%3A%0A%20%20%20%20%20%20%20%20print('%5CnApproximation%20error%20for%20n%20%3D%20%25d'%20%25%20n_1)%0A%20%20%20%20%20%20%20%20print('Using%20Normal%20distribution%3A%20%25.1f%25%25'%20%25%20(100%20*%20(_N%20-%20_T(n_1))%20%2F%20_T(n_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%20**Exercise%203.**%20For%20%24n%3D10%24%2C%20sample%20mean%200%2C%20SD%201%2C%20and%20%24t_%7B9%2C0.975%7D%3D2.262%24%2C%20compute%20the%20half-width%20for%20predicting%20one%20new%20observation.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20It%20is%20%242.262%5Csqrt%7B1%2B1%2F10%7D%3D2.373%24.%20The%20factor%20includes%20variability%20of%20the%20new%20observation%20as%20well%20as%20uncertainty%20in%20the%20estimated%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%20For%20n%3D1000%2C%20the%20approximation%20is%20good%2C%20for%20n%3D10%20it%20is%20bad%2C%20and%20it%20always%20underestimates.%0A%0A%20%20%20%20**Standard%20error%20of%20the%20mean**%0A%0A%20%20%20%20The%20%5Bstandard%20error%20of%20the%20mean%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FStandard_error)%20(sem)%20is%20the%20standard%20deviation%20of%20the%20sample-mean%20estimate%20of%20a%20population%20mean%20and%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20sem%20%3D%20%5Cfrac%7Bs%7D%7B%5Csqrt%7Bn%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20**Confidence%20interval**%0A%0A%20%20%20%20In%20statistics%2C%20a%20%5Bconfidence%20interval%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FConfidence_interval)%20(CI)%20is%20a%20type%20of%20interval%20estimate%20of%20a%20population%20parameter%20and%20is%20used%20to%20indicate%20the%20reliability%20of%20an%20estimate%20(%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FConfidence_interval)).%20For%20instance%2C%20the%2095%25%20confidence%20interval%20for%20the%20sample-mean%20estimate%20of%20a%20population%20mean%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%5B%5Cbar%7Bx%7D%20-%20T_%7Bn-1%7D%5C%3As%2F%5Csqrt%7Bn%7D%2C%5Cquad%20%5Cbar%7Bx%7D%20%2B%20T_%7Bn-1%7D%5C%3As%2F%5Csqrt%7Bn%7D%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24T_%7Bn-1%7D%24%20is%20the%20%24100((1%2Bp)%2F2)%5E%7Bth%7D%24%20percentile%20of%20the%20Student's%20t-distribution%20with%20n%E2%88%921%20degrees%20of%20freedom.%3Cbr%3E%0A%20%20%20%20For%20instance%2C%20the%20confidence%20interval%20for%20the%20mean%20with%2095%25%20of%20probability%20for%20a%20sample%20(%24%5Cbar%7Bx%7D%3D0%2C%5C%3Bs%5E2%3D1%24)%20with%20size%20equals%20to%2010%20is%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20stats)%3A%0A%20%20%20%20stats.t.interval(confidence%3D0.95%2C%20df%3D10-1%2C%20loc%3D0%2C%20scale%3D1)%20%2F%20np.sqrt(10)%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.**%20For%20the%20same%20sample%2C%20compute%20the%20confidence%20half-width%20for%20the%20population%20mean.%20Why%20is%20it%20smaller%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%204%0A%20%20%20%20It%20is%20%242.262%2F%5Csqrt%7B10%7D%3D0.715%24.%20The%20target%20is%20the%20mean%2C%20not%20a%20new%20observation%2C%20so%20the%20extra%20observation%20variance%20is%20absent.%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%20The%2095%25%20CI%20means%20that%20if%20we%20randomly%20obtain%20100%20samples%20of%20a%20population%20and%20calculate%20the%20CI%20of%20each%20sample%20(i.e.%2C%20we%20replicate%20the%20experiment%2099%20times%20in%20a%20independent%20way)%2C%20about%2095%25%20of%20these%20CIs%20would%20contain%20the%20population%20mean%20over%20many%20repetitions%20(the%20true%20mean).%20This%20is%20different%20from%20the%20prediction%20interval%2C%20which%20is%20larger%2C%20and%20gives%20the%20probability%20that%20a%20new%20observation%20is%20inside%20this%20interval.%20Note%20that%20the%20confidence%20interval%20DOES%20NOT%20give%20the%20probability%20that%20the%20true%20mean%20(the%20mean%20of%20the%20population)%20is%20inside%20this%20interval.%20The%20true%20mean%20is%20a%20parameter%20(fixed)%20and%20it%20is%20either%20inside%20the%20calculated%20interval%20or%20not%3B%20it%20is%20not%20a%20matter%20of%20chance%20(probability).%0A%0A%20%20%20%20Let's%20simulate%20samples%20of%20a%20population%20~%20%24N(%5Cmu%3D0%2C%20%5Csigma%5E2%3D1)%24%20and%20calculate%20the%20confidence%20interval%20for%20the%20samples'%20mean%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20stats)%3A%0A%20%20%20%20n_2%20%3D%2020%0A%20%20%20%20_x%20%3D%20np.random.randn(n_2%2C%20100)%0A%20%20%20%20m_1%20%3D%20np.mean(_x%2C%20axis%3D0)%0A%20%20%20%20s_1%20%3D%20np.std(_x%2C%20axis%3D0%2C%20ddof%3D1)%0A%20%20%20%20_T%20%3D%20stats.t.ppf(0.975%2C%20n_2%20-%201)%0A%20%20%20%20ci%20%3D%20m_1%20%2B%20np.array(%5B-s_1%20*%20_T%20%2F%20np.sqrt(n_2)%2C%20s_1%20*%20_T%20%2F%20np.sqrt(n_2)%5D)%0A%20%20%20%20out%20%3D%20ci%5B0%2C%20%3A%5D%20*%20ci%5B1%2C%20%3A%5D%20%3E%200%0A%20%20%20%20return%20ci%2C%20m_1%2C%20n_2%2C%20out%2C%20s_1%0A%0A%0A%40app.cell%0Adef%20_(ci%2C%20m_1%2C%20n_2%2C%20np%2C%20out%2C%20plt)%3A%0A%20%20%20%20(_fig%2C%20_ax)%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(9%2C%205))%0A%20%20%20%20ind%20%3D%20np.arange(1%2C%20101)%0A%20%20%20%20_ax.axhline(y%3D0%2C%20xmin%3D0%2C%20xmax%3Dn_2%20%2B%201%2C%20color%3D%5B0%2C%200%2C%200%5D)%0A%20%20%20%20_ax.plot(%5Bind%2C%20ind%5D%2C%20ci%2C%20color%3D%5B0%2C%200.2%2C%200.8%2C%200.8%5D%2C%20marker%3D'_'%2C%20ms%3D0%2C%20linewidth%3D3)%0A%20%20%20%20_ax.plot(%5Bind%5Bout%5D%2C%20ind%5Bout%5D%5D%2C%20ci%5B%3A%2C%20out%5D%2C%20color%3D%5B1%2C%200%2C%200%2C%200.8%5D%2C%20marker%3D'_'%2C%20ms%3D0%2C%20linewidth%3D3)%0A%20%20%20%20_ax.plot(ind%2C%20m_1%2C%20color%3D%5B0%2C%200.8%2C%200.2%2C%200.8%5D%2C%20marker%3D'.'%2C%20ms%3D10%2C%20linestyle%3D'')%0A%20%20%20%20_ax.set_xlim(0%2C%20101)%0A%20%20%20%20_ax.set_ylim(-1.1%2C%201.1)%0A%20%20%20%20_ax.set_title(%22Confidence%20interval%20for%20the%20samples'%20mean%20estimate%20of%20a%20population%20~%24N(0%2C%201)%24%22%2C%20fontsize%3D14)%0A%20%20%20%20_ax.set_xlabel('Sample%20(with%20%25d%20observations)'%20%25%20n_2%2C%20fontsize%3D14)%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%20Count%20how%20many%20intervals%20miss%20the%20true%20mean%20in%20this%20run.%20About%20five%20misses%20per%20hundred%20are%20expected%2C%20but%20the%20observed%20count%20varies%20from%20run%20to%20run.%0A%0A%20%20%20%20And%20the%20standard%20deviation%20of%20the%20samples'%20mean%20per%20definition%20should%20be%20equal%20to%20the%20standard%20error%20of%20the%20mean%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(m_1%2C%20np%2C%20s_1)%3A%0A%20%20%20%20print(%22Samples'%20mean%20and%20standard%20deviation%3A%22)%0A%20%20%20%20print('m%20%3D%20%25.3f%20%20%20s%20%3D%20%25.3f'%20%25%20(np.mean(m_1)%2C%20np.mean(s_1)))%0A%20%20%20%20print(%22Standard%20deviation%20of%20the%20samples'%20mean%3A%22)%0A%20%20%20%20print('%25.3f'%20%25%20np.std(m_1%2C%20ddof%3D1))%0A%20%20%20%20print('Standard%20error%20of%20the%20mean%3A')%0A%20%20%20%20print('%25.3f'%20%25%20(np.mean(s_1)%20%2F%20np.sqrt(20)))%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%20Likewise%2C%20it's%20common%20to%20use%201.96%20for%20the%2095%25%20confidence%20interval%20even%20when%20dealing%20with%20a%20sample%3B%20let's%20quantify%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_(stats)%3A%0A%20%20%20%20_T%20%3D%20lambda%20n%3A%20stats.t.ppf(0.975%2C%20n%20-%201)%0A%20%20%20%20_N%20%3D%20stats.norm.ppf(0.975)%0A%20%20%20%20for%20n_3%20in%20%5B1000%2C%20100%2C%2010%5D%3A%0A%20%20%20%20%20%20%20%20print('%5CnApproximation%20error%20for%20n%20%3D%20%25d'%20%25%20n_3)%0A%20%20%20%20%20%20%20%20print('Using%20Normal%20distribution%3A%20%25.1f%25%25'%20%25%20(100%20*%20(_N%20-%20_T(n_3))%20%2F%20_T(n_3)))%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%20good%2C%20for%20n%3D10%20it%20is%20bad%2C%20and%20it%20always%20underestimates.%0A%0A%20%20%20%20For%20the%20case%20of%20a%20multivariate%20random%20variable%2C%20see%20%5BPrediction%20ellipse%20and%20prediction%20ellipsoid%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fprediction_ellipse_ellipsoid.py).%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%205.**%20If%20sample%20size%20rises%20from%2025%20to%20100%20with%20similar%20SD%2C%20what%20happens%20to%20SEM%20and%20to%20the%20large-sample%20prediction%20width%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%205%0A%20%20%20%20SEM%20changes%20from%20%24s%2F5%24%20to%20%24s%2F10%24%2C%20approximately%20halving%20the%20mean%E2%80%99s%20interval.%20The%20prediction%20factor%20changes%20only%20from%20%24%5Csqrt%7B1.04%7D%24%20to%20%24%5Csqrt%7B1.01%7D%24%3B%20individual%20variability%20remains.%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%20Verification%20of%20the%20relation%20between%20standard%20deviation%20and%20standard%20error%20of%20the%20mean%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20_rc%20%3D%20%7B'axes.labelsize'%3A%2012%2C%20'font.size'%3A%2012%2C%20'legend.fontsize'%3A%2012%2C%20'axes.titlesize'%3A%2012%7D%0A%20%20%20%20plt.rcParams.update(**_rc)%0A%20%20%20%20(_fig%2C%20_ax)%20%3D%20plt.subplots(1%2C%205%2C%20sharey%3DTrue%2C%20squeeze%3DTrue%2C%20figsize%3D(9%2C%203))%0A%20%20%20%20print('%7B0%3A10%7D%20%7B1%3A10%7D%20%7B2%3A5%7D%20%7B3%3A10%7D'.format('n'%2C%20'm'%2C%20's'%2C%20'1%2Fnp.sqrt(n)'))%0A%20%20%20%20for%20(_i%2C%20n_4)%20in%20enumerate(%5B1%2C%202%2C%203%2C%2010%2C%20100%5D)%3A%0A%20%20%20%20%20%20%20%20_f%20%3D%20np.mean(np.random.randn(1000%2C%20n_4)%2C%201)%0A%20%20%20%20%20%20%20%20(m_2%2C%20s_2)%20%3D%20(np.mean(_f)%2C%20np.std(_f%2C%20ddof%3D1))%0A%20%20%20%20%20%20%20%20_x%20%3D%20np.linspace(np.min(_f)%2C%20np.max(_f)%2C%20100)%0A%20%20%20%20%20%20%20%20_fn%20%3D%201%20%2F%20(s_2%20*%20np.sqrt(2%20*%20np.pi))%20*%20np.exp(-(_x%20-%20m_2)%20**%202%20%2F%20(2%20*%20s_2%20**%202))%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.hist(_f%2C%2020%2C%20density%3DTrue%2C%20color%3D%5B0%2C%200.2%2C%200.8%2C%200.6%5D%2C%20edgecolor%3D'black')%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.set_title('n%3D%25d'%20%25%20n_4%2C%20fontsize%3D12)%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.plot(_x%2C%20_fn%2C%20color%3D%5B1%2C%200%2C%200%2C%200.6%5D%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.xaxis.label.set_size(12)%0A%20%20%20%20%20%20%20%20print('%7B0%3A3%7D%20%7B1%3A10.4f%7D%20%7B2%3A10.4f%7D%20%7B3%3A10.4f%7D'.format(n_4%2C%20m_2%2C%20s_2%2C%201%20%2F%20np.sqrt(n_4)))%0A%20%20%20%20plt.suptitle('Standard%20deviation%20and%20standard%20error%20of%20the%20mean'%2C%20fontsize%3D12%2C%20y%3D1.0)%0A%20%20%20%20plt.tight_layout()%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20_rc%20%3D%20%7B'axes.labelsize'%3A%2012%2C%20'font.size'%3A%2012%2C%20'legend.fontsize'%3A%2012%2C%20'axes.titlesize'%3A%2012%7D%0A%20%20%20%20plt.rcParams.update(**_rc)%0A%20%20%20%20(_fig%2C%20_ax)%20%3D%20plt.subplots(1%2C%205%2C%20sharey%3DTrue%2C%20squeeze%3DTrue%2C%20figsize%3D(9%2C%203))%0A%20%20%20%20print('%7B0%3A10%7D%20%7B1%3A10%7D%20%7B2%3A5%7D%20%7B3%3A10%7D'.format('n'%2C%20'm'%2C%20's'%2C%20'1%2Fnp.sqrt(n)'))%0A%20%20%20%20for%20(_i%2C%20n_5)%20in%20enumerate(%5B1%2C%202%2C%203%2C%2010%2C%20100%5D)%3A%0A%20%20%20%20%20%20%20%20_f%20%3D%20np.mean(np.random.randn(100%2C%20n_5)%2C%201)%0A%20%20%20%20%20%20%20%20(m_3%2C%20s_3)%20%3D%20(np.mean(_f)%2C%20np.std(_f%2C%20ddof%3D1))%0A%20%20%20%20%20%20%20%20_x%20%3D%20np.linspace(np.min(_f)%2C%20np.max(_f)%2C%20100)%0A%20%20%20%20%20%20%20%20_fn%20%3D%201%20%2F%20(s_3%20*%20np.sqrt(2%20*%20np.pi))%20*%20np.exp(-(_x%20-%20m_3)%20**%202%20%2F%20(2%20*%20s_3%20**%202))%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.hist(_f%2C%2010%2C%20density%3DTrue%2C%20color%3D%5B0%2C%200.2%2C%200.8%2C%200.6%5D%2C%20edgecolor%3D'black')%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.set_title('n%3D%25d'%20%25%20n_5%2C%20fontsize%3D12)%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.plot(_x%2C%20_fn%2C%20color%3D%5B1%2C%200%2C%200%2C%200.6%5D%2C%20linewidth%3D2)%0A%20%20%20%20%20%20%20%20_ax%5B_i%5D.xaxis.label.set_size(12)%0A%20%20%20%20%20%20%20%20print('%7B0%3A3%7D%20%7B1%3A10.4f%7D%20%7B2%3A10.4f%7D%20%7B3%3A10.4f%7D'.format(n_5%2C%20m_3%2C%20s_3%2C%201%20%2F%20np.sqrt(n_5)))%0A%20%20%20%20plt.suptitle('Standard%20deviation%20and%20standard%20error%20of%20the%20mean'%2C%20fontsize%3D12%2C%20y%3D1.0)%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%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20What%20does%20a%2095%25%20confidence%20procedure%20cover%20over%20repeated%20samples%3F%0A%20%20%20%202.%20Which%20interval%20predicts%20a%20new%20independent%20observation%3F%0A%20%20%20%203.%20What%20assumptions%20make%20the%20finite-sample%20t%20formulas%20exact%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20The%20fixed%20population%20mean%20in%2095%25%20of%20repetitions%20in%20the%20long%20run.%0A%20%20%20%202.%20A%20prediction%20interval.%0A%20%20%20%203.%20Independent%20observations%20from%20a%20normal%20population%2C%20with%20the%20future%20observation%20drawn%20independently%20from%20the%20same%20population.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20With%20%24n%3D25%24%2C%20mean%2010%2C%20SD%202%2C%20and%20%24t_%7B24%2C0.975%7D%3D2.064%24%2C%20give%20the%2095%25%20CI.%0A%20%20%20%202.%20Give%20the%20corresponding%2095%25%20prediction%20interval.%0A%20%20%20%203.%20If%20%24n%24%20becomes%20very%20large%2C%20does%20the%20prediction%20interval%20collapse%20to%20the%20mean%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%20Approximately%20%5B9.174%2C%2010.826%5D.%0A%20%20%20%202.%20Approximately%20%5B5.790%2C%2014.210%5D.%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%5BNIST%20confidence%20intervals%5D(https%3A%2F%2Fwww.itl.nist.gov%2Fdiv898%2Fhandbook%2Fprc%2Fsection1%2Fprc14.htm)%20%E2%80%94%20repeated-sampling%20interpretation.%20%5BPrediction%20ellipses%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Fprediction_ellipse_ellipsoid.py)%20extends%20prediction%20to%20several%20variables.%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%20use%20the%20lectures%20on%20estimation%20and%20confidence%20intervals%20to%20connect%20the%20numerical%20examples%20with%20repeated%20sampling.%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-%20Hahn%20GJ%2C%20Meeker%20WQ%20(1991)%20%5BStatistical%20Intervals%3A%20A%20Guide%20for%20Practitioners%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3DADGuRxqt5z4C).%20John%20Wiley%20%26%20Sons.%0A%0A%20%20%20%20-%20Montgomery%20(2013)%20%5BApplied%20Statistics%20and%20Probability%20for%20Engineers%5D(https%3A%2F%2Fbooks.google.com.br%2Fbooks%3Fid%3D_f4KrEcNAfEC).%20John%20Wiley%20%26%20Sons.%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
ae325cdabc0b0429cdb67cb3b088b21e