% Script to replicate Example 8.6.1 of Glasserman's book
% Specify asset data and option
S0 = 100; r = 0.05; sigma = 0.2; delta = 0.1;
T = 3; numSteps = 9; K = 100;
payoffFun = @(x) max(max(x)-K, 0);
% Make in-sample and out-of-sample scenarios
rng default
numInScenarios = 7000;
InPaths = MakePaths(S0,r,delta,sigma,T,numSteps,numInScenarios);
numOutScenarios = 50000;
OutPaths = MakePaths(S0,r,delta,sigma,T,numSteps,numOutScenarios);
% Choose basis functions
bf0 = @(x) 1;
bf1a = @(x) x(1);
bf1b = @(x) x(2);
bf2a = @(x) x(1).^2;
bf2b = @(x) x(2).^2;
bf3a = @(x) x(1).^3;
bf3b = @(x) x(2).^3;
bf1a1b = @(x) x(1).*x(2);
bfMax = @(x) max(x);
% learn policy
bfHandles = {bf0, bf1a, bf2a, bf3a, bf1b, bf2b, bf3b, bf1a1b, bfMax};
bfCoefficients = FindPolicyLS(InPaths,T,r,payoffFun,bfHandles);
% apply policy
pvPayoffs = RunPolicyLS(OutPaths,T,r,payoffFun,bfCoefficients,bfHandles);
display(mean(pvPayoffs))