Assignment_3_source_code_(1)

Assignment_3_source_code_(1) - end end %the function ex3(P)...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Assignment 2 1. Solutions: Test file: %tests echo off ; %%%%%%%%%% Tests %%%%%%%%% fprintf( 'Tests for exercise 3 \n' ); ex3(1000); fprintf( 'Tests for exercise 4 \n' ); ex4(1000); fprintf( 'Tests for exercise 5 \n' ); ex5(1000); Exercise 3: function ex3(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 u = rand; if (u <= 0.55) X = 2*floor(5*rand) + 5; else X = 2*floor(5*rand) + 6; end end Exercise 4: function ex4(N) mean = 0; for i=1:N mean = mean + sim_dist(); end mean = mean/N;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 u = rand; if (u <= 0.3) X = floor(5*rand) + 1; else P = (1/0.7) * [0.15; 0.13; 0.14; 0.15; 0.13]; X = HW2_ex3(P) + 5; % call inverse transform method from Homework 2.
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the 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.

Page1 / 3

Assignment_3_source_code_(1) - end end %the function ex3(P)...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online