# How to use a DO loop to generate a sample of an AR series

Hi all,

I tried to do some programming in Shazam, but I unfortunately I am stranded. I have the following problem that I’m struggling with: My objective is to generate a selected number of AR(1)-series by using the do-loop command on the following script (which only generates a single series):

SAMPLE 1 5
GENR YEAR = TIME(0)
GENR X2 = NOR(3)
GENR y = 1
GENR y = 1 + 0.2*LAG(Y) + X2
PRINT y year


Further – the problem is to store the n-AR(1) series in a n-column matrix and then be able to graph the series in a single figure and as well (optionally) separately (with the generated YEAR-variable on the x-axis). Great if I could get some help with the right SHAZAM -script which could sort this out :-)

Kind regards, TL

edit retag close merge delete

Sort by » oldest newest most voted

Using your script it is possible to store the results in a matrix and graph the output as follows:

* Set some useful constants
gen1 series = 10
gen1 length = 5
gen1 p = 0.2

* Dimension a matrix to store results
dim A length series

* Generate a number of series
do(# = 1, series)

* Sample length for each series
sample 1 length

* Generate an AR(p) series (with drift)
genr YEAR = TIME(0)
genr X2 = NOR(3)
genr Y = 1
genr Y = 1 + p*LAG(Y) + X2

* Store Y as a matrix column and make a line graph
* of the individual series
matrix A(0,#)=Y
graph Y YEAR / line

endo

* Line graph of all series
graph A YEAR / line nokey


The output produces individual graphs per series and the following graph of all series:

more

Hi experts at Shazam,

Your solution works perfectly. Thanks a lot for your help and good service.

T.L.

( 2016-03-14 07:24:18 +0000 )edit

Hi Developers of Shazam,

I have a following up question with regard to the programming problem you newly solved. The generated time series (or scenarios) are stored in the matrix A – column by column, and each row represents a time step. Question: Is it possible to (a) calculate the average value of the selected number of generated time series (scenarios) for each time step i.e. the average is calculated for each row in matrix A, and (b) then make it possible to graph the average?

Great if you can derive the Shazam-script.

TL

( 2016-03-31 08:20:31 +0000 )edit

The answer to the question asked in the comment above regarding generating row averages can be done simply using the STAT command after computing A. First transpose the A matrix so that we can use the STAT command, which treats columns as seperate series when used with a matrix:

matrix AT = A'


Compute the means and store them in a variable called rowMeans:

stat AT / means=rowMeans


Graph as before using:

graph rowMeans / nokey


Also it can be done the mechanical way using GENR to convert a vector to a data series as follows but note that process of converting to a data series is also not really required. The STAT command will work on a vector just the same as with a matrix. This is shown mainly to highlight some useful techniques:

* Dimension the results vector
dim Averages length

* Loop through the generated time steps
do(# = 1, length)
* Convert row of matrix to a vector
matrix vector=vec(A(#,0))
* Convert the vector to a data series
genr dataSeries=vector
* Compute the mean for the data series (row)
stat dataSeries / means = rowMean
* Store mean to vector at position #
matrix Averages(#) = rowMean
endo


And check the results are the same as follows:

* Check both are the same
print rowMeans Averages

more

Again - thanks a lot for your help :-)

TL

( 2016-04-01 11:13:44 +0000 )edit