Random Numbers Exercises


Instructions: Create a new notebook called RandomNumbersExercises in your RandomNumbers directory and solve the following problems inside it. Be sure to include the problem statements in a markdown cell above your solution. You don't need to put the "helper" code in the markdown cell, just implement the helper code in your code cell with your solution.

Preliminaries: At the top of your notebook, include a "Heading 1" cell with the title "Random" Numbers Exercises. Then include the inline functions and libraries by adding a code cell that invokes the %pylab inline magic and imports the needed packages.


Question 1

(a) Write a function to compute the mean of a set of random numbers and compare to the expected mean value:

def mean_test(samples,theory,pErrTol=0.1):
       '''Compute the mean of the distribution of samples and compare to 
       the expected mean value from the provided theoretical function.
       If the percent error is worse than the provided percent error (default of 0.1%),
       alert the user that the samples do not pass the test.

       Return the sample mean, percent error, and a boolean flag indicating
       whether the samples pass the test.
       '''
       testPassed = False  #initialize to False

       #
       #...your code here...
       #

       return sampleMean, percentError, testPassed

Your function should compute the mean of both the samples and the theoretical curve and the percent error. If the percent error is worse than the provided percent error tolerance value pErrTol, your function should alert the user that the samples do not pass. Return the values for the sample mean, the percent error and the flag indicating whether the test was passed or not.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Now test your function by generating sets of uniformly sampled random numbers on the interval U[0.,1.) for N = 10, 100, 1000, 10000. For each set of samples, use three values of the percent error tolerance: 1%, 0.1%, 0.01%.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 2

(a) Write a function to test whether the random samples pass the variance test:

def variance_test(samples,pErrTol=0.1,tolerance='strict'):
       '''Include a docstring describing what this function does
       '''
       testPassed = False  #initialize to False

       oneSigma = 0.68 #fraction of samples that should fall within +/- one sigma of the mean
       twoSigma = 0.95 #fraction of samples that should fall within +/1 two sigma of the mean
       #
       #...your code here...
       #

       return sampleSigma, sampleMean, testPassed

Your function should compute the mean and variance of the provided samples and compare the fraction of samples that fall within pErrTol percent of the expected values for one sigma and two sigma. Based on the tolerance, report to the user at what level the test passes or fails. i.e. if the user asks for "strict" tolerance, the samples must pass at the 2-sigma level. If the user asks for "loose" tolerance, the samples must pass at the 1-sigma level. Your function should alert the user if the samples do not pass.

Return the values for the sample mean, the sample sigma and the flag indicating whether the test was passed or not. Include a dosctring that summarizes what your function does.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Now test your function by generating sets of uniformly sampled random numbers on the interval U[0.,1.) for N = 10, 100, 1000, 10000. For each set of samples, run the test for both strict and loose criteria with the default percent error of 0.1%.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 3

(a) Write a function to determine whether a set of samples passes the chi-square test. For a set of N samples distributed uniformly on U[0.,1.), a perfect theoretical distribution divided into k bins would be


In [35]:
k = 100
theory = np.ones(k,'float')

The function to test the chi-square would look like

def chi2_test(samples,theory,pErrTol=0.1):
       '''Include a docstring describing what this function does
       '''
       testPassed = False  #initialize to False

       #
       #...your code here...
       #

       return chi2, k, percentError, testPassed

Your function should take as inputs an array of samples samples, the theoretical function they are meant to follow, and a percent error tolerance value pErrTol. Compute the chi-square from the provided inputs and compare to the expected mean value of k-1. If the percent error is less than pErrTol, consider the test passed.

Return the values for the chi-square, the number of bins used, the percent error and the flag indicating whether the test was passed or not. Include a dosctring that summarizes what your function does.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Now test your function by generating sets of uniformly sampled random numbers on the interval U[0.,1.) for N = 10, 100, 1000, 10000. For each set of samples, use a theoretical distribution divided into k = N/2 bins and run the test for the default percent error of 0.1%.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(c) Set up a loop to run your chi-square test 1000 times for samples of 1000 random numbers binned in k = 100 bins. Plot the distribution of chi-square values as a histogram. Compute the mean and variance of your chi-square distribution and compare them to the expected mean (k-1) and variance (2k-2). How well do they match?


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

Question 4

(a) Write a function to determine whether a set of samples passes the Kolmogorov-Smirnov test.

The function for the K-S test should look like:

def KS_test(samples,theory):
       '''Include a docstring describing what this function does
       '''
       testPassed = False  #initialize to False

       #
       #...your code here...
       #

       return Fnx_em, Fx_th, Kplus, Kminus, testPassed

Your function should take in the distribution of samples samples and the theoretical function the samples follow theory. The function should compute the theoretical cumulative distribution function (try np.cumsum or scipy.integrate.cumtrapz) from theory and the empirical distribution function from samples, then compute $K^+$ and $K^-$ and see if $K^+$ falls between 0.07089 and 1.5174. If so, the samples pass the test. Alert the user if they do not pass.

Return the empirical distribution function, the theoretical distribution function, the $K^+$ and $K^-$ values and the boolean flag.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(b) Now test your function by generating sets of uniformly sampled random numbers on the interval U[0.,1.) for N = 10, 100, 1000, 10000 and report the results from running the KS_test function on them.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(c) Make a figure with two subplots, one showing the empirical and theoretical distribution functions for the N = 10000 case on the same graph and one showing the ratio of the empirical to the theoretical distribution functions.


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

(d) Now run the K-S test 1000 times for a samples of 1000 uniformly distributed numbers on U[0.,1.). Plot distribution of $K^+$ values as a histogram. Describe the shape of the distribution. Compute the fraction of the values that fall within the range 0.07089 and 1.5174. Does it match the expectectation (98%)?


In [ ]:
#Copy the exercise statement to a markdown cell in your notebook and then implement a solution in a code cell

All content is under a modified MIT License, and can be freely used and adapted. See the full license text here.