2017年3月24日金曜日

Calculate the first and the last day of any designated month.

Below will pick up the start and the end of next month where PA data ends.


as.Date(as.yearmon(mondate(index(last(PA)))+1))
[1] "2017-03-01"
as.Date(as.yearmon(mondate(index(last(PA)))+2))-1
[1] "2017-03-31"


thus the combination of them will output as below.


as.xts(forecast(auto.arima(PA),h=10)$mean[1:10],as.Date(seq(mondate(index(last(PA)))+1,by=1,length.out=10)))

               [,1]
2017-03-02 145989.9
2017-04-02 146182.0
      <SKIP>
2017-11-02 147361.8

2017-12-02 147513.5


pick up the first day and the last day of the month which is exactly 36 months after any given date.

> as.Date(as.yearmon((mondate(Sys.Date())+36)))
[1] "2020-03-01"


> as.Date(as.yearmon((mondate(Sys.Date())+36)))-1
[1] "2020-02-29"
>

do the same thing by ISOdate, however it's much more redundant.

> as.Date(ISOdate(yea(mondarte(.())SysDate+36),month(mondate(Sys.Date())+36),1))-1
[1] "2020-02-29"

2017年3月23日木曜日

mondate and Calculate date by months

mondate and Calculate date by months

When you have to generate date sequence from 2017 march and other 9 following months, you can do as below.

seq(as.Date("2017-03-01"),as.Date("2017-12-01"),by="months")

If you like to do it in an arithmetic way. use mondate. you also need as.Date to make the generated sequence to use as as.xts argument.

as.Date(seq(mondate("2017-03-01"), by=1, length.out=10)))

below will create xts format data with forecast by auto regression of PAYEMS from 2017 MAR. to DEC. Don't forget to put [1:10] at the end of the first argument.

as.xts(forecast(auto.arima(PA),h=10)$mean[1:10],as.Date(seq(mondate("2017-03-01"), by=1, length.out=10)))

see below for output.

[,1]
2017-03-01 145989.9
2017-04-01 146182.0
<SKIP>
2017-11-01 147361.8
2017-12-01 147513.5


Without index number [1:10], it will end as an error like below.

