Tue Jan 2 22:47:28 2024 dueling_idiots_test(): Python version: 3.8.10 Test dueling_idiots(). ash(): A champ plays a chump at 100 games of chess. P is the probability an individual game is won by the champ. Q is the probability an individual game ends in a tie. For this code, P = Q = 1/3. Probability of a tie is 0.04876864857374662 Probability the champ wins is 0.47561567571312857 Probability the challenger wins is 0.4756156757131248 balls(): An urn contains 10 numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 3.66021568 T = 4 balls(): An urn contains 100 numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 12.209960630215974 T = 12 balls(): An urn contains 1000 numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 39.30321292617819 T = 37 balls(): An urn contains 10000 numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 124.99912186808116 T = 118 balls(): An urn contains 100000 numbered balls. A ball is drawn repeatedly from the urn, examined, and replaced. This file calculates: (1) E(n), the average number of drawings (with replacement) of numbered balls before a repetition occurs (2) T, the largest number of drawings that still allows the probability there is NOT a repetition is still greater than 1/2. E = 395.999726408384 T = 372 baseball() Calculate and plot (as a function of p) the ratio of the probabilities of a team winning at least 81p games out of 81 games to the probability of winning at least 162p games out of 162 games, where p is the probability of winning any individual game. Graphics saved as "baseball.png" biased(): A game of odd man out is played: 3 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.0 Estimate the average number of games played. Average number of games = 1.334 Theoretical number = 1.3333333333333333 biased(): A game of odd man out is played: 3 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.3 Estimate the average number of games played. Average number of games = 1.36 Theoretical number = 1.3333333333333333 biased(): A game of odd man out is played: 3 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.9 Estimate the average number of games played. Average number of games = 1.338 Theoretical number = 1.3333333333333333 biased(): A game of odd man out is played: 4 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.0 Estimate the average number of games played. Average number of games = 1.953 Theoretical number = 2.0 biased(): A game of odd man out is played: 4 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.3 Estimate the average number of games played. Average number of games = 2.052 Theoretical number = 2.0 biased(): A game of odd man out is played: 4 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.9 Estimate the average number of games played. Average number of games = 2.084 Theoretical number = 2.0 biased(): A game of odd man out is played: 5 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.0 Estimate the average number of games played. Average number of games = 3.353 Theoretical number = 3.2 biased(): A game of odd man out is played: 5 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.3 Estimate the average number of games played. Average number of games = 3.289 Theoretical number = 3.2 biased(): A game of odd man out is played: 5 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.9 Estimate the average number of games played. Average number of games = 3.152 Theoretical number = 3.2 biased(): A game of odd man out is played: 6 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.0 Estimate the average number of games played. Average number of games = 5.4 Theoretical number = 5.333333333333333 biased(): A game of odd man out is played: 6 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.3 Estimate the average number of games played. Average number of games = 5.106 Theoretical number = 5.333333333333333 biased(): A game of odd man out is played: 6 people each flip a coin. If one person's coin does not match all the others, that person is the odd man out and the game is over. In this version, N-1 people have fair coins, and one has a biased coin whose probability of heads is Q = 0.9 Estimate the average number of games played. Average number of games = 5.486 Theoretical number = 5.333333333333333 binomial: 6 choose 0 = 1.0 binomial: 6 choose 1 = 6.0 binomial: 6 choose 2 = 15.0 binomial: 6 choose 3 = 20.0 brownian() Simulate Brownian motion, a random walk by a particle suspended in a fluid and being hit by molecules. Graphics saved as "brownian.png" bulb(): Calculate and plot the probability of a bulb glowing, wired in series/parallel with 5 sheets of switches. Graphics saved as "bulb.png" bulb(): Calculate and plot the probability of a bulb glowing, wired in series/parallel with 10 sheets of switches. Graphics saved as "bulb.png" casino(): Simulate 10,000 games of "Chuck-a-Luck" and calculate the average winnings. That is, you bet $1 each and the winnings (W) are how much MORE money you have over and above the $10,000 betting money. The average winnings are, of course, W/10,000. Three dice are rolled, and you can bet on any number between 1 and 6. If one die comes up with your number, you win as much as you bet. If two or three dice come up with your number, you win twice or three times your bet. Average winnings per game is -0.07551 dollars. cc(): Generate a random vector, X, of length 20,009 and calculate the correlation coefficient X(n) and X(n+j) for j = 0,1,2,...,9. J Cor(X(i),X(i+j)) 0 0.964775 1 -0.0292856 2 -0.0306805 3 -0.0352818 4 -0.0313157 5 -0.0294016 6 -0.0364501 7 -0.0250268 8 -0.0362416 9 -0.0327547 chess(): Compute the probabilities, in an N-game chess match, of the match ending in a tie, in a win for the champ, or in a win for the challenger. The probability the champ wins an individual game is p, and the probability an individual game ends in a tie is q. Graphics saved as "chess.png" chess(): Compute the probabilities, in an N-game chess match, of the match ending in a tie, in a win for the champ, or in a win for the challenger. The probability the champ wins an individual game is p, and the probability an individual game ends in a tie is q. Graphics saved as "chess.png" correlation(): Plotting X(I) versus X(I+J) can exhibit patterns if there is correlation in the data. If not, the scatter plot will be patternless. Graphics saved as "correlation.png" esim(): Sample n = 1000 points in the unit interval. Put them into n bins of equal width. Let z be the number of empty bins. n/z is an estimate of E. Estimate for E = 2.7548209366391183 Exact value = 2.718281828459045 esim(): Sample n = 10000 points in the unit interval. Put them into n bins of equal width. Let z be the number of empty bins. n/z is an estimate of E. Estimate for E = 2.698327037236913 Exact value = 2.718281828459045 flycircle() Two flies land at random spots in the unit circle. What is the probability they are more than 1 unit apart? Probability flies are more than 1 unit apart = 0.00412 flysquare(): Two flies land at random spots in the unit square. What is the probability they are more than 1 unit apart? Probability flies are more than 1 unit apart = 0.0243 gas(): Simulate the diffusion of gas molecules in a sealed container, using the Ehrenfest ball exchange rules. Graphics saved as "gas.png" generator(): Create a histogram of 10000 uniform random values. Graphics saved as "generator.png" idiots1(): A and B fight a duel, but they only have one gun, a six-shot revolver. A will spin the cylinder and fire at B. If the gun does not fire, then B will spin the cylinder and fire at A. They continue to alternate until one of them is shot. What is the probablity that A will win? What is the average number of trigger pulls? The probability A wins is 0.5472 The average number of trigger-pulls/duel is 5.9674 Maximum number of trigger pulls was 53 Graphics saved as "idiots1.png" idiots2(): Simulate a Russian-roulette duel between A and B. A fires at B one time. If A misses, B fires at A two times. If B misses, A fires at B three times, and so on. Each shot is preceded by spinning the revolver cylinder, so there is a 1/6 chance that the revolver will fire. Calculate the probability that A survives. Probability that A survives = 0.5239191275550996 kids(): Simulate n = 10000 families that have children until a child is born that is the same sex as the first child, where the probability of a boy is p = 0.4 Average number of children in each family = 2.9924 markov() We have four states, 1, 2, 3, 4. If we are in state I, then on the next step we will move to state J with a proability P(I,J). Simulate this process over many steps. Display the results graphically. Graphics saved as "markov.png" match() Compute the probability that 2 people, flipping fair coins 10 times, will each get the same number of heads. Match probability is 0.17619705200195312 match() Compute the probability that 3 people, flipping fair coins 10 times, will each get the same number of heads. Match probability is 0.03554416820406914 match() Compute the probability that 4 people, flipping fair coins 10 times, will each get the same number of heads. Match probability is 0.007590023658849532 match() Compute the probability that 2 people, flipping fair coins 50 times, will each get the same number of heads. Match probability is 0.07958923738717874 monty(): There are three doors. A prize is behind one of the doors. You name a door. The host then opens one of the other doors, which is guaranteed not to conceal a prize. You may now choose to switch your choice to the remaining door, or not. What is the better strategy? Number of trials is 10000 No-switch strategy wins: 3379 Switch strategy wins: 6621 needle(): A circular table top has radius r. A needle of length 2*a <= 2*r is dropped onto the table. * how many times does one end stick out over the edge? * how manu times do both ends stick out? * how many times does neither end stick out? Graphics saved as "needle1.png" Graphics saved as "needle2.png" Graphics saved as "needle3.png" normal(): Display a histogram of 10000 normal random numbers. Graphics saved as "normal.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... Assuming 2 players, the average number of flips is 1.97 Graphics saved as "odd.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... Assuming 3 players, the average number of flips is 1.347 Graphics saved as "odd.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... Assuming 4 players, the average number of flips is 2.026 Graphics saved as "odd.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... Assuming 5 players, the average number of flips is 3.098 Graphics saved as "odd.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... Assuming 6 players, the average number of flips is 5.419 Graphics saved as "odd.png" odd(): Simulate 1,000 games of odd-person-out, for N people. Each person flips a fair coin. The elements of the row vector duration are the number of games of length i, i.e., duration(i)=# of games that require i flips to complete, where i=1,2,3... Assuming 7 players, the average number of flips is 9.021 Graphics saved as "odd.png" onedwalk(): Simulate the one-dimensional, symmetrical walk with an absorbing barrier at the right. The map and connections: 1 <--> 3 <--> 4 --> 2 We start at location 1, and will stop at location 2. How long is the average walk? Average walk duration = 8.9287 Graphics saved as "onedwalk.png" onewaytodoit(): This program generates 10000 normal random values using the Box-Muller transformation on uniform rnadom numbers. Display results as a histogram. Graphics saved as "onewaytodoit.png" onion(): Uniformly slice the unit interval, from left to right, ten times, and form the sum of the widths of the slices. Do this 5000 times and plot the distribution function of the sum. Graphics saved as "onion.png" paradox(): A simple system has an average lifetime of A. For reliability, a complex system is constructed of three simple systems. The complex system fails if at least two of the simple systems fail. What is the average lifetime of the complex system? Graphics saved as "paradox.png" paths(): Estimate the distribution and density functions of the random variable L denoting random path lengths across a unit square. Graphics saved as "paths_density.png" Graphics saved as "paths_distribution.png" pisim(): Sample n = 10000 points (x,y) in a square. The number of points M with x^2+y^2 <= 1 lie inside the quarter circle. M/N is an estimate of pi/4. Estimate for PI is 3.1848 radar(): As the number of observations nmax increases, compute * PD, the probability of detection. * PFA, the probability of false alarm. Graphics saved as "radar.png" randomsum(): Add a sequence of random values until you exceed 1. Let N be the number of terms in this sequence. Repeating this process 10000 times, let D(N) be the number of times that N terms were required. Then E can be approximated by 1*D(1) + 2*D(2) + ... * I * D(I) + ... Estimate for e = 2.7086 Exact value = 2.718281828459045 spider(): A web consists of locations 1 through 9. The web is connected, and each location is connected to some of the others. A spider begins at location 1, and a fly is trapped at location 2. By choosing random moves on the web, how long does it take the spider to find the fly? Average number of steps to reach the fly = 15.4449 Graphics saved as "spider.png" stirling1(): Compare the logarithms of n! and Stirling's approximation Graphics saved as "stirling1_error_abs.png" Graphics saved as "stirling1_error_rel.png" stirling2(): Stirling approximates n! by sqrt(2*pi*n) * n^n * exp(-n). Display a plot of absolute and relative errors. Graphics saved as "stirling2_error_abs.png" Graphics saved as "stirling2_error_rel.png" stirling3(): Compare n factorial and Stirling's approximation. Present the results graphically. Graphics saved as "stirling3_error_abs.png" Graphics saved as "stirling3_error_rel.png" theory(): A line is drawn through the unit square by choosing a point x in [0,1] and then choosing a random angle between 0 and 180 degrees. Plot the theoretical distribution of the length of the line between x and the intersection with the square's boundary. Graphics saved as "theory.png" thief() A thief is in a room with three doors. He chooses one at random. One door leads immediately to freedom. Another leads back to the room, after S hours. The third leads back to the room, after L hours. If the thief is returned to the room, he randomly tries a door again. How long is the average imprisonment? Estimated average total time in prison = 3.9345 Exact value is 4.0 tub(): Compute the optimal allocation of search boats looking for the UNSINKABLE TUB. The tub is sinking either 10 miles south (probability P1) or 10 miles north of the station (probability P2 = 1 - P1). 13 search boats are available. Each searcher has a probability of locating the tub PS = 0.2 How can we maximize the chance of locating the tub? Graphics saved as "tub.png" tub(): Compute the optimal allocation of search boats looking for the UNSINKABLE TUB. The tub is sinking either 10 miles south (probability P1) or 10 miles north of the station (probability P2 = 1 - P1). 40 search boats are available. Each searcher has a probability of locating the tub PS = 0.2 How can we maximize the chance of locating the tub? Graphics saved as "tub.png" tub(): Compute the optimal allocation of search boats looking for the UNSINKABLE TUB. The tub is sinking either 10 miles south (probability P1) or 10 miles north of the station (probability P2 = 1 - P1). 40 search boats are available. Each searcher has a probability of locating the tub PS = 0.05 How can we maximize the chance of locating the tub? Graphics saved as "tub.png" underdog1(): The World Series of Baseball involves up to 7 games between two teams. Suppose the stronger team has probability P of winning any one game. What is the probability that the weaker team will actually win the series? Graphics saved as "underdog1.png" underdog2(): The World Series of Baseball involves up to 7 games between two teams. Suppose the stronger team has probability P of winning any one game. What is the average duration of the World Series? Graphics saved as "underdog2.png" xplusy(): Produce 5000 values of the sum of 2 random variables, each variable uniform from 0 to 1, and independent. Plot a histogram of the sums. Graphics saved as "xplusy.png" xplusy(): Produce 5000 values of the sum of 6 random variables, each variable uniform from 0 to 1, and independent. Plot a histogram of the sums. Graphics saved as "xplusy.png" xpowery(): Plot the probability density function of X^Y, where X and Y are independent, and both uniform from 0 to 1. Graphics saved as "xpowery.png" xyhisto(): Produce 20000 values of X raised to the Y power, where X and Y are uniform random values. Graphics saved as "xyhisto.png" z(): Simulate the random variable Z=X/(X-Y), where X and Y are uniform random values. Graphics saved as "z.png" dueling_idiots_test(): Normal end of execution. Tue Jan 2 22:47:38 2024