Original link:tecdat.cn/?p=19688 

Original source:Tuo End number according to the tribe public number

 

When they were introduced, Copulas were generally considered interesting because they allowed modeling of edge distributions and dependent structures separately.

Copula models edges and dependencies

Given some edge distribution functions and a copula, we can generate a multivariate distribution function whose edges are specified above.

Consider a binary lognormal distribution


> library(mnormt)
> set.seed(1)
> Z=exp(rmnorm(25,MU,SIGMA))
Copy the code

We can start with the edge distribution.

Meanlog sdlog 1.168 0.930 (0.186) (0.131) meanlog sdlog 2.218 1.168 (0.233) (0.165)Copy the code

Based on these edge distributions, and considering the maximum likelihood estimates of copula parameters obtained from this pseudo-random sample, numerically, we obtain

> library(copula) > Copula() estimation based on 'maximum likelihood' and a sample of size 25. Estimate Std. Error z The value (Pr > | z |) rho. 1 0.86530 0.03799 22.77Copy the code

However, since the dependence relation is a function of edge distribution, we do not treat the dependence relation separately. If the global optimization problem is considered, the results are different. You can figure out the density


> optim(par=c(0,0,1,1,0),fn=LogLik)$par
[1] 1.165  2.215 0.923  1.161  0.864 
Copy the code

Not much difference, but not the same estimator. From a statistical point of view, it is almost impossible to deal with edge and dependent structures separately. The other thing we should keep in mind is that marginal distributions can be misspecified. For example, if we assume an exponential distribution,

Fitdistr (Z[,1],"exponential") Rate 0.222 (0.044) Fitdistr (Z[,2],"exponential")Copy the code

Parameter estimation of gaussian Copula

Copula() estimation based on 'maximum likelihood' and a sample of size 25. Estimate Std. Error z value Pr(>|z|) rho.1 0.87421 0.03617 24.17 <2e-16 *** -- signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 '1 The Maximized loglikelihood is 15.4 Optimization convergedCopy the code
 
Copy the code

Because we incorrectly specified the edge distribution, we could not get a uniform edge. If we use the code above to generate a sample size of 500,


barplot(counts, axes=FALSE,col="light blue"
Copy the code

 

If the edge distribution is well defined, we can clearly see that the dependent structure depends on the edge distribution,

 

Copula simulates the correlated random walk in the stock market

Next, we use the Copula function to simulate the correlation random walk in the stock market

 

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # load historical data #****************************************************************** load.packages('quantmod') data$YHOO = getSymbol.intraday.google('YHOO', 'NASDAQ', 60, '15d') data$FB = getSymbol.intraday.google('FB', 'NASDAQ', 60, '15d') bt.prep(data, Align = 'remove. Na') # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * # generated simulation # * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * rets = diff (log (prices)) # price matplot(exp(apply(rets,2,cumsum)), type='l')Copy the code

 

Plot (rets[,1], REts [,2], xlab= LABS [1], ylab= LABS [2], col='blue', las=1) points(fit.sim[,1], fit.sim[,2], Print (round(100*temp,2) print(round(100*temp,2)) print(round(100*temp,2)) print(round(100*temp,2)) print(round(100*temp,2)) print(round(100*temp,2)) print(round(100*temp,2)) print(round(100*temp,2)) print(round(100*temp,2)) print(round(100*temp,2)) Print (LABS [I]) print(ks.test(rets[, I], fit.sim[I])) exp(apply(fit.sim,2,cumsum)) Main ='Simulated Price path') # simulate Copula load.packages(' Copula ')Copy the code

 

Apply (rets,2,function(x) list(mean=mean(x), Sd =sd(x))) # Simulate rMvdc from fit distribution (4800, FIT)Copy the code

Actual Simulated Correlation 57.13 57.38 Mean Fb-0.31-0.47 Mean yhoo-0.40-0.17 StDev FB 1.24 1.25 StDev YHOO 1.23 1.23Copy the code

FB

	Two-sample Kolmogorov-Smirnov test

data:  rets[, i] and fit.sim[i]
D = 0.9404, p-value = 0.3395
alternative hypothesis: two-sided
Copy the code

HO

Two-sample Kolmogorov-Smirnov test data: REts [, I] and fit.sim[I] D = 0.8792, p-value = 0.4222 alternative hypothesis: two-sidedCopy the code

 

 visualize.rets(fit.sim)
Copy the code

 

# qnorm(runif(10^8)) and rnorm(10^8) are equivalent uniforms.sim = rCopula(4800, gumbelCopula(gumbel@estimate, dim=n))Copy the code

Actual Simulated Correlation 57.13 57.14 Mean Fb-0.31-0.22 Mean yhoo-0.40-0.56 StDev FB 1.24 1.24 StDev YHOO 1.23 1.21Copy the code

FB

	Two-sample Kolmogorov-Smirnov test

data:  rets[, i] and fit.sim[i]
D = 0.7791, p-value = 0.5787
alternative hypothesis: two-sided
Copy the code

HO

Two-sample Kolmogorov-Smirnov test data: REts [, I] and fit.sim[I] D = 0.795, p-value = 0.5525 alternative hypothesis: two-sidedCopy the code

 

 

	vis(rets)
Copy the code

 

The standard deviation is very large relative to the mean, close to zero; Therefore, in some cases, we are likely to get unstable results.


Most welcome insight

1. Empirical research on R language fitting and prediction based on ArMA-GarCH-VAR model

2. R language copula algorithm modeling dependency cases

3.R language COPULAS and VaR analysis of financial time series data

4.R language multivariate COPULA GARCH model time series prediction

5. VaR comparison of GARCH (1,1), MA and historical simulation method

6. Matlab used Copula simulation to optimize market risk data analysis

7.R language to achieve vector automatic regression VAR model

8.R language random search variable selection SSVS estimation Bayesian vector autoregression (BVAR) model

9. Impulse response analysis of different types of VAR models in R language