Professional Documents
Culture Documents
110071602 劉軒瑜
第二週作業
%BS.m
function [C,P]=BS(S0,X,r,sigma,T,q)
d1=(log(S0/X)+(r-q+sigma^2/2)*T)/(sigma*sqrt(T));
d2=d1-(sigma*sqrt(T));
%BS_input.m
C=S0*exp(-q*T)*normcdf(d1)-X*exp(-r*T)*normcdf(d2);
P=X*exp(-r*T)*normcdf(-d2)-S0*exp(-q*T)*normcdf(-d1);
S0=input("S0=");
X=input("X=");
r=input("r=")
T=input("T=")
sigma=input("sigma=")
q=input("q=")
[C,P]=BS(S0,X,r,sigma,T,q)
[c,p]=blsprice(S0,X,r,sigma,T,q)
%pv.m
function x=pv(y,n,Ct)
x=0;
d=1+y;
for i=1:n;
x=x+Ct(i)/d;
d=d*(1+y);
end
%pv_input.m
y=input("y=")
n=input("n=")
Ct=input("Ct=")
x=pv(y,n,Ct)
第三週作業
%pvrate.m
n=3
Ct=[10 10 110]
for y=0:0.001:0.5;
x=pv(y,n,Ct);
plot(y,x,'.blue');
hold on;
end
grid on;
title('bond price');
xlabel('r');
ylabel('bond price');
axis([0 0.5 40 130]);
%plotlongcall.m
X=50;
c=2;
St=40:0.05:60;
subplot(2,2,1);
plot(St,max(St-X,0)-c);
title('long call');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
subplot(2,2,2);
plot(St,c-max(St-X,0));
title('short call');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
subplot(2,2,3);
plot(St,max(X-St,0)-c);
title('long put');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
subplot(2,2,4);
plot(St,c-max(X-St,0));
title('short put');
xlabel('S');
ylabel('profit');
axis([40 60 -10 10]);
%DeltaBLS.m
x=50;
r=0.05;
t=0.5;
sig=0.2;
div=0;
s=30:120;
subplot(2,2,1)
[call_delta,put_delta]=blsdelta(s,x,r,t,sig,div);
plot(s,call_delta,'-');
title('Delta vs.s');
xlabel('s');
ylabel('Delta');
legend('Delta');
subplot(2,2,2)
[call_theta,put_theta]=blstheta(s,x,r,t,sig,div);
plot(s,call_theta,'-');
title('Theta vs.s');
xlabel('s');
ylabel('Theta');
legend('Theta');
subplot(2,2,3)
call_gamma=blsgamma(s,x,r,t,sig,div);
plot(s,call_gamma,'-');
title('Gamma vs.s');
xlabel('s');
ylabel('Gamma');
legend('Gamma');
subplot(2,2,4)
call_vega=blsvega(s,x,r,t,sig,div);
plot(s,call_vega,'-');
title('Vega vs.s');
xlabel('s');
ylabel('Vega');
legend('Vega')
%deltavst.m
x=50;
r=0.05;
t=0.05:0.01:0.5;
sig=0.2;
div=0;
s=30;
[call_delta,put_delta]=blsdelta(s,x,r,t,sig,div);
grid on;
plot(t,call_delta,'-');
title('Delta vs.t');
xlabel('t');
ylabel('Delta');
legend('Delta');
第四週作業
% protective_call.m protective call
c = 2;
X = 100;
S0 = 90:1:110;
plot(S0, 100-S0, "--");
hold on;
plot(S0, max(S0-X, 0)-c, '.');
plot(S0, 100-S0 + max(S0-X, 0)-c);
grid on;
title('protective call');
xlabel('S0');
ylabel('payoff');
legend('short stock', 'long call', 'protective call',
'Location','southwest')
% covered_put.m Covered put
p = 2;
X = 100;
S0 = 90:1:110;
plot(S0, 100-S0, '--');
hold on;
plot(S0, -(max(X-S0, 0)-p), '.');
plot(S0, 100-S0 - (max(X-S0, 0)-p));
grid on;
title('covered put');
xlabel('S0');
ylabel('profit');
legend('short stock', 'short put', 'covered put',
'Location','northeast')
% bull_spread_arbitrary.m bull spread call 套利
c1=1; c2=3;
X1=100; X2=105;
S0=95:1:110;
plot(S0, max(S0-X1, 0)-c1, '--');
hold on;
plot(S0, -(max(S0-X2, 0)-c2), '.');
plot(S0, max(S0-X1, 0)-c1-(max(S0-X2, 0)-c2));
grid on;
title('bull spread');
xlabel('S0');
ylabel('profit');
plot(S0, zeros(1,16), 'black')
% bull_spread_loss.m bull spread call 虧錢
c1=7; c2=1;
X1=100; X2=105;
S0=95:1:110;
plot(S0, max(S0-X1, 0)-c1, '--');
hold on;
plot(S0, -(max(S0-X2, 0)-c2), '.');
plot(S0, max(S0-X1, 0)-c1-(max(S0-X2, 0)-c2));
grid on;
title('bull spread');
xlabel('S0');
ylabel('profit');
plot(S0, zeros(1,16), 'black')
% bull_spread.m bull spread put
subplot(2,2,1);
c1=3; c2=1;
X1=100; X2=105;
S0=95:1:110;
plot(S0, max(S0-X1, 0)-c1, '--'); % long call
hold on;
plot(S0, -(max(S0-X2, 0)-c2), '.'); % short call
plot(S0, max(S0-X1, 0)-c1-(max(S0-X2, 0)-c2));
grid on;
title('bull spread(call)');
xlabel('S0');
ylabel('profit');
axis([95 110 -4 4]);
plot(S0, zeros(1,16), 'black');
subplot(2,2,2);
p1=1; p2=3;
X1=100; X2=105;
S0=95:1:110;
plot(S0, max(X1-S0,0)-p1, '--'); %long put
hold on;
plot(S0, -(max(X2-S0,0)-p2), '.'); % short put
plot(S0, max(X1-S0, 0)-p1-(max(X2-S0, 0)-p2));
grid on;
title('bull spread(put)');
xlabel('S0');
ylabel('profit');
axis([95 110 -4 4]);
plot(S0, zeros(1,16), 'black')
subplot(2,2,3)
p1=3; p2=1;
X1=105; X2=100;
S0=95:1:110;
plot(S0, max(X1-S0,0)-p1, '--'); %long put
hold on;
plot(S0, -(max(X2-S0,0)-p2), '.'); % short put
plot(S0, max(X1-S0, 0)-p1-(max(X2-S0, 0)-p2));
grid on;
title('bear spread(put)');
xlabel('S0');
ylabel('profit');
axis([95 110 -4 4]);
plot(S0, zeros(1,16), 'black')
subplot(2,2,4)
c1=1; c2=3;
X1=105; X2=100;
S0=95:1:110;
plot(S0, max(S0-X1, 0)-c1, '--'); % long call
hold on;
plot(S0, -(max(S0-X2, 0)-c2), '.'); % short call
plot(S0, max(S0-X1, 0)-c1-(max(S0-X2, 0)-c2));
grid on;
title('bear spread(call)');
xlabel('S0');
ylabel('profit');
axis([95 110 -4 4]);
plot(S0, zeros(1,16), 'black')
% condor.m condor call
c1 = 2; c2 = 2; c3 = 4; c4 = 4;
X1 = 70; X2 = 80; X3 = 130; X4 = 140;
S0 = 50:1:160;
plot(S0, max(S0-X1, 0)-c1, '--'); % long call
hold on;
plot(S0, -(max(S0-X2,0)-c2), '-'); % short call
plot(S0, -(max(S0-X3,0)-c3), '.'); % short call
plot(S0, max(S0-X4, 0)-c4, '+'); % long call
plot(S0, max(S0-X1, 0)-c1 -(max(S0-X2,0)-c2) -(max(S0-X3,0)-c3) + max(S0-X4, 0)-c4, '*');
grid on;
title(['condor(call)']);
xlabel('S0');
ylabel('profit');
axis([55 160 -11 11])
plot(S0, zeros(1,111), 'black');
% diagonal spread
c1 = 3; c2 = 2;
S0 = 100; X1 = 100; X2 = 95; r = 0.02; T = 0.1; sigma = 0.2;
S0 = 90:1:110;
[m, n] = size(S0);
for i = 1:n;
[C(i), P(i)] = blsprice(S0(i), X1, r, T, sigma);
end
plot(S0, C-c2, '--'); % long call
hold on;
plot(S0, -(max(S0-X1, 0)- c1), '.'); % short call
plot(S0, C-c2-(max(S0-X2, 0)- c1));
grid on;
title('diagonal spread');
xlabel('S0');
ylabel('profit');
plot(S0, zeros(1,21), 'black');
第五週作業
% Stock_strangle.m CMBO: Stock Index and strangle
X1=55;
X2=50;
c=2;
St=40:0.05:60;
plot(St,(-max(St-X1,0))+c+(-max(X2-St,0)+c), '.'); % short strangle(short call+short put)
hold on;
plot(St, St-X2, '--'); %long stock
plot(St, St-X2+(-max(St-X1,0))+c+(-max(X2-St,0)+c),
'-');
grid on;
title('CMBO');
xlabel('S0');
ylabel('profit');
legend({'short strangle', 'long stock', 'CMBO'},
'Location', 'SouthEast');
% BXM_Put.m BXM and Put
X1=50;
X2=45;
c=2;
St=40:0.1:60;
plot(St,-max(X2-St,0)+c, '.'); % short put
hold on;
plot(St, St-X2+(c-max(St-X1,0)), '--'); %BXM (long stock + short call)
plot(St, -max(X2-St,0)+c + St-X2 + c-max(St-X1,0), '-');
grid on;
title('CMBO');
xlabel('S0');
ylabel('profit');
legend({'short put', 'BXM', 'CMBO'}, 'Location',
'SouthEast');
% butterfly_spread_and_BS.m
c1 = 6; c2 = 3; c3 = 1;
X1 = 95; X2 = 100; X3 = 105;
S0 = 90:1:115;
end
plot(S0, max(S0-X1, 0)-c1 + max(S0-X3, 0)-c3-2*(max(S0-X2, 0)-c2));%T=0
hold on;
plot(S0, (C11-c1)-2*(C21-c2)+(C31-c3), '--'); %T=0.05
plot(S0, (C12-c1)-2*(C22-c2)+(C32-c3), '.'); %T=0.1
grid on;
title('butterfly spread');
xlabel('S0');
ylabel('profit');
legend({'t=0', 't=0.05', 't=0.1'}, 'Location', 'NorthEast');
axis([90 115 -2 5 ]);
第六週作業
% LatticeEurPut.m The CRR Tree-European put option
function [price, lattice] = LatticeEurPut(S0, X, r, T, sigma, N)
deltaT = T/N;
u = exp(sigma * sqrt(deltaT));
d = 1/u;
p = (exp(r*deltaT)-d)/(u-d);
lattice = zeros(N+1, N+1);
for j = 0:N
lattice(N+1, j+1)=max(0, X - S0*(u^j)*(d^(N-j)));
end
for i = N-1:-1:0
for j = 0:i
lattice(i+1, j+1) = exp(-r*deltaT)*(p * lattice(i+2, j+2) + ...
(1-p)*lattice(i+2, j+1));
end
end
price = lattice(1,1);
% CompLatticeBLSPut.m
S0 = 50;
X = 50;
r = 0.1;
sigma = 0.4;
T = 5/12;
N = 50;
[BlsC,BlsP] = blsprice(S0, X, r, T, sigma);
LatticeP = zeros(1, N);
for i = (1:N)
LatticeP(i)= LatticeEurPut(S0, X, r, T, sigma, i);
end
plot(1:N, ones(1,N)*BlsP);
hold on;
plot(1:N, LatticeP);
title('ComLatticBlsPut');
% LatticeAmCallvsBLS.m
S0 = 50;
X = 50;
r = 0.02;
T = 1;
sigma = 0.2;
N = 10;
div = 0.1;
tau = 3;
% LatticeAmCall vs BLS
subplot(1,2,1);
[BlsC,BlsP] = blsprice(S0, X, r, T, sigma);
LatticeC = zeros(1, N);
for i = (1:N)
LatticeC(i)= LatticeAmCall(S0, X, r, T, sigma, i);
end
plot(1:N, ones(1,N)*BlsC);
hold on;
plot(1:N, LatticeC);
title('LatticeAmCallvsBLS');
% LatticeEurCall
subplot(1,2,2);
[BlsC,BlsP] = blsprice(S0, X, r, T, sigma);
LatticeC = zeros(1, N);
for i = (1:N)
LatticeC(i)= LatticeEurCall(S0, X, r, T, sigma, i);
end
plot(1:N, ones(1,N)*BlsC);
hold on;
plot(1:N, LatticeC);
title('LatticeEurCallvsBLS');
% Dividend.m
S0 = 50;
X = 50;
r = 0.1;
sigma = 0.4;
T = 5/12;
N = 50;
q=0:0.1:1;
[m,n]=size(q);
for i = 1:n
Lattice_Am(i) = LatticeAmCall_q(S0, X, r, T, sigma, N, q(i));
Lattice_Eur(i) = LatticeEurCall_q(S0, X, r, T, sigma, N, q(i));
end
plot(q, Lattice_Am);
hold on;
plot(q, Lattice_Eur);
title('Dividend');
legend({'LatticeAmCall+q', 'LatticeEurCall+q'}, 'Location', ['SouthWest']);
第七週作業
% LatticeEurCallDivP.m dividend proportion
function [price, lattice] = LatticeEurCallDivP(S0, X, r, T, sigma, N, div, tau)
deltaT = T/N;
u = exp(sigma * sqrt(deltaT));
d = 1/u;
p = (exp(r*deltaT)-d)/(u-d);
lattice = zeros(N+1, N+1);
for j = 0:N
lattice(N+1, j+1)=max(0, S0*(u^j)*(d^(N-j))*(1-div) - X);
end
for i = N-1:-1:tau
for j = 0:i
lattice(i+1, j+1) = exp(-r*deltaT)...
*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1));
end
end
for i = tau-1:-1:0
for j = 0:i
lattice(i+1, j+1) = exp(-r*deltaT)...
*(p*lattice(i+2, j+2)+(1-p)*lattice(i+2, j+1));
end
end
price = lattice(1,1);
% LatticeAmCallDivP.m
function [price, lattice] = LatticeAmCallDivP(S0, X, r, T, sigma, N, div, tau)
deltaT = T/N;
u = exp(sigma * sqrt(deltaT));
d = 1/u;
p = (exp(r*deltaT) - d)/(u-d);
lattice = zeros(N+1, N+1);
for j = 0:N
lattice(N+1, j+1) = max(0, S0*(u^j)*(d^(N-j))*(1-div)-X);
end
for i = N-1:-1:tau
for j = 0:i
lattice(i+1, j+1) = max(S0*u^j*d^(i-j)*(1-div)-X,...
exp(-r*deltaT)*(p*lattice(i+2, j+2) + (1-p) * lattice(i+2, j+1)));
end
end
for i = tau-1:-1:0
for j = 0:i
lattice(i+1, j+1) = max(S0*u^j*d^(i-j)-X,...
exp(-r*deltaT)*(p*lattice(i+2, j+2)+(1-p)*lattice(i+2, j+1)));
end
end
price = lattice(1,1);
% LatticeEurPutDivP.m
function [price, lattice] = LatticeEurPutDivP(S0, X, r, T, sigma, N, div, tau)
deltaT = T/N;
u = exp(sigma * sqrt(deltaT));
d = 1/u;
p = (exp(r*deltaT)-d)/(u-d);
lattice = zeros(N+1, N+1);
for j = 0:N
lattice(N+1, j+1)=max(0, X - S0*(u^j)*(d^(N-j))*(1-div));
end
for i = N-1:-1:tau
for j = 0:i
lattice(i+1, j+1) = exp(-r*deltaT)...
*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1));
end
end
for i = tau-1:-1:0
for j = 0:i
lattice(i+1, j+1) = exp(-r*deltaT)*...
(p*lattice(i+2, j+2)+(1-p)*lattice(i+2, j+1));
end
end
price = lattice(1,1);
for j = 0:N
lattice(N+1, j+1)=max(0, S0a*(u^j)*(d^(N-j))-X);
end
for i = N-1:-1:tau
for j = 0:i
lattice(i+1, j+1) = ...
exp(-r*deltaT)*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1));
end
end
for i = tau-1:-1:0
for j = 0:i
lattice(i+1, j+1) = ...
exp(-r*deltaT)*(p*lattice(i+2, j+2)+(1-p)*lattice(i+2, j+1));
end
end
price = lattice(1,1);
for j = 0:N
lattice(N+1, j+1)=max(0, S0a*(u^j)*(d^(N-j))-X);
end
for i = N-1:-1:tau
for j = 0:i
lattice(i+1, j+1) = max(S0a*(u^j)*(d^(i-j))-X,...
exp(-r*deltaT)*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1)));
end
end
for i = tau-1:-1:0
for j = 0:i
lattice(i+1, j+1) = max(S0a*(u^j)*(d^(i-j))-X-D*exp(-r*(tau-i)*deltaT),...
exp(-r*deltaT)*(p*lattice(i+2, j+2)+(1-p)*lattice(i+2, j+1)));
end
end
price = lattice(1,1);
for j = 1:N+1
lattice(N+1, j) = max(0, X-S0*(d^(N-j+1)));
end
for j = N+2: 2*N+1
lattice(N+1, j) = max(0, X-S0*(u^(j-N-1)));
end
for i = N-1:-1:0
for j = 1:2*i+1
lattice(i+1, j) = exp(-r*deltaT)*...
(Pd*lattice(i+2, j) + Pm*lattice(i+2, j+1) +...
Pu*lattice(i+2, j+2));
end
end
price = lattice(1,1);
for j = 1:N+1
lattice(N+1, j) = max(0, X-S0*(d^(N-j+1)));
end
for j = N+2: 2*N+1
lattice(N+1, j) = max(0, X-S0*(u^(j-N-1)));
end
for i = N-1:-1:0
for j = 1:2*i+1
p = max(X-S0*(d^(N-j+1)), X-S0*(u^(j-N-1)));
lattice(i+1, j) = max(p, exp(-r*deltaT)*...
(Pd*lattice(i+2, j) + Pm*lattice(i+2, j+1) +...
Pu*lattice(i+2, j+2)));
end
end
price = lattice(1,1);
第十週作業
% ComBlsMc_P.m European put 4/19 hw1
S0 = 50;
X = 52;
r = 0.1;
T = 5/12;
sigma = 0.4;
NRepl1 = 1000;
NRepl2 = 200000;
Bls
MC1000
Pl1000
MC200000
Pl200000
% BlsMC_P.m BlsMC-Put
function[Price, Pl] = BlsMC_P(S0, X, r, T, sigma, NRepl)
nuT = (r - 0.5*sigma^2)*T;
siT = sigma * sqrt(T);
%compBlsMCAV_P.m
S0 = 50;
X = 52;
r = 0.1;
T = 5/12;
sigma = 0.4;
NRepl1 = 200000;
NRepl2 = 100000;
% BlsMCAV_P
function [Price, Cl] = BlsMCAV_P(S0, X, r, T, sigma, NRepl)
nuT = (r - 0.5*sigma^2)*T;
siT = sigma * sqrt(T);
Veps = randn(NRepl, 1);
function [price, lattice] = LatticeAmPut_5(S0, X1, X2, X3, X4, par, r, T, sigma, N)
deltaT = T/N;
n = N/5;
strike = [X1, X2, X3, X4];
u = exp(sigma*sqrt(deltaT));
d = 1/u;
p = (exp(r*deltaT)-d)/(u-d);
lattice = zeros(N+1, N+1);
for j = 0:N
lattice(N+1, j+1) = max(par, X4 - S0*(u^j)*(d^(N-j)));
end
for i = N-1:-1:N-n
for j = 0:i
lattice(i+1, j+1) = max(X4 - S0*(u^j)*(d^(N-j)), ...
exp(-r*deltaT)*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1)));
end
end
for i = N-n-1:-1:N-2*n
for j = 0:i
lattice(i+1, j+1) = max(X3 - S0*(u^j)*(d^(N-j)), ...
exp(-r*deltaT)*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1)));
end
end
for i = N-2*n-1:-1:N-3*n
for j = 0:i
lattice(i+1, j+1) = max(X2 - S0*(u^j)*(d^(N-j)), ...
exp(-r*deltaT)*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1)));
end
end
for i = N-3*n-1:-1:0
for j = 0:i
lattice(i+1, j+1) = max(X1 - S0*(u^j)*(d^(N-j)), ...
exp(-r*deltaT)*(p * lattice(i+2, j+2) + (1-p)*lattice(i+2, j+1)));
end
end
price = lattice(1,1);
end
%LAMP5_input.m
S0 = 1/131.75;
X1 = 1/131.75;
X2 = 1/129.50;
X3 = 1/127.00;
X4 = 1/124.50;
par = 9.25;
r = 0.001;
sigma = 0.5;
T = 5;
N = 100;
% MCIntegration2.m
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
exp10 = meanexp(10)
exp100 = meanexp(100)
exp1000 = meanexp(1000)
plot(1:N, meanexp);
第十四週作業
% Halton + Box Muller
function Price = HaltonBox_put(S0, X, r, T, sigma, Npoints, Base1, Base2)
[call, put]=blsprice(S0,X,r,T,sigma);
Halton27 = BlsHalton_put(S0,X,r,T,sigma,NRepl,Base11,Base12);
Halton117 = BlsHalton_put(S0,X,r,T,sigma,NRepl,Base21,Base22);
Halton24 = BlsHalton_put(S0,X,r,T,sigma,NRepl,Base31,Base32);
put
Halton27
Halton117
Halton24
% Haltoncdf_put.m
% Halton + cdf
function Price = Haltoncdf_put(S0, X, r, T, sigma, Npoints, Base1, Base2)
Norm1 = norminv(H1);
Norm2 = norminv(H2);
Norm = [Norm1 ; Norm2];
% CompHaltoncdf.m
S0=50;
X=52;
r=0.1;
T=5/12;
sigma=0.4;
NRepl=5000;
Base11=2;Base12=7;
Base21=11;Base22=7;
Base31=2;Base32=4;
[call, put]=blsprice(S0,X,r,T,sigma);
Halton27 = Haltoncdf_put(S0,X,r,T,sigma,NRepl,Base11,Base12);
Halton117 = Haltoncdf_put(S0,X,r,T,sigma,NRepl,Base21,Base22);
Halton24 = Haltoncdf_put(S0,X,r,T,sigma,NRepl,Base31,Base32);
put
Halton27
Halton117
Halton24
% randBox_put.m
% Random + Box Muller
function Price = randBox_put(S0, X, r, T, sigma, Npoints)
% ComrandBox.m
S0=50;
X=52;
r=0.1;
T=5/12;
sigma=0.4;
NRepl=5000;
[call, put]=blsprice(S0,X,r,T,sigma);
rand = randBox_put(S0,X,r,T,sigma,NRepl);
put
rand
% randcdf_put.m
% Rand + cdf
function Price = randcdf_put(S0, X, r, T, sigma, Npoints)
Norm1 = norminv(H1);
Norm2 = norminv(H2);
Norm = [Norm1 ; Norm2];
% Comprandcdf
S0=50;
X=52;
r=0.1;
T=5/12;
sigma=0.4;
NRepl=5000;
[call, put]=blsprice(S0,X,r,T,sigma);
rand = randcdf_put(S0,X,r,T,sigma,NRepl);
put
rand
第十五週作業
%DOPutMCCV.m
function [P,Cl,NCrossed] = DOPutMCCV(S0,x,r,T,sigma,Sb,NRepl,NPilot)
nuT = (r - 0.5*sigma^2) * T;
siT = sigma * sqrt(T);
Payoff1 = zeros(NPilot,1);
Payoff2 = zeros(NRepl,1);
NCrossed = 0;
% compute parameters
StockVals = S0*exp(nuT+siT*randn(NPilot,1));
for i = 1:NPilot
crossed = any (StockVals(i)<=Sb);
% 未跨過,傳統 put
if crossed == 0
Payoff1(i) = max(0,x-StockVals(i));
% 跨過
else
Payoff1(i) = 0;
NCrossed = NCrossed+1;
end
end
MatCov = zeros(2,2);
MatCov = cov(StockVals, OptionVals);
VarY = S0^2 * exp(2*r*T) * (exp(T * sigma^2) - 1);
c = - MatCov(1,2) / VarY;
ExpY = S0 * exp(r*T);
% simulation
NCrossed = 0;
NewStockVals = S0*exp(nuT+siT*randn(NRepl,1));
for i=1:NRepl
crossed = any(NewStockVals(i) <= Sb);
if crossed == 0
Payoff2(i) = max(0, x -NewStockVals(i));
else
Payoff2(i) = 0;
NCrossed = NCrossed + 1;
end
end
End
% CompDOPutMCCV.m
clear;
S0 = 50;
X = 50;
r = 0.1;
T = 2/12;
sigma = 0.4;
Sb = 40;
NSteps = 60;
NRepl = 50000;
NPilot = 5000;
randn('seed',0);
[DOPutMC,CI1,NCrossed1] = DOPutMC(S0,X,r,T,sigma,Sb,NSteps,NRepl);
[DOPutMCCV,CI2,NCrossed2]=DOPutMCCV(S0,X,r,T,sigma,Sb,NRepl,NPilot);
DOPutMC
DOPutMCCV
CI2
NCrossed2
%HaltonDOPut.m
function [Price,Cl,NCrossed] = HaltonDOPut(S0,X,r,T,sigma,Sb,NPoints,Base1,Base2)
nuT = (r - 0.5*sigma^2)*T;
siT = sigma * sqrt(T);
[Price,VarPrice,Cl] = normfit(ControlVars);
end
% UOCallMCCondIS.m
function [Cuo,Cl,NCrossed] = UOCallMCCondIS(S0,x,r,T,sigma,Sb,NSteps,NRepl,bp)
dt = T/NSteps;
nudt = (r-0.5*sigma^2)*dt;
b = bp*nudt;
sidt = sigma * sqrt(dt);
[Call,Put] = blsprice(S0,x,r,T,sigma);
% Generate asset paths and payoff for the down and in option
NCrossed = 0;
Payoff = zeros(NRepl,1);
Times = zeros(NRepl,1);
StockVals = zeros(NRepl,1);
ISRatio = zeros(NRepl,1);
for i = 1:NRepl
% generate normal
vetZ = nudt -b +sidt*randn(1,NSteps);
LogPath = cumsum([log(S0),vetZ]);
Path = exp(LogPath);
tcrossed = min(find(Path>=Sb));
if not(isempty(tcrossed))
NCrossed = NCrossed+1;
TBreach = tcrossed-1;
Times(NCrossed) = TBreach*dt;
StockVals(NCrossed) = Path(tcrossed);
end
if (NCrossed>0)
[Caux,Paux] = blsprice(StockVals(1:NCrossed),x,r,T-Times(1:NCrossed),sigma);
Payoff(1:NCrossed) = exp(-r*Times(1:NCrossed)) .* Caux .* ISRatio(1:NCrossed);
end
end
%UOCallMCCond.m
function [Cuo,Cl,NCrossed] = UOCallMCCond(S0,x,r,T,sigma,Sb,NSteps,NRepl)
dt = T/NSteps;
[Call,Put] = blsprice(S0,x,r,T,sigma);
% Generate asset paths and payoff for the down and in option
NCrossed = 0;
Payoff = zeros(NRepl,1);
Times = zeros(NRepl,1);
StockVals = zeros(NRepl,1);
for i = 1:NRepl
Path = AssetPaths1(S0,r,sigma,T,NSteps,1);
tcrossed = min(find(Path>=Sb));
if not(isempty(tcrossed))
NCrossed = NCrossed+1;
Times(NCrossed) = (tcrossed-1)*dt;
StockVals(NCrossed) = Path(tcrossed);
end
end
if (NCrossed>0)
[Caux,Paux] = blsprice(StockVals(1:NCrossed),x,r,T-Times(1:NCrossed),sigma);
Payoff(1:NCrossed) = exp(-r*Times(1:NCrossed)) .* Caux;
end
end
%UOCallMC.m
function [P,Cl,NCrossed] = UOCallMC(S0,x,r,T,sigma,Sb,NSteps,NRepl)
% simulation
for i = 1:NRepl
Path = AssetPaths1(S0,r,sigma,T,NSteps,1);
crossed = any (Path>=Sb);
% 未跨過,傳統 put
if crossed == 0
Payoff(i) = max(0,Path(NSteps+1)-x);
% 跨過
else
Payoff(i) = 0;
NCrossed = NCrossed+1;
end
end
[P,aux,Cl] = normfit(exp(-r*T)*Payoff);
end
% UOCall.m
function C = UOCall(S0,X,r,T,sigma,Sb)
a = (Sb/S0) ^ (-1+(2*r/sigma^2)) ;
b = (Sb/S0) ^ ( 1+(2*r/sigma^2)) ;
End
第十六週作業
% EuCallExpl1.m
function price = EuCallExpl1(S0,X,r,T,sigma,Smax,dS,dt)
S0=50;
X=50;
r=0.1;
T=5/12;
sigma1=0.4;
sigma2=0.3;
Smax=100;
dS1=2;
dS2=1.5;
dS3=1;
dT=5/1200;
[c04,p1]=blsprice(S0,X,r,T,sigma1);
Exps04=EuCallExpl1(S0,X,r,T,sigma1,Smax,dS1,dT);
[c03,p2]=blsprice(S0,X,r,T,sigma2);
Exps03dS2=EuCallExpl1(S0,X,r,T,sigma2,Smax,dS1,dT);
ExpdS15=EuCallExpl1(S0,X,r,T,sigma2,Smax,dS2,dT);
ExpdS1=EuCallExpl1(S0,X,r,T,sigma2,Smax,dS3,dT);
c04
Exps04
c03
Exps03dS2
ExpdS15
ExpdS1