足球联赛的贝叶斯模型
bpl的Python项目详细描述
bpl
bpl
是一个python 3库,用于将dixon&coles(1997)模型的贝叶斯版本拟合到数据中。
它使用stan
库使模型适合数据。
安装
你需要一个工作C++编译器。 如果您使用的是anaconda,则可以使用
conda install gcc
然后可以使用pip
:
pip install bpl
这可能需要一段时间,因为两个stan模型是作为构建的一部分编译的。
用法
bpl
提供了一个可用于预测足球比赛结果的类BPLModel
。
数据应以pandas
数据帧的形式提供给模型,其中包含home_team
、away_team
、home_goals
和away_goals
列。
你也可以选择为每支球队提供一组数值协变量(例如他们在国际足联的评分),这些变量将用于fit。
示例用法:
importbplimportpandasaspddf_train=pd.read_csv("<path-to-training-data>")df_X=pd.read_csv("<path-to-team-level-covariates>")forecaster=bpl.BPLModel(data=df_train,X=df_X)forecaster.fit(seed=42)# calculate the probability that team 1 beats team 2 3-0 at home:forecaster.score_probability("Team 1","Team 2",3,0)# calculate the probabilities of a home win, an away win and a draw:forecaster.overall_probabilities("Team 1","Team 2")# compute home win, away win and draw probabilities for a collection of matches:df_test=pd.read_csv("<path-to-test-data>")# must have columns "home_team" and "away_team"forecaster.predict_future_matches(df_test)# add a new, previously unseen team to the model by sampling from the priorX_3=np.array([0.1,-0.5,3.0])# the covariates for the new teamforecaster.add_new_team("Team 3",X=X_3,seed=43)