Problem
The constOptim function is R is giving me a set of parameter estimates. These parameter estimates are spending values at 12 different points in the year and should be monotonically decreasing.
I need them to be monotonic and for the gaps between each parameter to right for the application I have in mind. For the purposes of this the pattern in spending values is important and not the absolute values. I guess in optimization terms this means I need the tolerance to be small compared to the differences in parameter estimates.
Minimal Working Example (with simple Utility Function)
# Initial Parameters and Functions
Budget = 1
NumberOfPeriods = 12
rho = 0.996
Utility_Function <- function(x){ x^0.5 }
Time_Array = seq(0,NumberOfPeriods-1)
# Value Function at start of time.
ValueFunctionAtTime1 = function(X){
Frame = data.frame(X, time = Time_Array)
Frame$Util = apply(Frame, 1, function(Frame) Utility_Function(Frame["X"]))
Frame$DiscountedUtil = apply(Frame, 1, function(Frame) Frame["Util"] * rho^(Frame["time"]))
return(sum(Frame$DiscountedUtil))
}
# The sum of all spending in the year should be less than than the annual budget.
# This gives the ui and ci arguments
Sum_Of_Annual_Spends = c(rep(-1,NumberOfPeriods))
# The starting values for optimisation is an equal expenditure in each period.
# The denominator is multiplied by 1.1 to avoid an initial values out of range error.
InitialGuesses = rep(Budget/(NumberOfPeriods*1.1), NumberOfPeriods)
# Optimisation
Optimal_Spending = constrOptim(InitialGuesses,
function(X) -ValueFunctionAtTime1(X),
NULL,
ui = Sum_Of_Annual_Spends,
ci = -Budget,
outer.iterations = 100,
outer.eps = 1e-10)$par
The result:
The output of the function is not monotonic.
plot( Time_Array , Optimal_Spending)
My attempts at fixing it
I have tried:
- Increasing the tolerance (This is above in the code with
outer.eps = 1e-10
) - Increasing the amount of iterations (This is above in the code with
outer.iterations = 100
) - Improving the quality of the initial parameter values. I did this with my actual case (the same but with a much more complicated utility function) but did not solve the problem.
- Scaling the problem through either increasing the Budget or multiplying the utility function by a scalar.
Other questions on constOptim
Other SO questions focus on difficulties in writing the constraints for constOptim such as:
I have not found anything examining tolerances or dissatisfaction with the output.