<jupyter_start><jupyter_text>Examles of error propigationExamples are taken from http://ipl.physics.harvard.edu/wp-uploads/2013/03/PS3_Error_Propagation_sp13.pdf and used on MCMC to show how the answers work  Example 1 Example: suppose you measure the height H of a door and get 2.00 ± 0.03 m. This means thatH = 2.00 m and δH = 0.03 m. The door has a knob which is a height h = 0.88 ± 0.04 m from the bottomof the door. Then the distance from the doorknob to the top of the door is Q = H − h = 1.12 m. Whatis the uncertainty in Q?Q = 1.12 ± 0.05 m<jupyter_code>import numpy as np
import pymc3 as pm
import seaborn as sns

sns.set(font_scale=1.5)
%matplotlib inline<jupyter_output><empty_output><jupyter_text>This version uses prior distributions to do all the work. H and h are both informative priors that then drive the solution to the right answer.<jupyter_code>with pm.Model() as model:

    H = pm.Normal('H', 2.00, sigma=0.03)
    h = pm.Normal('h', 0.88, sigma=0.04)
    Q = pm.Deterministic('Q', H-h)
    trace = pm.sample(10000)

    
pm.summary(trace).round(3)

pm.traceplot(trace, combined=False)
print("MCMC gives {:.2f} +/- {:.2f}, analytic gives {} +/- {}".format(trace['Q'].mean(), 
                                                                          trace['Q'].std(), 1.12, 0.05))<jupyter_output>MCMC gives 1.12 +/- 0.05, analytic gives 1.12 +/- 0.05<jupyter_text>Example 2 Example: a bird flies a distance d = 120 ± 3 m during a time t = 20.0 ± 1.2 s. The average speed ofthe bird is v = d/t = 6 m/s. What is the uncertainty of v?0.39 m/s.<jupyter_code>with pm.Model() as model:
    d = pm.Normal('d', 123, tau=(3)**-2)
    t = pm.Normal('t', 20.0, tau=(1.2)**-2)
    v = pm.Deterministic('v', d/t)
    trace = pm.sample(40000, chains=4)
pm.summary(trace).round(3)
pm.traceplot(trace, combined=False, lines=[('d', {}, 123), ('t', {}, 20), ('v', {}, 6)])
print("MCMC gives {0:.2f}, analytic gives {1}".format(trace['v'].std(), 0.39))<jupyter_output>MCMC gives 0.40, analytic gives 0.39<jupyter_text>Example 3 Example: the period of an oscillation is measured to be T = 0.20 ± 0.01 s. Thus the frequency isf = 1/T = 5 Hz. What is the uncertainty in f? Answer: the percent uncertainty in T was 0.01/0.20 = 5%.Thus the percent uncertainty in f is also 5%, which means that δf = 0.25 Hz. So f = 5.0 ± 0.3 Hz (afterrounding).f = 5.0 ± 0.3 Hz<jupyter_code>with pm.Model() as model:
    T = pm.Normal('T', 0.20, tau=(0.01)**-2)

    pm.Deterministic('1/T', 1/T)

    trace = pm.sample(10000, tune=1000)

pm.traceplot(trace, combined=False)
pm.summary(trace).round(3)
print("MCMC gives {0:.1f} +/- {1:.1f}, analytic gives {2} +/- {3}".format(np.mean(trace['1/T']), 
                                                                          np.std(trace['1/T']), 
                                                                          5.0, 0.3))<jupyter_output>MCMC gives 5.0 +/- 0.3, analytic gives 5.0 +/- 0.3<jupyter_text>Example 4 Example: a ball is tossed straight up into the air with initial speed v0 = 4.0 ± 0.2 m/s. After a timet = 0.60±0.06 s, the height of the ball is y = v0t−12gt2 = 0.636 m. What is the uncertainty of y? Assumeg = 9.80 m/s2(no uncertainty in g).Thus y would be properly reported as 0.6 ± 0.4 m.<jupyter_code>with pm.Model() as model:
    g = 9.80
    t = pm.Normal('t', 0.60, tau=(0.06)**-2)
    v0 = pm.Normal('v0', 4.0, tau=(0.2)**-2)

    h = pm.Deterministic('h', v0*t - 0.5*g*t**2)

    trace = pm.sample(10000)


pm.traceplot(trace, combined=False)
pm.summary(trace).round(3)
print("MCMC gives {0:.1f} +/- {1:.1f}, analytic gives {2} +/- {3}".format(np.mean(trace['h']), 
                                                                          np.std(trace['h']), 
                                                                          0.6, 0.4))<jupyter_output>MCMC gives 0.6 +/- 0.2, analytic gives 0.6 +/- 0.4<jupyter_text>Example 5For example, suppose Ann and Billy both measure the speed of a moving ball. Ann measures 3.6±0.2 m/sand Billy gets 3.3 ± 0.3 m/s. Do the two measurements agree?D = 0.3 ± 0.4 m/s  so 0 is in the range, they do agree.<jupyter_code>with pm.Model() as model:

    A = pm.Normal('A', 3.6, tau=(0.2)**-2)
    B = pm.Normal('B', 3.3, tau=(0.3)**-2)

    D = pm.Deterministic('D', A-B)

    trace = pm.sample(1000, chains=6)

pm.summary(trace).round(3)
pm.traceplot(trace, combined=False);
print("MCMC gives {0:.1f} +/- {1:.1f}, analytic gives {2} +/- {3}".format(np.mean(trace['D']), 
                                                                          np.std(trace['D']), 
                                                                          0.3, 0.4))<jupyter_output>MCMC gives 0.3 +/- 0.4, analytic gives 0.3 +/- 0.4
{'fraction_non_alphanumeric': 0.15836336654653382, 'fraction_numerical': 0.06699173203307186, 'mean_word_length': 2.2832289491997217, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '338885', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1626, 'n_words': 514, 'path': 'Propagation_of_uncertainty/Examples-pymc3.ipynb', 'repo_name': 'balarsen/pymc_learning', 'size': '4717'}