ne.gif (2791 bytes)     NE582 Monte Carlo
                            Spring semester 1998

Return to Course Outline


 

Lesson 4 -Choosing random numbers from distributions

In the previous lessons reading, we learned how computers generate pseudo-random numbers uniformly in the range (0,1).  In practice, though we need make choices based on general distributions over other ranges.

In this lesson, we will study several ways of making choices from other distributions and over other (sometimes not continuous) ranges.  We will discuss these in order of increasing difficulty: 

  • Choosing random numbers uniformly over a general range (a,b)
  • Making choices from discrete distributions.
  • Choosing random numbers from continuous distributions directly.
  • Choosing random numbers from continuous distributions by rejection.
In each of these cases we will use wpe5E.gif (871 bytes) as the symbol for the uniform deviate (i.e., random number supplied by computer in range (0,1)).
 
 

Choosing a random number uniformly over a range (a,b)

If the distribution is uniform and the only problem is that the range is (a,b) instead of (0,1) use the intuitive:

wpe60.gif (1144 bytes)


Example: Choose random number uniformly in range wpe63.gif (1016 bytes).

Answer: wpe68.gif (1007 bytes)

Example:  Choose random number uniformly in range (-1,1):

Answer: wpe6E.gif (1013 bytes)


Enough said.
 
 

Making choices from a discrete distribution

For the case that we must choose between N items, each of which has a relative probability of wpe70.gif (901 bytes), the basic idea is that we divide the range (0,1) into appropriately sized sub-ranges, pick a wpe5E.gif (871 bytes), and then determine which range it falls into.  A procedure to follow for this is:

    1.  Normalize the probabilities by finding wpe71.gif (895 bytes) using:

wpe72.gif (1232 bytes)

    2.  Choose the item j as the integer that satisfies the relation:

wpe75.gif (1337 bytes)



Example: Choose between three items with relative probability of 1, 2, and 5.

Answer:  Step one gets us from:

wpe7B.gif (1220 bytes)

to

wpe7C.gif (1445 bytes)

Therefore, based on a given wpe5E.gif (871 bytes), we will choose:

    Item 1 if wpe7E.gif (1105 bytes)

    Item 2 if wpe7F.gif (1187 bytes)

    Item 3 if wpe80.gif (1044 bytes)


Choosing random numbers from a continuous distribution (directly)

For continuous distributions, we will show two methods.  The first, in this section, is the preferred one for well-behaved functions that can be integrated and inverted easily.

Mathematically, we proceed by assigning (as usual) the x-axis to the variable to be chosen from a normalized probability distribution function f(x) over a range (a,b) and the y-axis to the pseudo-random number supplied by the computer.  Then we set up a mapping function y=y(x) that relates the two.   Ultimately, though, we will have to invert this to get a mapping from y to x.   Okay, so it basically looks like this:

wpe82.gif (2197 bytes)

Note that the mapping function must be unique both in the x->y mapping and the y->x direction, so it must be a non-decreasing function.
 
 

To figure out the mapping function, we consider the mapping between two differential areas of the axes, dy and dx:

wpe84.gif (2325 bytes)

Since the two represent the same region of the curve, they must correspond to the same probability:

wpe85.gif (1878 bytes)

wpe87.gif (1117 bytes)

where f(x) is the probability distribution in x and the corresponding (uniform) distribution in y is 1.
 
 

Solving for the function y(x):

wpe88.gif (1498 bytes),

we see that the mapping function must be the same as the Cumulative Distribution Function, F(x).
 
 

A step-by-step procedure for choosing an x from an unnormalized function wpe89.gif (987 bytes)in the range wpe98.gif (988 bytes)is:

    Form a normalized probability distribution function (p.d.f.), f(x), using:
wpe8A.gif (1518 bytes)
    Find the cumulative distribution function (c.d.f.), F(x), using:
wpe8B.gif (1351 bytes)
    Invert F(x) to get x as a function of y:
wpe8C.gif (1279 bytes)


    From a uniform deviate, wpe5E.gif (871 bytes), x chosen from:
wpe8E.gif (1049 bytes)
will, therefore, be distributed according to wpe89.gif (987 bytes)

Example: Choose random number distributed according to wpe8F.gif (1064 bytes) in the range wpe90.gif (973 bytes).

Answer:  Step 1. Normalize the function:

wpe91.gif (1535 bytes)

Step 2. Find the c.d.f.:

wpe92.gif (1567 bytes)

Step 3.  Invert the c.d.f:

wpe93.gif (1105 bytes)

wpe94.gif (1166 bytes)

wpe95.gif (1172 bytes)

wpe96.gif (1281 bytes)

Step 4. From a computer-generated random number, wpe5E.gif (871 bytes), x chosen from:

wpe97.gif (1277 bytes)

will be distributed according to wpe8F.gif (1064 bytes) in the range wpe90.gif (973 bytes).



Choosing random numbers from a continuous distribution (rejection)

A second way of picking a variable x from a continuous distribution is the rejection method.  It is less efficient an less elegant than the direct method, but is needed in cases where the direct method cannot be used.  This can occur, of course, for a computer program if: 
  • the distribution is not pre-determined.  (e.g., input by user or created as the program runs)
  • the p.d.f. cannot be integrated.
  • the c.d.f. cannot be inverted.
