RandomNumbers

# If the pair falls under the curve is accepted

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: e. One draws a pair of random numbers, x, y and then compares if y is above or below the target distribution w(x). viii •  -.)/0&quot;()&quot;*+&amp;,(1&amp;2(3&quot;'&quot;24%'3(24',&amp;)(/&amp;.'*-( 5.*+(,.-*2.67%&amp;'(589:(54-(,&quot;,7\$&quot;,(6;(&lt;&amp;'( =&quot;7)4''&gt;( Figure 4: . The rejection method. If the pair falls under the curve is accepted, otherwise is rejected ;)49( 2&quot;#&quot;\$*( 4\$\$&quot;/*( ?@AB@A?( CD?EF( Example The following code produce numbers that follow P (x) = e−x for x = [0, 10]. Figure 5 shows the histogram of the numbers generated with this method. Listing 3: Rejection Method 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 % AM207 , REJECTION METHOD , SPRING 2013 % Pavlos Protopapas , Harvard University % REJECTION METHOD FOR exp ( - x ) for x = 0..10 clear ; xmin =0; xmax =10; N =10000; w = @ ( x ) exp ( - x ) ; cnt =0; k =1; while k &lt;= N % pick uniform rand on 0..10 x = ( xmax - xmin ) * rand () + xmin ; % since e ^ x (x &gt;0) &lt; 1 pick uniform on 0 to 1 y = rand () ; y_val = w ( x ) ; if y &lt; y_val ; xk ( k ) = x ; k = k + 1; end cnt = cnt +1; end ix 28 29 30 31 32 33 34 35 36 37 clf ; hold on ; h = hist ( xk , 50) ; % plot histogram hist ( xk , 50) ; x = xmin :0.1: xmax ; % normalize to first bin plot (x , h (1) * exp ( - x ) , ’r ’) xlabel ( ’x ’) legend ( ’ rejection method ’ , ’ exp ( - x ) ’) Figure 5: . Numbers generated using the rejection method for e−x . 350 300 250 200 150 100 50 0 0 1 2 3 4 x 5 x 6 7 8...
View Full Document

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

Ask a homework question - tutors are online