Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Python Crash Course dd 04-09-2013 Hour 8, accuracy and random 1. Show the result of the example program (cancellation.py) cancellation if you replace X = x.astype('f') in function sqrt_single() by: X = x.astype('f8') X = x.astype('f16') Do you observe improvements? Are there still cancellation effects if we use the highest precision numbers? 2. The effect of cancellation should be avoided as much as possible. Use the cancellation.py script to create a program that avoids cancellation and prints the 'correct' roots for single precision floats. 3. The sample variance (square of the standard deviation) can be calculated in two ways. The standard way is written in the script variance.py. Implement the 'one pass' alternative to calculate the variance using the single precision values of x. Do not use operator ** as in x**2 to square numbers use * instead (as in x*x. What is the result and what went wrong? What happens if you used ** instead of *? You should get the right answers now. This is because the ** operator upgrades the result to higher precision. 4. Polynomial evaluation can be done with powers of x calculations or can be done by reducing is factorially, i.e. p(x) = 2 x3 + 6 x2 + 2 x -1 as p(x) = x(x(2x-6)+2)-1. Use the following code snippet to show the difference between the two methods xx = numpy.linspace( 0,10, 100000 ).astype(numpy.float32) c0 = time.clock() for x in xx: y = 2*x*x*x -6*x*x + 2*x - 1 c1 = time.clock() print "Calculated in %f cpu seconds" % ((c1-c0)) 5. Modify your code from 4. to calculate the formulae in float64. Explain the differences 6. To investigate the quality of the Gaussian nature of the normal distribution, let’s assume we are observing an often observed measurement: spectral lineshifts as a result from object velocity. Use the following code snippet to generate a Gaussian distribution: import numpy as n def getvelocities( mu, sigma, totcount ): # Generate random numbers (normal distributed) return n.random.normal( mu, sigma, totcount ) sigma mu = 4.0 = 10.0 # width of distribution # mean of distribution totcount = 100000 # size of sample velocities = getvelocities(mu, sigma, totcount) use the resulting velocities to derive the min, max, mean, median, standard deviation and variance. Compare these results with the input parameters. Do this for a range of input parameters.