 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: grid-searching an optimal value

 From Jorge Eduardo Pérez Pérez To "statalist@hsphsun2.harvard.edu" Subject Re: st: grid-searching an optimal value Date Thu, 3 May 2012 16:03:11 -0400

```Here is an example with simulated data. I was not able to get it to
work well with your example data, you only have at most 4 observations
per code. Is that really all the data you have?

-------------------
clear all
* Generate simulated data
set obs 1000
set seed 500
gen x1=rnormal()
gen x2=rnormal()
* I set a=10, b= 0.3 , which is in the range [0,0.9]
gen y=10*x1+0.3*x2+rnormal()
* Gen codes
egen code=cut(x1), group(4)
* Loop over the values of code
levelsof code, local(codes)
foreach x in `codes' {
quietly {
* Constraint for a
local a (1.05 + (131.5-1.05)*invlogit({b1}))
* Constraint for b
local b ((`a'-1)/(`a'))*(1-invlogit({b2}))
* Minimize sum of square residuals
nl (y = `a'*x1 + `b'*x2) if code==`x'
* Get back the values of a and b
local a1 1.05 + (131.5-1.05)*invlogit(_b[b1:_cons])
local b1 ((`a1'-1)/(`a1'))*(1-invlogit(_b[b2:_cons]))
}
* Display
di "Code = `x'"
nlcom (a1: `a1') (b1: `b1'), noheader
}
------------------------
_______________________
Jorge Eduardo Pérez Pérez

On Thu, May 3, 2012 at 4:55 AM, Maarten Buis <maartenlbuis@gmail.com> wrote:
> --- On Thu, May 3, 2012 at 7:16 AM, econqian222 wrote:
>>>>   I'm searching for an optimal value of "a" belong to the scale [1.05,
>>>> 131.5] at jumping interval that is 0.05. Correspondingly, “b" belong to the
>>>> scale of [0, (a-1)/a] at jumping  interval that is (a-1)/30a. Actually， this
>>>> is a kind of grid-searching optimal value.
>>>>   To minimize the sum of "u^2" by each value of code(1,2,3,4,...), where
>>>> u=y-ax1-bx2, we need to search the optimal value of "a" and "b" within their
>>>> respective scale and interval for each value of code(1,2,3,4,...).
>
> --- Jorge Eduardo Pérez Pérez answered:
>>> This sounds to me like a regression with interval constraints
>>>
>>> http://www.stata.com/support/faqs/stat/interval-constraints.html
>
> --- On Thu, May 3, 2012 at 7:16 AM, econqian222 wrote:
>>   Thanks for a kind answer, unfortunately, it can't help to my question, I
>> would like to hear more regarding to my question.
>
> Based on your description of your problem, Jorge is correct. If you
> want to know more you need to say why what he says does not work. You
> seem to  think you need a grid search, but based on what you say that
> is not true: -nl- should be perfectly fine for that optimization
> problem, though you probably need some good starting values.
>
> -- Maarten
>
> --------------------------
> Maarten L. Buis
> Institut fuer Soziologie
> Universitaet Tuebingen
> Wilhelmstrasse 36
> 72074 Tuebingen
> Germany
>
>
> http://www.maartenbuis.nl
> --------------------------
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```