見出し画像

ggplot spx vs. vix diff and SPX vs. DXY

概要

  • x軸にvix の変化率、y軸にs&p500 の収益率を置いたグラフ。

  • 色でvixの値を表現している。scale_color_gradient()を使用。

  • 回帰直線付き。lm()およびloess()をそれぞれ使用。

サンプルコードその1

週次データを対象

m <- merge(weeklyReturn(GSPC)["2011::"], weeklyReturn(VIX)["2011::"][index(weeklyReturn(GSPC)["2011::"])], apply.weekly(VIX[,4],mean)[index(weeklyReturn(GSPC)["2011::"])])
colnames(m) <- c('spx','vixdiff','vix')
df <- m
p <- ggplot(df, aes(x=vixdiff,y=spx,color=vix))
p <- p + geom_point(alpha=1)
p <- p + scale_color_gradient(low = "red", high = "green",name = "vix")
p <- p + stat_smooth(aes(x=vixdiff,y=spx),method = lm, formula = y ~ x,se=T)
# p <- p + scale_size(name = "spx return")
plot(p)

サンプルコードその2

日次データ対象。最終データを「◇」でマーク。

m <- merge(dailyReturn(GSPC)["2021::"], dailyReturn(VIX)["2020::"][index(dailyReturn(GSPC)["2021::"])], VIX[,4][index(dailyReturn(GSPC)["2021::"])])
# m <- merge(weeklyReturn(GSPC)["2011::"], weeklyReturn(VIX)["2011::"][index(weeklyReturn(GSPC)["2011::"])], apply.weekly(VIX[,4],mean)[index(weeklyReturn(GSPC)["2011::"])])
colnames(m) <- c('spx','vixdiff','vix')
df <- m
p <- ggplot(df, aes(x=vixdiff,y=spx,color=vix))
p <- p + geom_point(alpha=1)
p <- p + scale_color_gradient(low = "red", high = "green",name = "vix")
p <- p + stat_smooth(aes(x=vixdiff,y=spx),method = lm, formula = y ~ x,se=T)
# p <- p + geom_point(aes(x=last(df$vixdiff),y=last(df$spx)))
# p <- p + scale_shape_manual(values=c(18))
# p <- p + scale_color_manual(values=c("black"))
# p <- p + scale_size(name = "spx return")
p <- p + annotate("text", x=last(df$vixdiff),y=last(df$spx), label = "◇",family = "HiraKakuProN-W3",alpha=1,color='red')

p <- p + theme_gray (base_family = "HiraKakuPro-W3")
# p <- p + guides(size = guide_legend(title="接種総数"))
p <- p + guides(color = guide_legend(title="VIX"))
p  <- p + scale_color_gradient( low = "#00FF00",high = "#FF0000")
p <- p + xlab("VIX日次変化率") + ylab("SPX日次収益率")
plot(p)
png("~/Dropbox/R-script/covid/w.png", width = 1200, height = 1000)
plot(p)
dev.off()

出力例 サンプルコードその2

サンプルコードその2 修正版

最終エントリ上下10%以内のデータのみを彩色するよう変更。
また、回帰分析対象も同様とした。

m <- merge(dailyReturn(GSPC)["2021::"], dailyReturn(VIX)["2020::"][index(dailyReturn(GSPC)["2021::"])], VIX[,4][index(dailyReturn(GSPC)["2021::"])])
# m <- merge(weeklyReturn(GSPC)["2011::"], weeklyReturn(VIX)["2011::"][index(weeklyReturn(GSPC)["2011::"])], apply.weekly(VIX[,4],mean)[index(weeklyReturn(GSPC)["2011::"])])
colnames(m) <- c('spx','vixdiff','vix')
df <- m
p <- ggplot(df) 
p <- p + geom_point(aes(x=vixdiff,y=spx),alpha=0.5,color='grey')

df <- dplyr::filter(as.data.frame(df),vix > as.numeric(last(df$vix))*0.9 & vix <  as.numeric(last(df$vix))*1.1)
p <- p + geom_point(data=df,alpha=1,aes(x=vixdiff,y=spx,color=vix))
p <- p + scale_color_gradient(low = "red", high = "green",name = "vix")
p <- p + stat_smooth(data=df,aes(x=vixdiff,y=spx),method = lm, formula = y ~ x,se=T)
p <- p + annotate("text", x=last(df$vixdiff),y=last(df$spx), label = "◇",family = "HiraKakuProN-W3",alpha=1,color='red')
p <- p + theme_gray (base_family = "HiraKakuPro-W3")
p <- p + guides(color = guide_legend(title="VIX"))
p  <- p + scale_color_gradient( low = "#00FF00",high = "#FF0000")
p <- p + xlab("VIX日次変化率") + ylab("SPX日次収益率")
plot(p)

サンプルコードその2修正版その2

色スケールを三段階にするため scale_color_gradient2 を使用。
回帰分析手法を loess に変更。

m <- merge(dailyReturn(GSPC)["2021::"], dailyReturn(VIX)["2020::"][index(dailyReturn(GSPC)["2021::"])], VIX[,4][index(dailyReturn(GSPC)["2021::"])])
# m <- merge(weeklyReturn(GSPC)["2011::"], weeklyReturn(VIX)["2011::"][index(weeklyReturn(GSPC)["2011::"])], apply.weekly(VIX[,4],mean)[index(weeklyReturn(GSPC)["2011::"])])
colnames(m) <- c('spx','vixdiff','vix')
df <- m
p <- ggplot(df, aes(x=vixdiff,y=spx,color=vix))
p <- p + geom_point(alpha=1)
p <- p + stat_smooth(aes(x=vixdiff,y=spx),method = loess, formula = y ~ x,se=F)
p <- p + annotate("text", x=last(df$vixdiff),y=last(df$spx), label = "◇",family = "HiraKakuProN-W3",alpha=1,color='red')

p <- p + theme_gray (base_family = "HiraKakuPro-W3")
p <- p + guides(color = guide_legend(title="VIX"))
p  <- p + scale_color_gradient2( low = "#FF0000",mid="#00FF00" , high = "#0000FF",midpoint=26)
p <- p + xlab("VIX日次変化率") + ylab("SPX日次収益率")
plot(p)


サンプルコードその3

spx日次収益率 vs. vix
stat_smooth()には"loess"を使用している。

m <- merge(dailyReturn(GSPC)["2021::"], dailyReturn(VIX)["2020::"][index(dailyReturn(GSPC)["2021::"])], VIX[,4][index(dailyReturn(GSPC)["2021::"])])
colnames(m) <- c('spx','vixdiff','vix')
df <- m
p <- ggplot(df, aes(x=vix,y=spx,color=vixdiff))
p <- p + geom_point(alpha=1)
p <- p + scale_color_gradient2(low = "red", mid='green',high = "blue",midpoint = 0.2,name='vix daily change')
p <- p + stat_smooth(aes(x=vix,y=spx),method = loess, formula = y ~ x,se=T)
p <- p + theme_dark(base_family = "HiraKakuPro-W3")
p <- p + annotate("text", x=last(df$vix),y=last(df$spx), label = "◇",family = "HiraKakuProN-W3",alpha=1,color='red')
plot(p)

出力例 サンプルコードその3


codes obsoleted

m <- merge(dailyReturn(GSPC)["2011::"], dailyReturn(VIX)["2010::"][index(dailyReturn(GSPC)["2011::"])], VIX[,4][index(dailyReturn(GSPC)["2011::"])])
colnames(m) <- c('spx','vixdiff','vix')
df <- m
p <- ggplot(df, aes(x=vix,y=spx,color=vixdiff))
p <- p + geom_point(alpha=1)
p <- p + scale_color_gradient(low = "red", high = "green",name = "vixdiff")
p <- p + stat_smooth(aes(x=vix,y=spx),method = loess, formula = y ~ x,se=T)
plot(p)

続き


いいなと思ったら応援しよう!