Branching Instructions1 Conditional Statements There are two forms of the IF instruction %if(x,y,z)...

21
Branching Instructions 1 Conditional Statements • There are two forms of the IF instruction %if(x,y,z) – if condition x is true, then y, else z Example: • SET flag = %if(resids > 0, 1, 0) • SET dummy = %if(t.GE.1992:1,1,0)

Transcript of Branching Instructions1 Conditional Statements There are two forms of the IF instruction %if(x,y,z)...

Branching Instructions 1

Conditional Statements

• There are two forms of the IF instruction

%if(x,y,z)– if condition x is true, then y, else z– Example:

• SET flag = %if(resids > 0, 1, 0)

• SET dummy = %if(t.GE.1992:1,1,0)

Branching Instructions 2

If-Else• IF condition 1

next statement or block executed if condition is trueExamples1. If x = = 0

display ‘Zero encountered’2. If x = = 0 {

program statements}

end if (needed when IF is not within a compiled section)• ELSE IF condition n• ELSE• RATS performs the first IF or ELSE IF condition that is true. If all are

false, RATS will perform ELSE (if present).

Branching Instructions 3

Relational Operators

• RATS uses the standard relational operators– A == B A.EQ.B

– A <> B A.NE.B

– A > B A.GE.B

– A < B A.LT.B

– A >= B A.GE.B

– A <= B A.LE.B

– A.AND.B

– A.OR.B

Branching Instructions 4

If-Else Examples

Example 1IF COUNT>=5 DISPLAY 'COUNT IS GREATER THAN OR EQUAL TO 5'ELSE DISPLAY 'COUNT IS LESS THAN 5'END IF

Example 2Examples of both types of IF are in PHI_NO.PRG

Example 3: Bootknown.prg

Branching Instructions 5

Notes

Series vs. Constants• There is an important distinction between a series and a constant. A

constant is a single number (i.e., a scalar). A series is a ‘vector’ of numbers. – compute a = 5 – compute a = 5.0– set a = 5– set a = 5.0

• The first crates an integer a, the second creates the floating point number 5.0, the third creates a series with each element equal to the integer 5, the last creates a series with each element equal to the floating point number 5.0.

• You cannot switch a ‘character’ between a series and a constant.• You can use multiple assignments on a COM statement

– e.g., com a = 5, b = 3.2, c = d = f = 4.

• COM can perform all standard numerical operations

Branching Instructions 6

Dates as Integers

• If CAL is used, each date can be expressed as an integer or in RATS date form.

• It is not necessary to use CAL. You can begin a program with:– ALLOCATE 100

RATE ALT DATE1959:01 2.84 11959:02 2.71 21959:03 2.85 31959:04 2.96 41959:05 2.85 51959:06 3.25 61959:07 3.24 71959:08 3.36 81959:09 4.00 91959:10 4.12 101959:11 4.21 111959:12 4.57 12

Branching Instructions 7

DATES as Integers: Example

cal 1947 1 4all 2003:2open data c:\\gdp.xlsdata(format=xls,org=obs) /

There are 226 observations between 1947:1 and 2003:2. The following statements are equivalent to those above:

all 226open data c:\\gdp.xlsdata(format=xls,org=obs) /

To estimate a regression over the first 100 observations use:

LIN rcons 1 100# constant trend t2

Branching Instructions 8

Series as Integers

• Each series has its own sequence number. The number can be determined from the position of the series obtained from TABLE /

• Example from gdp.xls

table• Series Obs Mean Std Error Minimum Maximum• RGDP 214 4350.07009346 2128.96321099 1481.70000000

9311.50000000• RCONS 214 2856.75700935 1461.19505124 963.40000000

6258.20000000• RINV 214 616.25560748 383.72298968 153.90000000

1862.40000000

Here rcons is series 2

Branching Instructions 9

Series as Integers II

• Anywhere RATS expects a series name, you can use the sequence number.– The series number is an integer

– EXAMPLES using gdp.xls

• print / 1 3 [prints rgdp and rinv]

• print / 1 to 3 [prints rgdp, rcons and rinv]

• lin 2

# constant 2{1}

– To check the series number, you can also use:• com snum = rgdp ; dis snum

Branching Instructions 10

Series as Integers III

• Care must be taken if a series is on the right hand-side of a FRML, SET or COM instruction since RATS will interpret the integer as a scalar. On the right hand side use: [series]number– Example: You want to SET y equal to the log

of series 2– NOT: set y = log(2)– USE: set y = log([series]2)

Branching Instructions 11

Rats Programming Language

Loops1. Do i = 1,100,increment

program statementsend do i

Notes: The increment is optional BUT i must always take on integer values.

The increment can be negative Loops can be nested BUT each must have its own end

do. end do can be used instead of end do i

Example:DO REGEND=1994:1,1994:12 LINREG Y 1980:1 REGEND # CONSTANT X1 X2END DO

Branching Instructions 12

Loops

Loops1. Do i = 1,100,increment

program statementsend do i

Notes: The increment is optional BUT i must always take on integer values.

The increment can be negative Loops can be nested

Example:DO LAST = 1994:1,1994:12 LINREG Y 1980:1 LAST # CONSTANT X1 X2END DO

Branching Instructions 13

Loops II

Example 2do ar = 1,3

do ma = 1,3box(constant,ar=ar,ma=ma) yend do ma

end do ar

2. DOFOR index = list of values end dofor

The index can be almost any type of variableExampledofor j = rgdp rcons

lin j ; # constant j{1}end dofor

Branching Instructions 14

Loops III

3. WHILE condition {program statements}

end while

4. UNTIL condition {program statements}

end while

5. Branch label:labelThis is an unconditional

jump

6. LOOPbreak

end loopThis will cycle forever until break

is encountered

Branching Instructions 15

Loops and Date Manipulation

1. DO REGEND=1994:1,1994:12LINREG Y 1980:1 REGEND# CONSTANT X1 X2END DO

Is Equivalent to:

2. DO I =169,180LINREG Y 1980:1 I# CONSTANT X1 X2END DO I

Branching Instructions 16

Loops and Series Manipulation

Example 1:

do i = 1,5lin i# constant i{1} x

end do i

Example 2:Loops.prg: The program illustrates date and series manipulations using real exchange rate data.

Branching Instructions 17

Introduction to Monte Carlo Analysis

• The goal is to recreate a random outcome many times is order to assess the probability distribution of the outcome. – In RATS you can create random numbers using:

• %ran(x)

• %uniform(x1,x2)

• Example:

– SET x = %ran(1)

– You can obtain the fractiles using: STATISTICS(FRACTILES) or FRACT.SRC

• NOTE: The DENSITY Instruction will estimate an entire density function

– EXAMPLE: MONTE.PRG

Branching Instructions 18

Introduction to Bootstrapping

• In bootstrapping, we typically perform a type of Monte Carlo analysis but we use a particular data set to generate the random variables.– Examples:

• bootgdp.prg

Branching Instructions 19

Simulating a Single Equation

• SIMULATE 1 steps start # equation forecasts forstart

Supplementary Informationequation: the equation name or numberforecasts: series for simulated valuesnewstart: starting entry for storing values (default: start)

Example: lin(define=eq1) y ; # constant y{1}SIMULATE 1 100 70:1# eq1 foresSimulates a model with random Normally distributed shocks.

An error in RATS 5.0 See Simulations.prg (USE RATSW)

Branching Instructions 20

Simulating Multiple Equations

• You can create a model using SYSTEM or GROUP. and them use SIMULATE. One way to use SIM is:

SIMULATE(model=model,results=output) * steps start VCM

model = model to simulate

results = vector[series] for result seriesThis provides a VECTOR of SERIES which will be filled with the results. For instance, RESULTS=SIMULS will make SIMULS(i) the series of simulations for the ith equation in the model.

Branching Instructions 21

Example Using SIMULATE

system(model=var1) 1 to 3

vars x1 x2 x3

lags 1 to 4

det constant

end(system)

estimate(noprint,noftests,outsigma=v)

do i = 1,1000

SIMULATE(Model=var1,results=outpt) 3 24 %sigma

program statements involving outpt

end do i