This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: end end %the function ex3(P) takes a vector representing the probability mass functions %and returns a random value following that distribution. function X=HW2_ex3(P) U = rand; acum_p = P(1); i = 1; while (U> acum_p) i = i+1; acum_p = acum_p + P(i); end X = i; Exercise 5: function ex5(N) mean = 0; for i=1:N mean = mean + sim_dist(); end mean = mean/N; V = 0; for i=1:N V = V + (sim_dist() - mean).^2; end V = V/N; fprintf( 'Estimated mean: %f \n' ,mean); fprintf( 'Estimated variance: %f \n' ,V); end function X = sim_dist p = 1/3; C = 2; U = rand; Y = get_Y_rand_number(p); while (U > (X_mass_func(Y)/(C*Y_mass_func(Y,p)))) U = rand; Y = get_Y_rand_number(p); end X = Y; end %generates a geometric random variable function Y = get_Y_rand_number(p) Y = floor(log(rand)/log(1-p)) + 1; end function P = X_mass_func(j) P = (0.5)^(j+1) + 0.5*(2^(j-1))/3^j; end function P = Y_mass_func(j,p) P = p*(1-p)^(j-1); end...
View Full Document
This note was uploaded on 01/16/2012 for the course IEOR 4404 taught by Professor Joseblanchet during the Spring '11 term at Columbia College.
- Spring '11