j1897
j1897

Reputation: 1557

How to find the best parameters which maximizes a function?

I have a function which takes 8 parameters, 5 (a,b,c,d,e) of which are constants while the other three (u1, u2, u3) are to be found out.

a <- 10.1
b <- 2.45
c <- 0.35
d <- 2
e <- 3.5
out <- function(a,b,c,d,e,u1,u2,u3){
temp <- 2*a*b^2 + (u1*u2/d) - c*e^2 + u3*e
return(temp)}

I need to find the values of u1, u2 and u3 from pre-defined distributions which maximizes the value of temp The distributions are:

u1 <- rnorm(100,2,1)
u2 <- rnorm(100, 1, 1.96)
u3 <- rnorm(100, 1, 2.48)

Upvotes: 0

Views: 61

Answers (1)

cgrafe
cgrafe

Reputation: 443

I'm sure there's a good way in R to find the exact values of u1, u2, and u3 that would maximize your temp equation, but for the case you've set up above (based on random samples from the three distributions), you could do the following:

a <- 10.1
b <- 2.45
c <- 0.35
d <- 2
e <- 3.5
out <- function(a,b,c,d,e,u1,u2,u3){
temp <- 2*a*b^2 + (u1*u2/d) - c*e^2 + u3*e
return(temp)}

u1 <- rnorm(100,2,1)
u2 <- rnorm(100, 1, 1.96)
u3 <- rnorm(100, 1, 2.48)

res <- out(a,b,c,d,e,u1,u2,u3)
i <- which(res == max(res), arr.ind=TRUE)

This just evaluates temp for each of the 100 random values for u1, u2, and u3, and finds which draws give the max value of temp:

> u1[i]
[1] 1.361594
> u2[i]
[1] 2.868618
> u3[i]
[1] 5.958975

For greater accuracy, you could increase the sample size to 1,000, 10,000, etc.

Upvotes: 1

Related Questions