Computer Science 101: Lab #6
Lab will be a little different this week. We’ll start by revisiting some topics from earlier labs, and then move on to the current chapter (6).
World Population and Random Numbers
As I mentioned at the end of the lecture yesterday, the constant e and the function ln show up a lot in formulas for generating random numbers with a given probability distribution. The formula e-x2/2 gives us a normal distribution with a mean of zero and a standard deviation of one; the exponential decay function for (1/f) noise is |k|ekt (where k is negative); and we can generate Monte Carlo samples of this function by running uniformly distributed random values r through the formula ln(r).
Instead of boring you with more lecture on this topic, I’m going to ask you to investigate it yourself, by revisiting the revisited World Population model from Lab 4. If you didn’t complete this program for Lab 4, just copy-and-paste it from page 74 of the textbook, put some blank lines and indents in to make it correct, readable, Python, and fill in the missing values like <current_year> appropriately. Run the program to make sure it’s generating sensible output. (It should only report the result once, not inside the loop.) Now modify it as follows:
- Report the final population rather than the year (you can also remove the “dead code” for initializing and accumulating the year at this point).
- Change the while loop into a for loop for looping over Y years, where Y is another constant you initialize at the top of the program.
Experiment with different values of Y to see how much the population grows.
Now, computer scientists are lazy and would rather not compute something in a loop that they can compute in a single command. Wikipedia has a nice entry showing a single formula for the sort of exponential growth that you are modeling in this world-population program. Replace the for loop in your program with this formula, making sure to check the results against the original looping version. For the exponent, you can use the double-multiply symbol **. You might also want to introduce a new variable calledINITIAL_POPULATION, so you can keep both the old and new population values around.
Of course, the population doesn’t jump to its new value all at once: people are being born every second of every minute of every hour of every day of every week of every month of every year. To use the monthly rate rather than the yearly rate, you can:
- Divide the growth rate by 12.
- Replace Y with (Y*12) in the formula. Better yet, factor the 12 by creating another variable at the top of the program, call it N, and use that variable instead of repeating the 12.
You should see a higher final population when you compute it monthly rather than yearly. Change N appropriately for weeks per year, days per year, hours per year, minutes per year, and seconds per year, noting the new population value each time.
Question 6.1: Report the initial population, then the new population based on years, then months, then weeks, then days, then hours, then minutes, then seconds.
So what does all this have to do with e and ln? Have faith; we’re almost there. Save your world-population program in a new file, called exponential.py, and modify it as follows:
- Replace the initial population with 1.
- Replace the number of years Y with 1.
- Replace the growth rate with 1/N, so e.g. .0114/N becomes 1/N.
So at this point the only variable in your program should be N; everything else has been replaced by 1.
Question 6.2: What happens to the final “population” as N gets very large (maybe just keep multiplying N by 10)? Is there a final value on which the population seems to settle? Once the values of the first three or four digits of this number stop changing, copy and paste them into Google and see what comes up. What is the common feature of the links you get?
Almost done! Copy the following line of code and paste it at the top of your exponential.py program:
from math import *
This will give us access to the ln function (and many others). Perhaps because of the three-letter pattern in other math functions like sin, cos, and tan, the usual name for ln in most programming languages is log. At the bottom of your program, add a statement that prints out the result of applying this log function to the final population.
Question 6.3: What is the approximate result of applying log (i.e. ln) to the final population? What does this say about relationship between e and ln? Is there a math function in Python that will compute ex for you, so that you don’t have to specify e as a constant?
Just for fun: We also saw the sin and cos functions in the formula for generating normally-distributed random numbers for Monte Carlo simulations and the like. Perhaps there an even deeper relationship between e and important functions and constants – a hidden order in the universe, just beyond our grasp!1 … Nah.
Goin’ Back to Scribby, Scribby, Scribby …
Appropriate to 14 February, Chapter 6 presents the work of Valentino Braitenberg, whose famous little book you may choose to read as your short-paper topic. Meanwhile, you should as usual implement and submit the programs in the Do This: sections, and then try your hand at writing one or more of the programs in the Exercises at the end of the chapter (which turn out to be the programs that the author has described earlier in the chapter). Also, as usual, I have some suggestions that will save you time and trouble:
- You may wish to run setName() again, because the firmware update has probably erased the personal name you gave your Scribbler in the first lab.
- The firmware update allows us to do setPicSize('small'), which I recommend doing once before takePicture(), getBright(), and any other command that uses the camera – for reasons we discovered in last week’s lab.
- Unless you feel like typing in the '/dev/tty...' stuff in each time your run your program, I would omit the part of the author’s code that runs ask() to get this information (as on page 111), and just do the initialize('/dev/tty...') once as usual.
- For the “playpen” and corral projects, you can use the paving stones I’ll supply to build barriers of various shapes.
- As we know by now, sensors are finicky and rarely perform as advertised – hence the need for Bayesian reasoning about their outputs in real-world tasks. So if you’re making no progress with the project you’ve chosen, try one that uses different sensors.
1Sería mejor leerlo aquí, si puedes.