额怎么发出来不完整……
函数:
function [EuroPrice AmerPrice] = BlackScholesLSM_asian_new(S,K,r,q,T,NS,NT,dt,PutCall,XmatrixHandle)
% Longstaff-Schwartz method for American options
% Fabrice Douglas Rouah, FRouah.com and Volopta.com
% INPUTS
% S = Matrix of simulated stock price, size NSxNT (rows are paths)
% K = Strike
% r = Risk free rate
% q = Dividend yield
% T = Maturity
% NS = Number of stock price paths
% NT = Number of time steps per path
% dt = Time increment (T/NT)
% PutCall = 'P' or 'C'
% Number of columns in the X matrix
NX = length(XmatrixHandle);
% Initialize the Cash Flows
CF = zeros(NS,NT);
%Create average price
S_avg=zeros(NS,NT);
S_sum=zeros(NS,NT);
parfor i=1:NT
S_sum(:,i)=sym(sum(S(:,1:i),2));
S_avg(:,i)=sym(S_sum(:,i)./i);
end
% Set the last cash flows to the intrinsic value
if strcmp(PutCall,'P')
CF(:,NT) = max(K - S_avg(:,NT), 0);
elseif strcmp(PutCall,'C')
CF(:,NT) = max(S_avg(:,NT) - K, 0);
end
% European option value
EuroPrice = exp(-r*T)*mean(CF(:,NT));
% Work backwards through the stock prices until time j=2.
% We could work through to time j=1 but the regression will not be
% of full rank at time 1, so this is cleaner.
for j=NT-1:-1:2
if strcmp(PutCall,'P')
I = find(S_avg(:,j) < K); % Indices for Puts
else
I = find(S_avg(:,j) > K); % Indices for Calls
end
X = S(I,j); % X-vector = time j stock prices.
EX=S_avg(I,j);
Y = CF(I,j+1)*exp(-r*dt); % Y-vector = time j+1 discounted CF.
Z = zeros(length(X),NX); % Design matrix for regression to predict cash flows
for k=1:NX
Z(:, k) = feval(XmatrixHandle{k}, X);
end
beta = Z\Y; % Regression parameters.
P = Z*beta; % Regression predicted CF.
if strcmp(PutCall,'P')
J = max(K - EX, 0) > P; % Early exercise for puts.
else
J = max(EX - K, 0) > P; % Early exercise for calls.
end
E = I(J); % Stock price indices where immediate exercise is optimal.
C = setdiff((1:NS),E)'; % Stock price indices where continuation is optimal.
if strcmp(PutCall,'P')
CF(E,j) = max(K - EX(J), 0); % Replace with early exercise for puts
else
CF(E,j) = max(EX(J) - K, 0);
end
CF(C,j) = exp(-r*dt)*CF(C,j+1); % Continued CF are discounted back one period.
end
% American option value
AmerPrice = exp(-r*dt)*mean(CF(:,2));