Reading several data arrays and performing Monte Carlo Study

For questions and discussion related to reading in and working with data.
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Reading several data arrays and performing Monte Carlo Study

Unread post by pedrocm »

Hi,
I need to perform a Monte Carlo study where I will use different data arrays generated in another software to estimate MS-VECM with two endogenous variables (say price A and price B) and two or three regimes.
I have one array with 500 series of price A (A1:A500), one array with 500 series of price B (B1:B500), and one vector (column (1:500)) with lag length for each price pair (A1,B1...A500,B500).
I am an R user and not really familiar yet with Rats syntax and objects, although I have already checked the manuals.

What I need to do is to import the three data sets, and do a loop to estimate the models and store the adjustment coefficients (from error correction term) of each regime and unconditional regime probabilities (p(s=1), p(s=2), p(s=3)).

For estimation purposes I have adapted the code: Replication file for Francis and Owyang(2005), "Monetary Policy in a Markov-Switching Vector Error-Correction Model: Implications for the Cost of Disinflation and the Price Puzzle", JBES, vol 23, no 3, 305-313.

My doubts are specifically: What is the best way to store the imported series to be called in the loop (I didn't name the series in the files)?
What is the best way to declare a variable or file to store the coefficients and probabilities, given that I have to keep track of origin of observations (say from 1 to 500) and further calculate parameters as mean, variance and quantiles?

Thanks in advance for your support.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by TomDoan »

Sorry. You have three separate files? Two with 500 columns and one with one? From your description, it sounds like this would get the data in:

dec vect[series] a(500) b(500)
open data myaseries.txt
data(format=csv,org=columns,nolabels) / a
open data mybseries.txt
data(format=csv,org=columns,nolabels) / b
*
dec vect[int] lags(500)
open data mylags.txt
read(format=free) lags

What out of F&O are you using? They are using estimated cointegrating vectors. Are you doing that, or are you using theoretical values (I assume a-b)?
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by pedrocm »

Thanks Tom.
Yes, I have 3 different files, two with 500 columns and one with one column and 500 rows, as you pointed.

I am assuming known co integrating vector, (a1-b1), as you also pointed.

I am also "forcing" regime 1 to have lower adjustment coef. for the case of two regimes. For the case of three regimes, I would assume regime 2 has lower adj.coef than regimes 1 and 3 (as my generated data assumes that) .
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by TomDoan »

Do the ML/EM estimation, not the Bayesian---you don't want to add MCMC error to what you're doing.
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by pedrocm »

Hi Tom.
Thanks for your reply.
Yes, I am doing the EM/ML, not the Bayesian.
So, can you inform me how I can store the coefficients and the unconditional probabilities?
I am not sure what is the best way of doing that.
Thanks again for your support.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by TomDoan »

best thing probably would be a separate VECTOR of dimension 500 for each. Inside your loop, compute them into say

compute loadj(i)=myloadj
compute midadj(i)=mymidadj
compute hiadj(i)=myhiadj

for the three adjustments (whatever you want to call those) and similarly for the other statistics.
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by pedrocm »

Thank you Tom,
I am trying to import the data as you suggested:

dec vect[series] a(500) b(500)
open data "C:\Users\pedrocm\Documents\End_dif_eta\Z2_A.csv"
data(format=csv,org=columns,nolabels) / a
open data "C:\Users\pedrocm\Documents\End_dif_eta\Z2_B.csv"
data(format=csv,org=columns,nolabels) / b
*

but I got the following error:
## SX22. Expected Type SERIES[REAL], Got VECTOR[SERIES[REAL]] Instead
>>>>umns,nolabels) / a<<<<

The vector of lags is correctly read.
My series are not dated. It is a data array of 520x500, where each column is a different series.

Thanks
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by TomDoan »

It looks like you have an older version of RATS. The ability to read a VECT[SERIES] was added with 9.2. You can accomplish the same thing with

data(format=csv,org=columns,nolabels) / a(1) to a(500)
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by pedrocm »

Thanks Tom.

