Documentation

simulate

Monte Carlo simulation of vector autoregression (VAR) model

Syntax

Y = simulate(Mdl,numobs)
Y = simulate(Mdl,numobs,Name,Value)
[Y,E] = simulate(___)

Description

example

Y= simulate(Mdl,numobs)returns a randomnumobs-period path of multivariate response series (Y) from simulating the fully specified VAR(p) modelMdl.

example

Y= simulate(Mdl,numobs,Name,Value)uses additional options specified by one or moreName,Valuepair arguments.For example, you can specify simulation of multiple paths, exogenous predictor data, or inclusion of future responses for conditional simulation.

example

[Y,E] = simulate(___)returns the model innovationsE使用任何输入参数在前面的年代yntaxes.

Examples

collapse all

Fit a VAR(4) model to the consumer price index (CPI) and unemployment rate data. Then, simulate responses from the estimated model.

Load theData_USEconModeldata set.

loadData_USEconModel

Plot the two series on separate plots.

figure; plot(DataTable.Time,DataTable.CPIAUCSL); title('Consumer Price Index'); ylabel('Index'); xlabel('Date');

figure; plot(DataTable.Time,DataTable.UNRATE); title('Unemployment Rate'); ylabel('Percent'); xlabel('Date');

Stabilize the CPI by converting it to a series of growth rates. Synchronize the two series by removing the first observation from the unemployment rate series. Create a new data set containing the transformed variables, and do not include any rows containing at least one missing observation.

rcpi = price2ret(DataTable.CPIAUCSL); unrate = DataTable.UNRATE(2:end); dates = DataTable.Time(2:end); idx = all(~ismissing([rcpi unrate]),2); Data = array2timetable([rcpi(idx) unrate(idx)],...'RowTimes',DataTable.Time(idx),'VariableNames',{'rcpi','unrate'});

Create a default VAR(4) model using the shorthand syntax.

Mdl = varm(2,4);

Estimate the model using the entire data set.

EstMdl = estimate(Mdl,Data.Variables);

EstMdlis a fully specified, estimatedvarmmodel object.

Simulate a response series path from the estimated model with length equal to the path in the data.

rng(1);% For reproducibilitynumobs = size(Data,1); Y = simulate(EstMdl,numobs);

Yis a 245-by-2 matrix of simulated responses. The first and second columns contain the simulated CPI growth rate and unemployment rate, respectively.

Plot the simulated and true responses.

figure; plot(Data.Time,Y(:,1)); holdon; plot(Data.Time,Data.rcpi) title('CPI Growth Rate'); ylabel('Growth rate'); xlabel('Date'); legend('Simulation','True')

figure; plot(Data.Time,Y(:,2)); holdon; plot(Data.Time,Data.unrate) ylabel('Percent'); xlabel('Date'); title('Unemployment Rate'); legend('Simulation','True')

Illustrate the relationship betweensimulateandfilterby estimating a 4-dimensional VAR(2) model of the four response series in Johansen's Danish data set. Simulate a single path of responses using the fitted model and the historical data as initial values, and then filter a random set of Gaussian disturbances through the estimated model using the same presample responses.

Load Johansen's Danish economic data.

loadData_JDanish

For details on the variables, enterDescription.

Create a default 4-D VAR(2) model.

Mdl = varm(4,2);

Estimate the VAR(2) model using the entire data set.

EstMdl = estimate(Mdl,Data);

When reproducing the results ofsimulateandfilter,重要的是take these actions.

  • Set the same random number seed usingrng.

  • Specify the same presample response data using the'Y0'name-value pair argument.

Set the default random seed. Simulate 100 observations by passing the estimated model tosimulate. Specify the entire data set as the presample.

rngdefaultYSim =模拟(EstMdl, 100,'Y0',Data);

YSimis a 100-by-4 matrix of simulated responses. Columns correspond to the columns of the variables inData.

Set the default random seed. Simulate 4 series of 100 observations from the standard Gaussian distribution.

rngdefaultZ = randn(100,4);

Filter the Gaussian values through the estimated model. Specify the entire data set as the presample.

YFilter = filter(EstMdl,Z,'Y0',Data);

YFilteris a 100-by-4 matrix of simulated responses. Columns correspond to the columns of the variables in the dataData. Before filtering the disturbances,filterscalesZby the lower triangular Cholesky factor of the model covariance inEstMdl.Covariance.

Compare the resulting responses betweenfilterandsimulate.

(YSim - YFilter)'*(YSim - YFilter)
ans =0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

The results are identical.

估计消费者价格的VAR模型(4)印度x (CPI), the unemployment rate, and the gross domestic product (GDP). Include a linear regression component containing the current and the last 4 quarters of government consumption expenditures and investment. Simulate multiple paths from the estimated model.

Load theData_USEconModeldata set. Compute the real GDP.

loadData_USEconModelDataTable.RGDP = DataTable.GDP./DataTable.GDPDEF*100;

Plot all variables on separate plots.

figure; subplot(2,2,1) plot(DataTable.Time,DataTable.CPIAUCSL); ylabel('Index'); title('Consumer Price Index'); subplot(2,2,2) plot(DataTable.Time,DataTable.UNRATE); ylabel('Percent'); title('Unemployment Rate'); subplot(2,2,3) plot(DataTable.Time,DataTable.RGDP); ylabel('Output'); title('Real Gross Domestic Product'); subplot(2,2,4) plot(DataTable.Time,DataTable.GCE); ylabel('Billions of $'); title('Government Expenditures');

Stabilize the CPI, GDP, and GCE by converting each to a series of growth rates. Synchronize the unemployment rate series with the others by removing its first observation. Create a new data set containing the transformed variables.

inputVariables = {'CPIAUCSL''RGDP''GCE'}; Data = varfun(@price2ret,DataTable,'InputVariables',inputVariables); Data.Properties.VariableNames = inputVariables; Data.UNRATE = DataTable.UNRATE(2:end); dates = DataTable.Time(2:end);

Expand the GCE rate series to a matrix that includes its current value and up through four lagged values. RemoveGCEand observations containing any missing values fromData.

rgcelag4 = lagmatrix(Data.GCE,0:4); idx = all(~ismissing([Data table(rgcelag4)]),2); Data = Data(idx,:); Data.GCE = [];

Create a default VAR(4) model using the shorthand syntax.

Mdl = varm(3,4); Mdl.SeriesNames = ["rcpi""unrate""rgdpg"];

Estimate the model using the entire sample. Specify the GCE matrix as data for the regression component.

EstMdl = estimate(Mdl,Data.Variables,'X',rgcelag4);

Simulate 1000 paths from the estimated model. Specify that the length of the paths is the same as the length of the data without any missing values. Supply the predictor data. Return the innovations (scaled disturbances).

numpaths = 1000; numobs = size(Data,1); rng(1);% For reproducibility[Y,E] = simulate(EstMdl,numobs,'X',rgcelag4,'NumPaths',numpaths);

Yis a 244-by-3-by-1000 matrix of simulated responses.Eis a matrix whose dimensions correspond to the dimensions ofY, but represents the simulated, scaled disturbances. The columns correspond to the CPI growth rate, unemployment rate, and GDP growth rate, respectively.simulateapplies the same predictor data to all paths.

For each time point, compute the mean vector of the simulated responses among all paths.

MeanSim = mean(Y,3);

MeanSimis a 244-by-3 matrix containing the average of the simulated responses at each time point.

Plot the simulated responses, their averages, and the data.

figure;forj = 1:Mdl.NumSeries subplot(2,2,j) plot(Data.Time,squeeze(Y(:,j,:)),'Color',[0.8,0.8,0.8]) title(Mdl.SeriesNames{j}); holdonh1 = plot(Data.Time,Data{:,j}); h2 = plot(Data.Time,MeanSim(:,j)); holdoffendhl = legend([h1 h2],'Data','Mean'); hl.Position = [0.6 0.25 hl.Position(3:4)];

