经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
Simulation of a GARCH(p,q) with Gaussian innovations | Matlab:
- function [Returns, Vol, varlimit, Prices] = SimGARCHGaussian( mu, omega, garchcoeff, archcoeff, x0, h0, n, graph )
- % Simulation of a GARCH(p,q) with Gaussian innovations:
- %
- % x_i = mu + sqrt(h_i) epsilon_i,
- %
- % h_i = omega + sum_{k=1}^q archcoef(k)(x_{i-k}-mu)^2
- % + sum_{k=1}^p garchcoef(k) h_{i-k}, i=max(p,q)+1,...,n,
- % with starting points x_0 and h_1.
- %
- % Caution: The condition alpha+beta <1 is required for stationarity. In
- % that case, the asymptotic variance (varlimit) is :
- % varlimit = omega/(1-alpha-beta).
- %
- % [Returns,Vol,varlimit,Prices] = SimGarchGed(-0.004,0.005, 0.6, 0.3,5.4,0,0.1,250,1)
- %
- % If graph=1, returns and volatilies will be plotted.
- if nargin < 8
- graph=0;
- end
- Prices = [];
- eps = randn(n,1);
- % memory allocation
- v = zeros(n,1);
- x = zeros(n,1);
- h = zeros(n,1);
- varlimit = Inf;
- persistence = sum(archcoeff) + sum(garchcoeff);
- if persistence < 1
- varlimit = omega / ( 1 - persistence );
- end
- % generate time series
- p = length(garchcoeff);
- q = length(archcoeff)
- r = max(p,q);
- h(1:r) = h0;
- x(1:r) = x0;
- tp = (1:p)';
- tq = (1:q)';
- for t = (r+1):n
- h( t ) = omega + garchcoeff'* h( t- tp ) + archcoeff'* ( x( t- tq ) - mu ).^2;
- x( t ) = mu + sqrt( h( t ) ) * eps( t );
-
- end
- t = (1:1:n)';
- Vol = sqrt( h );
- Returns = x;
- if graph
- Prices = 100*[1; exp(cumsum(x))];
- subplot(3,1,1); plot( [0;t], Prices );
- title({' Prices S starting from 100';['Parameters: \mu = ' num2str(mu) ' , \omega = ' num2str(omega) ]});
-
- subplot(3,1,2); plot( t, x );
- title([' Returns x starting from x_0 = ' num2str(x(1))]);
-
- subplot(3,1,3); plot( t, Vol );
- title([' Conditional volatilities v^{1/2} starting from h_1 = ' num2str(h(1))] );
-
- end
复制代码
| | R: | - sim.garch.gaussian <- function(mu, omega, garchcoef, archcoef, x0, h0, n, graph = FALSE){
- # Simulation of a GARCH(p,q) with Gaussian innovations:
- #
- # x_i = mu + sqrt(h_i) epsilon_i,
- #
- # h_i = omega + sum_{k=1}^q archcoef(k)(x_{i-k}-mu)^2
- # + sum_{k=1}^p garchcoef(k) h_{i-k}, i=max(p,q)+1,...,n,
- # with starting points x_0 and h_1.
- #
- # Caution: The condition alpha+beta <1 is required for stationarity. In
- # that case, the asymptotic variance (varlimit) is :
- # varlimit = omega/(1-alpha-beta).
- #
- # [Returns,Vol,varlimit,Prices] = SimGarchGed(-0.004,0.005, 0.6, 0.3,5.4,0,0.1,250,1)
- #
- # If graph=1, returns and volatilies will be plotted.
-
-
- #Prices <- []
-
- eps <- rnorm(n)
-
- # memory allocation
- v <- mat.or.vec(n,1)
- x <- mat.or.vec(n,1)
- h <- mat.or.vec(n,1)
- varlimit <- Inf
-
- persistence <- sum(archcoef) + sum(garchcoef)
-
- if(persistence < 1){
- varlimit <- omega / ( 1 - persistence )
- }
-
- # generate time series
- p <- length(garchcoef)
- q <- length(archcoef)
- r <- max(p,q)
- h[1:r] <- h0
- x[1:r] <- x0
-
-
- tp <- (1:p)
- tq <- (1:q)
- for(t in (r+1):n){
- h[t] <- omega + garchcoef* h[ t- tp ] + archcoef* ( x[t- tq ] - mu )^2
- x[t] <- mu + sqrt( h[ t] ) * eps[ t ]
-
- }
-
- t <- (0:n)
- Vol <- sqrt( h )
- Returns <- x
-
-
- if(graph){
- library('ggplot2')
- Prices <- 100*c(1,exp(cumsum(x)))
-
- values.graph <- ggplot(data.frame(x=t, y=Prices),
- aes(x=x, y=y))
-
- values.graph <- values.graph + geom_line() +
- ggtitle(sprintf(' Prices S starting from 100 Parameters: mu <- %2.2f , omega <- %2.2f',mu,omega))
-
- print(values.graph)
-
- values.graph <- ggplot(data.frame(x=t[-1], y=x),
- aes(x=x, y=y))
-
- values.graph <- values.graph + geom_line() +
- ggtitle(sprintf(' Returns x starting from x_0 <- %2.2f', x[1]))
-
- print(values.graph)
-
- values.graph <- ggplot(data.frame(x=t[-1], y=Vol),
- aes(x=x, y=y))
-
- values.graph <- values.graph + geom_line() +
- ggtitle(sprintf(' Conditional volatilities v^{1/2} starting from h_1 <- %2.2f', h[1]))
-
- print(values.graph)
-
- }
- return(list(Returns=Returns, Vol=Vol, varlimit=varlimit, Prices=Prices))
- }
-
- ##
- GEDRnd <- function(nu,n,m=1){
-
- V <- runif(n,m)-0.5
- eps <- sign(V)
-
- a<- 1/nu
- theta0 <- sqrt(gamma(a)/gamma(3*a))
-
-
-
- z <- replicate(m,rgamma(n,a,1))
-
-
- X <- theta0*eps*(z^a)
- return(x)
- }
复制代码
|
扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
|