12.07.2015 Views

Computer Assignment 2 - Stockholms universitet

Computer Assignment 2 - Stockholms universitet

Computer Assignment 2 - Stockholms universitet

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ans =0.2001for N = 10, p = 0.3 and k = 4.The corresponding value of the cumulative distribution function( )k∑ NΦ(k, N, p) = p i (1 − p) N−ii=0ican just as easily be obtained using binocdf(k,N,p) as>> binocdf(4,10,0.3)ans =0.8497If you do not have access to the statistics toolbox, you can still calculate the cumulativedistribution function as>> for i=0:4p(i+1)=nchoosek(10,i)*power(0.3,i)*power(1-0.3,10-i);end>> sum(p)ans =0.8497where the function nchoosek yields the binomial coefficient ( )10i . Since it is often necessaryto make these calculations several times when analysing properties of options, you cancreate the file binocdf.m, placed in the directory where you work with Matlab, with thecontentfunction y=binocdf(k,N,p)f(1)=power(1-p,N);for i=1:Nf(i+1)=f(i)*(N-i+1)/i*p/(1-p);endfor i=1:length(k)y(i)=cumsum(f(k(i)+1));end(This is similar to the implementation in Matlab, where you can use a vector x as anargument.)2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!