This assignment was given on 24 February 2009. The work is due 17 March 2009.
Question 1: Finish your work on a program that uses simulated annealing to minimize a function f(x) over an interval [A,B].
Use the function myf.m as the function to be minimized over the interval [0,7].
Your program should start with a random initial value x1 that is inside the interval, and store the function value f1=f(x1). Each step of your procedure considers a new value x2 as a possible replacement for x1. Generate x2 as another random value in [0,7].
Evaluate f2=f(x2). If f2 < f1, then immediately replace x1 by x2.
However, even if f2 > f1, we might choose x2! Whether or not we do this depends on the "temperature" and on how much greater f2 is than f1. The details of how to do this are outlined in the project writeup. Suggested values for the inital temperature T and the temperature reduction factor alpha
Run your simulated annealing program until it has made 1,000 updates to x1. Every time you update, store the values of x1 and f1. When you have reached 1,000 updates, make a plot of the x values versus the f values. This will show us how rapidly the function is decreasing.
1A) Plot the 1000 x values versus the f values that represent the successive candidates for the minimizer (even though, with simulated annealing, the function values can increase sometimes). This will show us how rapidly the function is decreasing.
Question 2: Random Walks
Many physical processes include diffusion, which describes the spreading of a substance that is initially highly concentrated. An example is the way a drop of ink in water will gradually spread. One way to model this behavior is to describe the ink as a collection of particles, each of which carries out a random walk.
To simplify the model, we will start with one particle, which can only move to the left or right, and can only take steps of size 1. We assume the particle starts at the origin, and that the direction of each step is determined by flipping a coin.
In preparation, write a function
function value = flip ( )which returns a random value of +1 or -1.
After N steps, where is the particle? Obviously, it could be anywhere between -N and N. So there's no answer to that question. But we're really interested in the behavior of many particles, all of which are carrying out the same kind of walk. So it makes sense if we rephrase the question as, What is the average position of a collection of such particles? And we can answer that question by simulating the walk many times, and averaging.
Write a function
function x = random_walk ( steps )which simulates a random walk involving steps steps. The function should return x, the current position of the particle.
If you think about it, since we only want the final position of the particle, you can write this function in one line of MATLAB code!
Rewrite your function to include the number of trials:
function x = random_walks ( steps, trials )which simulates TRIALS random walks involving steps steps, and returns a vector x containing the final position for each walk.
2A) Run random_walks for steps having the values 4, 16, 64, 256, 1024 and trials set to 10,000. The distance of the particle from the origin is the absolute value of x. Make a table of steps versus the average value of the absolute value of x. Compare your distance results to the function sqrt(steps).
The ink drop particles are not in an infinite ocean, but a finite bottle. To do a better job of simulation, let's change the rules slightly. Now the particle starts at the origin, but it must stay between -10 and +10. If a particle at -10 tries to go to -11, we reset it to -10. Similarly for x = +10.
Write a function
function x = random_walks_finite ( steps, trials )which simulates trials random walks involving steps steps, constrained to stay between -10 and 10. As before, the function should return a vector x, the current positions of the particle.
2B) Run your program for steps = 16 and trials = 1,000. Plot a histogram using the command:
hist ( x, 21 )Repeat for steps = 64. If the shape of the histogram is indicating the local concentration of the ink particles, then what seems to be happening? If your program is efficient, you can increase trials and steps to get a better idea of what is going on.
A 2D random walk starts a particle at the origin, flips a four sided coin, and allows the particle to move one step north, south, east or west, depending on the coin toss.
Write a function
function xy = random_walk_2d ( steps )which simulates a random walk in 2D involving steps steps. The function should return xy, the current position of the particle.
2C) Run your 2D program 1,000 times for steps having the values 4, 16, 64, 256, 1024. For each value of steps, compute the average euclidean distance of the point from the origin. Compare this with the function sqrt(steps).
Question 3: Permutations.
A permutation can be thought of as a rearrangement of the integers, or as a mapping that "sends" the value I to the value P(I). A permutation is often described simply by listing the "scrambled" values. Thus, we might write a sample permutation as (3,4,1,5,2). This really means that the value that was at 1 went to position 3, the value that was at 2 went to position 4, and so on.
When a permutation rearranges the integers from 1 to N, we say that the order of the permutation is N.
3A) write a function that computes a random permutation of the integers from 1 to N. Test your program by generating random permutations of order 4. Does every possible permutation show up about as often as it should?
If it happens that I=P(I), we say that I is a fixed point of the permutation. If a permutation has no fixed points, we say the permutation is a derangement.
Example: (5,1,4,2,3) is a derangement of order 5. (2,3,6,4,1,5) has 4 as a fixed point, so it is not a derangement.
Write a function that estimates the probability that a random permutation or order N will be a derangement. The function should examine TRIALS random permutations in order to estimate this probability:
function PROB = derangement_prob ( N, TRIALS )PROB is simply the ratio formed by the number of derangements you found divided by the number of trials.
3B) Compute a table of the derangement probabilities. Try to compute the chart for N from 1 to 15; see if your program can use a value of TRIALS that is at least 1000.
As a check, the exact value for N=7 is 0.36785713...
A permutation can be applied twice. Starting with I we get P(I). If we permute again, we get P(P(I)). For some permutations, P(P(I))=I for all I. These special permutations are called involutions. In an involution, each number either stays the same, or swaps places with another number, so doing it twice returns everything to the original state.
Example: (4,2,5,1,3) is an involution, because we have 2 as a fixed point, and pairwise swaps (1,4) and (3,5).
In MATLAB, if P is a permutation, then P(P) is the result of applying the permutation twice. To see if P is an involution, therefore, we simply need to compare P(P) to the vector 1:N as follows:
if ( p(p) == 1:n ) P is an involution! end
3C) Compute a chart of the involution probabilities. Try to compute the chart for N from 1 to 10; for each value of N, you are reporting your estimate of the probability that a permutation of order N is an involution.
As a check, the exact value for N=7 is 0.0460317...
Question 4: One Chance in a Hundred?
You've made the king very angry, and he plans to execute you and your pals Biff and Clem. However, the king likes to think he's fair, so he's going to give you a chance to live. The king has a deck of 100 cards. Each card has a different (real) number written on it. Only the king knows what numbers are on the cards.
He comes to Biff's cell and tells him to pick one card from the deck and turn it over. If the number on that card is the highest value, then he will live. Biff picks a card, it's a loser, and he is executed. But he did have one chance in 100 of living, so that's pretty fair, at least the way the king thinks.
However, it wasn't very much fun. So the next day, when the king comes to Clem's, he's changed the game. He tells Clem that he is allowed to pick a card and look at it. He can either keep the card, or discard it and try again. In fact, he can have as many tries as he wants. (However, if he rejects the first 99 cards, then he is stuck with the last one.) Once he has chosen a card to keep, he shows it to the king, and if the number written on that card is the biggest one in the deck, Clem will live.
Clem decides on the following strategy: he will pitck the first card, look at it, and ALWAYS reject it. Then he will continue to pick until he reaches the first card that is bigger than the first card (or he reaches the end of the deck) and that will be the card he chooses to show to the king.
Write a program that simulates this process. Instead of labeling each card with a real number, we'll simply assume they are labeled 1 to 100. That makes it easy for us to tell the lowest card (1) from the highest (100). To represent the shuffled deck, choose a random permutation of 100 objects. Clem obviously wants to pick the biggest card, which we are representing as "100". Your program should look at the first entry in the permutation, P(1), and store that value as TEST. Then your program should consider successive entries P(2), P(3), and so on, until finding the first value P(I) that is bigger than TEST, and choose that card. If P(I) is actually equal to 100, then Clem lives.
4A) Do enough simulations of Clem's strategy to estimate his chances of survival.
Now it's your turn. You've come up with the following strategy. You are going to look at, and reject, the first 25 cards. Then you will choose the very next card which is larger than the maximum of the first 25 (and if no such card shows up, you end up with the last card in the deck) and this is the card you will show the king.
4B) What are your chances of survival under this strategy?
Your strategy is pretty good, but 25 is not the very best stopping point.
Question 5: Allen and Bob decide to fight a duel, but they only have one gun and one bullet. They load the gun and spin the chamber and after flipping a coin, decide that Allen will go first. Allen fires at Bob once. Unless Bob is shot, he gets the gun, spins the chamber and fires at Allen. They keep doing this until someone is shot.
You can actually work out the probabilities mathematically. However, write a program that simulates this duel. Have it run 10,000 simulations of the duel. Have it answer the following questions:
Carl and Delbert also fight a duel. They decide to try to make their duel more fair. Carl will get one shot at Delbert, but then Delbert gets two shots at Carl, then Carl gets three shots at Delbert, and so on. Of course, before each shot, the chamber is spun to randomize the position of the bullet. Modify your program to simulate this duel. Have it run 10,000 simulations of the duel.
I am giving you more than a week's worth of problems! However, you do have two weeks to work on them (and I am NOT counting spring break), since we will not be meeting on 3 March.