function [valueTable, actionTable] = MakePolicy(maxOnHand, demandProbs, ...
    orderCost, invPenalty, horizon)
valueTable = zeros(maxOnHand+1,horizon+1);
actionTable = zeros(maxOnHand+1,horizon);
maxDemand = length(demandProbs)-1;
demandValues = (0:maxDemand)'; 
% Value at t = horizon is identically zero
for t = (horizon-1):-1:0
    for onHand = 0:maxOnHand
        minCost = Inf;
        bestOrder = NaN;
        for order = 0:(maxOnHand-onHand)
            nextInv = onHand+order-demandValues;
            expCost = orderCost*order + dot(demandProbs,...
                invPenalty*(nextInv.^2) + valueTable(max(nextInv,0)+1,t+2));
            if expCost < minCost
                minCost = expCost;
                bestOrder = order;
            end
        end
        valueTable(onHand+1,t+1) = minCost;
        actionTable(onHand+1,t+1) = bestOrder;
    end
end

