After searching for R packages that could quickly generate demonstrative numerical examples for “global optima” in multi-dimensional optimization with constraints, I decided to write the code myself with the hope that it might be a quicker option.

This option also helps reduce the risk of using packages like * optimx* without getting precisely the answer I was looking for. To use the available packages requires more understanding and confidence than I currently have on this subject. Nevertheless, it is my quest to go through the major algorithms developed for optimization and introduce them to users at EconCodex.

Going back to developing of analytical tools in R to answer specific research questions, in my case, I found it much more rewarding and efficient than searching for the right tools in the R library, and it wasn’t something too bad to get started at all. Although developing a fully functional software could take much more computing power and time than I currently have. But fortunately I have achieved my purposes with the following algorithm:

### Objectives

- To solve for the optimal allocation of wealth given by in a three-dimensional utility maximization.
- Identify the existence of corner solutions under certain conditions.

### Optimization

The function was created to generate optimal combinations for the optimization of a objective function of 3 choice variables, include consumption noted by , saving, noted by , and health spending, noted by . The objective function was defined by which subject to a linear / nonlinear constraint imposed by wealth .

### Key Steps

- generate all possible combinations of the choice variables in a n by 3 matrix;
- plugging these variables into objective function ;
- return the combination which generates the highest value of .

### R Code

[code language=”r”]

# Write function matgen to generate a

# matrix of all possible combinations

# of variables within the constraint w.

matgen &lt;- function(w, n, v = NULL){

x = seq(0, w, w/n)

v = w – x

for (i in c(1:(n+1))){

y = seq (0, v[i], w/n)

z = v[i] – y

s = cbind(x[i],y,z)

m = rbind(m,s)

}

return(m)

}

##### Function f for optimal choices:

f = function(w, n, h){

m = as.matrix(matgen(w,n))

u = function(var1, var2, var3){

(var1)^(1/2) + ((var2)^(1/2) )* (var3+h)/(var3+h+1)

}

uu = NULL

for (i in 1:nrow(m)) {

um = data.frame()

uu = c(uu, u(m[i, 1], m[i, 2], m[i,3]))

if (i == nrow(m)) {

um = data.frame(‘utility’ = uu, ‘cons’ = m[, 1],

‘sav’ = m[, 2], ‘health’ = m[, 3])

}

}

solution = subset(um, utility == max(um$utility))

return(list(solution, head(um)))

}

[/code]

### Numerical examples:

[code language=”r”]

##### Examples of optimal choices from f(w, n, h)

## When wealth level is relatively high,

## we have an interior solution f(76.5, 63, 10.5)

f(150, 100, 0.1)

[[1]]

utility cons sav health

3919 15.99944 76.5 63 10.5

[[2]]

utility cons sav health

1 0.000000 0 0.0 150.0

2 1.216558 0 1.5 148.5

3 1.720356 0 3.0 147.0

4 2.106850 0 4.5 145.5

5 2.432608 0 6.0 144.0

6 2.719542 0 7.5 142.5

## When wealth is at intermediate level

## and when initial health h is high

## we have a corner solution in health spending

## where the optimal solution is f(27.5, 22.5, 0)

f(50, 100, 10)

[[1]]

utility cons sav health

4116 9.556241 27.5 22.5 0

[[2]]

utility cons sav health

1 0.0000000 0 0.0 50.0

2 0.6954191 0 0.5 49.5

3 0.9833333 0 1.0 49.0

4 1.2041609 0 1.5 48.5

5 1.3902438 0 2.0 48.0

6 1.5541108 0 2.5 47.5

## When wealth drop to 4 or less and

## when initial health h is poor,

## f(4, 0, 0) gives the optimal utility.

f(4, 100, 0)

[[1]]

utility cons sav health

5151 2 4 0 0

[[2]]

utility cons sav health

1 0.0000000 0 0.00 4.00

2 0.1596774 0 0.04 3.96

3 0.2253544 0 0.08 3.92

4 0.2754245 0 0.12 3.88

5 0.3173554 0 0.16 3.84

6 0.3540441 0 0.20 3.80

[/code]