Chance is a part of science, and the computer can imitate it. In this course you make random numbers with a seeded generator, so that a simulation can be repeated, and see the law of large numbers. You estimate probabilities and the number pi by the Monte Carlo method, and see that its error falls as one over the square root of the number of trials. You simulate random walks and find that the typical distance grows as the square root of the number of steps, which is diffusion. You simulate radioactive decay and see why small samples are noisy and large samples are smooth. A project asks how sure a measurement is: the standard error, a whole experiment repeated thousands of times, the bootstrap, and the difference between random and systematic errors. The practice runs your code and tells you which results are wrong.
5 lessons, about 3 h 15 min5 simulations10 problems10 practice questionsPython in every lesson
A computer makes random numbers with a formula, so they are pseudo-random, and a generator made with np.random.default_rng and a seed gives the same numbers every time, which makes an experiment repeatable. rng.random gives decimals from 0 up to 1, rng.integers gives whole numbers such as the faces of a die, rng.uniform a range, rng.normal numbers from a bell curve with a given centre and spread, rng.choice a pick with optional weights and without replacement, and rng.shuffle changes an order. np.bincount counts the faces. The share of an outcome comes closer to its probability as the number of trials grows, which is the law of large numbers: 600 rolls give shares within about 0.04 of one sixth, and a million rolls within 0.0002.
The Monte Carlo method estimates a quantity by random trials. The probability of an event is estimated as the share of trials in which it happens: for two dice, the sum is 7 or more in 58.4 percent of 200 000 trials, near the exact 21 over 36, and a group of 23 people shares a birthday with a probability of about 0.5. Throwing random points at a square and counting the share inside a quarter circle gives pi over 4, so four times the share estimates pi. The error of a Monte Carlo estimate is about proportional to one over the square root of the number of trials, so a hundred times more trials give an estimate that is ten times more accurate, which is slow, but the method works for problems with no formula. The accuracy can be seen by repeating the whole experiment.
In a random walk on a line, every step is one to the left or one to the right with equal chance, and the position is the running total of the steps, made with np.cumsum. Many walkers are simulated at once as the rows of a two-dimensional array, with cumsum along axis 1. The average position stays at 0, but the walkers spread out: the mean of the squared distance after n steps is about n, so the typical distance is the square root of n and not n. The final positions of many walkers form a bell curve, which is the central limit theorem. This is how particles diffuse: the mean squared distance grows in proportion to the time, and the slope of the straight line of the mean squared distance against the steps is 1 for steps of size 1.
In radioactive decay, each atom decays at a random moment, with a probability p in each second. A simulation follows the number of atoms that are left: in each second, rng.binomial of the atoms and p gives how many decay. The result follows the expected curve n0 times one minus p to the power t, but it fluctuates, and the fluctuation of a count is about the square root of the count, so a small sample is rough and a large sample is smooth. With p equal to 0.05 the expected half-life is ln 2 over minus ln of 1 minus p, which is 13.5 seconds, and a simulation that counts whole seconds finds 14. The exponential law of decay is not a rule for single atoms, whose decay is unpredictable, but the result of the average over a huge number of atoms.
A measurement has a random error and perhaps a systematic one. Random errors are reduced by repeating: the standard error of the mean is the sample standard deviation with ddof equal to 1 divided by the square root of n, so four times more measurements halve it. A simulated experiment can be repeated thousands of times to see the true spread of the answer: a pendulum experiment that finds g from the slope of T squared against L, with a scatter of 0.01 seconds in each period, gives g equal to 9.81 with a spread of about 0.12. The bootstrap estimates the uncertainty from the data alone, by taking the mean of many samples drawn from it with replacement. A systematic error, such as an instrument that reads 0.3 too high, is not reduced by averaging: the standard error goes to zero but the mean stays 0.3 above the truth.
Log in to join the discussion.
Loading…