TOPIXと個別銘柄の価格
前回「2銘柄の価格」で行ったことをTOPIXと個別銘柄で行う。
TOPIX株価取得。
> YJ998405_20130101_20140101<-getSymbols("YJ998405", src="yahooj", auto.assign=FALSE, from='2013-01-01', to='2014-01-01') > head(YJ998405_20130101_20140101) YJ998405.Open YJ998405.High YJ998405.Low YJ998405.Close 2013-01-04 876.97 888.53 876.83 888.51 2013-01-07 895.11 895.14 879.22 881.06 2013-01-08 877.58 881.49 868.35 871.88 2013-01-09 863.19 882.29 862.62 879.05 2013-01-10 884.63 892.26 883.33 889.02 2013-01-11 899.32 901.98 895.13 898.69
個別銘柄は「日本水産」の銘柄を取得する。
> YJ1332_20130101_20140101<-getSymbols("YJ1332", src="yahooj", auto.assign=FALSE, from='2013-01-01', to='2014-01-01') > head(YJ1332_20130101_20140101) YJ1332.Open YJ1332.High YJ1332.Low YJ1332.Close YJ1332.Volume YJ1332.Adjusted 2013-01-04 182 183 181 183 1464100 183 2013-01-07 185 185 180 180 1783500 180 2013-01-08 180 181 178 180 1759800 180 2013-01-09 178 180 177 179 767800 179 2013-01-10 181 182 179 181 1503100 181 2013-01-11 183 185 182 185 2864200 185
それぞれの銘柄の前日との価格差を計算。
> prices<-cbind(YJ1332_20130101_20140101$YJ1332.Adjusted, YJ998405_20130101_20140101$YJ998405.Close) > head(prices) YJ1332.Adjusted YJ998405.Close 2013-01-04 183 888.51 2013-01-07 180 881.06 2013-01-08 180 871.88 2013-01-09 179 879.05 2013-01-10 181 889.02 2013-01-11 185 898.69 > price_changes<-apply(prices, 2, diff) > head(price_changes) YJ1332.Adjusted YJ998405.Close 2013-01-07 -3 -7.45 2013-01-08 0 -9.18 2013-01-09 -1 7.17 2013-01-10 2 9.97 2013-01-11 4 9.67 2013-01-15 4 7.53 >
プロットする。
plot(price_changes[,1], price_changes[,2], xlab="1332 NihonSuisan price changes", ylab="998405 TOPIX price changes", main="TOPIX vs NihonSuisan", cex.main=0.8, cex.lab=0.8, cex.axis=0.8,xlim=c(-100,100), ylim=c(-100,100))
「個別銘柄のリターン=β*(TOPIXのリターン)」を知りたい。
> price_changes<-apply(log(prices), 2, diff) > head(price_changes) YJ1332.Adjusted YJ998405.Close 2013-01-07 -0.016529302 -0.008420174 2013-01-08 0.000000000 -0.010473928 2013-01-09 -0.005571045 0.008189979 2013-01-10 0.011111225 0.011277954 2013-01-11 0.021858794 0.010818415 2013-01-15 0.021391190 0.008343955
プロットする。
>plot(price_changes[,2], price_changes[,1], ylab="1332 NihonSuisan return", xlab="998405 TOPIX return", main="TOPIX vs NihonSuisan", cex.main=0.8, cex.lab=0.8, cex.axis=0.8,xlim=c(-0.2,0.2), ylim=c(-0.2,0.2))
線形回帰。「個別銘柄のリターン=α+β*(TOPIXのリターン)+誤差項」の線形回帰。
> reg<-lm(price_changes[,1]~price_changes[,2]) > beta<-reg$coefficients[2] > beta price_changes[, 2] 1.02462 > abline(0,beta, col="red")
同様のことを「(株)三菱UFJフィナンシャル・グループ」について行う。
> YJ8306_20130101_20140101<-getSymbols("YJ8306", src="yahooj", auto.assign=FALSE, from='2013-01-01', to='2014-01-01') > head(YJ8306_20130101_20140101) YJ8306.Open YJ8306.High YJ8306.Low YJ8306.Close YJ8306.Volume YJ8306.Adjusted 2013-01-04 477 485 474 484 119113900 484 2013-01-07 487 488 475 476 86678900 476 2013-01-08 469 471 457 462 117835800 462 2013-01-09 451 472 449 469 122409400 469 2013-01-10 474 484 472 474 158715200 474 2013-01-11 482 485 476 479 102374600 479
リターンの計算。
> prices<-cbind(YJ8306_20130101_20140101$YJ8306.Adjusted, YJ998405_20130101_20140101$YJ998405.Close) > head(prices) YJ8306.Adjusted YJ998405.Close 2013-01-04 484 888.51 2013-01-07 476 881.06 2013-01-08 462 871.88 2013-01-09 469 879.05 2013-01-10 474 889.02 2013-01-11 479 898.69 > returns<-apply(log(prices) ,2 ,diff) > head(returns) YJ8306.Adjusted YJ998405.Close 2013-01-07 -0.016667052 -0.008420174 2013-01-08 -0.029852963 -0.010473928 2013-01-09 0.015037877 0.008189979 2013-01-10 0.010604553 0.011277954 2013-01-11 0.010493276 0.010818415 2013-01-15 0.008316056 0.008343955
線形回帰。
> reg<-lm(returns[,1] ~ returns[,2]) > beta<-reg$coefficients[2] > beta returns[, 2] 1.230943
プロットする。
> plot(returns[,2], returns[,1], ylab="8306 UFJ return", xlab="998405 TOPIX return", main="TOPIX vs UFJ", cex.main=0.8, cex.lab=0.8, cex.axis=0.8,xlim=c(-0.2,0.2), ylim=c(-0.2,0.2)) > abline(0, beta, col="red")