SHAZAM Questions and Answers (Q&A) - RSS feedhttp://community.econometrics.com/questions/SHAZAM Econometrics, Statistics and Analytics Communityen<font color="white">Copyright <b>SHAZAM Analytics, 2018</b></font>Fri, 01 Apr 2016 11:13:44 +0000How to use a DO loop to generate a sample of an AR serieshttp://community.econometrics.com/question/707/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
Fri, 11 Mar 2016 15:54:55 +0000http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/Answer by SHAZAMHelp for <p>Hi all,</p>
<p>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):</p>
<pre><code>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
</code></pre>
<p>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 :-)</p>
<p>Kind regards,
TL</p>
http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?answer=708#post-id-708Using 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:
![image description](/upfiles/14577940708468169.png)
Sat, 12 Mar 2016 14:47:56 +0000http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?answer=708#post-id-708Comment by TL for <p>Using your script it is possible to store the results in a matrix and graph the output as follows:</p>
<pre><code>* 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
</code></pre>
<p>The output produces individual graphs per series and the following graph of all series:
<img alt="image description" src="/upfiles/14577940708468169.png"/></p>
http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?comment=709#post-id-709Hi experts at Shazam,
Your solution works perfectly. Thanks a lot for your help and good service.
T.L.Mon, 14 Mar 2016 07:24:18 +0000http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?comment=709#post-id-709Comment by TL for <p>Using your script it is possible to store the results in a matrix and graph the output as follows:</p>
<pre><code>* 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
</code></pre>
<p>The output produces individual graphs per series and the following graph of all series:
<img alt="image description" src="/upfiles/14577940708468169.png"/></p>
http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?comment=710#post-id-710Hi 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.
TLThu, 31 Mar 2016 08:20:31 +0000http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?comment=710#post-id-710Answer by SHAZAMHelp for <p>Hi all,</p>
<p>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):</p>
<pre><code>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
</code></pre>
<p>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 :-)</p>
<p>Kind regards,
TL</p>
http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?answer=711#post-id-711The 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 Fri, 01 Apr 2016 10:39:37 +0000http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?answer=711#post-id-711Comment by TL for <p>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: </p>
<pre><code>matrix AT = A'
</code></pre>
<p>Compute the means and store them in a variable called rowMeans:</p>
<pre><code>stat AT / means=rowMeans
</code></pre>
<p>Graph as before using:</p>
<pre><code>graph rowMeans / nokey
</code></pre>
<p>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:</p>
<pre><code>* 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
</code></pre>
<p>And check the results are the same as follows:</p>
<pre><code>* Check both are the same
print rowMeans Averages
</code></pre>
http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?comment=712#post-id-712Your solution function perfectly.
Again - thanks a lot for your help :-)
TLFri, 01 Apr 2016 11:13:44 +0000http://community.econometrics.com/question/707/how-to-use-a-do-loop-to-generate-a-sample-of-an-ar-series/?comment=712#post-id-712