for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end...
-
Upload
charleen-stokes -
Category
Documents
-
view
218 -
download
0
Transcript of for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end...
![Page 1: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/1.jpg)
HW 13100071021
![Page 2: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/2.jpg)
Prime Number Generatorfunction 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
if isPrime == 1
most = most + 1;
Primes(most) = test;
end
if most >= num
break;
end
test = test + 1;
end
end
![Page 3: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/3.jpg)
Halton Pathfunction 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
![Page 4: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/4.jpg)
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
![Page 5: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/5.jpg)
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
![Page 6: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/6.jpg)
Demo CodeS = 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
![Page 7: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/7.jpg)
STrand(1000)
SThal(1000)
Krand(1000)
Khal(1000)
ans = 2.9223
ans = 3.1876
ans = 2.6753
ans = 2.7760
![Page 8: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/8.jpg)
figure;
plot(1:lim,STrand,1:lim,Krand);
legend('ST - A','K - A');
title('ST vs K (Rand)');
![Page 9: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/9.jpg)
![Page 10: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/10.jpg)
figure;
plot(1:lim,SThal,1:lim,Khal);
legend('ST - A','K - A');
title('ST vs K (Halton)');
![Page 11: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/11.jpg)
![Page 12: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/12.jpg)
figure;
plot(1:lim,STrand,1:lim,SThal);
legend('Rand','Halton');
title('Random vs Halton (ST - A)');
![Page 13: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/13.jpg)
![Page 14: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/14.jpg)
figure;
plot(1:lim,Krand,1:lim,Khal);
legend('Rand','Halton');
title('Random vs Halton (K - A)');
![Page 15: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/15.jpg)
![Page 16: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/16.jpg)
figure;
plot(1:lim,STrandci,1:lim,SThalci);
legend('Rand','Halton')
title('CI of (ST - A)')
![Page 17: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/17.jpg)
![Page 18: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/18.jpg)
figure;
plot(1:lim,Krandci,1:lim,Khalci);
legend('Rand','Halton')
title('CI of (K - A)')
![Page 19: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/19.jpg)
![Page 20: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/20.jpg)
Lookback Optionfunction [ 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
![Page 21: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/21.jpg)
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
![Page 22: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/22.jpg)
Demo CodeS = 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
![Page 23: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/23.jpg)
Maxrand(1000)
Maxhal(1000)
Minrand(1000)
Minhal(1000)
ans = 5.5906
ans = 5.5884
ans = 7.1912
ans = 7.2195
![Page 24: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/24.jpg)
figure;
plot(1:lim,Maxrand,1:lim,Minrand);
legend('Smax - ST','ST - Smin');
title('ST vs K (Rand)');
![Page 25: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/25.jpg)
![Page 26: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/26.jpg)
figure;
plot(1:lim,Maxhal,1:lim,Minhal);
legend('Smax - ST','ST - Smin');
title('ST vs K (Halton)');
![Page 27: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/27.jpg)
![Page 28: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/28.jpg)
figure;
plot(1:lim,Maxrand,1:lim,Maxhal);
legend('Rand','Halton');
title('Random vs Halton (Smax - ST)');
![Page 29: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/29.jpg)
![Page 30: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/30.jpg)
figure;
plot(1:lim,Minrand,1:lim,Minhal);
legend('Rand','Halton');
title('Random vs Halton (ST - Smin)');
![Page 31: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/31.jpg)
![Page 32: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/32.jpg)
figure;
plot(1:lim,Minrandci,1:lim,Minhalci);
legend('Rand','Halton')
title('CI of (Smax - ST)')
![Page 33: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/33.jpg)
![Page 34: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/34.jpg)
figure;
plot(1:lim,Maxrandci,1:lim,Maxhalci);
legend('Rand','Halton')
title('CI of (ST - Smin)')
![Page 35: for i = 1 : most for i = 1 : most if test_ub < Primes(i) if test_ub < Primes(i) break; break; end end if mod(test,Primes(i))==0 if mod(test,Primes(i))==0.](https://reader036.fdocuments.in/reader036/viewer/2022062409/5697bf731a28abf838c7eeaa/html5/thumbnails/35.jpg)