%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%22sympy%3E%3D1.13%22%2C%20%22uncertainties%3E%3D3.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%20Propagation%20of%20uncertainty%20(error)%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%20calculator%20reports%20many%20digits%2C%20but%20the%20measurements%20behind%20them%20may%20be%20uncertain.%20We%20start%20with%20density%2C%20work%20through%20a%20first-order%20uncertainty%20calculation%2C%20and%20let%20Python%20repeat%20the%20arithmetic%20and%20symbolic%20differentiation.%20Keep%20the%20physical%20units%20and%20the%20meaning%20of%20one%20standard%20deviation%20visible.%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%20density%20is%20mass%20divided%20by%20volume.%20If%20both%20measurements%20have%20uncertainty%2C%20is%20reporting%20their%20ratio%20to%20six%20decimal%20places%20useful%3F%20Which%20measurement%20would%20you%20improve%20first%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%20The%20%60uncertainties%60%20package%20tracks%20first-order%20propagation%20and%20shared%20variables%3B%20SymPy%20derives%20formulas.%20Both%20are%20declared%20dependencies.%20All%20examples%20use%20stated%20hypothetical%20measurements%2C%20not%20external%20files.%20The%20notebook%20fails%20visibly%20if%20a%20required%20package%20is%20unavailable.%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%20Evaluation%20of%20measurement%20data%0A%0A%20%20%20%20%3E%20The%20result%20of%20a%20measurement%20is%20only%20an%20approximation%20or%20estimate%20of%20the%20value%20of%20the%20measurand%20and%20thus%20is%20complete%20only%20when%20accompanied%20by%20a%20statement%20of%20the%20uncertainty%20of%20that%20estimate.%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20Uncertainty%20(of%20measurement)%20is%20a%20parameter%2C%20associated%20with%20the%20result%20of%20a%20measurement%2C%20that%20characterizes%20the%20dispersion%20of%20the%20values%20that%20could%20reasonably%20be%20attributed%20to%20the%20measurand.%0A%20%20%20%20%3E%0A%20%20%20%20%3E%20%5BEvaluation%20of%20measurement%20data%20-%20Guide%20to%20the%20expression%20of%20uncertainty%20in%20measurement%20(2008)%5D(https%3A%2F%2Fdoi.org%2F10.59161%2FJCGM100-2008E)%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%20instance%2C%20to%20calculate%20the%20density%20of%20a%20material%20someone%20made%20the%20following%20measurements%20of%20mass%20and%20volume%20(%24d%3Dm%2FV%24)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20m%20%3D%204.0%20%5Cpm%200.5%20%5C%3B%20g%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20V%20%3D%202.0%20%5Cpm%200.2%20%5C%3B%20cm%5E3%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%200.5%20g%20and%200.2%20cm%24%5E3%24%20each%20represent%20a%20value%20of%20one%20standard%20deviation%20and%20express%20the%20standard%20uncertainties%20of%20the%20measured%20mass%20and%20volume.%0A%0A%20%20%20%20Based%20on%20these%20numbers%2C%20the%20material%20density%20is%202%20g%2Fcm%24%5E3%24%3B%20but%20how%20much%20is%20the%20error%20(uncertainty)%20of%20the%20density%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%20One%20way%20to%20estimate%20the%20error%20in%20the%20density%20is%20by%20propagation%20of%20uncertainty%3A%0A%0A%20%20%20%20%3E%20Propagation%20of%20uncertainty%20(or%20propagation%20of%20error)%20is%20the%20effect%20of%20variables'%20uncertainties%20(or%20errors)%20on%20the%20uncertainty%20of%20a%20function%20based%20on%20them.%20When%20the%20variables%20are%20the%20values%20of%20experimental%20measurements%20they%20have%20uncertainties%20due%20to%20measurement%20limitations%20(e.g.%2C%20instrument%20precision%20and%20noise)%20which%20propagate%20to%20the%20combination%20of%20variables%20in%20the%20function%20(%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPropagation_of_uncertainty)).%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%20Propagation%20of%20uncertainty%20by%20linear%20approximation%0A%0A%20%20%20%20The%20classic%20way%20for%20propagation%20of%20uncertainty%20is%20through%20linear%20approximation%20of%20error%20propagation%20theory.%20We%20start%20with%20the%20mathematical%20function%20that%20relates%20the%20physical%20quantities%20and%20estimate%20the%20contribution%20of%20their%20variations%20by%20means%20of%20partial%20derivatives%20(the%20rate%20of%20variation)%20of%20each%20of%20these%20primary%20quantities.%20The%20linearity%20of%20the%20mathematical%20function%20in%20the%20vicinity%20of%20the%20obtained%20value%20and%20the%20statistical%20correlations%20between%20these%20quantities%20are%20also%20taken%20into%20consideration%20in%20the%20propagation%20of%20uncertainty.%0A%0A%20%20%20%20If%20the%20measurand%20%24f%24%20is%20defined%20in%20terms%20of%20the%20variables%20%24x%2C%20y%2C%20z%2C%20...%24%20by%20a%20general%20expression%20%24f(x%2C%20y%2C%20z%2C%20...)%24%2C%20a%20first%20order%20approximation%20of%20the%20propagation%20of%20uncertainty%20ignoring%20correlations%20between%20these%20variables%20is%20given%20by%20the%20well%20known%20formula%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma%20_%7Bf%7D%5C%3B%3D%5C%3B%5Csqrt%7B%5Cleft%20(%20%5Cfrac%7B%5Cpartial%20f%7D%7B%5Cpartial%20x%7D%20%20%5Cright)%5E2%5Csigma%20_%7Bx%7D%5E%7B2%7D%20%2B%20%5Cleft%20(%20%5Cfrac%7B%5Cpartial%20f%7D%7B%5Cpartial%20y%7D%20%5Cright)%5E2%20%5Csigma%20_%7By%7D%5E%7B2%7D%20%2B%20%5Cleft%20(%20%5Cfrac%7B%5Cpartial%20f%7D%7B%5Cpartial%20z%7D%20%5Cright)%5E2%20%5Csigma%20_%7Bz%7D%5E%7B2%7D%5C%3A%20%2B%20%5Cldots%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24%5Cpartial%20f%2F%5Cpartial%20i%24%20is%20the%20first%20partial%20derivative%20of%20%24f%24%20with%20respect%20to%20variable%20%24i%24%20and%20%24%5Csigma_i%24%20is%20the%20uncertainty%20of%20the%20measurement%20of%20the%20variable%20%24i%24%20(one%20standard%20deviation).%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%20Using%20the%20former%20formula%20for%20linear%20approximation%20of%20error%20propagation%2C%20the%20estimation%20of%20the%20uncertainty%20of%20the%20density%20is%20given%20by%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20d%3D%5Cfrac%7Bm%7D%7BV%7D%20%5C%5C%0A%20%20%20%20%5Csigma%20_%7Bd%7D%5C%3B%3D%5C%3B%5Csqrt%7B%20%5Cleft%20(%20%5Cfrac%7B%5Cpartial%20d%7D%7B%5Cpartial%20m%7D%20%20%5Cright)%5E2%20%5Csigma%20_%7Bm%7D%5E%7B2%7D%2B%5Cleft%20(%20%5Cfrac%7B%5Cpartial%20d%7D%7B%5Cpartial%20V%7D%20%5Cright)%5E2%20%5Csigma%20_%7BV%7D%5E%7B2%7D%20%7D%20%5C%5C%0A%20%20%20%20%5Csigma%20_%7Bd%7D%5C%3B%3D%5C%3B%5Csqrt%7B%20%5Cleft%20(%20%5Cfrac%7B1%7D%7BV%7D%20%5Cright)%5E2%20%5Csigma%20_%7Bm%7D%5E%7B2%7D%2B%5Cleft%20(%20-%5Cfrac%7Bm%7D%7BV%5E2%7D%20%5Cright)%5E2%20%5Csigma%20_%7BV%7D%5E%7B2%7D%20%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20And%20considering%20the%20values%20of%20mass%20and%20volume%20given%20earlier%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma_%7Bd%7D%5C%3B%3D%5C%3B%5Csqrt%7B%5Cleft(%5Cfrac%7B1%7D%7B2%7D%5Cright)%5E2%5Ctimes%200.5%5C%3A%5E2%2B%5Cleft(-%5Cfrac%7B4%7D%7B2%5E2%7D%5Cright)%5E2%5Ctimes%200.2%5C%3A%5E2%7D%5C%3B%5C%3B%5Capprox%5C%3B%200.32%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Then%2C%20the%20estimated%20value%20of%20the%20density%20can%20now%20be%20expressed%20in%20a%20complete%20form%3A%20d%20%3D%202.00%C2%B10.32%20g%2Fcm%24%5E3%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%201.**%20Using%20the%20density%20example%2C%20compare%20the%20mass%20and%20volume%20contributions%20to%20the%20standard%20uncertainty.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20The%20magnitudes%20are%20%24(1%2FV)u_m%3D0.25%24%20and%20%24(m%2FV%5E2)u_V%3D0.20%24%20g%2Fcm%C2%B3.%20Their%20squared%20sum%20is%20%240.0625%2B0.04%24%2C%20giving%20%24u_d%3D0.3202%24%20g%2Fcm%C2%B3.%20Improving%20the%20mass%20measurement%20removes%20the%20slightly%20larger%20contribution.%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%20Using%20the%20%60uncertainties%60%20package%20to%20calculate%20uncertainty%0A%0A%20%20%20%20It%20is%20essential%20that%20any%20result%20of%20a%20measurement%20is%20accompanied%20by%20the%20estimate%20of%20its%20uncertainty%20as%20above%2C%20this%20way%20we%20can%20better%20understand%20the%20value%20of%20the%20measure%20and%20how%20reliable%20it%20is.%20However%2C%20it%20is%20unlikely%20that%20anyone%20will%20repeat%20these%20calculations%20in%20the%20day-to-day%20work%20(although%20there%20are%20tables%20with%20partial%20derivatives%20for%20the%20most%20common%20expressions).%20Therefore%2C%20it%20is%20useful%20a%20software%20that%20can%20perform%20these%20calculations%20(at%20least%20for%20checking%20the%20results%20manually%20obtained).%20Fortunately%2C%20there%20are%20software%20for%20the%20propagation%20of%20uncertainty%2C%20%5Bsee%20this%20list%20on%20Wikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FList_of_uncertainty_propagation_software).%0A%0A%20%20%20%20One%20such%20software%2C%20free%2C%20open%20source%2C%20and%20cross-platform%2C%20is%20the%20library%20for%20%5BPython%5D(https%3A%2F%2Fpython.org)%20called%20%5Buncertainties%5D(https%3A%2F%2Fpypi.org%2Fproject%2Funcertainties%2F).%20With%20%5Buncertainties%5D(https%3A%2F%2Fpypi.org%2Fproject%2Funcertainties%2F)%20installed%2C%20let's%20calculate%20again%20the%20uncertainty%20of%20the%20density%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%20from%20uncertainties%20import%20ufloat%0A%0A%20%20%20%20return%20(ufloat%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%20Let's%20input%20the%20values%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ufloat)%3A%0A%20%20%20%20_m%20%3D%20ufloat(nominal_value%3D4.0%2C%20std_dev%3D0.5)%0A%20%20%20%20_V%20%3D%20ufloat(2.0%2C%200.2)%0A%20%20%20%20d%20%3D%20_m%20%2F%20_V%0A%20%20%20%20print(d)%0A%20%20%20%20return%20(d%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%20The%20result%20we%20obtained%20before%2C%20but%20without%20having%20to%20deduct%20the%20partial%20derivatives%20because%20%5Buncertainties%5D(https%3A%2F%2Funcertainties.readthedocs.io%2Fen%2Flatest%2F)%20does%20this%20with%20an%20%5Balgorithm%20for%20automatic%20differentiation%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FAutomatic_differentiation).%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%20Displaying%20the%20output%20in%20different%20formats%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(d%2C%20mo)%3A%0A%20%20%20%20mo.output.append(mo.md(f'%24%24d%20%3D%20%7Bd%3A.3L%7D%20g%2Fcm%5E3%24%24'))%0A%20%20%20%20mo.output.append(mo.md(f'%24%24d%20%3D%20%7Bd%3A.2eL%7D%20g%2Fcm%5E3%24%24'))%0A%20%20%20%20mo.output.append(mo.md(f'%24%24d%20%3D%20%7Bd%3A.2uS%7D%20g%2Fcm%5E3%24%24'))%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%20operation%20above%20could%20have%20been%20performed%20in%20a%20single%20line%20(after%20you%20imported%20the%20necessary%20libraries%20at%20the%20first%20time%20using%20them)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(ufloat)%3A%0A%20%20%20%20print(ufloat(4.0%2C%200.5)%20%2F%20ufloat(2.0%2C%200.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%20And%20how%20much%20each%20variable%20mass%20and%20volume%20contributed%20to%20the%20final%20uncertainty%20(i.e.%2C%20the%20partial%20derivative%20of%20density%20in%20relation%20to%20the%20variable%20mass%20or%20volume%20times%20its%20standard%20deviation%20(each%20term%20on%20the%20right%20side%20of%20the%20equation%20above%20without%20squaring))%20is%20given%20by%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(d)%3A%0A%20%20%20%20d.error_components().items()%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%20By%20definition%2C%20according%20to%20the%20formula%20above%2C%20the%20square%20root%20of%20the%20quadratic%20sum%20of%20these%20values%20is%20the%20total%20uncertainty%20for%20the%20density.%0A%0A%20%20%20%20The%20value%20of%20each%20partial%20derivative%20of%20the%20density%20function%20with%20respect%20to%20the%20mass%20and%20volume%20variables%20is%20given%20by%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(d)%3A%0A%20%20%20%20d.derivatives%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.**%20If%20%60x%60%20is%20one%20uncertain%20measurement%2C%20what%20should%20the%20uncertainty%20of%20%60x-x%60%20be%3F%20What%20if%20two%20independent%20measurements%20merely%20have%20identical%20reported%20values%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20The%20same%20measurement%20cancels%20exactly%2C%20so%20%60x-x%60%20has%20zero%20uncertainty.%20Two%20independently%20created%20quantities%20with%20SD%20%24u%24%20have%20difference%20SD%20%24%5Csqrt2u%24.%20Equal%20nominal%20values%20do%20not%20imply%20perfect%20correlation.%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%20Automatic%20deduction%20of%20the%20symbolic%20formula%20for%20error%20propagation%0A%0A%20%20%20%20Stepping%20back%2C%20we%20can%20even%20deduce%20the%20formula%20for%20the%20error%20propagation%20using%20the%20partial%20derivatives%20in%20case%20we%20want%20the%20actual%20formula%20using%20%5BSympy%5D(http%3A%2F%2Fsympy.org%2Fen%2Findex.html)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20from%20sympy%20import%20symbols%2C%20Symbol%2C%20sqrt%2C%20Add%2C%20Mul%2C%20Pow%2C%20init_printing%2C%20latex%0A%20%20%20%20init_printing()%0A%0A%20%20%20%20def%20stdform(formula%2C%20formula_symb%2C%20*errors)%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20Calculate%20the%20symbolic%20formula%20for%20error%20propagation%20using%20partial%20derivatives.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20from%20functools%20import%20reduce%0A%20%20%20%20%20%20%20%20formula2%20%3D%20sqrt(reduce(Add%2C%20(Mul(Pow(formula.diff(var)%2C%202%2C%20evaluate%3DFalse)%2C%20Symbol(f'%5C%5Csigma_%7Bvar.name%7D'%2C%20commutative%3DFalse)%20**%202%2C%20evaluate%3DFalse)%20for%20var%20in%20formula.atoms(Symbol)%20if%20var.name%20in%20errors)%2C%200.0))%0A%20%20%20%20%20%20%20%20formula3%20%3D%20sqrt(reduce(Add%2C%20(Mul(Pow(formula.diff(var)%20%2F%20formula%2C%202%2C%20evaluate%3DFalse)%2C%20Symbol(f'%5C%5Csigma_%7Bvar.name%7D'%2C%20commutative%3DFalse)%20**%202%2C%20evaluate%3DFalse)%20for%20var%20in%20formula.atoms(Symbol)%20if%20var.name%20in%20errors)%2C%200.0))%0A%20%20%20%20%20%20%20%20print('Uncertainty%3A')%0A%20%20%20%20%20%20%20%20mo.output.append(mo.md(f'%24%24%5C%5Csigma_%7Bformula_symb%7D%20%3D%20%7Blatex(formula2)%7D%24%24'))%0A%20%20%20%20%20%20%20%20print('Relative%20Uncertainty%3A')%0A%20%20%20%20%20%20%20%20mo.output.append(mo.md(f'%24%24%5C%5Cfrac%7B%7B%5C%5Csigma_%7Bformula_symb%7D%7D%7D%7Bformula_symb%7D%20%3D%20%7Blatex(formula3)%7D%24%24'))%0A%20%20%20%20%20%20%20%20return%20(formula2%2C%20formula3)%0A%0A%20%20%20%20return%20stdform%2C%20symbols%0A%0A%0A%40app.cell%0Adef%20_(stdform%2C%20symbols)%3A%0A%20%20%20%20print('Example%20for%20uncertainty%20propagation%20for%20the%20density%2C%20d%3Dm%2Fv%3A')%0A%20%20%20%20(_m%2C%20_V)%20%3D%20symbols('m%2C%20V')%0A%20%20%20%20d_1%20%3D%20_m%20%2F%20_V%0A%20%20%20%20_formula%20%3D%20stdform(d_1%2C%20'd'%2C%20'm'%2C%20'V')%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%20Same%20as%20before%2C%20but%20Sympy%20deduced%20for%20us.%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%20Another%20example%0A%0A%20%20%20%20Let's%20estimate%20the%20uncertainty%20in%20the%20measurement%20of%20the%20volume%20of%20a%20cylinder%20given%20its%20diameter%20(%24d%3D2.0%5Cpm0.1%24)%20cm%20and%20height%20%24h%3D10.0%5Cpm0.5%24%20cm.%20But%20now%2C%20let's%20go%20straight%20to%20the%20solution%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(stdform%2C%20symbols)%3A%0A%20%20%20%20from%20sympy%20import%20pi%20as%20_pi%0A%20%20%20%20(d_2%2C%20_h)%20%3D%20symbols('d%2C%20h')%0A%20%20%20%20_V%20%3D%20_h%20*%20_pi%20*%20(d_2%20%2F%202)%20**%202%0A%20%20%20%20_formula%20%3D%20stdform(_V%2C%20'V'%2C%20'd'%2C%20'h')%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20ufloat)%3A%0A%20%20%20%20from%20math%20import%20pi%20as%20_pi%0A%20%20%20%20_h%20%3D%20ufloat(nominal_value%3D10.0%2C%20std_dev%3D0.5)%0A%20%20%20%20d_3%20%3D%20ufloat(nominal_value%3D2.0%2C%20std_dev%3D0.1)%0A%20%20%20%20_V%20%3D%20_h%20*%20_pi%20*%20(d_3%20%2F%202)%20**%202%0A%20%20%20%20mo.output.append(mo.md(f'%24%24V%20%3D%20%7B_V%3A.3L%7D%20cm%5E3%24%24'))%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%20a%20cylinder%20%24V%3D%5Cpi%20d%5E2h%2F4%24%2C%20what%20happens%20to%20relative%20volume%20uncertainty%20when%20diameter%20uncertainty%20doubles%20and%20height%20is%20exact%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20The%20first-order%20relation%20is%20%24u_V%2FV%3D2u_d%2Fd%24%20when%20height%20is%20exact.%20Doubling%20%24u_d%24%20doubles%20%24u_V%2FV%24.%20The%20square%20on%20diameter%20makes%20relative%20diameter%20uncertainty%20twice%20as%20influential%20as%20equal%20relative%20height%20uncertainty.%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%20Other%20Python%20packages%20for%20uncertainty%20analysis%0A%0A%20%20%20%20The%20%60uncertainties%60%20package%20propagates%20means%20and%20standard%20deviations%20using%20a%20local%20linear%20approximation.%20It%20does%20not%20require%20Gaussian%20inputs%20for%20that%20propagation%2C%20but%20converting%20the%20result%20into%20a%20coverage%20probability%20needs%20distributional%20assumptions.%20Strong%20nonlinearity%20over%20the%20uncertainty%20range%20can%20make%20the%20approximation%20inaccurate.%20Alternatives%20include%3A%3Cbr%3E%0A%20%20%20%20-%20%5BNIST%20Uncertainty%20Machine%5D(https%3A%2F%2Funcertainty.nist.gov%2F)%3A%20*A%20Web-based%20software%20application%20to%20evaluate%20the%20measurement%20uncertainty%20associated%20with%20an%20output%20quantity%20defined%20by%20a%20measurement%20model%20of%20the%20form%20%24y%3Df(x_0%2C%09%5Cldots%2Cx_n)%24.*%20%20The%20NIST%20Uncertainty%20Machine%20allows%20to%20select%20a%20probability%20distribution%20for%20each%20of%20the%20input%20quantities%20and%20can%20evaluate%20the%20measurement%20uncertainty%20using%20two%20methods%3A%20the%20linear%20approximation%20and%20the%20Monte%20Carlo%20method%2C%0A%20%20%20%20-%20%5Bmcerp%5D(https%3A%2F%2Fgithub.com%2Ftisimst%2Fmcerp)%3A%20*A%20stochastic%20calculator%20for%20Monte%20Carlo%20methods%20that%20uses%20latin-hypercube%20sampling%20to%20perform%20non-order%20specific%20error%20propagation%20(or%20uncertainty%20analysis).*%3Cbr%3E%0A%20%20%20%20-%20%5Bsoerp%5D(https%3A%2F%2Fgithub.com%2Ftisimst%2Fsoerp)%3A%20*Python%20implementation%20of%20the%20original%20Fortran%20code%20SOERP%20by%20N.%20D.%20Cox%20to%20apply%20a%20second-order%20analysis%20to%20error%20propagation%20(or%20uncertainty%20analysis).%20The%20soerp%20package%20allows%20you%20to%20easily%20and%20transparently%20track%20the%20effects%20of%20uncertainty%20through%20mathematical%20calculations.*%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%20And%20there%20are%20other%20packages%20using%20linear%20error%20propagation%3A%0A%0A%20%20%20%20-%20%5Bscinum%5D(https%3A%2F%2Fgithub.com%2Friga%2Fscinum)%3A%20*Provides%20a%20simple%20Number%20class%20that%20wraps%20plain%20floats%20or%20NumPy%20arrays%20and%20adds%20support%20for%20multiple%20uncertainties%2C%20automatic%20(gaussian)%20error%20propagation%2C%20and%20scientific%20rounding.*%0A%20%20%20%20-%20%5BGUM%20Tree%20Calculator%5D(https%3A%2F%2Fgithub.com%2FMSLNZ%2FGTC)%3A%20*A%20Python%20package%20for%20processing%20data%20with%20measurement%20uncertainty.*%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%20Correlations%20and%20limits%20of%20the%20approximation%0A%0A%20%20%20%20For%20correlated%20inputs%2C%20the%20first-order%20variance%20is%20%24u_f%5E2%3D%5Cnabla%20f%5ET%20C%5Cnabla%20f%24%2C%20where%20%24C%24%20is%20the%20input%20covariance%20matrix.%20The%20sum%20of%20squared%20terms%20earlier%20assumes%20zero%20covariance.%20Reusing%20the%20same%20uncertain%20object%20preserves%20its%20dependence%3B%20creating%20a%20new%20object%20describes%20a%20distinct%20input.%0A%0A%20%20%20%20Near%20a%20turning%20point%20the%20first%20derivative%20can%20vanish%20even%20though%20the%20output%20varies.%20That%20is%20a%20warning%20to%20inspect%20higher-order%20effects%20or%20a%20simulation%20of%20the%20stated%20input%20distributions.%20Standard%20uncertainty%20is%20not%20an%20absolute%20error%20bound%20and%20is%20not%20automatically%20a%2095%25%20interval.%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%20In%20conclusion%0A%0A%20%20%20%20There%20is%20no%20excuse%20to%20not%20perform%20propagation%20of%20uncertainty%20of%20the%20result%20of%20a%20measurement.%20Remember%2C%20your%20work%20is%20complete%20only%20when%20you%20report%20the%20uncertainty.%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.%20Is%20uncertainty%20the%20same%20as%20a%20known%20measurement%20error%3F%0A%20%20%20%202.%20When%20can%20covariance%20terms%20be%20omitted%3F%0A%20%20%20%203.%20Does%20a%20one-SD%20uncertainty%20automatically%20mean%2095%25%20coverage%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20No%3B%20it%20quantifies%20dispersion%20consistent%20with%20available%20knowledge.%0A%20%20%20%202.%20When%20input%20covariances%20are%20zero%20or%20justifiably%20negligible.%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.%20Give%20the%20density%20and%20standard%20uncertainty%20for%20the%20original%20independent%20measurements.%0A%20%20%20%202.%20For%20independent%20lengths%20%24a%3D3.0%5Cpm0.1%24%20cm%20and%20%24b%3D4.0%5Cpm0.2%24%20cm%2C%20give%20the%20standard%20uncertainty%20of%20%24a%2Bb%24.%0A%20%20%20%203.%20For%20the%20cylinder%20example%2C%20give%20volume%20and%20first-order%20standard%20uncertainty.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%20%242.00%5Cpm0.32%24%20g%2Fcm%C2%B3.%0A%20%20%20%202.%20Approximately%200.224%20cm.%0A%20%20%20%203.%20Approximately%20%2431.42%5Cpm3.51%24%20cm%C2%B3.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20%5BJCGM%20GUM%20publications%5D(https%3A%2F%2Fwww.bipm.org%2Fen%2Fcommittees%2Fjc%2Fjcgm%2Fpublications)%20%E2%80%94%20measurement%20models%20and%20uncertainty.%20%5Buncertainties%20technical%20guide%5D(https%3A%2F%2Funcertainties.readthedocs.io%2Fen%2Flatest%2Ftech_guide.html)%20%E2%80%94%20correlations%20and%20local%20linearization.%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%20lectures%20on%20estimation%20provide%20a%20useful%20comparison%20with%20measurement%20uncertainty.%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%5BEvaluation%20of%20measurement%20data%20-%20Guide%20to%20the%20expression%20of%20uncertainty%20in%20measurement%5D(https%3A%2F%2Fdoi.org%2F10.59161%2FJCGM100-2008E).%20JCGM%20100%3A2008.%0A%0A%20%20%20%20-%20%5BAvalia%C3%A7%C3%A3o%20de%20dados%20de%20medi%C3%A7%C3%A3o%20-%20Guia%20para%20a%20express%C3%A3o%20de%20incerteza%20de%20medi%C3%A7%C3%A3o%5D(http%3A%2F%2Fwww.inmetro.gov.br%2Finovacao%2Fpublicacoes%2Fgum_final.pdf).%20GUM%202008%20in%20Portuguese.%0A%0A%20%20%20%20-%20%5Buncertainties%5D(https%3A%2F%2Funcertainties.readthedocs.io%2Fen%2Flatest%2F)%3A%20a%20Python%20package%20for%20calculations%20with%20uncertainties%2C%20Eric%20O.%20Lebigot.%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
f36dc126a6cfa0c7e30fe28fab77d8ab