2019年3月12日火曜日

VIX vs. CLI 6 month delta. translucent histgram hist setdiff


# downloads ^VIX historical data from Yahoo Finance.
#
VIX <- as.xts(read.zoo(read.csv("~/VIX.csv")))

# select dates when CLI moves negative during 6 months.
mnt <- index(cli_xts$oecd["2000::2018"][cli_xts$oecd["2000::2018"]/as.vector(cli_xts$oecd["1999-07-01::2018-06-01"]) < 1])

  [1] "2000-06-01" "2000-07-01" "2000-08-01" "2000-09-01" "2000-10-01" "2000-11-01" "2000-12-01" "2001-01-01"
  [9] "2001-02-01" "2001-03-01" "2001-04-01" "2001-05-01" "2001-06-01" "2001-07-01" "2001-08-01" "2001-09-01"
<skip>
[105] "2018-05-01" "2018-06-01" "2018-07-01" "2018-08-01" "2018-09-01" "2018-10-01" "2018-11-01" "2018-12-01"

# select dates of positve movement.
> as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))
  [1] "2000-01-01" "2000-02-01" "2000-03-01" "2000-04-01" "2000-05-01" "2001-12-01" "2002-01-01" "2002-02-01"
  [9] "2002-03-01" "2002-04-01" "2002-05-01" "2002-06-01" "2002-07-01" "2003-06-01" "2003-07-01" "2003-08-01"
<skip>
[105] "2017-03-01" "2017-04-01" "2017-05-01" "2017-06-01" "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01"
[113] "2017-11-01" "2017-12-01" "2018-01-01" "2018-02-01"


as.vector(VIX[,2][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))])
#   [1] 29.00 28.12 25.87 34.31 32.89 26.38 26.88 27.32 21.12 24.50 22.71 30.98 48.46 22.81 20.80 23.89 23.26 22.82 19.61
#  [20] 18.86 18.68 18.06 22.67 17.98 20.45 17.04 14.39 17.19 15.66 12.44 14.56 13.73 13.34 13.09 19.87 23.81 19.58 16.15
#  [39] 14.49 12.91 12.55 12.68 12.83 19.01 21.25 15.46 14.60 18.98 24.17 37.50 32.77 33.05 28.39 29.57 31.59 31.84 24.51
#  [58] 28.01 29.22 19.94 23.20 48.20 37.38 37.58 28.92 25.13 24.34 23.84 21.43 20.08 23.22 31.28 19.07 21.24 21.06 25.46
#  [77] 15.93 19.28 16.82 18.20 16.35 21.91 17.32 17.81 17.49 21.34 14.14 16.75 18.99 21.48 18.22 17.85 14.49 12.89 25.20
#  [96] 23.43 22.81 14.93 20.51 17.95 23.01 14.72 14.07 12.96 15.11 16.28 16.30 15.16 13.05 17.28 14.06 13.20 14.51 14.58
# [115] 15.42 50.30
# > as.vector(VIX[,2][mnt])
#   [1] 25.01 21.65 20.84 22.66 30.80 31.11 32.32 30.80 30.62 35.45 35.20 26.49 24.42 25.61 25.84 49.35 36.95 34.57 45.21
#  [20] 41.86 43.44 32.60 31.20 35.33 35.66 34.40 30.04 22.33 17.93 19.97 15.98 16.87 16.76 13.74 14.75 13.20 14.89 18.59
#  [39] 17.70 13.34 13.92 14.41 28.82 24.15 31.09 24.86 37.57 29.70 35.60 25.61 20.95 24.56 30.81 23.86 48.40 89.53 81.48
#  [58] 68.60 57.36 53.16 53.25 45.60 36.88 20.03 24.65 25.94 48.00 43.87 46.88 37.53 30.91 23.73 21.98 27.73 21.00 19.25
#  [77] 18.96 19.65 19.40 23.23 17.11 17.57 17.08 31.06 15.93 17.19 16.66 16.36 19.80 20.05 53.29 33.82 25.23 20.67 26.81
#  [96] 32.09 30.90 20.17 17.09 17.65 26.72 17.04 26.22 25.72 18.78 19.61 18.08 16.86 15.63 28.84 23.81 36.20

# compare monthly high between negative vs. positve
t.test(as.vector(VIX[,2][mnt]),as.vector(VIX[,2][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]))

# Welch Two Sample t-test
#
# data:  as.vector(VIX[, 2][mnt]) and as.vector(VIX[, 2][as.Date(setdiff(seq(as.Date("2000-01-01"), as.vector(VIX[, 2][mnt]) and     as.Date("2018-12-01"), by = "months"), mnt))])
# t = 4.8495, df = 174.7, p-value = 2.725e-06
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  4.164086 9.879529
# sample estimates:
# mean of x mean of y
#  28.55741  21.53560

# compare monthly close.
t.test(as.vector(VIX[,4][mnt]),as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]))

# Welch Two Sample t-test
#
# data:  as.vector(VIX[, 4][mnt]) and as.vector(VIX[, 4][as.Date(setdiff(seq(as.Date("2000-01-01"), as.vector(VIX[, 4][mnt]) and     as.Date("2018-12-01"), by = "months"), mnt))])
# t = 4.7722, df = 178.21, p-value = 3.781e-06
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#  2.860369 6.893837
# sample estimates:
# mean of x mean of y
#  22.19598  17.31888

par(mfrow=c(2,1))

hist(as.vector(VIX[,4][mnt]),ylim=c(0,40),xlim=c(10,60),breaks=20)
hist(as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]),ylim=c(0,40),xlim=c(10,60),breaks=10)

# 半透明色ヒストグラム

hist(as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]),ylim=c(0,40),xlim=c(10,60),breaks=10,col=2)
par(new=T)
hist(as.vector(VIX[,4][mnt]),ylim=c(0,40),xlim=c(10,60),breaks=20,col=rgb(0, 1, 0, alpha=0.1))

                              OR

hist(as.vector(VIX[,4][as.Date(setdiff(seq(as.Date("2000-01-01"),as.Date("2018-12-01"),by='months'),mnt))]),ylim=c(0,30),xlim=c(10,60),breaks=10,col=rgb(1, 0, 0, alpha=0.5))
par(new=T)
hist(as.vector(VIX[,4][mnt]),ylim=c(0,30),xlim=c(10,60),breaks=20,col=rgb(0, 0, 1, alpha=0.1))

Red for positive and Blue for negative.



change color scheme and transparency. 





0 件のコメント: