RATS 11
RATS 11

In addition to the standard bookkeeping routines, most simulation and bootstrapping programs will make use of one or more of the specialized instructions and functions described below. These are used to generate the random draws and simulations that will be used to compute the moment statistics, probabilities, etc.

 

SIMULATE, FORECAST and UFORECAST

The instruction SIMULATE forecasts an equation or system of equations, automatically drawing shocks from a Normal distribution. It can handle linear equations or non-linear formulas. The syntax for SIMULATE is very similar to FORECAST, except there are extra options for inputting the covariance matrix for the Normal draws (CV=covariance matrix or FACTOR=factor of covariance matrix).

 

You can do random Normal simulations for a single equation using UFORECAST with the SIMULATE option. The variance for the draws for UFORECAST is the one saved with the equation or regression being forecast.

 

You can also generate data using a FORECAST instruction, by including an INPUT, SHOCKS, PATHS, or MATRIX option. While SIMULATE can only draw from a single multivariate Normal distribution, FORECAST with the MATRIX or PATHS options will let you feed any set of shocks into your forecasts.

 

Virtually every simulation or bootstrapping project requires a loop of some kind (usually a DO loop) and various bookkeeping operations. Most of the bookkeeping is handled using COMPUTE, EWISE, SET and GSET.

 

The following is a stylized example of the use of SIMULATE. This has a five equation model, with three structural equations (formulas X1EQ, X2EQ and X3EQ) and two identities (X4ID and X5ID).

 

vcv(matrix=v)

# resids1 resids2 resids3

group(vcv=v) model5 x1eq>>fx1 x2eq>>fx2 x3eq>>fx3 $

   x4id>>fx4 x5id>>fx5

smpl 2011:1 2011:12

do draw=1,ndraws

  simulate(model=model5)

  ... bookkeeping ...

end do draw

smpl

Built-In Functions for Generating Random Draws

RATS provides the following functions drawing from univariate real distributions:

 

%ran(x)

Returns a draw from a Normal(0,\(x^2\))

%uniform(x1,x2)

Returns a draw from a Uniform(x1,x2)

%ranbeta(a,b)

Random beta with shape parameters a and b

%ranchisqr(df)

Random chi-squared with df degrees of freedom

%rangamma(r)

Random gammas with shape parameter r

%rant(df)

Random Student-t with df degrees of freedom

%ranlognormal(mu,sigma)

Random log normal with given mean and standard deviation

%rantruncate(m,s,l,u)

Random Normal(m,\(s^2\)) truncated to the interval l to u

                    

You can use SET to fill elements of a series using random draws. For example:

 

set u = %ran(sqrt(%seesq))

 

creates U as a series of random Normals with variance %SEESQ.

 

You can use COMPUTE for scalars and arrays. For arrays, you generally need to declare and dimension the array before using COMPUTE. However, there is a special function %RANMAT for the most common operation of this type—it both dimensions a matrix and fills it with standard Normals.

 

compute e = %ran(1.0)

declare symmetric s(4,4)

compute s = %rangamma(nu)

compute r = %ranmat(5,5)

 

E will be a single N(0,1) draw, S will be a \(4 \times 4\) SYMMETRIC filled with random gammas, and R will be a \(5 \times 5\) RECTANGULAR filled with N(0,1) draws.

 

Inverse Method

If a random variable has an invertible cumulative distribution function, you can draw variates easily by just applying the inverse to a uniform (0,1) draw. For instance, the logistic has distribution function

\begin{equation} {1 \mathord{\left/ {\vphantom {1 {\left( {1 + \exp \left( { - x} \right)} \right)}}} \right. } {\left( {1 + \exp \left( { - x} \right)} \right)}} \end{equation}

If \(y\) is a \(U(0,1)\) draw, then solve

\begin{equation} y = 1/(1 + \exp ( - x)) \Rightarrow \left( {1/y} \right) - 1 = \exp ( - x) \Rightarrow x = \log \left( {y/(1 - y)} \right) \end{equation}

Since the distribution is symmetric, you can also get logistic draws with the slightly simpler \(x = \log \left( {\left( {{1\mathord{\left/{\vphantom {1 y}} \right.} y}} \right) - 1} \right)\).  

 

The following code generates a vector of 100 draws from a standard logistic:

 

dec vect draws(100)

ewise draws(i) = log(1/%uniform(0,1)-1)

 

