hw 13 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
HW 13 PowerPoint Presentation
Download Presentation
HW 13

Loading in 2 Seconds...

play fullscreen
1 / 35

HW 13 - PowerPoint PPT Presentation


  • 209 Views
  • Uploaded on

HW 13. 100071021. Prime Number Generator. if isPrime == 1 most = most + 1; Primes(most) = test; end if most >= num break; end test = test + 1; end end. function Primes = getPrime ( num ) Primes = zeros (num,1); Primes(1) = 2; test = 3;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'HW 13' - janna


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
hw 13

HW 13

100071021

prime number generator
Prime Number Generator

if isPrime == 1

most = most + 1;

Primes(most) = test;

end

if most >= num

break;

end

test = test + 1;

end

end

function Primes = getPrime( num)

Primes = zeros(num,1);

Primes(1) = 2;

test = 3;

most = 1;

while 1

test_ub = ceil(sqrt(test));

isPrime = 1;

for i = 1 : most

if test_ub < Primes(i)

break;

end

if mod(test,Primes(i))==0

isPrime = 0;

break;

end

end

halton path
Halton Path

function SPaths =HaltonPaths( S,mu,sigma,T,NSteps,NRepl )

NRepl = 2*ceil(NRepl/2);

dt = T/NSteps;

nudt = (mu-0.5*sigma^2)*dt;

sidt = sigma*sqrt(dt);

RandMat = zeros(NRepl, NSteps);

seeds = getPrime(2*NSteps);

for i = 1 : NSteps

H1 = Halton(NRepl/2,seeds(i*2-1));

H2 = Halton(NRepl/2,seeds(i*2));

Vlog = sqrt(-2*log(H1));

Norm1 = Vlog .* cos(2*pi*H2);

Norm2 = Vlog .* sin(2*pi*H2);

RandMat(:,i) = [Norm1 Norm2];

end

Increments = nudt + sidt*RandMat;

Init = log(S) * ones(NRepl,1);

LogPath = cumsum( [Init , Increments], 2 );

SPaths = exp(LogPath);

end

asian option monti carlo
Asian Option (Monti Carlo)

function [ call, put, cci, pci ] = AsianMC( S,K,r,sigma,T,mode,NSample,NRepl,Rtype )

% mode 0=>use ST 1=>use K

% Rtype 0=>rand 1=>Halton

% call=>ST(K)-A put=>A-ST(K)

Payoffs = zeros(NRepl,2);

if Rtype == 0

Paths = AssetPaths(S,r,sigma,T,NSample,NRepl);

else

Paths = HaltonPaths(S,r,sigma,T,NSample,NRepl);

end

slide5

for i = 1 : NRepl

if mode == 0

Payoffs(i,:) = [ max(0, Paths(i,NSample+1)-mean(Paths(i,2:NSample+1)) ) , max(0, mean(Paths(i,2:NSample+1)-Paths(i,NSample+1)) ) ];

else

Payoffs(i,:) = [ max(0, K-mean(Paths(i,2:NSample+1)) ) , max(0, mean(Paths(i,2:NSample+1)-K) ) ];

end

end

[call aux cci] = normfit( exp(-r*T)*transpose(Payoffs(:,1)) );

[put aux pci] = normfit( exp(-r*T)*transpose(Payoffs(:,2)) );

end

demo code
Demo Code

S = 50; K = 50; r = 0.1; T = 5/12; sigma = 0.4; NSample= 5; NRepl= 1000;

lim = 1000;

for i = 1 : lim

[STrand(i), a, b, d ] = AsianMC( S,K,r,sigma,T,0,NSample,i,0 );

STrandci(i) = b(2)-b(1);

[SThal(i), a, b, d ] = AsianMC( S,K,r,sigma,T,0,NSample,i,1 );

SThalci(i) = b(2)-b(1);

[Krand(i), a, b, d ] = AsianMC( S,K,r,sigma,T,1,NSample,i,0 );

Krandci(i) = b(2)-b(1);

[Khal(i), a, b, d ] = AsianMC( S,K,r,sigma,T,1,NSample,i,1 );

Khalci(i) = b(2)-b(1);

end

slide7

STrand(1000)

SThal(1000)

Krand(1000)

Khal(1000)

