# Re: [R] optimization problem in R ... can this be done?

From: Uwe Ligges <ligges_at_statistik.uni-dortmund.de>
Date: Sun 26 Jun 2005 - 19:59:18 EST

Gregory Gentlemen wrote:

> Spencer: Thank you for the helpful suggestions. I have another
> question following some code I wrote. The function below gives a
> crude approximation for the x of interest (that value of x such that
> g(x,n) is less than 0 for all n).
>
> # // btilda optimize g(n,x) for some fixed x, and then approximately
> finds that g(n,x) # such that abs(g(n*,x)=0 // btilda <-
> function(range,len) { # range: over which to look for x bb <-
> seq(range,range,length=len) OBJ <- sapply(bb,function(x) {fixed
> <- c(x,1000000,692,500000,1600,v1,217227);
> return(optimize(g,c(1,1000),maximum=TRUE,tol=0.0000001,x=fixed)\$objective)})
> tt <- data.frame(b=bb,obj=OBJ) tt\$absobj <- abs(tt\$obj) d <-
> tt[order(tt\$absobj),][1:3,] return(as.vector(d)) }
>
> For instance a run of
>

```>> btilda(c(20.55806,20.55816),10000) .... returns:

>
> b                  obj                    absobj 5834   20.55812

> -0.0004942848    0.0004942848 5833   20.55812     0.0011715433
```
> 0.0011715433 5835 20.55812 -0.0021601140 0.0021601140
>
> My question is how to improve the precision of b (which is x) here.
> It seems that seq(20.55806,20.55816,length=10000 ) is only precise to
> 5 or so digits, and thus, is equivalent for numerous succesive

Why do you think so? It is much more accurate! See ?.Machine

Uwe Ligges

> values. How can I get around this?
>
>
> Spencer Graves <spencer.graves@pdf.com> wrote: Part of the R culture
> is a statement by Simon Blomberg immortalized in library(fortunes)
> as, "This is R. There is no if. Only how."
>
> I can't see now how I would automate a complete solution to your
> problem in general. However, given a specific g(x, n), I would start
> by writing a function to use "expand.grid" and "contour" to make a
> contour plot of g(x, n) over specified ranges for x = seq(0, x.max,
> length=npts) and n = seq(0, n.max, npts) for a specified number of
> points npts. Then I'd play with x.max, n.max, and npts until I got
> what I wanted. With the right choices for x.max, n.max, and npts, the
> solution will be obvious from the plot. In some cases, nothing more
> will be required.
>
> If I wanted more than that, I would need to exploit further some
> specifics of the problem. For that, permit me to restate some of what
> I think I understood of your specific problem:
>
> (1) For fixed n, g(x, n) is monotonically decreasing in x>0.
>
> (2) For fixed x, g(x, n) has only two local maxima, one at n=0 (or
> n=eps>0, esp arbitrarily small) and the other at n2(x), say, with a
> local minimum in between at n1(x), say.
>
> With this, I would write functions to find n1(x) and n2(x) given x. I
> might not even need n1(x) if I could figure out how to obtain n2(x)
> without it. Then I'd make a plot with two lines (using "plot" and
> "lines") of g(x, 0) and g(x, n2(x)) vs. x.
>
> By the time I'd done all that, if I still needed more, I'd probably
> have ideas about what else to do.
>
> hope this helps. spencer graves
>
>
> Gregory Gentlemen wrote:
>
>

```>> Im trying to ascertain whether or not the facilities of R are
>> sufficient for solving an optimization problem I've come accross.
>> Because of my limited experience with R, I would greatly appreciate
>> some feedback from more frequent users. The problem can be
>> delineated as such:
>>
>> A utility function, we shall call g is a function of x, n ...
>> g(x,n). g has the properties: n > 0, x lies on the real line. g may
>> take values along the real line. g is such that g(x,n)=g(-x,n). g
>> is a decreasing function of x for any n; for fixed x, g(x,n) is
>> smooth and intially decreases upon reaching an inflection point,
>> thereafter increasing until it reaches a maxima and then declinces
>> (neither concave nor convex).
>>
>> My optimization problem is to find the largest positive x such that
>> g(x,n) is less than zero for all n. In fact, because of the
>> symmetry of g around x, we need only consider x > 0. Such an x does
>> exists in this problem, and of course g obtains a maximum value of
>> 0 at some n for this value of x. my issue is writing some code to
>> systematically obtain this value.
>>
>> Is R capable of handling such a problem? (i.e. through some sort of
>> optimization fucntion, or some sort of grid search with the
>> relevant constraints)
>>
>> Any suggestions would be appreciated.
>>
>> Gregory Gentlemen gregory_gentlemen@yahoo.ca
>>
>>
>>
>> The following is a sketch of an optimization problem I need to
>> solve.
>>
>> __________________________________________________
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help@stat.math.ethz.ch mailing list