Do not do LOG(%UNIFORM(0,1)/(1-%UNIFORM(0,1)). Each use of %UNIFORM will give a different value, when we want the same one in both places. The original inverse formula (with the y’s occurring in two places) can be done either in two steps:

 

ewise draws(i) = %uniform(0,1)

ewise draws(i) = log(draws(i)/(1-draws(i)))

 

or in one by using a comma to separate the two parts of the calculation:

 

ewise draws(i) = y=%uniform(0,1), log(y/(1-y))

 

Direct Method

Many random variables can be obtained by some function of Normal, uniform, chi-squared, gamma and beta random variables. For instance, one way to get draws from a Cauchy is to take the ratio of two standard Normals:

 

ewise draws(i) = %ran(1.0)/%ran(1.0)

 

though a more efficient way to do this is to use the fact that a Cauchy is a t with 1 degree of freedom and use %rant(1).

 

Multivariate Normal

To draw a multivariate Normal with mean vector \(\bf{X}\) and covariance matrix \(\Sigma\), it is necessary first to get a factor of the \(\Sigma\) matrix: a matrix \(\bf{F}\) such that \({\bf{FF'}} = \Sigma \). Any such factor will do; the simplest one to obtain with RATS is the Cholesky factor computed using the function %CHOL or %DECOMP. The (mean zero) multivariate Normal can then be drawn using the function %RANMVNORMAL(F)

 

The following draws a single vector (XDRAW) from this distribution. If, as is typical, the actual draws would be inside a loop, the first two lines can be outside it:

 

dec rect f

compute f = %chol(sigma)

compute xdraw = x+%ranmvnormal(f)

 

Convenience Functions for Multivariate Normals

There are some specialized functions for drawing multivariate Normals whose means and covariance matrices are derived from standard calculations. The most important of these is %RANMVPOST(H1,B1,H2,B2), which takes a pair of mean vectors (B) and precision matrices (H) and draws from the multivariate Normal posterior derived by combining them. By using this, you can avoid calculating the posterior mean and variance yourself.

 

Multivariate Student-t

To get a draw from a multivariate Student-t, use the function %RANMVT(F,degrees). As with %RANMVNORMAL, the F gives a factor of the covariance matrix \(\Sigma\), though here this is the covariance matrix of the underlying Normal. A draw from that multivariate Normal is divided through by the square root of a single draw from a chi-squared distribution with degrees degrees of freedom to give a draw for a multivariate t.

 

For example:

 

compute betau = xbase + %ranmvt(fxx,nuxx)

 

will draw a multivariate t with mean XBASE, degrees of freedom NUXX, and factor matrix FXX.

 

Wishart Distribution

A Wishart distribution is a multivariate generalization of a gamma and is used in modeling a covariance matrix. It specifies a density function for symmetric \(N \times N\) matrices. The %RANWISHART(N,x) function in RATS takes two parameters: the first is the dimension of the output matrix (N) and the second is the degrees of freedom (x). It produces an \(N \times N\) matrix whose expected value is \(x\) times an \(N \times N\) identity matrix. More commonly, however, the “scale matrix” isn’t the identity, but a covariance matrix estimated from the sample. As is the case with the multivariate Normal, you start with a factor of this scale matrix. The two functions available for more general Wishart draws are %RANWISHARTF(F,x) and %RANWISHARTI(F,x). The first draws a Wishart, the second an inverse Wishart. The latter is more commonly used, as it arises in standard Bayesian methods applied to multivariate regressions. The kernel of the density function from which %RANWISHARTI draws \(\Sigma\) is

\begin{equation} \left| \sum \right|{\kern 1pt} {\kern 1pt} ^{{{\left( { - x - k - 1} \right)} \mathord{\left/ {\vphantom {{\left( { - x - k - 1} \right)} 2}} \right. } 2}} \exp \left( { - \frac{1}{2}tr\left( {\left( {{\bf{FF'}}} \right)^{ - 1} \Sigma ^{ - 1} } \right)} \right) \end{equation}

where \(k\) is the dimension of \(\Sigma\). For instance, if the posterior for \(\Sigma\) is

\begin{equation} \left| \Sigma \right|^{ - \left( {T - p - k - 1} \right)/2} \exp \left( { - \frac{1}{2}tr\left( {\left( {T{\kern 1pt} {\kern 1pt} \hat \Sigma } \right)\Sigma ^{ - 1} } \right)} \right) \end{equation}

then, by inspection, \(x = T - p\) and \({\bf{FF'}} = \left( {T{\kern 1pt} {\kern 1pt} \hat \Sigma } \right)^{ - 1} \)

 

If \(T\) is the number of observations from the most recent regression, and \(\hat \Sigma \) is %SIGMA, the following will draw a matrix W from the posterior:

 

compute f = %decomp(inv(%nobs*%sigma))

compute w = %ranwisharti(f,%nobs-p)

 

Random Directions (%RANSPHERE)

The function %RANSPHERE(N) draws a random “direction” in \(N\) space. It draws uniformly from the unit sphere in \(N\) dimensions, that is, from \(\left\{ {x \in R^N :\left\| x \right\| = 1} \right\}\).

 

For example, the following draws a random point on the sphere in NVAR dimensions.

 

compute v1 = %ransphere(nvar) 

 

Dirichlet Distribution

The Dirichlet distribution is a generalization of the beta to more than two choices; it’s used to model probabilities when there are three or more alternatives. %RANDIRICHLET(shapes) is used to draw from this. SHAPES is an N-vector of positive numbers, and this draws an N-vector whose elements sum to one. The mean of each component is the ratio of its shape to the sum of all the shape parameters.

 

%RANLOGKERNEL()

In some cases, it’s necessary to know the density function at the random draw. If you arrange your calculation so that there is a single function call to one of the random functions, you can fetch the log of the kernel of the density for that draw using the function %RANLOGKERNEL(). In other words, this will work if you do %RANMVT to draw a random multivariate Normal, but won’t if you do it yourself by dividing a random Normal by the square root of a random gamma—in that case, %RANLOGKERNEL() will just give you the value for the last of the component draws that you did. 

 

Note that, unlike all the functions which return the densities or log densities themselves, %RANLOGKERNEL() will leave out integrating constants that don’t involve any of the parameters—they aren’t needed to compare the relative probabilities of two draws.

 


Copyright © 2025 Thomas A. Doan