楼主: 匿名
2853 5

[其他] [MATLAB/R]Simulation of a GARCH(p,q) with Gaussian innovations [推广有奖]

匿名网友
楼主
匿名网友  发表于 2015-8-28 09:55:28 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Simulation of a GARCH(p,q) with Gaussian innovations

Matlab:
  1. function [Returns, Vol, varlimit, Prices] = SimGARCHGaussian( mu, omega, garchcoeff, archcoeff, x0, h0, n, graph )
  2. % Simulation of a GARCH(p,q) with Gaussian innovations:
  3. %
  4. %          x_i = mu + sqrt(h_i) epsilon_i,
  5. %
  6. %          h_i = omega + sum_{k=1}^q archcoef(k)(x_{i-k}-mu)^2
  7. %                 + sum_{k=1}^p garchcoef(k) h_{i-k}, i=max(p,q)+1,...,n,
  8. % with starting points x_0 and h_1.
  9. %
  10. % Caution: The condition alpha+beta <1 is required for stationarity. In
  11. % that case, the asymptotic variance (varlimit) is :
  12. %       varlimit = omega/(1-alpha-beta).
  13. %
  14. % [Returns,Vol,varlimit,Prices] = SimGarchGed(-0.004,0.005, 0.6, 0.3,5.4,0,0.1,250,1)
  15. %
  16. % If graph=1, returns and volatilies will be plotted.


  17. if nargin < 8
  18.     graph=0;
  19. end

  20. Prices = [];
  21. eps = randn(n,1);

  22. % memory allocation
  23. v = zeros(n,1);
  24. x = zeros(n,1);
  25. h = zeros(n,1);
  26. varlimit = Inf;

  27. persistence = sum(archcoeff) + sum(garchcoeff);

  28. if persistence < 1
  29.     varlimit = omega / ( 1 - persistence );
  30. end

  31. % generate time series
  32. p = length(garchcoeff);
  33. q = length(archcoeff)
  34. r = max(p,q);
  35. h(1:r) = h0;
  36. x(1:r) = x0;


  37. tp = (1:p)';
  38. tq = (1:q)';
  39. for t = (r+1):n
  40.     h( t ) = omega + garchcoeff'* h( t- tp ) + archcoeff'* ( x( t- tq ) - mu ).^2;
  41.     x( t ) = mu + sqrt( h( t ) ) * eps( t );
  42.    
  43. end

  44. t       = (1:1:n)';
  45. Vol     = sqrt( h );
  46. Returns = x;


  47. if graph
  48.     Prices  = 100*[1; exp(cumsum(x))];
  49.     subplot(3,1,1); plot( [0;t], Prices );
  50.     title({' Prices S starting from 100';['Parameters: \mu = ' num2str(mu) ' , \omega = ' num2str(omega) ]});
  51.    
  52.     subplot(3,1,2); plot( t, x );
  53.     title([' Returns x starting from x_0 = ' num2str(x(1))]);
  54.    
  55.     subplot(3,1,3); plot( t, Vol );
  56.     title([' Conditional volatilities v^{1/2} starting from h_1 = ' num2str(h(1))] );
  57.    
  58. end
复制代码

