427 Matlab 103071470 Generating pseudo random number Generating
4/27 Matlab作業 103071470 石磊
Generating pseudo random number •
Generating pseudo random number • • • • >> norm=zeros(10000, 1); for i=1: 10000 norm(i)=sum(rand(12, 1))-6; end >> S 0=50; X=52; r=0. 1; T=5/12; sigma=0. 4; nu. T=(r-0. 5*sigma^2)*T; si. T=sigma*sqrt(T); Disc. Payoff=exp(-r*T)*max(0, X-S 0*exp(nu. T+si. T*norm)); Price=mean(Disc. Payoff);
Generating pseudo random number • >> Price • Price = • 5. 1007 • >>[call, put]=blsprice(S 0, X, r, T, sigma); • >> put • put = • 5. 0689
Randn • function Price=Bls. Randn. Put(S 0, X, r, T, sigma, NPoints) • nu. T=(r-0. 5*sigma^2)*T; • si. T=sigma*sqrt(T); • rng(1); H 1=randn(NPoints, 1); • rng(2); H 2=randn(NPoints, 1); • Norm=[H 1; H 2]; • Disc. Payoff=exp(-r*T)*max(0, X-S 0*exp(nu. T+si. T*Norm)); • Price=mean(Disc. Payoff);
Randn • • • >> S 0=50; X=52; r=0. 1; T=5/12; sigma=0. 4; NPoints=5000; [call, put]=blsprice(S 0, X, r, T, sigma); >> put Price=Bls. Randn. Put(S 0, X, r, T, sigma, NPoints) • put = • 5. 0689 • Price = • 5. 1189
Rand+Box Muller • • • function Price=Bls. Rand. Put(S 0, X, r, T, sigma, NPoints) nu. T=(r-0. 5*sigma^2)*T; si. T=sigma*sqrt(T); rng(1); H 1=rand(NPoints, 1); rng(2); H 2=rand(NPoints, 1); VLog=sqrt(-2*log(H 1)); Norm 1=VLog. *cos(2*pi*H 2); Norm 2=VLog. *sin(2*pi*H 2); Norm=[Norm 1; Norm 2]; Disc. Payoff=exp(-r*T)*max(0, X-S 0*exp(nu. T+si. T*Norm)); Price=mean(Disc. Payoff);
Rand+Box Muller • >> S 0=50; • X=52; • r=0. 1; • T=5/12; • sigma=0. 4; • NPoints=5000; • [call, put]=blsprice(S 0, X, r, T, sigma); • Price=Bls. Rand. Put(S 0, X, r, T, sigma, NPoints); • put • Price • put = • 5. 0689 • Price = • 5. 0244
Halton+Box Muller:European Put • • • function Price = Bls. Halton. Put(S 0, X, r, T, sigma, NPoints, Base 1, Base 2) nu. T=(r-0. 5*sigma^2)*T; si. T=sigma*sqrt(T); % Use Box Muller to generate standard normals H 1=Get. Halton(ceil(NPoints/2), Base 1); H 2=Get. Halton(ceil(NPoints/2), Base 2); VLog=sqrt(-2*log(H 1)); Norm 1=VLog. *cos(2*pi*H 2); Norm 2=VLog. *sin(2*pi*H 2); Norm=[Norm 1; Norm 2]; Disc. Payoff=exp(-r*T)*max(0, X-S 0*exp(nu. T+si. T*Norm)); Price=mean(Disc. Payoff);
Halton+Box Muller:European Put • • • • >> S 0=50; X=52; r=0. 1; T=5/12; sigma=0. 4; NRepl=5000; Base 11=2; Base 12=7; Base 21=11; Base 22=7; Base 31=2; Base 32=4; [call, put]=blsprice(S 0, X, r, T, sigma); Halton 27 = Bls. Halton. Put(S 0, X, r, T, sigma, NRepl, Base 11, Base 12); Halton 117 = Bls. Halton. Put(S 0, X, r, T, sigma, NRepl, Base 21, Base 22); Halton 24 = Bls. Halton. Put(S 0, X, r, T, sigma, NRepl, Base 31, Base 32);
Halton+Box Muller:European Put • • put Halton 27 Halton 117 Halton 24 • put = • 5. 0689 • Halton 27 = • 5. 0725 • Halton 117 = • 5. 0735 • Halton 24 = • 4. 5808
Comp. Randn. Halton • • • %randn('seed', 0); subplot(2, 2, 1); plot(randn(100, 1), 'o'); title('radn distribution'); grid on % Halton+Box Muller 2 vs 7 subplot(2, 2, 2); H 1=Get. Halton(ceil(200/2), 2); H 2=Get. Halton(ceil(200/2), 7); VLog=sqrt(-2*log(H 1)); Norm 1=VLog. *cos(2*pi*H 2); Norm 2=VLog. *sin(2*pi*H 2); plot(Norm 1, Norm 2, 'o'); title('Halton. Box 2 vs 7 distribution'); • grid on • • •
Comp. Randn. Halton • • • % rand+Box Muller subplot(2, 2, 3); H 1=rand(100, 1); H 2=rand(100, 1); VLog=sqrt(-2*log(H 1)); Norm 1=VLog. *cos(2*pi*H 2); Norm 2=VLog. *sin(2*pi*H 2); plot(Norm 1, Norm 2, 'o'); title('Rand. Box distribution'); grid on % Halton+Box Muller 2 vs 4 subplot(2, 2, 4); H 1=Get. Halton(ceil(200/2), 2); H 2=Get. Halton(ceil(200/2), 4); VLog=sqrt(-2*log(H 1)); Norm 1=VLog. *cos(2*pi*H 2); Norm 2=VLog. *sin(2*pi*H 2); plot(Norm 1, Norm 2, 'o'); title('Halton. Box 2 vs 4 distribution'); • grid on • • •
Comp. Randn. Halton
- Slides: 14