拓端数据tecdat|R语言Copula函数股市相关性建模:模拟Random Walk(随机游走)

原文链接:http://tecdat.cn/?p=19688 

在引入copula时,大家普遍认为copula很有趣,因为它们允许分别对边缘分布和相依结构进行建模。

copula建模边缘和相依关系

给定一些边缘分布函数和一个copula,那么我们可以生成一个多元分布函数,其中的边缘是前面指定的。

考虑一个二元对数正态分布

  1.  
     
  2.  
    > library(mnormt)
  3.  
    > set.seed(1)
  4.  
    > Z=exp(rmnorm(25,MU,SIGMA))

我们可以从边缘分布开始。

  1.  
     
  2.  
    meanlog sdlog
  3.  
    1.168 0.930
  4.  
    (0.186 ) (0.131 )
  5.  
     
  6.  
    meanlog sdlog
  7.  
    2.218 1.168
  8.  
    (0.233 ) (0.165 )

基于这些边缘分布,并考虑从该伪随机样本获得的copula参数的最大似然估计值,从数值上讲,我们得到

  1.  
     
  2.  
    > library(copula)
  3.  
     
  4.  
    > Copula() estimation based on 'maximum likelihood'
  5.  
    and a sample of size 25.
  6.  
    Estimate Std. Error z value Pr(>|z|)
  7.  
    rho.1 0.86530 0.03799 22.77

但是,由于相依关系是边缘分布的函数,因此我们没有对相依关系进行单独处理。如果考虑全局优化问题,则结果会有所不同。可以得出密度

  1.  
     
  2.  
    > optim(par=c(0,0,1,1,0),fn=LogLik)$par
  3.  
    [1] 1.165 2.215 0.923 1.161 0.864

差别不大,但估计量并不相同。从统计的角度来看,我们几乎无法分别处理边缘和相依结构。我们应该记住的另一点是,边际分布可能会错误指定。例如,如果我们假设指数分布,

  1.  
     
  2.  
    fitdistr(Z[,1],"exponential")
  3.  
    rate
  4.  
    0.222
  5.  
    (0.044 )
  6.  
    fitdistr(Z[,2],"exponential"
  7.  
    rate
  8.  
    0.065
  9.  
    (0.013 )

高斯copula的参数估计

  1.  
     
  2.  
    Copula() estimation based on 'maximum likelihood'
  3.  
    and a sample of size 25.
  4.  
    Estimate Std. Error z value Pr(>|z|)
  5.  
    rho.1 0.87421 0.03617 24.17 <2e-16 ***
  6.  
    ---
  7.  
    Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  8.  
    The maximized loglikelihood is 15.4
  9.  
    Optimization converged
 

由于我们错误地指定了边缘分布,因此我们无法获得统一的边缘。如果我们使用上述代码生成大小为500的样本,

  1.  
     
  2.  
    barplot(counts, axes=FALSE,col="light blue"

如果边缘分布被很好地设定时,我们可以清楚地看到相依结构依赖于边缘分布,

copula模拟股市中相关随机游走

接下来我们用copula函数模拟股市中的相关随机游走

  1.  
    #*****************************************************************
  2.  
    # 载入历史数据
  3.  
    #******************************************************************
  4.  
     
  5.  
    load.packages('quantmod')
  6.  
     
  7.  
    data$YHOO = getSymbol.intraday.google('YHOO', 'NASDAQ', 60, '15d')
  8.  
    data$FB = getSymbol.intraday.google('FB', 'NASDAQ', 60, '15d')
  9.  
    bt.prep(data, align='remove.na')
  10.  
     
  11.  
     
  12.  
    #*****************************************************************
  13.  
    # 生成模拟
  14.  
    #******************************************************************
  15.  
     
  16.  
    rets = diff(log(prices))
  17.  
     
  18.  
    # 绘制价格
  19.  
    matplot(exp(apply(rets,2,cumsum)), type='l')

  1.  
    # 可视化分布的辅助函数
  2.  
     
  3.  
    # 检查Copula拟合的Helper函数
  4.  
    # 模拟图与实际图
  5.  
     
  6.  
    plot(rets[,1], rets[,2], xlab=labs[1], ylab=labs[2], col='blue', las=1)
  7.  
    points(fit.sim[,1], fit.sim[,2], col='red')
  8.  
     
  9.  
    # 比较模拟和实际的统计数据
  10.  
    temp = matrix(0,nr=5,nc=2)
  11.  
     
  12.  
    print(round(100*temp,2))
  13.  
     
  14.  
    # 检查收益率是否来自相同的分布
  15.  
    for (i in 1:2) {
  16.  
    print(labs[i])
  17.  
    print(ks.test(rets[,i], fit.sim[i]))
  18.  
     
  19.  
     
  20.  
    # 绘制模拟价格路径
  21.  
    matplot(exp(apply(fit.sim,2,cumsum)), type='l', main='Simulated Price path')
  22.  
     
  23.  
     
  24.  
    # 拟合Copula
  25.  
    load.packages('copula')
  1.  
     
  2.  
    # 通过组合拟合边缘和拟合copula创建自定义分布
  3.  
    margins=c("norm","norm")
  4.  
    apply(rets,2,function(x) list(mean=mean(x), sd=sd(x)))
  5.  
     
  6.  
     
  7.  
    # 从拟合分布模拟
  8.  
    rMvdc(4800, fit)
  9.  
     

  1.  
    Actual Simulated
  2.  
    Correlation 57.13 57.38
  3.  
    Mean FB -0.31 -0.47
  4.  
    Mean YHOO -0.40 -0.17
  5.  
    StDev FB 1.24 1.25
  6.  
    StDev YHOO 1.23 1.23

FB

  1.  
    Two-sample Kolmogorov-Smirnov test
  2.  
     
  3.  
    data: rets[, i] and fit.sim[i]
  4.  
    D = 0.9404, p-value = 0.3395
  5.  
    alternative hypothesis: two-sided
  6.  
     

HO

  1.  
    Two-sample Kolmogorov-Smirnov test
  2.  
     
  3.  
    data: rets[, i] and fit.sim[i]
  4.  
    D = 0.8792, p-value = 0.4222
  5.  
    alternative hypothesis: two-sided
  6.  
     

 visualize.rets(fit.sim)

  1.  
    # qnorm(runif(10^8)) 和 rnorm(10^8) 是等价的
  2.  
    uniform.sim = rCopula(4800, gumbelCopula(gumbel@estimate, dim=n))
  3.  
     

  1.  
    Actual Simulated
  2.  
    Correlation 57.13 57.14
  3.  
    Mean FB -0.31 -0.22
  4.  
    Mean YHOO -0.40 -0.56
  5.  
    StDev FB 1.24 1.24
  6.  
    StDev YHOO 1.23 1.21

FB

  1.  
    Two-sample Kolmogorov-Smirnov test
  2.  
     
  3.  
    data: rets[, i] and fit.sim[i]
  4.  
    D = 0.7791, p-value = 0.5787
  5.  
    alternative hypothesis: two-sided
  6.  
     

HO

  1.  
    Two-sample Kolmogorov-Smirnov test
  2.  
     
  3.  
    data: rets[, i] and fit.sim[i]
  4.  
    D = 0.795, p-value = 0.5525
  5.  
    alternative hypothesis: two-sided
  6.  
     

	vis(rets)

标准偏差相对于均值而言非常大,接近于零;因此,在某些情况下,我们很有可能获得不稳定的结果。


 

最受欢迎的见解

1.R语言基于ARMA-GARCH-VaR模型拟合和预测实证研究

2.r语言实现copula算法建模依赖性案例

3.R语言COPULAS和金融时间序列数据VaR分析

4.R语言多元COPULA GARCH 模型时间序列预测

5.GARCH(1,1),MA以及历史模拟法的VaR比较

6.matlab使用Copula仿真优化市场风险数据分析

7.R语言实现向量自动回归VAR模型

8.R语言随机搜索变量选择SSVS估计贝叶斯向量自回归(BVAR)模型

9.R语言VAR模型的不同类型的脉冲响应分析

 
 
▍关注我们 【大数据部落】第三方数据服务提供商,提供全面的统计分析与数据挖掘咨询服务,为客户定制个性化的数据解决方案与行业报告等。 ▍咨询链接:http://y0.cn/teradat ▍联系邮箱:3025393450@qq.com
原文地址:https://www.cnblogs.com/tecdat/p/14396964.html