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

 From Paddy Carter To statalist@hsphsun2.harvard.edu Subject Re: st: Generating a bounded AR(1) series for Monte-Carlo Simulation Date Sat, 11 Jul 2009 14:53:49 +0100

```Austin,

Thanks a million - that replace, max, min construction is just what I need.

```
I confess I didn't see from the help file, and still can't, that if only executes {code} if the first observation in the data satisfies the condition, but I feel foolish because if I'd looked more closely at the data, I'd have seen `aid' was the original uniform draw and none of the commands had executed.
```
best,

```
--On 11 July 2009 09:38 -0400 Austin Nichols <austinnichols@gmail.com> wrote:
```
```
```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:
```
```Paddy Carter<Paddy.Carter@bristol.ac.uk> :

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

----------------------
Department of Economics
University of Bristol