2019年2月22日金曜日

短歌



暗黒を
 一人彷徨う
  煉獄に
せめて月光の
 欠片が欲しい

(暗きより 
 暗き道にぞ 
  入りぬべき 
はるかに照らせ 
 山の端の月) 

影を友
 星を標に
  旅をする  
世界の果てを
 何もないのに

なにがあっても
 ほんとうのことを
  いわないと
   こころにきめた
    じゅうはちのはる

何があっても
 本当のことを
  言わないと
心に決めた
 十八の春

さけのみは
  らべるさかなに
     さけのむと
ばーのあるじは
  しずかにわらう

酒飲みは
 ラベル肴に
  酒飲むと
バーの女主人は
 静かに笑う

世の中の
 誰も接続
  せぬ心
持ちて生まれて
 楽しかったよ

日々の泡
 濾過して見れば
  手の中に
背景放射
 の跡のみ残る
 
訃報有り
 我をこの世に
  繋とむ
記憶の一つ
 消えた心地す

訃報あり
 この先交差
  不可能の
世界線など
 数えたくなる

2019/8/14

あの女は
 帰り来て告ぐ
  夢の中
暑い日が好き
 煉獄の夏

↓のほうが良いか。。

あの女は
 帰り来て告ぐ
  夢の中
夏の盛りの
 暑い日が好き
 
 
  

2019年2月19日火曜日

マカオ カジノ収入合計年次データ maca casino annual revenue.


マカオ カジノ収入合計年次データ maca casino annual revenue.
単位 million MOP.


  • 2003   28,672
  • 2004   41,378
  • 2005   46,047
  • 2006   56,623
  • 2007   83,022
  • 2008 108,772
  • 2009 119,369
  • 2010 188,343
  • 2011 267,867
  • 2012 304,139
  • 2013 360,749
  • 2014 351,521
  • 2015 230,840
  • 2016 223,210
  • 2017 265,743
  • 2018 302,846

CLI vs. 1 month delta + legend



plot.default(diff(cli_xts$oecd["2014-12-01::"])[-1],cli_xts$oecd["2015::"],type='b')
tmp <- par('usr')
plot.default(diff(cli_xts$oecd["2014-12-01::"])[-1],cli_xts$oecd["2015::"],type='b',xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]))
par(new=T)
plot.default(diff(cli_xts$oecd["2016-12-01::2017"])[-1],cli_xts$oecd["2017"],xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=3,lwd=2)
par(new=T)
plot.default(diff(cli_xts$oecd["2015-12-01::2016"])[-1],cli_xts$oecd["2016"],xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=4,lwd=2)
par(new=T)
plot.default(diff(cli_xts$oecd["2015-12-01::2016"])[-1],cli_xts$oecd["2016"],xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=5,lwd=2)
par(new=T)
plot.default(diff(cli_xts$oecd["2014-12-01::2015"])[-1],cli_xts$oecd["2015"],xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=6,lwd=2)
par(new=T)
plot.default(diff(cli_xts$oecd["2017-12-01::"])[-1],cli_xts$oecd["2018"],xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=2,lwd=2)
#
#    belows are for the case the data after 2019 are released. comment out 2 ines above, and remove comment below 4 lines.
#
# par(new=T)
# plot.default(diff(cli_xts$oecd["2017-12-01::2018"])[-1],cli_xts$oecd["2018"],xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=7,lwd=2)
# par(new=T)
# plot.default(diff(cli_xts$oecd["2018-12-01::"])[-1],cli_xts$oecd["2019"],xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=2,lwd=2)
#
abline(v=0)
abline(h=100)
legend("topleft", legend = "Pink: 2015\nLight Blue: 2016\nLime: 2017\nRed: 2018",bty='n')



2019年2月17日日曜日

day to day comparison 2018 vs. 2019 intersect merge



 use intersect() to calculate AND between index(bp.day["2019"]) and 2019 calendar.

> intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))
 [1] 17899 17900 17901 17902 17903 17904 17905 17906 17907 17908 17909 17910 17911 17912 17913 17914 17915 17916 17917 17918
<skip>
[41] 17939 17940 17941 17942 17943 17944 17945
> length(seq(as.Date("1970-01-01"),as.Date("2019-01-01"),by='days'))
[1] 17898
> length(seq(as.Date("1970-01-01"),as.Date("1970-01-01"),by='days'))
[1] 1

resutls are given as the number of the days since "1970-01-01". therefore sbstract 17896 and 17896-365 will calculate the case for 2019 and 2018 respectively. intersecting between 2019 and 2018 will give back the date which exit both 2018 and 2019 in the daily sequential data.

>intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))
[1]  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
[39] 41 43 44 45 46 47

below will creates the day-to-day comparison graph between 2018 and 2019.


bp.day <- apply.daily(bp.bangkok,mean)
d <- intersect(intersect(index(bp.day["2019"]),seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days'))-17896,intersect(index(bp.day["2018"]),seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days'))-(17896-365))

bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]]
bp.day[seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]

merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18"))


plot(merge(bp.day[seq(as.Date("2019-01-01"),as.Date("2019-12-31"),by='days')[d]],as.vector(bp.day[,1][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),as.vector(bp.day[,2][seq(as.Date("2018-01-01"),as.Date("2018-12-31"),by='days')[d]]),suffixes = c("","h18","h18")))





plot abline eps GSPC




plot.default(diff(eps_year_xts["2007::2018"])[-1],type='h',axes=F)
par(new=T)
qtr <- seq(as.Date("2007-04-01"),as.Date("2018-12-31"),by='quarters')
plot.default(qtr,to.quarterly(GSPC["2007-04::2018"])[,4],type='l')
abline(v=as.Date("2018-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2015-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2017-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2016-01-01"),col = "gray60",lty=3)
abline(h=2000,col="gray60",lty=3)
abline(v=as.Date("2014-01-01"),col = "gray60",lty=3)


For the longer period, use SP5 instead of GSPC

plot.default(diff(eps_year_xts["1992::2018"])[-1],type='h',axes=F,col=3)
par(new=T)
qtr <- seq(as.Date("1992-04-01"),as.Date("2018-12-31"),by='quarters')
plot.default(qtr,to.quarterly(SP5["1992-04::2018"])[,4],type='l')
abline(v=as.Date("2018-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2015-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2017-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2016-01-01"),col = "gray60",lty=3)
abline(h=2000,col="gray60",lty=3)
abline(v=as.Date("2014-01-01"),col = "gray60",lty=3)




To see the relation between S&P500 and payroll. Change frequency from quarterly to monthly.

plot.default(diff(PA["1992::2018"])[-1],type='h',axes=F,col=3)
par(new=T)
mnt <- seq(as.Date("1992-02-01"),as.Date("2018-12-31"),by='months')
plot.default(mnt,to.monthly(SP5["1992-02::2018"])[,4],type='l')
abline(v=as.Date("2018-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2015-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2017-01-01"),col = "gray60",lty=3)
abline(v=as.Date("2016-01-01"),col = "gray60",lty=3)
abline(h=2000,col="gray60",lty=3)
abline(v=as.Date("2014-01-01"),col = "gray60",lty=3)
par(new=T)
plot.default(mnt,as.vector(cli_xts$oecd["1992-02::2018"]),axes=F,col=4,type='l')
par(new=T)
plot.default(mnt,na.trim(diff(cli_xts$oecd["1991-08::2018"],lag=6)),axes=F,col=6,type='l')




2019年2月16日土曜日

eps update at 2019/2/16


before

2018-10-01 139.50000
2019-01-01 141.16000
2019-04-01 144.81000
2019-07-01 148.56000
2019-10-01 153.33000

after

> eps_year_xts["2019-10-01"] <- 154.37
> eps_year_xts["2019-07-01"] <- 148.75
> eps_year_xts["2019-04-01"] <- 144.92
> eps_year_xts["2019-01-01"] <- 140.74
> eps_year_xts["2018-10-01"] <- 138.87

2018-10-01 138.87000
2019-01-01 140.74000
2019-04-01 144.92000
2019-07-01 148.75000
2019-10-01 154.37000

Upgrading R from 340 to 352


necessary to install packages below manually.

install.packages("xts")
install.packages("quantmod")
install.packages("vars")
install.packages("mondate")
install.packages("RMySQL")
install.packages("forecast")
install.packages("beepr")

2019年2月6日水曜日

index mondate,merge



>  index(bp.day["2019"])
 [1] "2019-01-03 UTC" "2019-01-04 UTC" "2019-01-05 UTC" "2019-01-06 UTC" "2019-01-07 UTC" "2019-01-08 UTC"
 [7] "2019-01-09 UTC" "2019-01-10 UTC" "2019-01-11 UTC" "2019-01-12 UTC" "2019-01-13 UTC" "2019-01-14 UTC"
<skip>
[31] "2019-02-02 UTC" "2019-02-03 UTC" "2019-02-04 UTC" "2019-02-05 UTC"

>  mondate(index(bp.day["2019"]))-12
mondate: timeunits="months"
 [1] 2018-01-03 2018-01-04 2018-01-05 2018-01-06 2018-01-07 2018-01-08 2018-01-09 2018-01-10 2018-01-11 2018-01-12
<skip>
[31] 2018-02-02 2018-02-03 2018-02-04 2018-02-05

> as.character(mondate(index(bp.day["2019"]))-12)
 [1] "2018-01-03" "2018-01-04" "2018-01-05" "2018-01-06" "2018-01-07" "2018-01-08" "2018-01-09" "2018-01-10"
<skip>
[25] "2018-01-27" "2018-01-28" "2018-01-29" "2018-01-30" "2018-01-31" "2018-02-01" "2018-02-02" "2018-02-03"
[33] "2018-02-04" "2018-02-05"

>  bp.day[as.character(mondate(index(bp.day["2019"]))-12)]
               high       low
2018-01-03 147.0000 100.00000
2018-01-04 150.0000 104.00000
2018-01-05 131.0000  87.50000
2018-01-06 143.0000 108.00000
<skip>
2018-02-03 148.0000  91.00000
2018-02-04 124.3333  77.00000

> bp.day["2019"]
            high  low
2019-01-03 130.0 78.0
2019-01-04 136.0 80.0
2019-01-05 129.0 83.0
2019-01-06 127.0 77.5
<skip>
2019-02-03 117.5 75.5
2019-02-04 119.0 76.5
2019-02-05 116.5 71.0

merge them and draw graph.
the code below doesn't work when the data exits on the particular date which exits in 2019 but not in 2018.

> bp.day["2019-02-11"]
           high  low
2019-02-11  126 77.5
> bp.day["2018-02-11"]
     high low

updated code is available at 2019-02-17. please refer the new code.

bp.day <- apply.daily(bp.bangkok,mean)
merge(bp.day["2019"], as.vector(bp.day[as.character(mondate(index(bp.day["2019"]))-12)][,1]), as.vector(bp.day[as.character(mondate(index(bp.day["2019"]))-12)][,2]),suffixes=c("","h2018","l2018"))

2019年2月5日火曜日

merge, suffixes, index and plot



THIS IS OBSOLETE. PLEASE REFER TO 2019-02-17

  1. This is the sample for when the number of entries is 34.
  2. Please see "suffixes" parameter. the number of parameters should be equal to the number of xts objects to merged. Not the number of columns.

>bp.day <- apply.daily(bp.bangkok,mean)
> length(bp.day["2019"][,1])
[1] 34

bp.day <- apply.daily(bp.bangkok,mean)

len <- length(bp.day["2019"][,1])
plot(merge(bp.day["2019"],as.xts(as.vector(bp.day[,1]["2018"][1:len]),index(bp.day["2019"])),as.xts(as.vector(bp.day[,2]["2018"][1:len]),index(bp.day["2019"])),suffixes = c("","h2018","l2018")))










2019年2月1日金曜日

Calcurate your mortgage vol2.


the update source code from the version of this.

> mortgage
function(P=5000000, I=0.775, L=300, amort=T, plotData=T) {
J <- I/(12 * 100)
N <- L
M <- P*J/(1-(1+J)^(-N))
monthPay <<- M
cat("\nThe payments for this loan are:\n\n\n",
"\tMonthly payment: \t¥",format(M, big.mark=",", scientific=F)," (stored in monthPay)\n",
"\tTotal cost: \t\t¥", format(M*N, big.mark=",", scientific=F), "\n\n", sep="")
# Calculate Amortization for each Month
if(amort==T) {
Pt <- P # current principal or amount of the loan
currP <- NULL
while(Pt>=0) {
H <- Pt * J # this is the current monthly interest
C <- M - H # this is your monthly payment minus your monthly interest, so it is the amount of principal you pay for that month
Q <- Pt - C # this is the new balance of your principal of your loan
Pt <- Q # sets P equal to Q and goes back to step 1. The loop continues until the value Q (and hence P) goes to zero
currP <- c(currP, Pt)
}
monthP <- c(P, currP[1:(length(currP)-1)])-currP
aDFmonth <<- data.frame(
      Amortization=c(P, currP[1:(length(currP)-1)]),
      Monthly_Payment=monthP+c((monthPay-monthP)[1:(length(monthP)-1)],0),
      Monthly_Principal=monthP,
      Monthly_Interest=c((monthPay-monthP)[1:(length(monthP)-1)],0),
      Year=sort(rep(1:ceiling(N/12), 12))[1:length(monthP)]
)
aDFyear <- data.frame(
     Amortization=tapply(aDFmonth$Amortization, aDFmonth$Year, max),
     Annual_Payment=tapply(aDFmonth$Monthly_Payment, aDFmonth$Year, sum),
     Annual_Principal=tapply(aDFmonth$Monthly_Principal, aDFmonth$Year, sum),
     Annual_Interest=tapply(aDFmonth$Monthly_Interest, aDFmonth$Year, sum),
     Year=as.vector(na.omit(unique(aDFmonth$Year)))
     )
aDFyear <<- aDFyear
cat("The amortization data for each of the", N, "months are stored in \"aDFmonth\".\n\n")
cat("The amortization data for each of the", L/12, "years are stored in \"aDFyear\".\n\n")
# may edit "L/12" in the previous line.
}
if(plotData==T) {
barplot(t(aDFyear[,c(3,4)]),
col=c("blue", "red"),
main="Annual Interest and Principal Payments",
sub="The data for this plot is stored in aDFyear.",
xlab="Years", ylab=" Amount",
legend.text=c("Principal", "Interest"),
ylim=c(0, max(aDFyear$Annual_Payment)*1.3))
}
}