Input Arguments

collapse all

VAR model, specified as avarmmodel object created byvarmorestimate.Mdlmust be fully specified.

Number of random observations to generate per output path, specified as a positive integer. The output argumentsYandEhavenumobsrows.

Data Types:double

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside single quotes (' '). You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

Example:'Y0',Y0,'X',Xuses the matrixY0as presample responses and the matrixXas predictor data in the regression component.

collapse all

Number of sample paths to generate, specified as the comma-separated pair consisting of'NumPaths'and a positive integer. The output argumentsYandEhaveNumPathspages.

Example:'NumPaths',1000

Data Types:double

Presample responses providing initial values for the model, specified as the comma-separated pair consisting of'Y0'and anumpreobs-by-numseriesnumeric matrix or anumpreobs-by-numseries-by-numprepathsnumeric array.

Y0must haveMdl.NumSeriescolumns.Y0must have at leastMdl.Prows. If you supply more rows than necessary,simulateuses the latestMdl.Pobservations only. The last row contains the latest observation.

  • IfY0is a matrix, thensimulateapplies it to simulate each sample path (page). Therefore, all paths in the output argumentYderive from common initial conditions.

  • Otherwise,simulateappliesY0(:,:,j)to initialize simulating pathj.Y0must have at leastnumpathspages (seeNumPaths), andsimulateuses only the firstnumpathspages.

By default,simulatesets any necessary presample observations.

  • For stationary VAR processes without regression components,simulatesets presample observations to the unconditional mean μ = Φ 1 ( L ) c .

  • For nonstationary processes or models that contain a regression component,simulatesets presample observations to zero.

Data Types:double

Predictor data for the regression component in the model, specified as the comma-separated pair consisting of'X'and a numeric matrix containingnumpredscolumns, wherenumpreds = size(Mdl.Beta,2). Rows correspond to observations, and columns correspond to individual predictor variables. All predictor variables are present in the regression component of each response equation.simulateappliesXto each path (page); that is,Xrepresents one path of observed predictors.

Xmust have at leastnumobsrows. If you supply more rows than necessary, thensimulateuses only the latestnumobsobservations. The last row contains the latest observation.

simulatedoes not use the regression component in the presample period.

By default,simulateexcludes the regression component, regardless of its presence inMdl.

Data Types:double

Future multivariate response series for conditional simulation, specified as the comma-separated pair consisting of'YF'and a numeric matrix or array containingnumseriescolumns, wherenumseriesisMdl.NumSeries.YFmust have at leastnumobsrows to cover the simulation horizon. If you supply more rows than necessary, thensimulateuses only the firstnumobsrows. Rows correspond to time points and contain observations, columns correspond to response variables, and pages correspond to sample paths.

PathkofYF(YF(:,:,k)) captures the state or knowledge of response series as they evolve from the presample past (Y0) into the future. Rowj(YF(j,:,k)) contains the responsesjperiods into the future.

Elements ofYFcan be numeric scalars or missing values (indicated byNaNvalues).simulatetreats numeric scalars as deterministic future responses that are known in advance, for example, set by policy.simulatesimulates responses for correspondingNaNvalues conditional on the known values.

  • IfYFis a matrix, thensimulateappliesYFto each of thenumpathsoutput paths (seeNumPaths).

  • Otherwise,YFmust have at leastnumpathspages. If you supply more pages than necessary, thensimulateuses only the firstnumpathspages.

By default,YFis an array composed ofNaNvalues indicating a complete lack of knowledge of the future state of all simulated responses. Therefore,simulateobtains the output responsesYfrom a conventional, unconditional Monte Carlo simulation.

For more details, seeAlgorithms.

Example:Consider simulating one path of a VAR model composed of four response series three periods into the future. Suppose that you have prior knowledge about some of the future values of the responses, and you want to simulate the unknown responses conditional on your knowledge. SpecifyYFas a matrix containing the values that you know, and useNaNfor values you do not know but want to simulate. For example,'YF',[NaN 2 5 NaN; NaN NaN 0.1 NaN; NaN NaN NaN NaN]specifies that you have no knowledge of the future values of the first and fourth response series; you know the value for period 1 in the second response series, but no other value; and you know the values for periods 1 and 2 in the third response series, but not the value for period 3.

Data Types:double

Note

NaNvalues inY0andXindicate missing values.simulateremoves missing values from the data by list-wise deletion. IfY0is a 3-D array, thensimulateperforms these steps.

  1. Horizontally concatenate pages to form anumpreobs-by-numpaths*numseriesmatrix.

  2. Remove any row that contains at least oneNaNfrom the concatenated data.

In the case of missing observations, the results obtained from multiple paths ofY0can differ from the results obtained from each path individually.

For conditional simulation (seeYF), ifXcontains any missing values in the latestnumobsobservations, thensimulatethrows an error.

Output Arguments

collapse all

Simulated multivariate response series, returned as anumobs-by-numseriesnumeric matrix or anumobs-by-numseries-by-numpathsnumeric array.Yrepresents the continuation of the presample responses inY0.

If you specify future responses for conditional simulation using theYFname-value pair argument, then the known values inYFappear in the same positions inY. However,Ycontains simulated values for the missing observations inYF.

Simulated multivariate model innovations series, returned as anumobs-by-numseriesnumeric matrix or anumobs-by-numseries-by-numpathsnumeric array.

If you specify future responses for conditional simulation (see theYFname-value pair argument), thensimulateinfers the innovations from the known values inYFand places the inferred innovations in the corresponding positions inE. For the missing observations inYF,simulatedraws from the Gaussian distribution conditional on any known values, and places the draws in the corresponding positions inE.

Algorithms

  • simulateperforms conditional simulation using this process for all pagesk= 1,...,numpathsand for each timet= 1,...,numobs.

    1. simulateinfers (or inverse filters) the innovationsE(t,:,k)from the known future responsesYF(t,:,k). ForE(t,:,k),simulatemimics the pattern ofNaNvalues that appears inYF(t,:,k).

    2. For the missing elements ofE(t,:,k),simulateperforms these steps.

      1. DrawZ1, the random, standard Gaussian distribution disturbances conditional on the known elements ofE(t,:,k).

      2. ScaleZ1by the lower triangular Cholesky factor of the conditional covariance matrix. That is,Z2=L*Z1, whereL=chol(C,'lower')andC条件协方差的高斯距离吗ribution.

      3. ImputeZ2in place of the corresponding missing values inE(t,:,k).

    3. For the missing values inYF(t,:,k),simulatefilters the corresponding random innovations through the modelMdl.

  • simulateuses this process to determine the time origint0of models that include linear time trends.

    • If you do not specifyY0, thent0= 0.

    • Otherwise,simulatesetst0tosize(Y0,1)Mdl.P. Therefore, the times in the trend component aret=t0+ 1,t0+ 2,...,t0+numobs. This convention is consistent with the default behavior of model estimation in whichestimateremoves the firstMdl.Presponses, reducing the effective sample size. Althoughsimulateexplicitly uses the firstMdl.Ppresample responses inY0to initialize the model, the total number of observations inY0(excluding any missing values) determinest0.

References

[1]Hamilton, J. D.Time Series Analysis. Princeton, NJ: Princeton University Press, 1994.

[2]Johansen, S.Likelihood-Based Inference in Cointegrated Vector Autoregressive Models. Oxford: Oxford University Press, 1995.

[3]Juselius, K.The Cointegrated VAR Model. Oxford: Oxford University Press, 2006.

[4]Lütkepohl, H.New Introduction to Multiple Time Series Analysis. Berlin: Springer, 2005.

Introduced in R2017a

Was this topic helpful?