PyMC3ã§é層ãã¤ãºã¢ãã« - ã·ãã¯ã¹ãã¼ã·ã§ã³ãº
ãã®è¨äºã¯ãPyMC3ã®ããã¥ã¡ã³ã A Hierarchical model for Rugby prediction — PyMC3 3.0 documentation ã®ã³ã¼ããå¼ç¨ãã¦ãã¾ãã
PyMC3ã§ã©ã°ãã¼ã®é層ãã¤ãºã¢ããªã³ã°
å è¨äºã§ã¯ãã·ãã¯ã¹ãã¼ã·ã§ã³ãº2014ã®å試åãé層ãã¤ãºã§ã¢ããªã³ã°ãã¦ãããå è¨äºèªä½ãããã¬ãã¢ãªã¼ã°ã®ã¢ããªã³ã°ãããè¨äºãå ãã¿ã«ãã¦ãããããã A Hierarchical Bayesian Model of the Premier League
ã·ãã¯ã¹ãã¼ã·ã§ã³ãº
ã·ãã¯ã¹ãã¼ã·ã§ã³ãºã欧å·ãã£ã³ããªã³ã決ãã大ä¼ã2015å¹´ã©ã°ãã¼ã¯ã¼ã«ãã«ããã§ã¸ã£ãã³ãã¹ããªã³ã°ãã¯ã¹ãã大éæãæãã¦ããã¼ã ã¨å¼ã¹ãã»ã©ã®æ³¨ç®ãéããã©ã°ãã¼ã ãã©ããã·ãã¯ã¹ãã¼ã·ã§ã³ãºãã®ç¥å度ã¯ã¾ã ã¾ã æãããã ã§ãã§ããããããã¯ã©ããã¤ã³ã¿ã¼ãã·ã§ãã«ãæ°ãããã©ã¼ãããã®ä¸ã§ããæãæ´å²ã¨ä¼çµ±ãã大ä¼ã§ãå®åçã«ãååçæ強決å®æ¦ã¨è¨ã£ã¦å·®ãæ¯ããªãã§ãããã éå¹æ¦ã¯2/4, Scotland vs Irelandãç¢éæ¦ãã風ã«è¨ãã¨ãã極ä¸ã®5é±éãå§ã¾ãã¾ãï¼ãã£ã¦æãã
ãã¦ãå è¨äºã§ã®ã·ãã¯ã¹ãã¼ã·ã§ã³ãºã®èª¬æã¯ã
- Six Nations consists of Italy, Ireland, Scotland, England, France and Wales
- Game consists of scoring tries (similar to touch downs) or kicking the goal.
- Average player is something like 100kg and 1.82m tall.
Paul OâConnell the Irish captain is Height: 6â 6â (1.98 m) Weight: 243 lbs (110 kg)
ã·ãã¯ã¹ãã¼ã·ã§ã³ãºã®åå å½ã¯ãã¤ã¿ãªã¢ãã¢ã¤ã«ã©ã³ããã¹ã³ããã©ã³ããã¤ã³ã°ã©ã³ãããã©ã³ã¹ãã¦ã§ã¼ã«ãºã®6ã«å½
- 試åã®å¾ç¹ã¯ããã©ã¤ãã´ã¼ã«ããã¯
- ãã¬ã¤ã¤ã¼ã®å¹³åã¯1.82mã100kg
- ãã¼ã«ã»ãªã³ã³ãã«ã¯ã¢ã¤ã«ã©ã³ãã®ãã£ããã³ã§ã1.98mã110kg
ååã®éãã欧å·6ã«å½å¯¾ææ¦ãå ã ã¯ããã¼ã ã¦ããªã³ã¨å¼ã°ããã¤ã³ã°ã©ã³ããã¦ã§ã¼ã«ãºãã¢ã¤ã«ã©ã³ããã¹ã³ããã©ã³ãã§è¡ããã¦ãã大ä¼ãåéå¬ã¯1883å¹´ããã©ã³ã¹ã1910å¹´ã«åå ã2000å¹´ã«ã¤ã¿ãªã¢ãåå ããç¾å¨ã®ãã©ã¼ãããã«ãªã£ããã¡ãªã¿ã«ãã¼ã«ã»ãªã³ã³ãã«ã¯ã2014å¹´ã«åªåããã¢ã¤ã«ã©ã³ãã®ãã£ããã³ãã¢ã¤ãªãã·ã¥éã象徴ãããããªãæ¿ããæ°è¿«ã®ããã£ããã¬ã¼ãæã¡å³ã®ãªã¢ã«ããã¯ãå ¨çæã¯ä¸åå°éã«å¹æµããæ¦åã¨ã®åãã
ãã¦æ¬è¨äºã¯ã2016å¹´ã®ãã¼ã¿ãå©ç¨ããããã ãããåå¹´çè ã¤ã³ã°ã©ã³ãã®ãã£ããã³ãDylan Hartley(ãã£ã©ã³ã»ãã¼ããªã¼)ã追è¨ãããã2015å¹´ã©ã°ãã¼ã¯ã¼ã«ãã«ãããéå¬å°ã¨ãã¦ä¸ä½é²åºãæå¾ ããã¦ãããã®ã®ããªã¼ã¹ãã©ãªã¢ãã¦ã§ã¼ã«ãºã¨åçµã®ãããããæ»ã®ãã¼ã«ãã§2æãã¯ã¼ã«ãã«ããå²ä¸åã®éå¬å½äºé¸æéãçµé¨ãã失æã®ã¤ã³ã°ã©ã³ãã§ããã£ããã³ã«æåãããã®ã¯ããã£ã©ã³ã»ãã¼ããªã¼ã ã£ããæ´è¨ãå±éºãªãã¬ã¼ãç¹°ãè¿ãããæªç«¥ãã¨å¼ã°ãã巨漢HOã®ãã£ããã³å°±ä»»ã¯ãã©ã°ãã¼çã«ã¨ã£ã¦å¤§ããªãµãã©ã¤ãºãæåããã®ã¯ãå æ¥æ¬ä»£è¡¨ç£ç£ã¨ãã£ã¼ã»ã¸ã§ã¼ã³ãºã ã£ãã 2016å¹´ã¯ã°ã©ã³ãã¹ã©ã (å ¨ååªå)ã§å¤§ä¼ãçµãããã®å¾ã®ãã¹ããããã§ãè² ãç¡ãã§14é£åä¸ã®ã¤ã³ã°ã©ã³ããä»å¹´ã®ã·ãã¯ã¹ãã¼ã·ã§ã³ãºã§ã¯ãåªååè£ã®1çªæã ããåæã«ä¸çæ°è¨é²ã®ãã¹ãããã19é£åãè¦ãã¦ããã
ã¢ãã«å
https://pymc-devs.github.io/pymc3/notebooks/rugby_analytics.html#The-model.
åç½®ããé·ããªã£ã¦ãã¾ã£ãã ã¢ãã«ã®èª¬æããã£ããæ¸ãã¨ã
- å¾ç¹ãã¢ãã«å(ãã¢ã½ã³åå¸)
- ã¢ã¿ããã³ã°ãã©ã¡ã¼ã¿ã¨ãã£ãã§ã³ã¹ãã©ã¡ã¼ã¿ãåãã¼ã ãã¨ã«å®ç¾©
- æ¬æ å°ã§ã®è©¦åãã©ãããèæ ®
ä¸å¼ãã¿ãã¨ãèªãã¼ã ã®attã¨æµãã¼ã ã®defã®åã大ããã¨ãå¾ç¹ã大ãããªãã attã大ããã¨å¾ç¹è½åãé«ããdefã大ããã¨ç¸æã«ããããå¾ç¹ãä¸ãã¦ãã¾ããã¨ãæå³ãã¦ããã ã¢ãã«ã®èª¬æã¨ãã¯ãã³ã¼ãã«ã³ã¡ã³ããã¦ããã詳細ç¥ãããã£ããå è¨äºèªãã§ãã å ãã¼ã¿ã¯ãwikipediaããã https://en.wikipedia.org/wiki/2016_Six_Nations_Championship
import numpy as np import pandas as pd try: from StringIO import StringIO except ImportError: from io import StringIO %matplotlib inline import pymc3 as pm, theano.tensor as tt # 2016 Six Nations data_csv = StringIO("""home_team,away_team,home_score,away_score France,Italy,23,21 Scotland,England,9,15 Ireland,Wales,16,16 France,Ireland,10,9 Wales,Scotland,27,23 Italy,England,9,40 Wales,France,19,10 Italy,Scotland,20,36 England,Ireland,21,10 Ireland,Italy,58,15 England,Wales,25,21 Scotland,France,29,18 Wales,Italy,67,14 Ireland,Scotland,35,25 France,England,21,31""") # ãã¼ã¿èªã¿è¾¼ã¿ df = pd.read_csv(data_csv) teams = df.home_team.unique() teams = pd.DataFrame(teams, columns=['team']) teams['i'] = teams.index df = pd.merge(df, teams, left_on='home_team', right_on='team', how='left') df = df.rename(columns = {'i': 'i_home'}).drop('team', 1) df = pd.merge(df, teams, left_on='away_team', right_on='team', how='left') df = df.rename(columns = {'i': 'i_away'}).drop('team', 1) observed_home_goals = df.home_score.values observed_away_goals = df.away_score.values home_team = df.i_home.values away_team = df.i_away.values num_teams = len(df.i_home.drop_duplicates()) num_games = len(home_team) g = df.groupby('i_away') att_starting_points = np.log(g.away_score.mean()) g = df.groupby('i_home') def_starting_points = -np.log(g.away_score.mean()) model = pm.Model() with pm.Model() as model: # global model parameters # ç¡æ å ±äºååå¸ home = pm.Normal('home', 0, tau=.0001) tau_att = pm.Gamma('tau_att', .1, .1) tau_def = pm.Gamma('tau_def', .1, .1) intercept = pm.Normal('intercept', 0, tau=.0001) # team-specific model parameters atts_star = pm.Normal("atts_star", mu=0, tau=tau_att, shape=num_teams) defs_star = pm.Normal("defs_star", mu=0, tau=tau_def, shape=num_teams) # ãã¼ã ãã¨ã¢ã¿ã㯠atts = pm.Deterministic('atts', atts_star - tt.mean(atts_star)) # ãã¼ã ãã¨ãã£ãã§ã³ã¹ defs = pm.Deterministic('defs', defs_star - tt.mean(defs_star)) # ãã¼ã ã®Î¸(å¾ç¹) home_theta = tt.exp(intercept + home + atts[home_team] + defs[away_team]) # ã¢ã¦ã§ã¤ã®Î¸(å¾ç¹) away_theta = tt.exp(intercept + atts[away_team] + defs[home_team]) # likelihood of observed data home_points = pm.Poisson('home_points', mu=home_theta, observed=observed_home_goals) away_points = pm.Poisson('away_points', mu=away_theta, observed=observed_away_goals) with model: # åæå¤ start = pm.find_MAP() step = pm.NUTS(state=start) # ãµã³ããªã³ã° trace = pm.sample(2000, step, init=start) # ã°ã©ãæç» pm.traceplot(trace) # æåã®ã»ãæ¨ã¦ããã£ãã # pm.traceplot(trace[500:])
ãµã³ããªã³ã°çµæã¯ãtraceã«ä¿æããã¦ããã
attsãdefsã®åãã©ã¡ã¼ã¿ã«ã¤ãã¦ãã¾ããattsã«ã¤ãã¦ãããã大ããã»ã©ãå¾ç¹è½åãé«ãã¨ãããã ãããéã«ãdefsã¯å°ããã»ãã失ç¹ãå°ãªããã¼ã ã¨ãããã¨ã表ç¾ãã¦ãããéç«ã¤ã¤ã³ã°ã©ã³ãã®ãã£ãã§ã³ã¹ã(ã¤ã¿ãªã¢ã…)
pm.forestplot(trace, varnames=['atts'], ylabels=['France','Scotland','Ireland','Wales','Italy','England'], main="Team Offense")
pm.forestplot(trace, varnames=['defs'], ylabels=['France','Scotland','Ireland','Wales','Italy','England'], main="Team Deffence")
ã¡ãªã¿ã«ãåãã©ã¡ã¼ã¿ã¯ãvarnamesã¨get_valuesã§åå¾ã§ããã
for name in trace.varnames: values = trace.get_values(name)[500:] if name in ('atts', 'defs'): for i, team_values in enumerate(values.T): print(name, teams.ix[i].values[0], np.median(team_values)) else: print(name, np.median(values)) """ >>> home 0.197413525093 tau_att_log_ 2.36765017677 tau_def_log_ 1.62635825121 intercept 2.94638413619 atts_star 0.0243648852216 defs_star -0.0579292662131 tau_att 10.6722849008 tau_def 5.08532149318 atts France -0.316409735727 atts Scotland 0.102634304541 atts Ireland 0.0401948177547 atts Wales 0.188146988173 atts Italy -0.128166189509 atts England 0.11534176357 defs France -0.0267028885955 defs Scotland 0.0636352715494 defs Ireland -0.170924989081 defs Wales -0.139824762146 defs Italy 0.679569506375 defs England -0.391210743404 """
ãã©ã¡ã¼ã¿ã®ä¸å¤®å¤ã使ã£ã2017ã·ãã¯ã¹ãã¼ã·ã§ã³ãºã®æ¨å®å¤â (注: ããã¾ã§2016ã®ãã¼ã¿ã§ã¢ããªã³ã°ããçµæãããããæ¨å®å¤ãåºãã¾ãã¨ããã ãã§ãã絶対ã«ãã®ã¹ã³ã¢éãã«ãªãã¨ããããã§ã¯å ¨ç¶ãªãã§ãã念ã®ããã)
Scotland 21-21 Ireland England 25-9 France Italy 17-45 Wales Italy 17-39 Ireland Wales 18-18 England France 18-20 Scotland Scotland 22-24 Wales Ireland 23-11 France England 51-11 Italy Wales 23-17 Ireland Italy 19-27 France England 27-14 Scotland Scotland 50-17 Italy France 14-22 Wales Ireland 16-18 England
awkã¦ã¼ã¶ã¼ã®ããã®Perlã¯ã³ã©ã¤ãã¼
awk(ã¨ä»ã®ã³ãã³ã)ã¦ã¼ã¶ã¼ãPerlã¯ã³ã©ã¤ãã¼ãè¦ããã¨ãããããã¨ââ
- -aãªãã·ã§ã³ã§awkã¨ä¼¼ããããªæ¸ãæ¹ãã§ãã(awkã¨ã®æ¯è¼ãåè)
- ã«ã©ã 1ã¨ã«ã©ã 2ã表示
awk '{print $1, $2}'
perl -waln -e 'print $F[0], $F[1]'
ã
awk 'BEGIN{sum=0};{sum+=1};END{print sum}' file.txt
perl -wln -e 'BEGIN{my $sum=0}{$sum+=1}END{print $sum}' file.txt
ã
æ£è¦è¡¨ç¾ãperlã ãè¦ããã°ãã(awk, sed, grepã®ã³ãã³ããã¨ã«è¦ããªãã¦ãã)
è¤éãªãã¨ããããã¨ããã¨ãã«æ¡å¼µã楽(cpanã¢ã¸ã¥ã¼ã«ä½¿ãããè¤æ°ãã¡ã¤ã«ã§ãåé¡ãªã)
perlãªãã·ã§ã³
ãªãã·ã§ã³ | æå³ |
---|---|
-e | ã¹ã¯ãªãããã³ãã³ãã©ã¤ã³ã§å®è¡ |
-w | è¦å |
-n | æé»ã®ã«ã¼ã |
-p | èªåprint |
-l | è¡æ«å¦ç |
-a | å ¥åã¬ã³ã¼ããé å@Fã¸åå² |
-F | INPUTã»ãã¬ã¼ã¿ãæå® |
ç¹æ®å¤æ°(awkã¨ã®æ¯è¼)
perl | awk | æå³ |
---|---|---|
$_ | $0 | æå¾ã«èªã¿è¾¼ãã ã¬ã³ã¼ã |
$. | NR | ã¬ã³ã¼ãçªå· |
$#F | NF | ãã£ã¼ã«ãæ° |
$/ | RS | INPUTã¬ã³ã¼ãã»ãã¬ã¼ã¿ |
$\ | ORS | OUTPUTã¬ã³ã¼ãã»ãã¬ã¼ã¿ |
$ARGV[0] | FILENAME | ãã¡ã¤ã«å |
$, | OFS | OUTPUTãã£ã¼ã«ãã»ãã¬ã¼ã¿ |
ãã®ã»ã
# $_ã«hogeãå«ã¾ãã perl -waln -e 'if(/hoge/) {print $_}' file.txt # $_ã«hogeãå«ã¾ããªã perl -waln -e 'if(!/hoge/) {print $_}' file.txt # $F[0]ã«hogeãå«ã¾ãã perl -waln -e 'if($F[0] =~ /hoge/) {print $_}' file.txt # $F[0]ã«hogeãå«ã¾ããªã perl -waln -e 'if($F[0] !~ /hoge/) {print $_}' file.txt # hogeãfooã«ç½®æ perl -waln -e '$_ =~ s/hoge/foo/; print $_' file.txt
温度ã®ãããã©ã³ã¸ã¥ãã³æ¹ç¨å¼
物理学 Advent Calendar 2014 - Adventar 20æ¥ç®ã®è¨äºã§ãã
ãã©ã¦ã³éåã表ç¾ãã確çå¾®åæ¹ç¨å¼ã§ããLangevinæ¹ç¨å¼ã¨ãããæ¡å¼µãã温度ã®ãããLangevinæ¹ç¨å¼ã®è©±ããã¾ãã ã¾ããå¹¾ä½ãã©ã¦ã³éåã¯éèååã®ã¢ãã«åã«å©ç¨ããããããã®ã§ããã®ãããã«ã¤ãã¦ãå°ã触ãã¾ãã
ãã©ã¦ã³éå
ãã©ã¦ã³éåã¯ãç°¡åã«èª¬æããã¨ã液ä½ä¸ã®å°ããªç²åãã©ã³ãã ã«åããããªç¾è±¡ã§ãã æ°´ååãä¸è¦åã«è¡çªãããã¨ã«ãã£ã¦ããã©ã¦ã³éåãçãã¾ãã
ã©ã³ã¸ã¥ãã³æ¹ç¨å¼
質é1ã®ãã©ã¦ã³ç²åã«ã¤ãã¦ãé度ãvãæµæä¿æ°ãγ(>0)ãã¬ã¦ã¹åå¸ã«å¾ãé ãηããã®ä¿æ°ãαã¨ããã¨ã
ã®ããã«æ¸ãã¾ãã第ï¼é ã¯é度ã¨ãã¤ãã¹ã®ç©ãªã®ã§ãé度ã¨å対æ¹åã«ä½ç¨ããç²æ§åã®é ã§ãã第2é ã¯ã©ã³ãã é ã§ãæ°´ååã®ä¸è¦åãªè¡çªã«å¯¾å¿ãã¾ãã
ãã¦ãå¹¾ä½ãã©ã¦ã³éåã¯éèååã®ã¢ãã«åã«å©ç¨ããããã¨ãããã¾ãããå®éã®ä¾¡æ ¼å¤åããã¾ãã¢ãã«åã§ãã¦ããªãã¨ããæ¹å¤ãããã¾ãããã®æ¹ç¨å¼ã§è¨è¿°ããããã©ã¦ã³ç²åã®é度åå¸ã¯ã¬ã¦ã¹åå¸ã«å¾ãããã§ãããå®éã®éèååã®ä¾¡æ ¼å¤åã¯ãã裾ã®åºãåå¸ã«å¾ã£ã¦ãããããã¬ã¦ã¹åå¸ã§æ³å®ãããããã大ããªä¾¡æ ¼å¤åãçããå¯è½æ§ãããã¾ãã
温度ã®ãããã©ã³ã¸ã¥ãã³æ¹ç¨å¼
温度ã®ãããã©ã³ã¸ã¥ãã³æ¹ç¨å¼ã§ã¯ãæ°ããªãã©ã¡ã¼ã¿ã¨ãã¦Î²(=γ/α2)ãå°å ¥ããéç·å½¢ãªã©ã³ã¸ã¥ãã³æ¹ç¨å¼ã表ç¾ãã¾ããβã¯çµ±è¨åå¦ã«ããã温度ã®éæ°ã«ãããã¾ãã å¼ã®å°åºã¯こちらã«è¼ã£ã¦ããã®ã§çç¥ãã¾ããããã®Î²ãã«ã¤2ä¹åå¸ã«å¾ãå½¢ã§å¤åãã¾ããã¾ããβã®å¤åã¯ãã©ã¦ã³ç²åã観測ããæéå¹ ããã大ããªæéã¹ã±ã¼ã«ã§ã(ãã£ããå¤åãã)ã
ãã®æ¹ç¨å¼ã§ã¯ããã©ã¦ã³ç²åã®é度åå¸ã¯ã¬ã¦ã¹åå¸ã§ã¯ãªãããã裾ã®åºãåå¸ã¨ãªãã¾ãã å®éã«ãã«åã®è¦æ ¼åãããã°ãªã¿ã¼ã³ã®ãã¹ãã°ã©ã ã®ãããããã¿ã¦ã¿ãã¨ãæ£è¦åå¸(ã°ãªã¼ã³ã®ç·)ããã裾ã®åºãåå¸ã§è¡¨ç¾ã§ãã¦ãããã¨ãåããã¾ãã
åèæç®
Dynamical Foundations of Nonextensive Statistical Mechanics Phys. Rev. Lett. 87, 180601 â Published 10 October 2001 Christian Beck http://journals.aps.org/prl/pdf/10.1103/PhysRevLett.87.180601