Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.05 KB | None | 0 0
  1. function price = GenericLS(S0,X,r,T,sigma,NSteps,NRepl,fhandles)
  2. dt = T/NSteps;discount = exp(-r*dt);
  3. discountVet = exp(-r*dt*(1:NSteps));
  4. NBasis = length(fhandles); % number of basis functions
  5. a = zeros(NBasis,1); % regression
  6. parametersRegrMat = zeros(NRepl,NBasis);
  7. % generate sample paths
  8. SPaths=GenPathsA(S0,r,sigma,T,NSteps,NRepl);
  9. SPaths(:,1) = []; % get rid of starting prices
  10. %
  11. CashFlows = max(0, X - SPaths(:,NSteps));
  12. ExerciseTime = NSteps*ones(NRepl,1);
  13. for step = NSteps-1:-1:1
  14.     InMoney = find(SPaths(:,step) < X);
  15.     XData = SPaths(InMoney,step);
  16.     for i=1:NBasis
  17.         RegrMat(1:length(XData), i) = feval(fhandles(i), XData);
  18.     end
  19.     YData = CashFlows(InMoney) .* discountVet(ExerciseTime(InMoney) - step);
  20.     a = RegrMat(1:length(XData),:) \ YData;
  21.     IntrinsicValue = X - XData;
  22.     ContinuationValue = RegrMat(1:length(XData),:) * a;
  23.     Exercise = find(IntrinsicValue > ContinuationValue);
  24.     k = InMoney(Exercise);
  25.     CashFlows(k) = IntrinsicValue(Exercise);
  26.     ExerciseTime(k) = step;
  27. end % for
  28. price = mean(CashFlows.*discountVet(ExerciseTime));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement