[R] optimization problem in R ... can this be done?
Spencer Graves
spencer.graves at pdf.com
Sun Jun 26 20:11:52 CEST 2005
The precision is not a problem, only the display, as Uwe indicated.
Consider the following:
> (seq(25.5,25.6,length=200000)-25.5)[c(1, 2, 199999, 200000)]
[1] 0.000000e+00 5.000025e-07 9.999950e-02 1.000000e-01
> ?options
> options(digits=20)
> seq(25.5,25.6,length=200000)[c(1, 2, 199999, 200000)]
[1] 25.5000000000000 25.5000005000025 25.5999994999975 25.6000000000000
>
spencer graves
Gregory Gentlemen wrote:
> Okay let me attempt to be clear:
> if i construct the following sequence in R:
>
> seq(25.5,25.6,length=200000)
>
> For instance, the last 10 elements of the sequence are all 26, and the
> preceding 20 are all 25.59999. Presumably some rounding up is being
> done. How do I adjust the precision here such that each element is distinct?
>
> Thanks in advance guys,
> Gregory
> gregory_gentlemen at yahoo.ca <mailto:gregory_gentlemen at yahoo.ca>
>
> Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
>
> 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[1],range[2],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 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 betwee! n 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 at yahoo.ca
> >>
> >>
> >>
> >> The following is a sketch of an optimization problem I need to
> >> solve.
> >>
> >> __________________________________________________
> >>
> >>
> >>
> >> [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the
> >> posting guide! http://www.R-project.org/posting-guide.html
> >
> >
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA
spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel: 408-938-4420
Fax: 408-280-7915
More information about the R-help
mailing list