function  pvPayoffs = RunPolicyLS(sPaths,T,r,payoffFun,bfCoefficients,bfHandles)
[numScenarios, numSteps, ~] = size(sPaths);
dt = T/numSteps;
discountFactors = exp(-r*dt*(1:numSteps));
numBasis = length(bfHandles); % number of basis functions
% run scenarios for t=1,...
pvPayoffs = zeros(numScenarios,1);
for s = 1:numScenarios
    burned = false;
    for t = 1:numSteps-1  % time instants before maturity
        prices = squeeze(sPaths(s,t,:));
        intrinsicValue = feval(payoffFun, prices);
        continuationValue = getContinuation(t, prices);
        if intrinsicValue > continuationValue
            pvPayoffs(s) = discountFactors(t)*intrinsicValue;
            burned = true;
            break;
        end
    end
    % now check at maturity
    if ~burned
        prices = squeeze(sPaths(s,numSteps,:));
        intrinsicValue = feval(payoffFun, prices);
        if intrinsicValue > 0
            pvPayoffs(s) = discountFactors(numSteps)*intrinsicValue;
        end
    end
end
% nested function
    function value = getContinuation(idxTime, priceNow)
        value = 0;
        for k = 1:numBasis
            value = value+bfCoefficients(k,idxTime)* ...
                feval(bfHandles{k}, priceNow);
        end
    end
end

