Skip to content

Instantly share code, notes, and snippets.

@yongbin
Created January 9, 2017 22:50
Show Gist options
  • Save yongbin/9e297cb26f1ef97d2814fd5dedab9dee to your computer and use it in GitHub Desktop.
Save yongbin/9e297cb26f1ef97d2814fd5dedab9dee to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use PDL;
use PDL::Stats;
use PDL::NiceSlice;
use Mojo::UserAgent;
use Time::Local;
my $user = shift || 'rjbs';
my $URL = "https://github.com/users/%s/contributions";
my $ua = Mojo::UserAgent->new;
#
# 지난 1년간 내가 커밋한 기록 가지고 오기
#
my @response;
$ua->get( sprintf( $URL, $user ) )->res->dom->find('rect')->each(
sub {
my ( $date, $cnt ) = @{$_}{qw/data-date data-count/};
my $wd = wday($date);
push @{ $response[$wd] }, $cnt;
}
);
my $commits = pdl(@response);
#
# 수치자료를 범주형으로 변경하기
#
my $ratio = $commits / $commits->max;
my $rank =
( 0 == $ratio ) * 0 +
( ( 0 < $ratio ) & ( $ratio <= 0.25 ) ) * 1 +
( ( 0.25 < $ratio ) & ( $ratio <= 0.5 ) ) * 2 +
( ( 0.5 < $ratio ) & ( $ratio <= 0.75 ) ) * 3 +
( ( 0.75 < $ratio ) & ( $ratio <= 1 ) ) * 4;
#
# 상태의 변화 추적하기
#
my $tmat = zeros( 5, 5, 7 );
my $n = $rank->getdim(0) - 1;
my $idx =
pdl( [ sequence($n), sequence($n) + 1 ] )->transpose->dummy(1)->transpose;
$rank->dummy(1)->range($idx);
my $s = $rank->dummy(1)->range($idx)->squeeze;
$s = $s->append( $s->slice(0)->zvals );
for my $i ( @{ $s->unpdl } ) {
$tmat->range( [ @{$_} ] )++ for ( @{$i} );
}
#
# 베이지안 업데이팅
#
for my $wd ( 0 .. 6 ) {
my $k = $tmat->slice(":,:,$wd")->squeeze;
my $sumover = $k->copy->sumover;
for my $r ( 0 .. 4 ) {
for my $c ( 0 .. 4 ) {
my $sum = $sumover->at($r);
my $val = $k->at( $c, $r );
my $x = zeroes(100)->xlinvals( 0, 1 );
my $prob = $x->index(
maximum_ind( pdf_beta( $x, $val + 1, $sum - $val + 1 ) ) );
$k->range( [ $c, $r ] ) .= $prob;
}
}
}
#
# 마코브 연쇄
#
my $lw = zeros( 5, 7 );
my $lw_idx = $rank (-2)->append( $rank (-2)->yvals );
$lw->range($lw_idx)++;
print( ( $lw->dummy(1) x $tmat )->squeeze );
sub wday { # 2014-03-15 -> DayName ( Dayname element of @wday )
my ( $mday, $mon, $year ) = reverse( split( /-/, shift ) );
return ( localtime( timelocal( 0, 0, 0, $mday, $mon - 1, $year ) ) )[6];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment