2019年10月30日水曜日

calculate S&P 500 n month return.

w <- c()
for(i in seq(1,13,1)){
 m <- matrix(as.vector(to.monthly(SP5)[,4])[i:(i+818)],nrow=13,ncol=63)
  w <- append(
    w,m[13,] / m[1,]

  )
}


gap <- 61 # when month gap is 61.
a<-Sys.time()
w <- c()
j <- (floor(length(index(to.monthly(SP5)))/gap)-1)*gap-1
for(i in seq(1,gap,1)){
#  m <- matrix(as.vector(to.monthly(SP5)[,4])[i:(i+j)],nrow=gap,ncol=(floor(length(index(to.monthly(SP5)))/gap)-1))
m <- matrix(as.vector(to.monthly(SP5)[,4])[i:(i+j)],nrow=gap,ncol=((j+1)/gap))
  w <- append(
    w,m[gap,] / m[1,]
  )
}

s <- as.vector(matrix(t(matrix(w,ncol=gap)),ncol=1))

length(s[s<1])
mean(s[s<1])
mean(s[s>1])
length(s[s>1])
b<-Sys.time()
b -a

# テスト用サンプル gap = 61のとき有効。
# > s[1:60]
#  [1] 2.148387 2.134727 2.115674 2.113586 2.018637 2.319389 2.439462 2.344191 2.245244 2.167947
# [11] 2.332650 2.226138 2.023084 2.079817 2.256983 2.156933 2.100372 2.240935 2.204911 2.040807
# [21] 1.949699 1.986922 1.970280 1.963399 1.852527 1.859845 1.810012 1.961407 1.987846 1.897837
# [31] 1.886220 1.806632 1.728606 1.674551 1.625877 1.505081 1.580743 1.576834 1.664689 1.764419
# [41] 1.796658 1.874068 1.906667 2.047599 2.143897 2.091687 2.119548 2.225312 2.126150 2.118929
# [51] 2.057906 2.037863 2.010277 2.001712 1.959521 1.997989 1.760446 1.815657 1.702103 1.664536
# > as.vector(to.monthly(SP5)[,4])[61:120]/as.vector(to.monthly(SP5)[,4])[1:60]
#  [1] 2.148387 2.134727 2.115674 2.113586 2.018637 2.319389 2.439462 2.344191 2.245244 2.167947
# [11] 2.332650 2.226138 2.023084 2.079817 2.256983 2.156933 2.100372 2.240935 2.204911 2.040807
# [21] 1.949699 1.986922 1.970280 1.963399 1.852527 1.859845 1.810012 1.961407 1.987846 1.897837
# [31] 1.886220 1.806632 1.728606 1.674551 1.625877 1.505081 1.580743 1.576834 1.664689 1.764419
# [41] 1.796658 1.874068 1.906667 2.047599 2.143897 2.091687 2.119548 2.225312 2.126150 2.118929
# [51] 2.057906 2.037863 2.010277 2.001712 1.959521 1.997989 1.760446 1.815657 1.702103 1.664536

# > s[101:160]
#  [1] 1.605806 1.533378 1.464929 1.518325 1.432281 1.441847 1.395389 1.358812 1.389360 1.404079
# [11] 1.424603 1.379753 1.369632 1.397127 1.374649 1.372987 1.479958 1.475313 1.448524 1.415094
# [21] 1.574537 1.557912 1.556921 1.638955 1.583736 1.477864 1.535759 1.530372 1.680867 1.731036
# [31] 1.649442 1.590604 1.503399 1.437894 1.371503 1.394273 1.294020 1.310953 1.252247 1.132658
# [41] 1.147310 1.168755 1.128015 1.122711 1.258135 1.240423 1.296909 1.440987 1.493879 1.655525
# [51] 1.627168 1.583897 1.718678 1.650743 1.509798 1.528843 1.393354 1.389952 1.354965 1.396275
# > as.vector(to.monthly(SP5)[,4])[161:220]/as.vector(to.monthly(SP5)[,4])[101:160]
#  [1] 1.605806 1.533378 1.464929 1.518325 1.432281 1.441847 1.395389 1.358812 1.389360 1.404079
# [11] 1.424603 1.379753 1.369632 1.397127 1.374649 1.372987 1.479958 1.475313 1.448524 1.415094
# [21] 1.574537 1.557912 1.556921 1.638955 1.583736 1.477864 1.535759 1.530372 1.680867 1.731036
# [31] 1.649442 1.590604 1.503399 1.437894 1.371503 1.394273 1.294020 1.310953 1.252247 1.132658
# [41] 1.147310 1.168755 1.128015 1.122711 1.258135 1.240423 1.296909 1.440987 1.493879 1.655525
# [51] 1.627168 1.583897 1.718678 1.650743 1.509798 1.528843 1.393354 1.389952 1.354965 1.396275

