美式期权最小二乘蒙特卡洛模拟matlab代码中不懂得地方(红色标注),大家给看看啊
CheckLS.m文件:
S0 = 1; K = 1.1; r = 0.05;
sigma = 0.2; T = 1; NSteps = 100;
NRepl = 10000;
rng(0,'v5normal');
% fhandles = {@(x)ones(length(x),1), @(x)x, @(x)x.^2};
fhandles = {@(x)x, @(x)x.^2,@(x)x.^3};
pricebyLMS = GenericLS(S0,K,r,T,sigma,NSteps,NRepl,fhandles)
[assetprice, optionvalue]=binprice(S0,K,r,T,T/NSteps,sigma,0);
pricebyBIN = optionvalue(1,1)
GenericLS.m文件
function price = GenericLS(S0,K,r,T,sigma,NSteps,NRepl,fhandles)
%% 功能最小二乘的蒙特卡洛模拟
dt = T/NSteps;
discountVet = exp(-r*dt*(1:NSteps)');
NBasis = length(fhandles); %基函数个数???
% alpha = zeros(NBasis,1); %回归参数矩阵
% RegrMat = zeros(NRepl,NBasis); %初始化设计矩阵。
% generate sample paths
SPaths=AssetPaths(S0,r,sigma,T,NSteps,NRepl);
% SPaths(:,1) = []; % 删除第一列
CashFlows = max(0, K - SPaths(:,NSteps));
ExerciseTime = NSteps*ones(NRepl,1);
for step = NSteps-1:-1:1
InMoney = find(SPaths(:,step) < K);
XData = SPaths(InMoney,step);
RegrMat = zeros(length(XData), NBasis);
for k=1:NBasis
RegrMat(:, k) = feval(fhandles{k}, XData);
end
YData = CashFlows(InMoney).*discountVet(ExerciseTime(InMoney)-step);
alpha = RegrMat \ YData;%???
IntrinsicValue = K - XData;
ContinuationValue = RegrMat * alpha;
Index = find(IntrinsicValue > ContinuationValue);
ExercisePaths = InMoney(Index);
CashFlows(ExercisePaths) = IntrinsicValue(Index);
ExerciseTime(ExercisePaths) = step;
end
price = max(K-S0, mean(CashFlows.*discountVet(ExerciseTime)));
AssetPaths.m文件
function SPaths=AssetPaths(S0,mu,sigma,T,NSteps,NRepl)
%% 功能:生成股票价格路径
SPaths = zeros(NRepl, 1+NSteps);
SPaths(:,1) = S0;
dt = T/NSteps;
nudt = (mu-0.5*sigma^2)*dt;
sidt = sigma*sqrt(dt);
for i=1:NRepl
for j=1:NSteps
SPaths(i,j+1)=SPaths(i,j)*exp(nudt + sidt*randn);
end
end
x1=0:dt:T;
y1=SPaths';
y2=mean(SPaths);
figure
subplot(2,1,1);
plot(x1,y1)
subplot(2,1,2);
plot(x1,y2)
xlabel('期权存续期间')
ylabel('股票价格模拟路径')
SPaths(:,1) = [];