RandomNumbers

# To generate numbers that follow wx therefore we draw

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: et function from -∞ to x (or the CDF until x, F (x)) is evaluated, we solve for x yielding a new equation x = F −1 (r). To generate numbers that follow w(x) therefore, we draw from the uniform distribution and we apply the transformation x = F −1 (r). For example, lets assume we would like to generate random numbers that follow the 1 exponential distribution w(x) = λ e−x/λ for x ≥ 0 and w(x) = 0 otherwise. Following the recipe from above x1 r= e−x /λ dx = 1 − e−x/λ 0λ Solving for x x = −λ ln(1 − r) v 4.1 Example The following code will produce numbers that follow the exp (−x) distribution. Figure 3 shows the resulting histogram of the generated numbers compared to the actual exp (−x). Listing 2: Inverse Transformation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 4.2 % AM207 , Spring 2013 % Pavlos Protopapas , Harvard University % INVERSE TRANSFORM FOR w ( x ) = e ( - x ) from 0 to 6 clear ; xmin = 0; xmax = 6; % probability distribution wanted w = @ ( x ) exp ( - x ) ; % The CDF of w until x F_w = @ ( x ) 1 - exp ( - x ) ; % solve for x by inverting the CDF invF_w = @ ( r ) - log (1 - r ) ; % Getting the limits rmin = F_w ( xmin ) ; rmax = F_w ( xmax ) ; % N = 10000; % Creating a rescaled uniform random number vector % and transforming R = ( rmax - rmin ) * rand (1 , N ) + rmin ; X = invF_w ( R ) ; clf hold on h = hist (X ,100) ; % plot histogram hist (X , 100) x = xmin :0.1: xmax ; % Plot distribution normalized to histogram plot (x , h (1) * w ( x ) , ’r - ’) Scaling Assume you want to produce numbers following 1/(x + 1) distribution. This can be inverted as x 1 r= dx = ln(x + 1) ∞ x +1 HOWEVER if r ∼ U [0, 1] then x will take values in the range [0, e − 1] vi Figure 3: Numbers generated using inverse transform for e−x P(z)= exp(−z) 600 500 400 300 200 100 0 0 1 2 3 z 4 5 6 To generate values x, from xmin to xmax we can generate r ∼ U [rmin , rmax ] where rmin = F (xmin ) and rmax = F (xmax ). In the example above F (x) = ln(x + 1) and therefore umin = ln(xmin + 1) and umax = ln(xmax + 1)...
View Full Document

## This document was uploaded on 10/07/2013.

Ask a homework question - tutors are online