2019年7月17日水曜日

EPS 2019JUL17


> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.39
2019-04-01 136.78
2019-07-01 139.10
2019-10-01 150.00
2020-01-01 153.31
2020-04-01 157.90
2020-07-01 162.84
2020-10-01 167.92

で、


~$cat eps.txt 
12/31/2020 $49.06 $45.22 16.34 17.74 $183.63 $169.14
9/30/2020 $47.36 $44.00 16.83 18.29 $178.22 $164.01
6/30/2020 $45.00 $41.28 17.33 18.86 $173.11 $159.06
3/31/2020 $42.21 $38.65 17.91 19.46 $167.48 $154.18
12/31/2019 $43.65 $40.09 18.38 19.93 $163.26 $150.55
9/30/2019 $42.25 $39.05 19.40 21.52 $154.64 $139.42
6/30/2019 (5.1%) 2941.76 $39.37 $36.39 19.51 21.94 $153.77 $136.73
3/31/2019 2834.40 $37.99 $35.02 18.76 21.30 $153.05 $134.39

のとき

$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] <- 136.73
eps_year_xts["2019::"][3] <- 139.42
eps_year_xts["2019::"][4] <- 150.55
eps_year_xts["2019::"][5] <- 154.18
eps_year_xts["2019::"][6] <- 159.06
eps_year_xts["2019::"][7] <- 164.01
eps_year_xts["2019::"][8] <- 169.14

2019年7月14日日曜日

ffprobe でファイルの属性を調べる 標準エラー出力のリダイレクト ffmpeg



ffprobe , ffmpegはなぜか標準エラー出力をつかうので”2>&1 | ”でリダイレクトしてやる。

$ ls *.mp4 | awk '{print "echo "$1"\n";print "ffprobe "$1" 2>&1 | grep Stream"}' | sh

K-OnS2E01.mp4
    Stream #0:0(jpn): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 636 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
    Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)
    Stream #0:2(eng): Data: bin_data (text / 0x74786574)
K-OnS2E02.mp4
    Stream #0:0(jpn): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 645 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
    Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Stream #0:2(eng): Data: bin_data (text / 0x74786574)
K-OnS2E03.mp4
    Stream #0:0(jpn): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 629 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
    Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Stream #0:2(eng): Data: bin_data (text / 0x74786574)

2019年7月10日水曜日

ffmpeg / 正規表現 単語の区切り / map オプション

ffmpegをつかってできるだけ簡単にmp4を作る方法


[avi @ 0x7ffaf7800000] Could not find codec parameters for stream 1 (Audio: mp3 (mp3float) (U[0][0][0] / 0x0055), 48000 Hz, 2 channels, fltp, 128 kb/s): unspecified frame size

が出たときは「-codec:a copy 」をやると良いのかも。。。。

# no conversion just switch container. highly reliable.

ffmpeg -i <INPUT>.avi -codec:v copy -codec:a copy <OUTPUT>.mp4

OR

ffmpeg -i <INPUT>.avi -codec:a copy out.mp4

ffmpeg -i <INPUT>.mkv -vcodec copy <OUTPUT>.mp4

ffmpeg -i sample_input.mkv -vcodec copy sample_output.mp4

for 文をつかってシーケンシャルに数字を振りながらmp4を作る方法


j=0;for i in  *.mkv; do echo $i;  let ++j ; echo $j ; ffmpeg -y -i $i -vcodec copy $j.mp4; done


  • 文の最後に;を書く
  • let をつかってカウンタを加算する
  • ffmpeg -yで既存ファイルがあっても強制上書き
  • forはdo とdoneで受ける
  • 変数は初期化すること


以下と等価となる。

j=0;for i in  *.mkv;
> do echo $i
> let ++j
> echo $j
> ffmpeg -y -i $i -vcodec copy $j.mp4
> done

j=0;for i in *.mp4; do let ++j; mv $j.mp4 hibike_euphonuim_S2E$j.mp4; done


forとsedと正規表現をつかってファイル名を編集しながらmp4を作る方法


  • さいごの「2」で出現順序を指定して置換する。
  • \(<patter>\)で指定して\1で受けてる。
  • -e 大事。これがないと\1 が使えない。(間違い。なくてもOK)



Hibike Euphonium 1 [BD 720p]$ ls *.mp4 | awk '{ print "mv "$1" "$1}' | gsed -e 's/E\([1-9]\)\./E0\1./2'
mv hibike_euphonuim_S1E1.mp4 hibike_euphonuim_S1E01.mp4
mv hibike_euphonuim_S1E10.mp4 hibike_euphonuim_S1E10.mp4
mv hibike_euphonuim_S1E11.mp4 hibike_euphonuim_S1E11.mp4

awk のgensub関数を使えば、sedは使わなくて良いはず。第二引数の"g"はグローバル?例えば2を指定すると2番めにヒットした正規表現だけを処理してくれるらしい。(要研究)

ls *.mp4 | awk '{ print "ls "$1" "gensub("0([1-9])","1\\1","g",$1)}'

map の使い方


$ffmpeg -i <INPUT_FILE> -map 0:0 -map 0:2 <OUTPUT_FILE>

<sample> honomoto@~$ffmpeg -i k01.mkv -map 0:0 -map 0:2 k01.mp4

この場合、k01.mkvには以下のストリームがある。

Stream #0:0(jpn): Video: h264 (High), yuv420p(progressive), 1280x720, SAR 1:1 DAR 16:9, 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
Stream #0:1(eng): Audio: aac (LC), 48000 Hz, stereo, fltp (default)
Metadata:
  title           : Animax Dub, Released by [SrK-Mitsu89]
Stream #0:2(jpn): Audio: aac (HE-AACv2), 48000 Hz, stereo, fltp
Stream #0:3(eng): Subtitle: ass (default)
Stream #0:4(eng): Subtitle: ass

これらのうち、オプション"-map 0:0 -map 0:2"をつかって、0:0と0:2だけを取り込んでファイルに出力する。結果は以下の通り。0:0は0:0に取り込まれる、0:0をスキップしているので0:2が0:1として生成されている。

Stream #0:0(jpn): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 636 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
Metadata:
  handler_name    : VideoHandler
Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)

以下、かんたんな説明。

ffmpeg は -iで指定したファイルの順番を0から数えます。またそれぞれのファイルのストリームの順番も0から数えます。

なので-map 0:0 -map 1:0 -map 2:0 と言うのは、
0つ目のファイルの、0番目のストリームと
1つ目のファイルの、0番目のストリームと
2つ目のファイルの、0番目のストリームをそれぞれ0番目、1番目、2番目としてマージするという意味になります。

つまり、<sample>のmapオプション"-map 0:0 -map 0:2"は0つ目のファイルの0番目のストリームとおなじく2番目のストリームをマージして0つ目のファイルの0番目のストリームと1番目のストリームとして出力するという意味になる。


ファイル名変更時のzero padding


~$ head kon2file.list
K-ON!! Ep 01 - Seniors!.mkv
K-ON!! Ep 03 - Drummer!.mkv
K-ON!! Ep 04 - Field Trip!.mkv
<SKIP>
K-ON!! Ep 26 - Visit!.mkv
K-ON!! Ep 27 (OVA) - Plan!.mkv
K-ON!! Ep 5 - Staying Behind!.mkv
K-ON!! Ep 8 - The Future!.mkv

のとき


~$ awk '{print "ffmpeg  -i \""$0"\" -map 0:0 -map 0:2 K-On!!S2E"$3".mp4"}' kon2file.list |sed 's/E\([1-9]\)\.mp4/E0\1.mp4/'

ffmpeg  -i "K-ON!! Ep 01 - Seniors!.mkv" -map 0:0 -map 0:2 K-On!!S2E01.mp4
ffmpeg  -i "K-ON!! Ep 03 - Drummer!.mkv" -map 0:0 -map 0:2 K-On!!S2E03.mp4
ffmpeg  -i "K-ON!! Ep 04 - Field Trip!.mkv" -map 0:0 -map 0:2 K-On!!S2E04.mp4
<SKIP>
ffmpeg  -i "K-ON!! Ep 26 - Visit!.mkv" -map 0:0 -map 0:2 K-On!!S2E26.mp4
ffmpeg  -i "K-ON!! Ep 27 (OVA) - Plan!.mkv" -map 0:0 -map 0:2 K-On!!S2E27.mp4
ffmpeg  -i "K-ON!! Ep 5 - Staying Behind!.mkv" -map 0:0 -map 0:2 K-On!!S2E05.mp4
ffmpeg  -i "K-ON!! Ep 8 - The Future!.mkv" -map 0:0 -map 0:2 K-On!!S2E08.mp4

となる。Zero Paddingされてるところが味噌!ただし、gensub関数を使うほうが楽なので以下を参照すること。

gensub関数を使用したzero padding


gensub関数を使うともっと簡単。\yは単語の区切りを示す。”\y.\y”でキャラクター一文字を示すので、その場合に前に”0”を足してやる。(注意! 他のGNU softwareは単語の区切りに\bを使う)。

~$ awk '{print "ffmpeg -i \""$0"\" K-OnS2E"gensub(/(\y.\y)/,"0\\1","g",$3)".mp4"}'  kon2file.list

ffmpeg -i "K-ON!! Ep 01 - Seniors!.mkv" K-OnS2E01.mp4
ffmpeg -i "K-ON!! Ep 03 - Drummer!.mkv" K-OnS2E03.mp4
ffmpeg -i "K-ON!! Ep 04 - Field Trip!.mkv" K-OnS2E04.mp4
<SKIP>
ffmpeg -i "K-ON!! Ep 27 (OVA) - Plan!.mkv" K-OnS2E27.mp4
ffmpeg -i "K-ON!! Ep 5 - Staying Behind!.mkv" K-OnS2E05.mp4
ffmpeg -i "K-ON!! Ep 8 - The Future!.mkv" K-OnS2E08.mp4

gensubを2回使用して出力結果をさらに調整する


$ ls *.mkv
K-ON! Ep 01 - Dissolution!.mkv                 K-ON! Ep 10 - Another Training Camp!.mkv
K-ON! Ep 02 - Instruments!.mkv                 K-ON! Ep 11 - Crisis!.mkv
K-ON! Ep 03 - Special Lessons!.mkv             K-ON! Ep 12 (Season Finale) - Light Music!.mkv
K-ON! Ep 04 - Training Camp!.mkv               K-ON! Ep 13 (Extra) - Winter Days!.mkv
K-ON! Ep 05 - Advisor!.mkv                     K-ON! Ep 14 (OVA) - Live House!.mkv
K-ON! Ep 06 - School Festival!.mkv             K-ON! Ep 7 - Christmas!.mkv
K-ON! Ep 09 - New Club Member!.mkv             K-ON! Ep 8 - Freshman Reception!.mkv



$ ls *.mkv | awk '{$3=gensub(/(\y.\y)/,"0\\1","g",$3);print "ffmpeg -i \""$0"\" K-OnS1E"$3gensub(/ /,"_","g",$0)".mp4"}'

ffmpeg -i "K-ON! Ep 01 - Dissolution!.mkv" K-OnS1E01K-ON!_Ep_01_-_Dissolution!.mkv.mp4
ffmpeg -i "K-ON! Ep 02 - Instruments!.mkv" K-OnS1E02K-ON!_Ep_02_-_Instruments!.mkv.mp4
ffmpeg -i "K-ON! Ep 03 - Special Lessons!.mkv" K-OnS1E03K-ON!_Ep_03_-_Special_Lessons!.mkv.mp4
<SKIP>
ffmpeg -i "K-ON! Ep 14 (OVA) - Live House!.mkv" K-OnS1E14K-ON!_Ep_14_(OVA)_-_Live_House!.mkv.mp4
ffmpeg -i "K-ON! Ep 07 - Christmas!.mkv" K-OnS1E07K-ON!_Ep_07_-_Christmas!.mkv.mp4
ffmpeg -i "K-ON! Ep 08 - Freshman Reception!.mkv" K-OnS1E08K-ON!_Ep_08_-_Freshman_Reception!.mkv.mp4


最終バージョンはこちら。


  1. $3 をゼロパディングして戻す。
  2. $0の最初から12バイト目までを削って、変数outstringに代入する。
  3. outstringの最後の4バイト(.mkv)を削って戻す。
  4. outstringから"!"を削って、戻す。
  5. 過熱をさけるため1行ごとに60秒休むよう、"\nsleep 60"。を付加する。
  6. 最終的に””で囲まれたもとのファイル名$0と$3,outstring,固定文字列などなどを結合して変換後のファイル名を作成し、組み合わせて最終的なコマンド文字列を生成する。
  7. ゼロパディングは$3=sprintf("%02d", $3)でやったほう良いかも!(「本当の本当の完成バージョン」を参照)

$ ls *.mkv | awk '{$3=gensub(/(\y.\y)/,"0\\1","g",$3);
>  outstring=substr($0,13);
>  outstring=substr(outstring,1,length(outstring)-4);
>  outstring=gensub(/!/,"","g",outstring);
>  print "ffmpeg -i \""$0"\" K-OnS1E"$3gensub(/ /,"_","g",outstring)".mp4\nsleep 60"}'
ffmpeg -i "K-ON! Ep 01 - Dissolution!.mkv" K-OnS1E01-_Dissolution.mp4
sleep 60
ffmpeg -i "K-ON! Ep 02 - Instruments!.mkv" K-OnS1E02-_Instruments.mp4
sleep 60
ffmpeg -i "K-ON! Ep 03 - Special Lessons!.mkv" K-OnS1E03-_Special_Lessons.mp4
sleep 60
ffmpeg -i "K-ON! Ep 04 - Training Camp!.mkv" K-OnS1E04-_Training_Camp.mp4
sleep 60
<SKIP>
ffmpeg -i "K-ON! Ep 13 (Extra) - Winter Days!.mkv" K-OnS1E13(Extra)_-_Winter_Days.mp4
sleep 60
ffmpeg -i "K-ON! Ep 14 (OVA) - Live House!.mkv" K-OnS1E14(OVA)_-_Live_House.mp4
sleep 60
ffmpeg -i "K-ON! Ep 07 - Christmas!.mkv" K-OnS1E07-_Christmas.mp4
sleep 60
ffmpeg -i "K-ON! Ep 08 - Freshman Reception!.mkv" K-OnS1E08-_Freshman_Reception.mp4

本当の本当の完成バージョン


 ls *.mkv | awk '{$3=sprintf("%02d", $3);
 outstring=substr($0,13);
 outstring=substr(outstring,1,length(outstring)-4);
 outstring=gensub(/!/,"","g",outstring);
 print "ffmpeg -i \""$0"\" K-OnS1E"$3gensub(/[ ()]/,"_","g",outstring)".mp4\nsleep 60"}'

可変長で文字を切り出したい時用。注意!!

ls *.mkv | awk '{$3=sprintf("%02d", $3);
match($0,/\s-\s/)
outstring=substr($0,RSTART+2);
outstring=substr(outstring,1,length(outstring)-4);
outstring=gensub(/!/,"","g",outstring);
print "ffmpeg -i \""$0"\" K-OnS1E"$3gensub(/[ ()&,]/,"_","g",outstring)".mp4\nsleep 60"}'

以下でも良かったかも!

ls *.mkv | awk '{$3=sprintf("%02d", $3);
match($0,/-.*-/)
outstring=substr($0,RSTART+RLENGTH);
outstring=substr(outstring,1,length(outstring)-4);
outstring=gensub(/!/,"","g",outstring);
print "ffmpeg -i \""$0"\" K-OnS1E"$3gensub(/[ ()&,]/,"_","g",outstring)".mp4\nsleep 60"}'

正規表現で単語として認められるのはアルファベット、数字、アンダーバーだけなので、注意!

おわり

2019年7月5日金曜日

年と月の計算 yearmon mfrow




> wp
           result period_length         rate  open_p close_p
 1 1973 1.3763938            28  0.011474868   84.30  116.03
 5 1976 1.3013769            16  0.016600207   76.98  100.18
10 1978 0.9423369            15 -0.003951666   98.85   93.15
12 1980 1.1883753             6  0.029182211  114.24  135.76
 1 1984 1.3672189            17  0.018569047  119.52  163.41
 2 1985 1.1075926             3  0.034649717  163.58  181.18
 8 1987 1.3967474            13  0.026036742  236.12  329.80
12 1988 1.0727335             9  0.007831630  258.89  277.72
 2 1990 0.9751147             4 -0.006280247  340.36  331.89
 8 1991 1.1498066             7  0.020142134  343.91  395.43
 4 1992 0.9950124             4 -0.001249244  417.03  414.95
 9 1994 1.0727019            22  0.003195123  431.35  462.71
10 1995 1.0674621             4  0.016454915  544.75  581.50
 9 1997 1.4893871            20  0.020117927  636.02  947.28
 3 2000 1.3639946            17  0.018427587 1098.67 1498.58
 4 2002 1.0345650             7  0.004866239 1040.94 1076.92
 3 2004 1.3277960            12  0.023908021  848.18 1126.21
 5 2006 1.0659588            12  0.005337085 1191.50 1270.09
 6 2007 1.1836842            12  0.014151848 1270.06 1503.35
 5 2010 1.4932221            15  0.027089509  729.57 1089.41
 2 2011 1.1983603             7  0.026187620 1107.53 1327.22
 3 2012 1.1295682             4  0.030927485 1246.91 1408.47
 3 2014 1.2994239            18  0.014657552 1440.90 1872.34
12 2014 1.0273593             4  0.006770750 2004.07 2058.90
11 2017 1.2807753            19  0.013109691 2067.17 2647.58
>

のとき、その20番目から25番目までのエントリを対象に一括でグラフを書く。パラメータ中のデータ(月数)をyearmon で正確に計算するために12で割っている。1/12で1ヶ月、7/12で7ヶ月、19/12で1年と7ヶ月。

par(mfrow=c(3,2))
for(i in seq(1,6,1)){plot.default(to.monthly(SP5)[,4][paste(as.Date((yearmon(index(wp))[20:25] -(wp[20:25][,2]/12))[i]),as.Date(index(wp[20:25])[i]),sep="::")]) }


OR

あるいは、mondateを使って月数の計算を行う。またここでは最初の月を計算ミスするバグを修正するため’+1’を追加した。下の図はバグあり期間計算で作成したもので実際より一月早く始まっている。

for(i in seq(1,6,1)){plot.default(to.monthly(SP5)[,4][paste(as.Date(mondate(index(wp))[20:25][i] -    as.vector(wp[,2][20:25])[i]+1),as.Date(index(wp[20:25])[i]),sep="::")]) }




14番目から19番目までのエントリを対象に一括でグラフを書くときは以下の通り。序数を指定する場所が三箇所あることに注意。

for(i in seq(1,6,1)){plot.default(to.monthly(SP5)[,4][paste(as.Date((yearmon(index(wp))[14:19] -(wp[14:19][,2]/12))[i]),as.Date(index(wp[14:19])[i]),sep="::")]) }

2019年6月26日水曜日

Windows profile bug


Another important recommendation from our developers is to contact Microsoft directly. In other words, once any customer manages to reproduce the issue while collecting the requested log they should raise a ticket with Microsoft. Without analysis from MS we won’t be able to do anything on our side, so involving MS is crucial at this point. To speed things up, the customer should refer to advisory case # 117062115926216. 

2019年6月19日水曜日

EPS 2019JUN19



> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.48
2019-04-01 137.14
2019-07-01 139.58
2019-10-01 150.77
2020-01-01 153.98
2020-04-01 158.28
2020-07-01 162.93
2020-10-01 167.58
> eps_year_xts["2019::"][1]
             [,1]
2019-01-01 134.48
> eps_year_xts["2019::"][1] <- 134.32
> eps_year_xts["2019::"][2] <- 137.01
> eps_year_xts["2019::"][3] <- 139.53
> eps_year_xts["2019::"][4] <- 150.72
> eps_year_xts["2019::"][5] <- 154.31
> eps_year_xts["2019::"][6] <- 158.70
> eps_year_xts["2019::"][7] <- 163.52
> eps_year_xts["2019::"][8] <- 168.46
> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.32
2019-04-01 137.01
2019-07-01 139.53
2019-10-01 150.72
2020-01-01 154.31
2020-04-01 158.70
2020-07-01 163.52
2020-10-01 168.46

eps_year_xts["2019::"][1] <- 134.32
eps_year_xts["2019::"][2] <- 137.01
eps_year_xts["2019::"][3] <- 139.53
eps_year_xts["2019::"][4] <- 150.72
eps_year_xts["2019::"][5] <- 154.31
eps_year_xts["2019::"][6] <- 158.70
eps_year_xts["2019::"][7] <- 163.52
eps_year_xts["2019::"][8] <- 168.46

$cat eps.txt 
12/31/2020 $49.27 $45.10 15.65 17.17 $184.82 $168.46
9/30/2020 $47.66 $43.70 16.09 17.68 $179.67 $163.52
6/30/2020 $45.31 $41.13 16.55 18.22 $174.77 $158.70
3/31/2020 $42.58 $38.53 17.06 18.74 $169.53 $154.31
12/31/2019 $44.12 $40.16 17.53 19.19 $164.93 $150.72
9/30/2019 $42.76 $38.88 18.56 20.72 $155.84 $139.53
6/30/2019 $40.07 $36.74 18.72 21.11 $154.46 $137.01
3/31/2019 (98.2%) 2834.40 $37.98 $34.95 18.89 21.53 $153.04 $134.32

のとき

$tac eps.txt | awk '{gsub("\\$","",$NF);print "eps_year_xts[\"2019::\"]["NR"] <- "$NF}'
eps_year_xts["2019::"][1] <- 134.32
eps_year_xts["2019::"][2] <- 137.01
eps_year_xts["2019::"][3] <- 139.53
eps_year_xts["2019::"][4] <- 150.72
eps_year_xts["2019::"][5] <- 154.31
eps_year_xts["2019::"][6] <- 158.70
eps_year_xts["2019::"][7] <- 163.52

eps_year_xts["2019::"][8] <- 168.46

2019年6月18日火曜日

サトウキビ(砂糖キビ) -- sugarcane.



from 2019/3/26 conversation.

Cost for 3 acre plowing and planting.

  1. sugarcane varieties 
  2. worker wage,plow and planning 
  3. Fertilizer packages price = 40,000 baht for 1 acre.. ????
  4. Food pice 10,000 baht...

Harvest


  1. 3 acre = 90 ton
  2. selling price is 1 ton =900 baht.
  3. 90 ton * 900 bhat = 81,000 baht
  4. if add 2 more acre, 2 acre = 60 ton =  54,000 THB
  5. total 5 acre = 150 ton = 135,000 THB 
  6. 5 acre = 2.02343 ha.

Cost 2 acre to plow and plant

  1.  30,000 baht..
買取価格

上記「Harvest」と値が違うので注意。
  1. 自分で収穫、工場に納める場合 700 thb/t
  2. 収穫、輸送を依頼する場合 500 thb/t


2019年6月17日月曜日

CLI - composite leading indicator - USA, China and Europe Area


> summary(lm(cli_xts$oecd ~ cli_xts$usa + cli_xts$ea19 + cli_xts$china))

Call:
lm(formula = cli_xts$oecd ~ cli_xts$usa + cli_xts$ea19 + cli_xts$china)

Residuals:
     Min       1Q   Median       3Q      Max
-0.68475 -0.09634  0.03744  0.11972  0.41636

Coefficients:
              Estimate Std. Error t value Pr(>|t|) 
(Intercept)   9.265072   1.036438   8.939   <2e-16 ***
cli_xts$usa   0.402771   0.013317  30.246   <2e-16 ***
cli_xts$ea19  0.438171   0.012302  35.618   <2e-16 ***
cli_xts$china 0.065993   0.006695   9.857   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1958 on 348 degrees of freedom
  (420 observations deleted due to missingness)
Multiple R-squared:  0.9577, Adjusted R-squared:  0.9573
F-statistic:  2624 on 3 and 348 DF,  p-value: < 2.2e-16

> last(cli_xts,n=12)
                       oecd           usa           china         ea19
2018-05-01 100.33250 100.47600 99.29018 100.53100
2018-06-01 100.22810 100.42840 99.13142 100.40680
2018-07-01 100.11390 100.36340 98.96715 100.28170
2018-08-01  99.98618 100.27230 98.80726 100.14970
2018-09-01  99.84174 100.13410 98.67062 100.01150
2018-10-01  99.68746  99.94146 98.57309  99.87226
2018-11-01  99.53398  99.71795 98.51999  99.73539
2018-12-01  99.38741  99.48866 98.50362  99.59542
2019-01-01  99.25877  99.28568 98.52641  99.45764
2019-02-01  99.15687  99.12820 98.58793  99.32590
2019-03-01  99.08728  99.01845 98.69128  99.20160
2019-04-01  99.03429  98.93993 98.80725  99.08523

for future reference.

> last(tmp.predict,n=6)
        SP5.Open SP5.High SP5.Low SP5.Close  SP5.Volume   spline      eps
 1 2019  2476.96  2708.95 2443.96   2704.10 80391630000 2906.849 2760.208
 2 2019  2702.32  2813.49 2681.83   2784.49 70183430000 2929.656 2779.348
 3 2019  2798.22  2860.31 2722.27   2834.40 78596280000 2956.669 2804.840
 4 2019  2848.63  2949.52 2848.63   2945.83 69604840000 2983.000 2829.000
 5 2019  2952.33  2954.13 2750.52   2752.06 76860120000 3010.000 2854.000
 6 2019  2751.53  2910.61 2728.81   2886.98 33703630000 3037.000 2879.000

2019年6月3日月曜日

EPS 2019JUN03


Download XLS from here.

> eps_year_xts["2019::"]
             [,1]
2019-01-01 134.74
2019-04-01 137.38
2019-07-01 140.00
2019-10-01 151.44
2020-01-01 154.73
2020-04-01 159.04
2020-07-01 163.53
2020-10-01 167.98

> eps_year_xts["2019-01-01"] <- 134.48
> eps_year_xts["2019-04-01"] <- 137.14
> eps_year_xts["2019-07-01"] <- 139.58
> eps_year_xts["2019-10-01"] <- 150.77
> eps_year_xts["2020-01-01"] <- 153.98
> eps_year_xts["2020-04-01"] <- 158.28
> eps_year_xts["2020-07-01"] <- 162.93
> eps_year_xts["2020-10-01"] <- 167.58

>  eps_year_xts["2019::"]
             [,1]
2019-01-01 134.48
2019-04-01 137.14
2019-07-01 139.58
2019-10-01 150.77
2020-01-01 153.98
2020-04-01 158.28
2020-07-01 162.93
2020-10-01 167.58

eps_year_xts["2019-01-01"] <- 134.48
eps_year_xts["2019-04-01"] <- 137.14
eps_year_xts["2019-07-01"] <- 139.58
eps_year_xts["2019-10-01"] <- 150.77
eps_year_xts["2020-01-01"] <- 153.98
eps_year_xts["2020-04-01"] <- 158.28
eps_year_xts["2020-07-01"] <- 162.93
eps_year_xts["2020-10-01"] <- 167.58


histogram performance comparison between cli 1 month delta positive and negative.







when "func()" is "cli_delta_vs_period_return_rate.r"

> hist(as.vector(func("minus","1970-01-01")[,1])-1,col=rgb(0.5,1,0),breaks=20,xlim=c(-0.6,0.5),ylim=c(0,7))
0111 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[1] 11.34615
[1] 0.9858096
> par(new=T)
> hist(as.vector(func("plus","1970-01-01")[,1])-1,col=rgb(0.5,0,1,alpha=0.4),breaks=10,xlim=c(-0.6,0.5),ylim=c(0,7))

source code is as below.

#
# 1)pick up months whose clie_xts$oecd  is up from the previous
# 2)create the stream of flags, in which up is 1 and down is 0
# 3)compare SPX close price between the end month of cli_xts delta is plus(or minus) and its start.
# 4)return xts objects which contains. start month of period, updown ration, length of months and monthly average return during period.

func <- function(pm="plus",s="1970-01-01",l=1){
  w <- c()
  cat("0")
  cat(length(s))
  if(nchar(s) == 10){ # use nchar() to measure strings length, not length()
    cat("1")
    last_date <- last(index(cli_xts$oecd))
    start_date <- s
    period <- paste(start_date,last_date,sep='::')
  }else{
     # last_date <- l
     period <- s
  }
  # last_date <- last(index(cli_xts$oecd))
  # start_date <- s
  # period <- paste(start_date,last_date,sep='::')
  start_index <- 1
  iteration <- 0
  performance_val <- c()
  period_length <- c()
  lag_month <- l
  result <- c()
  rate <- c()
  plus_or_minus <- pm
  open_p <- c()
  close_p <- c()

# put flag on the months accoring to the parameter. for "minus" cli delta is less than ZERO, for plus the opposite.
  for(i in seq(1,length(diff(cli_xts$oecd,lag=lag_month)[period]),1,)){
    if(plus_or_minus == "minus"){
      if(as.vector(diff(cli_xts$oecd,lag=lag_month)[period])[i] < 0){  # up is "> 0"
        w <- append(w,1)
      }else{
        w <- append(w,0)
      }
    }else if(plus_or_minus == "plus"){
      if(as.vector(diff(cli_xts$oecd,lag=lag_month)[period])[i] > 0){  # up is "> 0"
        w <- append(w,1)
      }else{
        w <- append(w,0)
      }
    }else{
      stop("please use plus or minus as 1st parameter")
    }
  }
  month_flag <- 0 # status flag
# check stream and when flag is changes 0 to 1. it is the start of period.
  for(i in seq(1,length(diff(cli_xts$oecd,lag=lag_month)[period]),1,)){
    if(w[i] == 1){
      if(month_flag == 0){ # when w is 1 and month_flag is 0, the period starts
        month_flag <- 1
        start_price <- as.vector(to.monthly(SP5[period])[,1][i]) #dc 0531
        # print(index(to.monthly(SP5[period])[,4][i]))
        # cat("from ")
        # cat(as.character(as.Date(index(to.monthly(SP5[period])[,4][i]))))
        start_index <- i
      }
      # dc 0602 add output at the end of loop
      if(i == length(diff(cli_xts$oecd,lag=lag_month)[period])){
        # print("end of the loop")
        result <- append(result,as.xts(as.vector(to.monthly(SP5[period])[,4][i]) / start_price,index(to.monthly(SP5[period])[,4][i])))
                period_length <- append(period_length,i-start_index)
        performance_val <- append(performance_val,as.vector(to.monthly(SP5[period])[,4][i-1]) / start_price)
        open_p <- append(open_p,start_price)
        close_p <- append(close_p,as.vector(to.monthly(SP5[period])[,4][i-1]))
        rate <- append(rate,last(performance_val)**(1/last(period_length))-1)
      }
# check stream and when flag is changes 1 to 0. it is the start of period.
    }else if(w[i] ==0){
      if(month_flag == 1){ # when w is 0 and month_flag is 1, the period ends
        # cat(" ")
        # cat(i - start_index)
        # cat(" month(s)")
        # cat("\n")
        iteration <- iteration +1
        cat(iteration)
        cat(" ")
        result <- append(result,as.xts(as.vector(to.monthly(SP5[period])[,4][i-1]) / start_price,index(to.monthly(SP5[period])[,4][i-1])))
        # print(as.xts(as.vector(to.monthly(SP5[period])[,4][i]) / start_price,index(to.monthly(SP5[period])[,4][i])))
        # print(i - start_index)
        month_flag <- 0 # when period ends, intialize the flag.
        period_length <- append(period_length,i-start_index)
        performance_val <- append(performance_val,as.vector(to.monthly(SP5[period])[,4][i-1]) / start_price)
        open_p <- append(open_p,start_price)
        close_p <- append(close_p,as.vector(to.monthly(SP5[period])[,4][i-1]))
        rate <- append(rate,last(performance_val)**(1/last(period_length))-1)
      }
    }
  }
  cat("\n")
  print(mean(period_length))
  print(mean(performance_val))
  return(merge(result,period_length,rate,open_p,close_p))

}
# t_minus <- performance_val
# t_plus <- performance_val
func("minus","1970-01-01")




2019年6月2日日曜日

S&P 500 performance comparison between CLI 1 month delta is positive and negative.




The period when OECD Composite Leading Inditacor 1 month delta is positive and negative come one after another. Here to calculate S&P 500 return during those each period.

The parameter "minus" indicates this is period when CLI delta is negative. The result is they came 25 times since 1970/1/1. The average length is approx. 11.35 month and the average return is -1.41%.

On the other hand, positive have come 25 times. The average length is 11.8 month and the average return is 19.85%.

> func("minus","1970-01-01")
0111 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[1] 11.34615
[1] 0.9858096
            result period_length          rate  open_p close_p
Sep 1970 0.9157072             9 -0.0097365802   92.06   84.30
Jan 1975 0.6634491            24 -0.0169506551  116.03   76.98
Jul 1977 0.9867239            14 -0.0009541901  100.18   98.85
Jun 1980 1.2136407            20  0.0097282501   94.13  114.24
Aug 1982 0.8803035            20 -0.0063541523  135.76  119.51
Nov 1984 1.0010403            10  0.0001039829  163.41  163.58
Jul 1986 1.3032344            17  0.0157013530  181.18  236.12
Mar 1988 0.7849672             7 -0.0339963144  329.81  258.89
Oct 1989 1.2255509            10  0.0205472899  277.72  340.36
Jan 1991 1.0362770            11  0.0032447523  331.89  343.93
Dec 1991 1.0547758             4  0.0134213316  395.43  417.09
Nov 1992 1.0395228             7  0.0055527568  414.95  431.35
Jun 1995 1.1773542             9  0.0183066251  462.69  544.75
Jan 1996 1.0937576             3  0.0303236982  581.50  636.02
Oct 1998 1.1598155            13  0.0114699680  947.28 1098.67
Sep 2001 0.6946176            18 -0.0200405678 1498.58 1040.94
Mar 2003 0.7875979            11 -0.0214722593 1076.92  848.18
May 2005 1.0579732            14  0.0040334726 1126.21 1191.50
Jun 2006 1.0001180             1  0.0001180284 1270.05 1270.20
Feb 2009 0.4885423            20 -0.0351826442 1504.66  735.09
Jun 2010 0.9479536             1 -0.0520464319 1087.30 1030.71
Nov 2011 0.9385236             9 -0.0070249109 1328.64 1246.96
Sep 2012 1.0547405             7  0.0076425915 1365.90 1440.67
Aug 2014 1.0690570             5  0.0134449700 1873.96 2003.37
Apr 2016 1.0031085            16  0.0001940004 2058.90 2065.30
Mar 2019 1.0599561            15  0.0034295890 2645.10 2784.49



> func("plus","1970-01-01")
0111 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[1] 11.8
[1] 1.198459
            result period_length         rate  open_p close_p
Jan 1973 1.3763938            28  0.011474868   84.30  116.03
May 1976 1.3013769            16  0.016600207   76.98  100.18
Oct 1978 0.9423369            15 -0.003951666   98.85   93.15
Dec 1980 1.1883753             6  0.029182211  114.24  135.76
Jan 1984 1.3672189            17  0.018569047  119.52  163.41
Feb 1985 1.1075926             3  0.034649717  163.58  181.18
Aug 1987 1.3967474            13  0.026036742  236.12  329.80
Dec 1988 1.0727335             9  0.007831630  258.89  277.72
Feb 1990 0.9751147             4 -0.006280247  340.36  331.89
Aug 1991 1.1498066             7  0.020142134  343.91  395.43
Apr 1992 0.9950124             4 -0.001249244  417.03  414.95
Sep 1994 1.0727019            22  0.003195123  431.35  462.71
Oct 1995 1.0674621             4  0.016454915  544.75  581.50
Sep 1997 1.4893871            20  0.020117927  636.02  947.28
Mar 2000 1.3639946            17  0.018427587 1098.67 1498.58
Apr 2002 1.0345650             7  0.004866239 1040.94 1076.92
Mar 2004 1.3277960            12  0.023908021  848.18 1126.21
May 2006 1.0659588            12  0.005337085 1191.50 1270.09
Jun 2007 1.1836842            12  0.014151848 1270.06 1503.35
May 2010 1.4932221            15  0.027089509  729.57 1089.41
Feb 2011 1.2871884             8  0.032060761 1031.10 1327.22
Feb 2012 1.0952515             3  0.030792577 1246.91 1365.68
Mar 2014 1.2994239            18  0.014657552 1440.90 1872.34
Dec 2014 1.0273593             4  0.006770750 2004.07 2058.90
Nov 2017 1.2807753            19  0.013109691 2067.17 2647.58

their results are tested by "t.test()". p-value is 7.391e-05. Therefore they should be distiguised each other with the probability of 99.99261%

> t.test(func("plus","1970-01-01")[,1],func("minus","1970-01-01")[,1])
<skip>
Welch Two Sample t-test

data:  func("plus", "1970-01-01")[, 1] and func("minus", "1970-01-01")[, 1]
t = 4.3306, df = 48.714, p-value = 7.391e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.1138069 0.3109339
sample estimates:
mean of x mean of y
1.1984592 0.9860888

There is a histogram in the next entry for the better comparison.

see source code github. commit id is d20207a

2019年5月15日水曜日

2019MAY15



間違い
eps_year_xts[128] <- 167.98
eps_year_xts[127] <- 163.53
eps_year_xts[126] <- 163.53
eps_year_xts[126] <- 159.04
eps_year_xts[125] <- 154.73
eps_year_xts[124] <- 151.44
eps_year_xts[123] <- 140
eps_year_xts[122] <- 137.38
eps_year_xts[121] <- 134.74

正解
eps_year_xts[129] <- 167.98
eps_year_xts[128] <- 163.53
eps_year_xts[127] <- 159.04
eps_year_xts[126] <- 154.73
eps_year_xts[125] <- 151.44
eps_year_xts[124] <- 140
eps_year_xts[123] <- 137.38
eps_year_xts[122] <- 134.74

