# Re: st: Generating a bounded AR(1) series for Monte-Carlo Simulation

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject Re: st: Generating a bounded AR(1) series for Monte-Carlo Simulation Date Sat, 11 Jul 2009 09:38:20 -0400

```Maybe you want something like:

clear
set seed 1
range year 1 100 100
expand 9
bys year: g i=_n
g aid=uniform()/3 if year==1
tsset i year
loc r=0.5
loc c=0.1
g s=rnormal()/10
replace aid=min(max(`c'+`r'*L.aid+s,0),.3) if year>1
xtline aid

On Sat, Jul 11, 2009 at 9:32 AM, Austin Nichols<austinnichols@gmail.com> wrote:
>
> Read -help ifcmd- and note that
>
> if year>1 {
>  some code
> }
>
> executes "some code" only if the first observation on year in the data
> is greater than 1.  Similar problems apply to the if commands you have
> put inside the main set of braces.
>
> On Sat, Jul 11, 2009 at 9:02 AM, Paddy Carter<Paddy.Carter@bristol.ac.uk> wrote:
>> Dear Statalisters
>>
>> I have a puzzle, concerning a section of a program intended to generate an
>> AR(1) series for `aid' that is bounded between 0 and 0.3
>>
>> Prior to this section of the program, I have already generated countries,
>> time (and xtset the data) and a variable `aid' consisting of random draws
>> from a uniform distribution on [0,0.3]  - all but the first observation of
>> which for each country, I wish to replace, using the following code:
>>
>> if year>1 {
>>        if  L.aid<0  {
>>           replace aid=const+`rho'*L.aid+rnormal(0.1,0.01)
>>                   }
>>        else if L.aid>0.3 {
>>           replace aid=const+`rho'*L.aid+rnormal(-0.1,0.01)
>>                   }
>>        else {
>>            replace aid=const+`rho'*L.aid+(rnormal()/100)
>>                   }
>>
>>           }
>>
>> I expect this code to do the following: whenever aid wanders beyond the
>> bounds 0 and 0.3, the shock in the next period is set to bounce it away from
>> these bounds. Hence I would expect to see observations of aid that do stray
>> beyond 0 and 0.3 but which bounce back in the subsequent period. My puzzle
>> is that when I run this code with large T, the aid series never strays
>> beyond bounds. By my understanding, that shouldn't be happening.
>>
>> I would actually like to write a program such that aid never strays beyond
>> bounds, but I didn't know how to; I appear to have done so inadvertently,
>> but the fact I don't understand what it's doing makes me fear I've gone
>> totally wrong.
>>
>> I apologise if I'm missing something obvious; this is my first ever attempt
>> at programming.
>>
>> This is on Stata/IC 10.1 for Windows
>>
>>
>

*
*   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/
```