R:
  1. sim.garch.gaussian <- function(mu, omega, garchcoef, archcoef, x0, h0, n, graph = FALSE){
  2.   # Simulation of a GARCH(p,q) with Gaussian innovations:
  3.   #
  4.   #          x_i = mu + sqrt(h_i) epsilon_i,
  5.   #
  6.   #          h_i = omega + sum_{k=1}^q archcoef(k)(x_{i-k}-mu)^2
  7.   #                 + sum_{k=1}^p garchcoef(k) h_{i-k}, i=max(p,q)+1,...,n,
  8.   # with starting points x_0 and h_1.
  9.   #
  10.   # Caution: The condition alpha+beta <1 is required for stationarity. In
  11.   # that case, the asymptotic variance (varlimit) is :
  12.   #       varlimit = omega/(1-alpha-beta).
  13.   #
  14.   # [Returns,Vol,varlimit,Prices] = SimGarchGed(-0.004,0.005, 0.6, 0.3,5.4,0,0.1,250,1)
  15.   #
  16.   # If graph=1, returns and volatilies will be plotted.
  17.   
  18.   
  19.   #Prices <- []
  20.   
  21.   eps <- rnorm(n)
  22.   
  23.   # memory allocation
  24.   v <- mat.or.vec(n,1)
  25.   x <- mat.or.vec(n,1)
  26.   h <- mat.or.vec(n,1)
  27.   varlimit <- Inf
  28.   
  29.   persistence <- sum(archcoef) + sum(garchcoef)
  30.   
  31.   if(persistence < 1){
  32.   varlimit <- omega / ( 1 - persistence )
  33.   }
  34.   
  35.   # generate time series
  36.   p <- length(garchcoef)
  37.   q <- length(archcoef)
  38.   r <- max(p,q)
  39.   h[1:r] <- h0
  40.   x[1:r] <- x0
  41.   
  42.   
  43.   tp <- (1:p)
  44.   tq <- (1:q)
  45. for(t in (r+1):n){
  46.   h[t] <- omega + garchcoef* h[ t- tp ] + archcoef* ( x[t- tq ] - mu )^2
  47.   x[t] <- mu + sqrt( h[ t] ) * eps[ t ]
  48.   
  49. }
  50.   
  51.   t       <- (0:n)
  52. Vol     <- sqrt( h )
  53.   Returns <- x
  54.   
  55.   
  56.   if(graph){
  57.     library('ggplot2')
  58.   Prices  <- 100*c(1,exp(cumsum(x)))
  59.   
  60.   values.graph <- ggplot(data.frame(x=t, y=Prices),
  61.                          aes(x=x, y=y))
  62.   
  63.   values.graph <- values.graph + geom_line() +
  64.     ggtitle(sprintf(' Prices S starting from 100 Parameters: mu <-  %2.2f , omega <- %2.2f',mu,omega))
  65.   
  66.   print(values.graph)
  67.    
  68.     values.graph <- ggplot(data.frame(x=t[-1], y=x),
  69.                            aes(x=x, y=y))
  70.    
  71.     values.graph <- values.graph + geom_line() +
  72.       ggtitle(sprintf(' Returns x starting from x_0 <- %2.2f', x[1]))
  73.    
  74.     print(values.graph)
  75.    
  76.     values.graph <- ggplot(data.frame(x=t[-1], y=Vol),
  77.                            aes(x=x, y=y))
  78.    
  79.     values.graph <- values.graph + geom_line() +
  80.       ggtitle(sprintf(' Conditional volatilities v^{1/2} starting from h_1 <- %2.2f', h[1]))
  81.    
  82.     print(values.graph)
  83.   
  84.   }
  85. return(list(Returns=Returns, Vol=Vol, varlimit=varlimit, Prices=Prices))
  86. }
  87.   
  88.   ##
  89. GEDRnd <- function(nu,n,m=1){
  90.   
  91.   V <- runif(n,m)-0.5  
  92.   eps <- sign(V)
  93.   
  94.   a<- 1/nu
  95.   theta0 <- sqrt(gamma(a)/gamma(3*a))
  96.   
  97.   
  98.   
  99.   z <- replicate(m,rgamma(n,a,1))
  100.   
  101.   
  102.   X <- theta0*eps*(z^a)

  103. return(x)
  104. }
复制代码

untitled.bmp



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Innovations Innovation Simulation Gaussian ulation function required starting Caution points

已有 2 人评分经验 论坛币 学术水平 热心指数 信用等级 收起 理由
accumulation + 100 + 1 + 1 + 1 精彩帖子
xddlovejiao1314 + 100 + 100 + 3 + 3 + 3 精彩帖子

总评分: 经验 + 200  论坛币 + 100  学术水平 + 4  热心指数 + 4  信用等级 + 4   查看全部评分

本帖被以下文库推荐

沙发
rrjj101022 发表于 2015-8-28 09:57:41
谢谢分享~~~
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

藤椅
亚米UM 发表于 2015-8-28 16:56:50
棒!!谢谢楼主!
已有 1 人评分经验 论坛币 收起 理由
xddlovejiao1314 + 10 + 3 鼓励积极发帖讨论

总评分: 经验 + 10  论坛币 + 3   查看全部评分

板凳
xddlovejiao1314 学生认证  发表于 2015-9-24 21:33:26
谢谢分享。
已有 1 人评分经验 论坛币 热心指数 收起 理由
niuniuyiwan + 10 + 3 + 1 精彩帖子

总评分: 经验 + 10  论坛币 + 3  热心指数 + 1   查看全部评分

报纸
wutaibo 发表于 2016-3-24 11:08:51
Nice!!!

地板
evelyne 发表于 2016-12-8 10:16:49
谢谢!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-9 05:57