2017年1月28日土曜日

Suica - 電子改札

https://zuuonline.com/archives/78495/2

から。

「約500人の利用者を改札の中から行動観察した結果、以下のことがわかりました。

  ・ ほとんどの利用者はSuicaをパスケースや財布に入れており、カードのまま利用する人は皆無。財布のままタッチも多い。
  ・ 50人以上の利用者が、明らかに読み取り部の液晶を見ていない。まっすぐ前を向いたままタッチして通過。
  ・ 約30人が通過時に読み取りエラーや残高不足などでゲートが閉まる。
  ・ 改札を通過した後、そのままチャージする人が5人。改札を通った時に残高を確認してチャージを判断している。
  ・ 逆手(左手)でタッチする人は、約30人。スムーズにタッチしていることから普段から逆手で実施している可能性大。
  ・ タッチ部で立ち止まる人も、約30人。使い慣れていないシニアの方や大荷物の人、音楽を聴きながらコードの絡まる人等。」

だそうです。あれは左手でタッチするほうが合理的だとおもうなぁ。。

2017年1月26日木曜日

Conver to quarterly data - apply.quarterly

convert monthly data into quarterly data. when monthly data is available in xts format. "apply.monthly" function to convert.

when <monthly_xts_data> is as example,

2016-07-01  529
2016-08-01  472
2016-09-01  530
2016-10-01  453
2016-11-01  454
2016-12-01  444

apply.quarterly(<monthly_xts_data>,mean)

will output quarterly data as the average of data of 3 months belonging to each quarter.

2016-09-01 510.3333
2016-12-01 450.3333

please see below for the case of apply.quarterly(<monthly_xts_data>,last),

2016-09-01  530
2016-12-01  454

mean,max,min,last are confirmed. it can use standard R functions. apply.monthly seems just to provide interface to them. for other terms, use apply.daily, apply.weekly, apply.monthly and apply.yearly.

2017年1月25日水曜日

Edit CSV with Perl - use foreach

the previous script was really agree. rewrite it to process multiple files in a sequence. the list of file name is stored in the array. loop to process each element. thus eliminating overwrapping parts in the script.

use strict;

use Text::CSV_XS;
my $csv = Text::CSV_XS->new ({ binary => 1 }); # open file w/ binary mode
# prepare array to store input file names;
my @inputfile = ("emp_in.csv","os_in.csv");
# "foreach" extracts filename, stores in $count and loop till the end of array
foreach my $count(@inputfile){
  print "input file = $count\n"; # this line is for debug
  open my $fh, "<", $count; # case for foreach
  while (my $columns = $csv->getline ($fh)) {
    # columns no 3 stores bug id(s) which might be concateated with ,
    $$columns[3] =~ s/ //g; # eliminate unnecessary space from column no.3
    $$columns[3] =~ s/,/;/g; # replace , by ;
    my $rm_id = ""; #initialize the parameter.
    # if columns includes # the line is header. it is defunct.
    if($$columns[0] =~ /#/){
      # do nothing here!
    }else{ # if not a header, do below
      $rm_id = $$columns[0];
    }
    if($$columns[3] =~ /[0-9]/){  # if $$columns[3]= TFS bug ID includes any bug
      # print $$columns[0]," ",$$columns[3],"\n";
      my @bug_num = split(/;/,$$columns[3]);
      foreach my $parts(@bug_num){
        print "$rm_id;$parts\n";
      }
    }
  }
  close $fh;
  # print "close fh\n";  # this is for debug
}
$csv->eof;

2017年1月24日火曜日

Revised model for S&P 500 from 1992-01-01 to 2016-12-31


don't use "2016-10-01" to indicate 2016Q3 as data starts from 2016-10-03 in the designated period.

in order to extend data period of GDPC96, incorporate data from GDPnow. It suggest 2.8% annual GDP growth for 4th quarter, therefore adding another 0.7% will be the forecast of Q4.

> GDPC96 <- c(GDPC96,xts(as.numeric(last(GDPC96)*1.007),as.Date("2016-10-01")))
> start_date <- "1992-01-01"
> end_date <- "2016-12-31"
> start_date; end_date
[1] "1992-01-01"
[1] "2016-12-31"
> G <- GDPC96
> PA <- PAYEMS
> UC <- UNDCONTSA
> kikan <- paste(start_date,end_date,sep='::')

don't do "C <- UNDCONTSA", it will destroy the precious c() function.

> (summary(lm(to.quarterly(SP5[kikan])[,4] ~ to.quarterly(PA[kikan])[,4] * to.quarterly(UC[kikan])[,4] * G[kikan])))

Call:
lm(formula = to.quarterly(SP5[kikan])[, 4] ~ to.quarterly(PA[kikan])[,
    4] * to.quarterly(UC[kikan])[, 4] * G[kikan])

Residuals:
     Min       1Q   Median       3Q      Max
-308.451  -66.658    9.279   83.037  216.776

Coefficients:
                                                                     Estimate Std. Error t value Pr(>|t|)
(Intercept)                                                         2.752e+04  7.082e+03   3.886 0.000192 ***
to.quarterly(PA[kikan])[, 4]                                       -2.924e-01  6.109e-02  -4.786 6.46e-06 ***
to.quarterly(UC[kikan])[, 4]                                       -1.005e+01  9.216e+00  -1.090 0.278529
G[kikan]                                                           -1.970e+00  5.688e-01  -3.463 0.000813 ***
to.quarterly(PA[kikan])[, 4]:to.quarterly(UC[kikan])[, 4]           1.945e-04  7.119e-05   2.732 0.007551 **
to.quarterly(PA[kikan])[, 4]:G[kikan]                               2.139e-05  4.288e-06   4.989 2.86e-06 ***
to.quarterly(UC[kikan])[, 4]:G[kikan]                              -2.774e-04  7.493e-04  -0.370 0.712028
to.quarterly(PA[kikan])[, 4]:to.quarterly(UC[kikan])[, 4]:G[kikan] -6.310e-09  5.209e-09  -1.211 0.228875
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 126.8 on 92 degrees of freedom
Multiple R-squared:  0.9313, Adjusted R-squared:  0.9261
F-statistic: 178.2 on 7 and 92 DF,  p-value: < 2.2e-16

Perl and .bashrc

​don't forget to set env parameters below in order to use Perl's "CSV_XS" library. they might be included in ".bashrc". please check your environment first.

PERL5LIB,PERL_LOCAL_LIB_ROOT,PERL_MB_OPT,PERL_NM_OPT


PATH="<some directry>/perl5/bin${PATH:+:${PATH}}"; export PATH;
PERL5LIB="<some directry>/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="<some directry>/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"<some directry>/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=<some directry>/perl5"; export PERL_MM_OPT;


It might be convenient to set up terminal app as,

  1. go to "set up"
  2. go to "profile"
  3. go to "shell"
  4. input execute command as ". .bashrc"

2017年1月18日水曜日

Edit CSV with Perl

​Install CSV_VS module for you through CPAN
$ perl -MCPAN -e shell
cpan[3] > install Text::CSV_XS

  1. read csv file
  2. disassemble each column in the line.
  3. replace "," with ";" in 3rd column.
  4. replace "," with " " in 7th column.
  5. rewrite 1,2,3,5,6,7,8 column in the first line.
  6. when 1 st column includes "#", surpress file output
  7. append "," at the end of each data.


use strict;

use Text::CSV_XS;

my $csv = Text::CSV_XS->new ({ binary => 1 });

open my $fh, "<", "emp_in.csv";

open my $os, "<", "os_in.csv";

open my $fh_out, ">", "emp_out2.csv";
open my $os_out, ">", "os_out2.csv";
my $line_num = 1;
while (my $columns = $csv->getline ($fh)) {
  # print $fh_out map({ "[$_]\t" } @$columns), "\n";
  @$columns[3] =~ s/,/; /g; # replace , by ; in column no. 3. this is for the case to include multiple TFS Bug ID in this column.
  @$columns[7] =~ s/,/ /g; #remove comma from the subject.
  # replace original header with english version.
  if($line_num == 1){ @$columns[0] = "RM ID";
                      @$columns[1] = "RM Priority";
                      @$columns[2] = "RM Status";
                      @$columns[3] = "TFS Bug ID";
                      @$columns[5] = "RM Project";
                      @$columns[6] = "RM Filer";
                      @$columns[8] = "RM Component";
                      @$columns[7] = "RM subject";}
  # print @$columns[3],"\n";
  # print $fh_out map({"$_,"} @$columns), "\n"; # put , at the end of each column
  # if the first column includes pound, this line might be duplicated
  if(@$columns[0] =~ /#/){
    print "mark found\n"
  }else{
    print $fh_out map({"$_,"} @$columns), "\n";
  }
  $line_num++;

}
my $line_num = 1; # initialize the line counter
while (my $columns = $csv->getline ($os)) {
  # print $fh_out map({ "[$_]\t" } @$columns), "\n";
  @$columns[3] =~ s/,/; /g;
  @$columns[7] =~ s/,/ /g; #remove comma from the subject.

  if($line_num == 1){ @$columns[0] = "RM ID";
                        @$columns[1] = "RM Priority";
                        @$columns[2] = "RM Status";
                        @$columns[3] = "TFS Bug ID";
                        @$columns[5] = "RM Project";
                        @$columns[6] = "RM Filer";
                        @$columns[8] = "RM Component";
                        @$columns[7] = "RM subject";
  }
  if(@$columns[0] =~ /#/){
    # print "mark found\n"
  }else{
    print $os_out map({"$_,"} @$columns), "\n";
  }
  # print $columns;
  $line_num++;
}

$csv->eof;
close $fh_out;
close $os_out;
close $fh;
close $os;


Splilt PDF with Mac Automator