> as.xts(forecast(auto.arima(PA),h=10)$mean,as.Date(seq(mondate("2017-03-01"), by=1, length.out=10)))
Error in do.call(paste("as", dateFormat, sep = "."), list(as.numeric(time(x)),  :
  'what' must be a function or character string

2017年3月21日火曜日

Graph S&P500 actual and theoretical value


Plot actual and theoretical from 1992

plot(predict(lm(to.quarterly(SP5[kikan])[,1] ~ apply.quarterly(PA[kikan],mean) * apply.quarterly(UC[kikan],mean) * GDP[kikan] - apply.quarterly(UC[kikan],mean) )),type='l',col=2,axes=F,ylim=c(0,2500),ylab="")
par(new=T)
plot(to.quarterly(SP5[kikan])[,1],type='l',ylim=c(0,2500))


桃應問曰

桃應問曰、
舜爲天子、
皋陶爲士、
瞍殺人、
如之何、
子曰、
之而已矣、
則舜不禁與、
舜惡得而禁之、
有所受之也、
則舜如之何、
視棄天下、
棄敝蹝也、
負而逃、
海濱而處、
身訢然、
而忘天下。

2017年3月13日月曜日

人生足別離


勸君金屈卮
滿酌不須辭
花發多風雨
人生足別離

私的直訳

君に盃を勧めよう。
酒は目一杯に注ぐ。断れないぞ。
花は咲く。雨風は多し。
人は生る。別離も足りる。

意訳

さあ飲めよ。
一杯につぐぞ、飲めないなんて言うなよ。
花は咲くもの、風雨は吹くもんだ。
生きてれば、別れはつきものだよ。

2017年3月10日金曜日

花粉集計 by awk


get 2017 data from http://nagakura-ac.com/2017-kafun-info/index.shtml

when inputs are as below

2月1日 0 0 0 13.8 2.0 明けましておめでとうございます。今年も花粉情報をお伝えします。
2月2日 10 0 0 13.3 3.8
2月3日 20.3 0 0 13.7 3.5 温かい穏やかなお正月を迎え、最高気温が高い影響を受け、スギ花粉が0.3個測定されており。1月3日が今年のスギ花粉初観測日となりました。これは例年より早めで、昨年は1月19日、一昨年は11日でした。
               <SKIP>
3月4日 30 0 0 14 3.6 花粉症症状を自覚している方も出始めています。
3月5日 20.3 0 0 10.4 3.7 スギ花粉が0.3個測定されています。
3月6日 0 0 0 8.8 1.5


  1. Calculate the total of each February, March, and April. 
  2. Print results at the end.


awk '/^2/{feb=feb+$2;print $1" "$2}/^3/{mar=mar+$2;print $1" "$2}/^4/{apr=apr+$2;print $1" "$2}END{print "######\n feb="feb"  \n mar="mar"  \n apr="apr}' < kafun

2017年3月9日木曜日

Normalize Historical EPS data from S&P.


S&P500 eps data comes from here http://us.spindices.com/documents/additional-material/sp-500-eps-est.xlsx?force_download=true

When inputs are as below.


09/30/2016,$28.69,$25.39
06/30/2016,$25.70,$23.28
  <SKIP>
06/30/1988,$6.05,$6.22
03/31/1988,$5.48,$5.53

awk to remove "$" mark in 2nd and 3rd columns is

~$ awk  -F, '{gsub("\\$","",$2);gsub("\\$","",$3);print $1","$2","$3}' < inputfile 

results are,

09/30/2016,28.69,25.39
06/30/2016,25.70,23.28
      <SKIP>
06/30/1988,6.05,6.22
03/31/1988,5.48,5.53

don't forget to use "\\"  not "\". it is because "\" itself requires own escape sequence. for the case above, "^." might work in the same way.

furthermore.

Below will remove the 1st field, print header in the first line and reverse order for latter usages.

awk  -F, '{gsub("\\$","",$2);gsub("\\$","",$3);print $2","$3}' < inputfile | tail -r | awk 'BEGIN{print "ope,rep"}{print $0}'

ope,rep
5.48,5.53
6.05,6.22
6.22,6.38
6.37,5.62
6.41,6.74

2017年3月8日水曜日

do forecast by ARIMA

The length of periods are given as the parameter "h".


library(forecast)
am <- auto.arima( PA[k1992], ic="aic", trace=T, stepwise=F, approximation=F, start.p=0, start.q=0, start.P=0, start.Q=0)
forecast(am,level = c(50,95), h = 12)

2017年3月5日日曜日

Calculate rolling 4 quarter's sum of eps and plot its graph.


when eps_xts is like below.

>eps_xts
           ope  rep
1988-01-01 5.48 5.53
1988-04-01 6.05 6.22
      <SKIP>
2016-01-01 23.97 21.72
2016-04-01 25.70 23.28
2016-07-01 28.69 25.39

calculate rolling 4 quarter sum of eps from data and remove N/A.

  na.trim(filter(eps_xts$ope,rep(1,4))

the length of the result and convert it to # of quarters.

  (length(na.trim(filter(eps_xts$ope,rep(1,4))))-1)*3

the last quarter is..

  as.Date(index(last(eps_xts)))

the starting quarter is calculated from data above. "last" to get the last entry of eps_xts, "index" to pick up date data from the entry and "mondate" to convert date to calculate-able data.

  as.Date(mondate(index(last(eps_xts))) - (length(na.trim(filter(eps_xts$ope,rep(1,4))))-1)*3)

get sequence of dates and plot the graph with seq and data.

plot(seq(as.Date(mondate(index(last(eps_xts))) - (length(na.trim(filter(eps_xts$ope,rep(1,4))))-1)*3),as.Date(index(last(eps_xts))),by="quarters"),na.trim(filter(eps_xts$ope,rep(1,4))),type="h")