Matlab exercise 101071041 Bond pricing function price lattice
Matlab exercise 101071041 陳怡鳳
Bond pricing Ι function [price, lattice] = Lattice. Pricing(S 0, r, T, sigma, N) delta. T = T/N; u=exp(sigma * sqrt(delta. T)); d=1/u; p=(exp(r*delta. T) - d)/(u-d); lattice = zeros(N+1, N+1); for j=0: N lattice(N+1, j+1)=S 0*(u^j)*(d^(N-j)); if lattice(N+1, j+1)>=40 lattice(N+1, j+1)=3550; end if 40>lattice(N+1, j+1)>=25 lattice(N+1, j+1)=1000+(lattice(N+1, j+1)-25)*170; end if lattice(N+1, j+1)<25 lattice(N+1, j+1)=1000; end for i=N-1: 0 for j=0: i lattice(i+1, j+1) = exp(-r*delta. T) *. . . (p * lattice(i+2, j+2) + (1 -p) * lattice(i+2, j+1)); end price = lattice(1, 1); for i=1: 1: 500 price(i)=Lattice. Pricing(30, 0. 02, 1, 0. 2, i); end plot (1: 1: 500, price) xlabel N ylabel price
Bond pricing Ι Ι function [price, lattice] = Lattice. Pricing(S 0, r, T, sigma, N) delta. T = T/N; u=exp(sigma * sqrt(delta. T)); d=1/u; p=(exp(r*delta. T) - d)/(u-d); lattice = zeros(N+1, N+1); x=zeros(1, N); for j=0: N x(j+1)=S 0*(u^j)*(d^(N-j)); if x(j+1)>=169 lattice(N+1, j+1)=1000; end if 169>x(j+1)>=84. 5 lattice(N+1, j+1)=1000 -1000*((169/x(j+1))-1); end if x(j+1)<84. 5 lattice(N+1, j+1)=0; end for i=N-1: 0 for j=0: i lattice(i+1, j+1) = exp(-r*delta. T) *. . . (p * lattice(i+2, j+2) + (1 -p) * lattice(i+2, j+1)); end price = lattice(1, 1); for i=1: 1: 500 price(i)=Lattice. Pricing(100, 0. 02, 1, 0. 5, i); end plot (1: 1: 500, price) xlabel N ylabel price
Monte carlo--Antithetic variates % Bls. MCAV. m function [Price, CI] = Bls. MCAV(S 0, X, r, T, sigma, NRepl) nu. T = (r - 0. 5*sigma^2)*T; si. T = sigma * sqrt(T); Veps = randn(NRepl, 1); Payoff 1 = max( 0 , XS 0*exp(nu. T+si. T*Veps) ); Payoff 2 = max( 0 , XS 0*exp(nu. T+si. T*(-Veps)) ); Disc. Payoff = exp(-r*T) * 0. 5 * (Payoff 1+Payoff 2); [Price, ~, CI] = normfit(Disc. Payoff); S 0=50; X=52; r=0. 1; T=5/12; sigma=0. 4; NRepl 1=100000; NRepl 2=200000; [c, Bls]=blsprice(S 0, X, r, T, sigma) randn('seed', 0); [MC 200000, CI 1] = Bls. MC(S 0, X, r, T, sigma, NRepl 2) randn('seed', 0); [MCAV 100000, CI 2] = Bls. MCAV(S 0, X, r, T, sigma, NRepl 1)
Mc-integration rand('seed', 0); N=1000; meanexp=zeros(1, N); for i=(1: N) meanexp(i)=2*mean(exp(2*rand(1, i))); end exp=exp(2)-1 exp 10=meanexp(10) exp 100=meanexp(100) exp 1000=meanexp(1000) plot(1: N, meanexp); exp =6. 3891 exp 10 =5. 9957 exp 100 =6. 3768 exp 1000 =6. 3803
蒙地卡羅估計圓周率 x=rand(1, 1000000); y=rand(1, 1000000); count=0; for i=1: 1000000 if x(i)*x(i)+y(i)*y(i) <=1 count=count+1; end pia=4*(count/1000000)
http: //upload. wikimedia. org/wikipedia/commons/8/84/Pi_30 K. gif
- Slides: 11