Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Eviews5 / EViews5 / Example Files / docs / mcintro

.htm
Скачиваний:
18
Добавлен:
23.03.2015
Размер:
10.66 Кб
Скачать

Monte Carlo Basics Monte Carlo Basics Storing results in a series (mc1ser.prg) Storing results in a matrix (mc1mat.prg) The following document guides you through the steps required to carry out a Monte Carlo simulation exercise using EViews. To understand this material you must be fairly comfortable with the EViews programming language. If you have not yet done so, we recommend that you first read Chapter 3 (Matrix Language) and Chapter 6 (EViews Programming) of the EViews 5 Command and Programming Reference before proceeding. A typical Monte Carlo simulation exercise consists of the following steps: Specify the "true" model (data generating process) underlying the data. Simulate a draw from the data and estimate the model using the simulated data. Repeat step 2 many times, each time storing the results of interest. The end result is a series of estimation results, one for each repetition of step 2. We can then characterize the empirical distribution of these results by tabulating the sample moments or by plotting the histogram or kernel density estimate. Step 2 typically involves simulating a random draw from a specified distribution. EViews provides built-in pseudo-random number generating functions for a wide range of commonly used distributions; see Appendix D, Statistical Distribution Functions of the EViews 5 Command and Programming Reference. The step that requires a little thinking is how to store the results from each repetition (step 3). There are two methods that you can use in EViews; each with its advantages and disadvantages. The first method is to store the results into a series or group of series. The difficulty with this approach is that series elements are most easily indexed by a sample in EViews. To store the result from each replication as a different observation in a series, you must shift  the sample every time you store a new result. Moreover, the length of the series will be constrained by the size of your workfile sample. If you wish to perform 1000 replications on a workfile with 100 observations, you will not be able to store all 1000 results in a series since the latter only has 100 observations. The alternative method is to store the results in a matrix (or vector). The matrix is indexed by its row-column position and its size is independent of the workfile sample. For example, you can declare a matrix with 1000 rows and 10 columns in a workfile with only 1 observation. The disadvantage of the matrix method is that the matrix object does not have as much built-in functions as a series object. For example, there is no kernel density estimate view out of a matrix (which is available for a series object). For pedagogical purposes, I will illustrate both approaches. (My own recommendation is a mixed approach. Store all the results in a matrix. If you need to do further processing, convert the matrix into a group of series.) As a concrete example, consider the following exercise (3.26) from Damodar Gujarati Basic Econometrics, 3rd edition: Refer to the 10 X values of Table 3.2 (which are: 80, 100, 120, 140, 160, 180, 200, 220, 240, 260). Let Beta(1) = 25 and Beta(2) = 0.5. Assume that the errors are distributed N(0, 9), that is, the errors are normally distributed with mean 0 and variance 9. Generate 100 samples using these values, obtaining 100 estimates of Beta(1) and Beta(2). Graph these estimates. What conclusions can you draw from the Monte Carlo study? The first step is to write a program file that performs one replication of the Monte Carlo experiment. For this problem, the program file will look something like the following: ' create workfile wfcreate mcarlo u 1 10 ' create data series for x series x x.fill 80, 100, 120, 140, 160, 180, 200, 220, 240, 260 ' set seed for random number generator rndseed 123456 ' simulate y data series y = 2.5 + 0.5*x + 3*nrnd ' regress y on a constant and x equation eq1.ls y c x ' display results show eq1.output Since we want to run the regression several times (in this case 100 times), we will add a loop in the program as follows: ' create workfile wfcreate mcarlo u 1 10 ' create data series for x ' NOTE: x is fixed in repeated samples series x x.fill 80, 100, 120, 140, 160, 180, 200, 220, 240, 260 ' set seed for random number generator rndseed 123456 ' assign number of replications to a control variable !reps = 100 ' begin loop for !i = 1 to !reps ' simulate y data series y = 2.5 + 0.5*x + 3*nrnd ' regress y on a constant and x equation eq1.ls y c x next ' end of loop This program is not useful as it is since it does not store the results from each replication. At the end of the program, the C coefficient vector only contains the estimated parameter values from the last replication. ^Top

Storing results in a series To store results for 100 replications of our experiment in a series, we must first expand the workfile if the desired number of replications is greater than the number of observations for the workfile. Here, we must have a workfile with 100 observations.  Then inside the loop, we set the sample to the first 10 observations, draw a sample for y, and estimate the model. Then we reset the sample to the replication number and store the estimated coefficient in the appropriate observation number. This is repeated for each replication. Thus for the 1st replication, we store the estimates in observation 1, the 2nd replication estimates are stored in observation 2, and so on until the last replication which is stored in the last observation. A program which performs these steps is given by:

' store monte carlo results in a series ' checked 4/1/2004 ' set workfile range to number of monte carlo replications wfcreate mcarlo u 1 100 ' create data series for x ' NOTE: x is fixed in repeated samples ' only first 10 observations are used (remaining 90 obs missing) series x x.fill 80, 100, 120, 140, 160, 180, 200, 220, 240, 260 ' set true parameter values !beta1 = 2.5 !beta2 = 0.5 ' set seed for random number generator rndseed 123456 ' assign number of replications to a control variable !reps = 100 ' begin loop for !i = 1 to !reps ' set sample to estimation sample smpl 1 10 ' simulate y data (only for 10 obs) series y = !beta1 + !beta2*x + 3*nrnd ' regress y on a constant and x equation eq1.ls y c x ' set sample to one observation smpl !i !i ' and store each coefficient estimate in a series series b1 = eq1.@coefs(1) series b2 = eq1.@coefs(2) next ' end of loop ' set sample to full sample smpl 1 100 ' show kernel density estimate for each coef freeze(gra1) b1.kdensity ' draw vertical dashline at true parameter value gra1.draw(dashline, bottom, rgb(156,156,156)) !beta1 show gra1 freeze(gra2) b2.kdensity ' draw vertical dashline at true parameter value gra2.draw(dashline, bottom, rgb(156,156,156)) !beta2 show gra2 The crucial step in the above program is the line smpl !i !i inside the loop. If you can answer the following question, you are probably ready to write code for your own application: what happens if that smpl statement was not there? Without that line, the sample is set to the first 10 observation at the top of the loop and hence the first 10 observations of the series B1, B2 will be assigned the estimated coefficient value from that iteration. Therefore when the program exits the loop, B1 and B2 will contain only estimates from the last iteration in observations 1 to 10 and observations 11 to 100 will be missing. ^Top

Storing results in a matrix The alternative approach is to store the replication results in a matrix object. The workfile range can now be set to the sample size. Before you enter the loop, you declare the matrix to store the results. Then inside the loop, you store the results in the appropriate cell of the storage matrix. The code will be something like this: ' store monte carlo results in a matrix ' checked 4/1/2004 ' set workfile range to number of obs wfcreate mcarlo u 1 10 ' create data series for x ' NOTE: x is fixed in repeated samples series x x.fill 80, 100, 120, 140, 160, 180, 200, 220, 240, 260 ' set seed for random number generator rndseed 123456 ' assign number of replications to a control variable !reps = 100 ' declare storage matrix matrix(!reps,2) beta ' begin loop for !i = 1 to !reps ' simulate y data series y = 2.5 + 0.5*x + 3*nrnd ' regress y on a constant and x equation eq1.ls y c x ' store each coefficient estimate in matrix beta(!i,1) = eq1.@coefs(1) ' column 1 is intercept beta(!i,2) = eq1.@coefs(2) ' column 2 is slope next ' end of loop ' show descriptive stats of coef distribution beta.stats If you run this program, you will notice that the matrix version runs much faster than the series version. However, you will not be able to draw the kernel density estimate directly from a matrix object. ^Top

Соседние файлы в папке docs