首页 > 解决方案 > VAR模型的蒙特卡罗模拟

问题描述

我一直在尝试使用蒙特卡罗模拟来估计 VAR 模型。我有 3 个内生变量。我需要一些关于这方面的指导。首先,我想添加一个异常值作为样本量的百分比。第二(同一模型的第二次模拟),我想添加多元污染正态分布,如 0.9N (0, I) + 0.1((0,0,0)',(100, 100, 100)) 而不是异常值。你能告诉我怎么做这些吗?谢谢你。

 RR <- function(n, out){
 # n is number of observations
 k <- 3 # Number of endogenous variables
 p <- 2 # Number of lags
 # add outlier
 n[1]<- n[1]+out
    
 # Generate coefficient matrices
 B1 <- matrix(c(.1, .3, .4, .1, -.2, -.3, .03, .1, .1), k) # Coefficient matrix of lag 1
 B2 <- matrix(c(0, .2, .1, .07, -.4, -.1, .5, 0, -.1), k) # Coefficient matrix of lag 2
 M <- cbind(B1, B2) # Companion form of the coefficient matrices

 # Generate series
 DT <- matrix(0, k, n + 2*p) # Raw series with zeros
 for (i in (p + 1):(n + 2*p)){ # Generate series with e ~ N(0,1)
 DT[, i] <- B1%*%DT[, i-1] + B2%*%DT[, i-2] + rnorm(k, 0, 1)
 }

 DT <- ts(t(DT[, -(1:p)])) # Convert to time series format
 #names <- c("V1", "V2", "V3") # Rename variables
 colnames(DT) <- c("Y1", "Y2", "Y3")

 #plot.ts(DT) # Plot the series

 # estimate VECM
 vecm1 <- VECM(DT, lag = 2, r = 2, include = "const", estim ="ML")
 vecm2 <- VECM(DT, lag = 2, r = 1, include = "const", estim ="ML")

 # mse

 mse1 <- mean(vecm1$residuals^2)
 mse2 <- mean(vecm2$residuals^2)


#param_list <- unname(param_list)
return(list("mse1" = mse1, "mse2" = mse2, "mse3" = mse3))

 }

# defined the parameter grids(define the parameters ranges we want to run our function with)

n_grid = c(50, 80, 200, 400)
out_grid = c(0 ,5, 10)

# collect parameter grids in a list (to enter it into the Monte Carlo function)

prml = list("n" = n_grid, "out" = out_grid)

# run simulation

RRS <- MonteCarlo(func = RR, nrep = 1000, param_list = prml)
summary(RRS)


# make table:
rows = "n"
cols = "out"
MakeTable(output = RRS, rows = rows, cols = cols)

标签: routliersmixture

解决方案


推荐阅读