when gap = 61,

> length(s[s<1])
[1] 137
> mean(s[s<1])
[1] 0.8869793
> mean(s[s>1])
[1] 1.629065
> length(s[s>1])
[1] 595

when gap = 14,

> length(s[s<1])
[1] 216
> mean(s[s<1])
[1] 0.8863247
> mean(s[s>1])
[1] 1.160806
> length(s[s>1])
[1] 602
> b<-Sys.time()

2019年10月28日月曜日

CLI delta plus period graph.


# draw wp graph starts.


wpx <- wp

wp <- wp[wp[,2] > 10]

par(mfrow=c(2,3))
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),1)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),2)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),3)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),4)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),5)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),6)[1]],main="")




plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),7)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),8)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),9)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),10)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),11)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),12)[1]],main="")




plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),13)[1]],main="")
plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),14)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),15)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),16)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),17)[1]],main="")
# plot(to.monthly(SP5[,4])[,4][last(paste(  substr(mondate(index(wp)) - as.vector(wp[,2]-1),1,7), substr(mondate(index(wp)),1,7),sep="::"),18)[1]],main="")


par(mfrow=c(1,1))
wp <- wpx
remove(wpx)


# draw wp graph ends.






2019年10月24日木曜日

総力戦

異次元緩和はともかく、ここ10年ほど世界中で資産の価格は上がるが労働者の賃金は全然上がらない。労働と資本の間のパワーバランスが崩れて、資本が決定的な優位を占める状態となっている。(続

総力戦体制が福祉国家を要求したが、一旦は実現された福祉国家自体が持続可能な国家モデルとしての妥当性を欠いていることが明らかになると同時に、総力戦は時代遅れで不可能になった。今の時代に誰が総力戦を戦うことを真剣に考えているだろうか?(続

同時に、技術進歩は経済を資本集約的なものに変化させた。総力戦に必須だった忠誠心も持ち熟練した技能を持つ大量の労働者はもはや必要とされていない。練度の低い、規格化した労働を、その時々の要求に応じてこなすだけの大衆の群れが残った。(続

今でも必要とされる熟練労働者は高度な教育を受けた知識労働者だ。しかし、彼らは本質的には一種の資本財である。知識集約型産業は形を変えた資本集約型産業に過ぎない。現代の知識労働者は付加価値生産のために莫大な設備投資を必要とする。コンピュータを持たないプログラマーになんの価値があるか?(続

知識と設備、両者のどちらがより本質的かという問いは極めてトリビアルなものと言わざるを得ない。かくして、経済の焦点が労働から資本に移動した以上、資産の価格は上がるが労働者の賃金は全然上がらないのは自然なことである。(続

これらはいずれも本質的な変化なので、所得税の累進割合を変えたり、資産課税を導入したり小手先の政策をいくらいじっても事態の本質は変わらない。今更総力戦を戦いたい人間はいない。しかし、総力戦なき福祉国家の漂流を止めることも誰にもできない。(終

2019年10月8日火曜日

MOV and VIX on each months.



# 各月のCOVを計算して棒グラフにする。
# COVのデータを遡って、12で割り切れる最長のデータ長を計算する。
# データ長に基づいて、配列を作成し、それを数=12の行列に代入する。(行数=12でなくてはならない。注意!)
# apply(行列,1,mean)を実行して、月ごとの平均を計算する。
# 最終COVの月を計算して、データの並びを変える。最終月が10月なら、3,4,5,6,7,8,9,10,11,12,1,2となるように並べる。



df <- data.frame(cov=apply(matrix(last(COV,floor(length(COV)/12)*12),nrow=12),1,mean)[c(last(seq(1,12,1),month(last(index(COV)))),seq(1,(12 - month(last(index(COV)))),1))],
month=factor(seq(1,12,1)))
p <- ggplot(df,aes(x=month,fill=month))
p <- p + geom_bar(aes(y=cov),stat='identity')
p <- p + scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))  #x-axis label
p <- p + theme(legend.position = 'none')  # erase legend
plot(p)





df <- data.frame(cov=apply(matrix(last(VIX[,4],floor(length(VIX[,4])/12)*12),nrowncol=12),12,mean)[c(last(seq(1,12,1),month(last(index(VIX[,4])))),seq(1,(12 - month(last(index(VIX[,4])))),1))],
month=factor(seq(1,12,1)))
p <- ggplot(df,aes(x=month,fill=month))
p <- p + geom_bar(aes(y=cov),stat='identity')
p <- p + scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))  #x-axis label
p <- p + theme(legend.position = 'none')  # erase legend
plot(p)



2019年10月7日月曜日

EPS 2019OCT07

$cat eps.txt 

12/31/2020 $47.81 $44.23 16.53 18.00 $180.06 $165.38
9/30/2020 $46.25 $42.60 17.06 18.62 $174.46 $159.83
6/30/2020 $44.53 $40.53 17.61 19.29 $169.02 $154.31
3/31/2020 $41.47 $38.02 18.08 20.02 $164.63 $148.71
12/31/2019 $42.21 $38.69 18.47 20.43 $161.15 $145.71
9/30/2019 2976.74 $40.81 $37.08 19.33 21.89 $153.97 $135.99
6/30/2019 (97.7%) 2941.76 $40.14 $34.93 19.26 22.01 $154.54 $135.27

3/31/2019 2834.40 $37.99 $35.02 18.76 21.30 $153.05 $134.39

# use tac command to invert eps.txt.



$ tac eps.txt 

3/31/2019 2834.40 $37.99 $35.02 18.76 21.30 $153.05 $134.39
6/30/2019 (97.7%) 2941.76 $40.14 $34.93 19.26 22.01 $154.54 $135.27
9/30/2019 2976.74 $40.81 $37.08 19.33 21.89 $153.97 $135.99
12/31/2019 $42.21 $38.69 18.47 20.43 $161.15 $145.71
3/31/2020 $41.47 $38.02 18.08 20.02 $164.63 $148.71
6/30/2020 $44.53 $40.53 17.61 19.29 $169.02 $154.31
9/30/2020 $46.25 $42.60 17.06 18.62 $174.46 $159.83
12/31/2020 $47.81 $44.23 16.53 18.00 $180.06 $165.38

$ tac eps.txt | awk '{gsub("\\$","",$NF);print "eps_year_xts[\"2019::\"]["NR"] <- "$NF}'

eps_year_xts["2019::"][1] <- 134.39
eps_year_xts["2019::"][2] <- 135.27
eps_year_xts["2019::"][3] <- 135.99
eps_year_xts["2019::"][4] <- 145.71
eps_year_xts["2019::"][5] <- 148.71
eps_year_xts["2019::"][6] <- 154.31
eps_year_xts["2019::"][7] <- 159.83
eps_year_xts["2019::"][8] <- 165.38


> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.39
2019-04-01 135.44
2019-07-01 136.41
2019-10-01 146.35
2020-01-01 149.59
2020-04-01 155.21
2020-07-01 160.75
2020-10-01 166.45
> eps_year_xts["2019::"][1] <- 134.39
> eps_year_xts["2019::"][2] <- 135.27
> eps_year_xts["2019::"][3] <- 135.99
> eps_year_xts["2019::"][4] <- 145.71
> eps_year_xts["2019::"][5] <- 148.71
> eps_year_xts["2019::"][6] <- 154.31
> eps_year_xts["2019::"][7] <- 159.83
> eps_year_xts["2019::"][8] <- 165.38
> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.39
2019-04-01 135.27
2019-07-01 135.99
2019-10-01 145.71
2020-01-01 148.71
2020-04-01 154.31
2020-07-01 159.83
2020-10-01 165.38