ans = 2.9223

ans = 3.1876

ans = 2.6753

ans = 2.7760

slide8

figure;

plot(1:lim,STrand,1:lim,Krand);

legend('ST - A','K - A');

title('ST vs K (Rand)');

slide10

figure;

plot(1:lim,SThal,1:lim,Khal);

legend('ST - A','K - A');

title('ST vs K (Halton)');

slide12

figure;

plot(1:lim,STrand,1:lim,SThal);

legend('Rand','Halton');

title('Random vs Halton (ST - A)');

slide14

figure;

plot(1:lim,Krand,1:lim,Khal);

legend('Rand','Halton');

title('Random vs Halton (K - A)');

slide16

figure;

plot(1:lim,STrandci,1:lim,SThalci);

legend('Rand','Halton')

title('CI of (ST - A)')

slide18

figure;

plot(1:lim,Krandci,1:lim,Khalci);

legend('Rand','Halton')

title('CI of (K - A)')

lookback option
Lookback Option

function [ call, put, cci, pci ] = LookBackMC( S,r,sigma,T,mode,NSample,NRepl,Rtype )

% mode 0=>use Smax 1=>use Smin

% Rtype 0=>rand 1=>Halton

% call=>ST-Sm put=>Sm-ST

Payoffs = zeros(NRepl,2);

if Rtype == 0

Paths = AssetPaths(S,r,sigma,T,NSample,NRepl);

else

Paths = HaltonPaths(S,r,sigma,T,NSample,NRepl);

end

slide21

for i = 1 : NRepl

if mode == 0

Payoffs(i,:) = [ max(0, Paths(i,NSample+1)-max(Paths(i,2:NSample+1)) ) , max(0, max(Paths(i,2:NSample+1)-Paths(i,NSample+1)) ) ];

else

Payoffs(i,:) = [ max(0, Paths(i,NSample+1)-min(Paths(i,2:NSample+1)) ) , max(0, min(Paths(i,2:NSample+1)-Paths(i,NSample+1)) ) ];

end

end

[call aux cci] = normfit( exp(-r*T)*transpose(Payoffs(:,1)) );

[put aux pci] = normfit( exp(-r*T)*transpose(Payoffs(:,2)) );

end

demo code1
Demo Code

S = 50; K = 50; r = 0.1; T = 5/12; sigma = 0.4; NSample= 5; NRepl= 1000;

lim = 1000;

for i = 1 : lim

[a, Maxrand(i), d, b ] = LookBackMC( S,r,sigma,T,0,NSample,i,0 );

Maxrandci(i) = b(2)-b(1);

[a, Maxhal(i), d, b ] = LookBackMC( S,r,sigma,T,0,NSample,i,1 );

Maxhalci(i) = b(2)-b(1);

[Minrand(i), a, b, d ] = LookBackMC( S,r,sigma,T,1,NSample,i,0 );

Minrandci(i) = b(2)-b(1);

[Minhal(i), a, b, d ] = LookBackMC( S,r,sigma,T,1,NSample,i,0 );

Minhalci(i) = b(2)-b(1);

end

slide23

Maxrand(1000)

Maxhal(1000)

Minrand(1000)

Minhal(1000)

ans = 5.5906

ans = 5.5884

ans = 7.1912

ans = 7.2195

slide24

figure;

plot(1:lim,Maxrand,1:lim,Minrand);

legend('Smax - ST','ST - Smin');

title('ST vs K (Rand)');

slide26

figure;

plot(1:lim,Maxhal,1:lim,Minhal);

legend('Smax - ST','ST - Smin');

title('ST vs K (Halton)');

slide28

figure;

plot(1:lim,Maxrand,1:lim,Maxhal);

legend('Rand','Halton');

title('Random vs Halton (Smax - ST)');

slide30

figure;

plot(1:lim,Minrand,1:lim,Minhal);

legend('Rand','Halton');

title('Random vs Halton (ST - Smin)');

slide32

figure;

plot(1:lim,Minrandci,1:lim,Minhalci);

legend('Rand','Halton')

title('CI of (Smax - ST)')

slide34

figure;

plot(1:lim,Maxrandci,1:lim,Maxhalci);

legend('Rand','Halton')

title('CI of (ST - Smin)')