Hints for Lab 2: Summary Statistics

Hints for Lab 2: Summary Statistics#

For the class on Wednesday, January 17th

See also

Go back to Lab 2

A. Sample Average and Central Limit Theorem#

Hints for Step 1
  • Here’s a snippet to get you started:

    import numpy as np
    def generate_exponential_rvs(l, size):
        rng = np.random.default_rng()
        exp_rvs = rng.exponential(1/l, size)
        return exp_rvs
    
    l = 0.5
    n = 500
    exp_rvs = generate_exponential_rvs(l, n)
    # TODO: calculate mean and variance of exp_rvs here
    
  • For the calculation of average and variance, consider using np.mean and np.var

Hints for Step 2
  • Here’s a snippet to get you started:

    k = 1000
    sample_averages = []
    for i in range(k):
        sample_averages.append(...) # TODO: replace ... with one sample average
    
    # TODO: Make a histogram of `sample_averages`
    # TODO: Calculate the variance of `sample_averages`
    

    Tip

    You can replace the Lines 2-4 with list comprehension

    sample_averages = [... for _ in range(k)] # TODO: replace ... with one sample average
    

B. Propagation of Uncertainty#

Hints for Steps 1 and 2
  • How would you model repeated measurements? We don’t usually know how the measurement errors are distributed, but they are commonly modeled as a normal distribution. Measurements of positive variables (e.g., magnitudes, sizes), can also be modeled as a log-normal distribution, especially if the measurement error tend to scale with the measured value (i.e., you care more about the fractional error, not the absolute). Note that when the error is very small, the specific distribution does not matter as much.

  • Here’s a snippet to get you started:

    import numpy as np
    
    rng = np.random.default_rng()
    
    mean = 5.0
    fractional_error = 0.01
    n_measurements = 1000
    
    # If modeled as a normal distribution
    measurements = rng.normal(loc=mean, scale=(mean * fractional_error), size=n_measurements)
    
    # _OR_, if modeled as a log-normal distribution
    measurements = rng.lognormal(sigma=np.log1p(fractional_error), size=n_measurements) * mean
    
    # TODO: Choose one of the above models, or use your own!
    # TODO: Verify that the fractional error of the simulated measurements is about 1%.
    
Hints for Step 3
  • Edit your code so that it generates measurements for both voltage and current.

  • Calculate the resistance using Ohm’s law \(V=IR\).

  • Calculate the fractional error of the resistance measurements.

Hints for Step 4
  • Here we can start with generating two sets of correlated uniform random variates, then use the inverse transform method (Lab 1A) to generate the measurements. Because the input uniform random variates are correlated, the “measurements” are correlated too.

    import numpy as np
    from scipy.stats import lognorm
    
    def correlated_uniform(size, strength):
        rng = np.random.default_rng()
        size = int(size)
        a = rng.random(size=size)
        if not strength:
            b = rng.random(size=size)
            return a, b
        strength = float(strength)
        flip = strength < 0
        s = np.argsort(a + rng.normal(size=size) / strength)
        if flip:
            s = s[::-1]
        b = np.sort(rng.random(size=size))[np.argsort(s)]
        return a, b
    
    n_measurements = 1000
    a, b = correlated_uniform(n_measurements, 3)
    # TODO: Check how correlated a and b are.
    
    # Generate measurements
    fractional_error = 0.01
    mean_V = 10.0
    mean_I = 5.0
    
    # Here we are using log-normal distribution as the model, but you can swap it with other distributions.
    measurements_V = lognorm.ppf(a, s=np.log1p(fractional_error)) * mean_V
    measurements_I = lognorm.ppf(b, s=np.log1p(fractional_error)) * mean_I
    
    # TODO: (1) Check how correlated measurements of V and I are. Is it similar to the correlation between a and b?
    # TODO: (2) Calculate the resistance measurements
    # TODO: (3) Measure the variance of the resistance measurements
    # TODO: (4) Check how the variance varies with the correlation between the measurements of V and I