The method is similar to the approach we took in the first Monte Carlo exercise we did, finding pi.gif (868 bytes) by picking points inside an enclosing square.  Basically, we do the same thing: create a uniform distribution that contains (i.e., bounds) the desired function wpe89.gif (987 bytes), pick an (x,y) point inside the rectangle "under" the bounding function, then keep it only if it is also under wpe89.gif (987 bytes).   It is not even necessary to normalize the function first.
 
 

A step-by-step procedure for using this method to choose an x from an unnormalized function wpe89.gif (987 bytes)in the range wpe98.gif (988 bytes)is:

    1. Find the maximum value, wpe99.gif (945 bytes), of the function in the range.

    2. Pick an x uniformly in the range using

wpe60.gif (1144 bytes)

    3. Choose a y uniformly between 0 and wpe99.gif (945 bytes) using:

wpe9A.gif (1039 bytes)

    4. If wpe9B.gif (1062 bytes), keep the x.  Otherwise, repeat 1-3.
 
 
 

Probability mixing method

The probability mixing method is a simple mathematical technique used for choosing random numbers from linear combinations of p.d.f.'s.  It gets its name from the fact that it allows the user to "mix" different p.d.f.'s.  The method shows up in many different forms, so in this lesson we will present the simple mathematics and then proceed to show several of the forms that it can show up in.

Mathematical form

The basic idea of the probability mixing method is that if you have a p.d.f. (possibly unnormalized) that is the sum of other functions:

wpeBE.gif (1931 bytes)over a range (a,b)

and all of the are greater than zero in the range (a,b).  Then you can choose a random number between (a,b) according to wpeC0.gif (981 bytes) with a two-step procedure of:

    Choosing one of the N distributions

    Choosing from that individual distribution.

where the first choice is reduced to a discrete choice using each function's integral over the range as its relative probability.
 
 

The step-by-step procedure for doing this is:

    1. For each of the i functions, find its integral over the range:

wpeBC.gif (1301 bytes)

    2. Normalize the wpeCA.gif (901 bytes)'s so that they sum to 1:

    3. Choose one of the functions, j, from 1 to N, from a discrete distribution using these  probabilities.

    4. Choose a value of x using the (now normalized) continuous distribution:

Example #1 -- Sum of functions over entire range

The most straight-forward application is when each of the functions is defined over the entire range.  As an example, let us use

wpeC3.gif (1131 bytes) over the range (1,2).

This, of course, can be broken down into:

where:

Following the procedure, we get:

Step1.  Find the wpeCA.gif (901 bytes)s:

wpeC8.gif (1546 bytes)

wpeC9.gif (1608 bytes)

Step 2. Normalize the wpeCA.gif (901 bytes)'s:
 
 

Step 3.  Choose one of the functions, j, using the wpeBA.gif (895 bytes) values in a discrete distribution:

        We would do this by:

    Choosing a random number, xsi.gif (871 bytes).

    If wpeCD.gif (1089 bytes), j=1.

    Otherwise, j=2.

Step 4: Choose a number x using the normalized distribution for the j chosen

        If j=1, this comes down to wpeCF.gif (1088 bytes)

        If j=2, then it is wpeD0.gif (1285 bytes)
 
 

The overall answer is that we choose from wpeC3.gif (1131 bytes) over the range (1,2) by choosing x from 

    wpeCF.gif (1088 bytes) 90.94% of the time

    wpeD0.gif (1285 bytes) 9.06% of the time
     
     

Example #2 -- Histogram function

Another example is choosing a random number from a histogram function, which is a step function over contiguous regions:

wpeD3.gif (2141 bytes)

For example:

wpeDD.gif (4168 bytes)

Although this function is a single continuous function, we can force into the multi-function format using:

wpeD4.gif (1711 bytes)

This formally re-formulates wpeC0.gif (981 bytes) as a sum of single-step functions, for example,

wpeDC.gif (4315 bytes)

Because these are well-behaved functions, we can derive that:

wpeD6.gif (1170 bytes)

which can be normalized and used to choose j, which in this case is a choice of the jth "step".
 
 

Once a step has been chosen, the choice of a point on the step is done using:

wpeDE.gif (1235 bytes)


Example #3 -- Linear fits to continuous functions

The same basic idea applies to linear continuous fits to continuous functions, which are like histograms except that a function is approximated with connected line segments:

wpeE2.gif (4229 bytes)

Once again, we make this a sum of functions that are 0 except within a single region.  If we let wpeE1.gif (1068 bytes) be the values of the functions at the endpoints, the equations for the function within region i is:

wpeE4.gif (2178 bytes)

wpeE7.gif (4090 bytes)

The choices that must be made are:

    Choose a region j using the relative region probabilities of wpeE5.gif (1343 bytes)

    Choose an x within region j using the relation:

wpeE6.gif (2148 bytes)





Return to Course Outline                                                                                                                   © 2004 by Ronald E. Pevey.  All rights reserved.