eps_year_xts[121] <- 132.39

作業前
> eps_year_xts["2018::"]
             [,1]
2018-01-01 115.44
2018-04-01 122.48
2018-07-01 130.39
2018-10-01 132.39
2019-01-01 132.47
2019-04-01 135.11
2019-07-01 137.93
2019-10-01 149.61
2020-01-01 154.77
2020-04-01 159.43
2020-07-01 163.95
2020-10-01 168.40

作業後
> eps_year_xts["2018::"]
             [,1]
2018-01-01 115.44
2018-04-01 122.48
2018-07-01 130.39
2018-10-01 132.39
2019-01-01 134.74
2019-04-01 137.38
2019-07-01 140.00
2019-10-01 151.44
2020-01-01 154.73
2020-04-01 159.04
2020-07-01 163.53
2020-10-01 167.98
> 

2019年5月9日木曜日

.Last <- function()






restore working image in the parent directory with the name "YYYY-MM-DD-HH:MM:SS". the smaple above is 17:00:50 of May 09, 2019.

$pwd
/Users/honomoto/R_proj
$ls
2019-05-09-17:00:50  startup.R           tmp1                work
R                   startup.R~          tmp2

.Last <- function() {
  # save.image(file=paste(getwd(),Sys.time(),sep="/"))
  #
  # save working image in the parent directory. expected the length of the directory name is fixed.
  #
  save.image(file=gsub(" ","-",paste(substr(getwd(),1,22),Sys.time(),sep="/")))
  # 
  # OR store working image at the working directory
  #
  # save.image(file=gsub(" ","-",paste(getwd(),Sys.time(),sep="/")))
  cat("bye bye...\n")
}

2019年5月8日水曜日

cli_5mon.r : draw spiral graph of cli 5 months delta vs. its reading.



do "> func("2011-01-01::",9)" after execute all codes below.

#
# plese refer the latter half of
# https://00819.blogspot.com/2019/03/vix-cli-6-month-delta-and-s.html
# lag is set to  5 months.
#
#   s = start date of the spiral like "1992-01-01::" DON'T FORGET DOUBLE COLON!!
#       Don't set before "1964-01-01"
#   l = length of years like 9. recommend to use equal or less than 9. Don't exceed the current end of data.
#
#   use like  > func("2001-01-01::",5)
#
func <- function(s="2011-01-01::",l=9){

  head_of_record <- "1964-01-01::"
  print(head_of_record)
  offset <- length(seq(as.Date(head_of_record),as.Date(s),by='months'))
  max_length <- length(seq(as.Date(head_of_record),as.Date(index(last(cli_xts))),by='months'))
  len_mon <- l*12-1
  lag_month <- 5
  # when the end period exceeds the current end, adjust # of months and years to avoid the counters go beyond the limit.
  if(offset + len_mon > max_length){
    len_mon <- max_length - offset
    l <- ceiling(len_mon/12) # ceiling is to round up
  }
  # len_mon <- l
  # print(offset)
  # print(len_mon)
  # print(seq(as.Date(head_of_record),as.Date(s),by='months')[offset])
  # print(seq(as.Date(head_of_record),as.Date("2100-01-01"),by='months')[offset+len_mon])

  # print(offset)
  plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][offset:(offset+len_mon)],cli_xts$oecd[head_of_record][offset:(offset+len_mon)],type='b')
  # print(offset)
  tmp <- par('usr')
  # par(new=T)
  plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][offset:(offset+len_mon)],cli_xts$oecd[head_of_record][offset:(offset+len_mon)],type='b',xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),lwd=1,main=paste("from",substr(s,1,10),"for",len_mon+1,"months",sep=" "),ylab="",xlab="")
  par(new=T)
  for(i in seq(0,l-1,1)){
    print(i)
    # print(offset)
    # print(offset+i*12)
    # print(offset+i*12+11)
    par(new=T)
      # when the end period exceeds the current end, adjust # of months to avoid OOB
      # otherwise months to go in each iteration is always 11.
    if(offset+i*12+11 < max_length){
      m <- 11
    }else{
      #adjust months to go as go within max_length.
      m <- max_length - (offset+i*12)
    }
    # print(m)
    # print(max_length)
    # print(len_mon)
    # print(offset)
    plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][(offset+i*12):(offset+i*12+m)],cli_xts$oecd[head_of_record][(offset+i*12):(offset+i*12+m)],type='b',xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),col=i+1,lwd=2,ylab="",xlab="",axes = F)
    if(i == 6){
      # when i ==6 "yellow" is used for dots, but offers poor visibility. plot 'x' upon them to improve.
      par(new=T)
      plot.default(na.trim(diff(cli_xts$oecd,lag=lag_month))[head_of_record][(offset+i*12):(offset+i*12+m)],cli_xts$oecd[head_of_record][(offset+i*12):(offset+i*12+m)],type='p',xlim=c( tmp[1],tmp[2]), ylim=c(tmp[3], tmp[4]),pch='x',ylab="",xlab="",axes = F)
    }
    par(new=T)
  }
  abline(v=0)
  abline(h=100)
  # abline(v=seq(0.5,-1,-0.1),col=6,lty=3)
  # automatically calculate the upper and lower limit of vline.
  # might be able to use 'floor(tmp[2]*10)/10' instead of round(x,digit=1)
  abline(v=seq(ceiling((tmp[1]*10))/10,floor(tmp[2]*10)/10,0.1),col=6,lty=3)

}
func("2001-01-01::",9)