It looks like it doesn't work either.
The following error appears:
## SX11. Identifier A is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>mns,nolabels) / a(<<<<
Is it possible you meant
ABS
TAN
%NA
If the name isn't mistyped, it's possible that you have a poorly formatted instruction
Common errors are
* a space before the ( in an option field
* a missing space before = in a SET or FRML
* a missing $ at the end of a long line which continues to the next

The easiest way I found to input the data was labeling the series and using the wizard, although not practical for several files.
Regarding calling the loop and saving the coefficients and probabilities within the @mssysregression procedure, can you inform me what is the syntax to do that?
Maybe if you could refer to an existing code, it may be easier for me to figure it out.
Thanks in advance.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by TomDoan »

You still need those DECLARE VECT[SERIES] instructions. All that changes is the syntax of the DATA instruction.
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by pedrocm »

Thanks Tom.
I didn't realize that.
It works with the declare vect[series] instruction. But another error appears:

## IO43. Format csv is illegal or not supported

using format=PRN in:

declare Vect[series] a(500) b(500)
open data "C:\Users\pedrocm\Documents\End_dif_eta\Z2_A.csv"
data(format=prn,org=columns,nolabels) / a(1) to a(500)

solves the problem, but for some reason, the last observation is missing from all series. any idea why?

Please, let me know about the syntax to retrieve the coefficients and probabilities:

I'll need to save, among other coefficients, for example: betasys(2)(4,1) and the unconditional probability of regimes 1, 2 and 3.

Thanks again
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by TomDoan »

pedrocm wrote:Thanks Tom.
I didn't realize that.
It works with the declare vect[series] instruction. But another error appears:

## IO43. Format csv is illegal or not supported

using format=PRN in:

declare Vect[series] a(500) b(500)
open data "C:\Users\pedrocm\Documents\End_dif_eta\Z2_A.csv"
data(format=prn,org=columns,nolabels) / a(1) to a(500)

solves the problem, but for some reason, the last observation is missing from all series. any idea why?
If you give it a hard range (if it's 1000 observations in each:

data(format=prn,org=columns,nolabels) 1 1000 a(1) to a(500)

it should be fine. It's the autodetect of the range with the NOLABELS that isn't working correctly.
pedrocm wrote: Please, let me know about the syntax to retrieve the coefficients and probabilities:

I'll need to save, among other coefficients, for example: betasys(2)(4,1) and the unconditional probability of regimes 1, 2 and 3.
That falls under the "Bookkeeping" in the User's Guide:

https://estima.com/docs/RATS%2010%20Use ... f#page=518

The unconditional probabilities are most easily computed using the %MCERGODIC function.
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by pedrocm »

Thanks Tom.
I can properly load the data, run the model and store the parameters of interest when I set a fixed number for the i-th series (eg. computing i=1).

However, when I try to loop over the series, I have the following errors:

## CP17. PROCEDURE/FUNCTION Must be Initial Statement in a Compiled Section
>>>>function <<<<
## CP18. MSSYSREGRESSION is not the Name of a PROCEDURE. (Did you forget to SOURCE?)
>>>>function <<<<

There must be a different way to declare the procedure for a loop I guess.
Could you please, help me on fixing this error?
Thanks in Advance

my code is:

do i=1,4

compute gstart=%regstart(),gend=%regend()

* Generate the error correction series
set ect1 = A(i) - B(i)
*
set dA(i) = A(i) - A(i){1}
set dB(i) = B(i) - B(i){1}
*
@MSSysRegression(states=2,switch=ch)
# dA(i) dB(i)
# constant dA(i){lags(i)} dB(i){lags(i)} ect1{1}
*
gset pt_t = %zeros(nstates,1)
gset pt_t1 = %zeros(nstates,1)
gset psmooth = %zeros(nstates,1)
*
* Do 50 EM iterations
*
@MSSysRegInitial gstart gend
compute betasys(1)(4,2)=+0.5
compute betasys(1)(4,1)=-0.5
*compute betasys(2)(4,2)=+0.1
*compute betasys(2)(4,1)=-0.1
*
@MSSysRegEMGeneralSetup
do emits=1,50
@MSSysRegEMStep gstart gend
disp "Iteration" emits "Log likelihood" %logl
end do emits
* store parameters of interest
compute uncond=%mcergodic(p)
compute adj1(i)= betasys(1)(4,2)-betasys(1)(4,1)
compute adj2(i)= betasys(2)(4,2)-betasys(2)(4,1)
compute prob1(i)= uncond(1)
compute prob2(i)= uncond(2)
end do i
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by TomDoan »

That will work fine as you've written it with version 10, but not with version 9. With v9, you have to do a source instruction for the mssysregression.src file before you can use the procedure in a loop.
pedrocm
Posts: 11
Joined: Tue May 14, 2019 1:23 pm

Re: Reading several data arrays and performing Monte Carlo S

Unread post by pedrocm »

Hi Tom,
Thanks for your answer.
Indeed, I am using version 9. So that's why I am seeing this error.
Could you help me with this source instruction? I didn't find anything on the manuals about it.
Or could you refer me to some existing code, so I can figure it out as well.
Thanks
Post Reply