Python接口访问后验数据库
项目描述
Python版本
目前仅支持Python 3.6+。如有需要,可以添加Python 3.5+的支持。我们不打算支持Python 2。
安装
推荐从PyPI安装。
pip install posteriordb
从本地克隆安装
git clone https://github.com/MansMeg/posteriordb
cd posteriordb
pip install python/
使用Python从后验数据库
包含的数据库包含访问数据、模型代码和单个后验信息的便利函数。
首先我们创建要使用的后验数据库,这里是指定的克隆后验数据库。
>>> from posteriordb import PosteriorDatabase
>>> import os
>>> pdb_path = os.path.join(os.getcwd(), "posterior_database")
>>> my_pdb = PosteriorDatabase(pdb_path)
上述代码要求您的当前工作目录位于此项目的副本的主文件夹中。或者,您可以直接指定文件夹的路径。
可以使用PosteriorDatabaseGithub
类使用在线数据库。请记住创建并设置GITHUB_PAT
环境变量。建议用户为posteriordb
使用创建一个只读(没有额外权限)的GitHub个人访问令牌(PAT)。还建议将GITHUB_PAT
环境变量添加到用户环境变量中,并且不要像下面的示例那样在Python脚本中显示。
如果没有明确定义,PosteriorDatabase
和PosteriorDatabaseGithub
将在定义了POSTERIOR_DB_PATH
的情况下创建一个新数据库(或使用旧数据库),位置在POSTERIOR_DB_PATH
。如果没有设置环境变量,PosteriorDatabaseGithub
将最终使用$HOME/.posteriordb/posterior_database
作为备用位置。每个模型和数据只在需要时下载和缓存。
>>> from posteriordb import PosteriorDatabaseGithub
>>> import os
>>> # It is recommended that GITHUB_PAT is added to the user environmental variables
>>> # outside python and not in a python script as shown in this example code
>>> os.environ["GITHUB_PAT"] = "token-string-here"
>>> my_pdb = PosteriorDatabaseGithub()
要列出可用的后验名称,使用posterior_names
。
>>> pos = my_pdb.posterior_names()
>>> pos[:5]
['roaches-roaches_negbin',
'syn_gmK2D1n200-gmm_diagonal_nonordered',
'radon_mn-radon_variable_intercept_centered',
'syn_gmK3D2n300-gmm_nonordered',
'radon-radon_hierarchical_intercept_centered']
同样地,我们可以列出数据库中包含的数据和模型
>>> mn = my_pdb.model_names()
>>> mn[:5]
['gmm_diagonal_nonordered',
'radon_pool',
'radon_partial_pool_noncentered',
'blr',
'radon_hierarchical_intercept_noncentered']
>>> dn = my_pdb.dataset_names()
>>> dn[:5]
['radon_mn',
'wells_centered',
'radon',
'wells_centered_educ4_interact',
'wells_centered_educ4']
后验名称由拟合数据的数据和模型组成。这两个一起唯一地定义了一个后验分布。要访问后验对象,我们可以使用后验名称。
>>> posterior = my_pdb.posterior("eight_schools-eight_schools_centered")
从后验中我们可以访问数据集和模型
>>> model = posterior.model
>>> data = posterior.data
我们还可以访问后验、模型和数据集的名称。
>>> posterior.name
"eight_schools-eight_schools_centered"
>>> model.name
"eight_schools_centered"
>>> data.name
"eight_schools"
我们还可以直接从后验数据库中访问相同的模型和数据集
>>> model = my_pdb.model("eight_schools_centered")
>>> data = my_pdb.data("eight_schools")
从模型中我们可以访问模型代码和有关模型的信息
>>> model.code("stan")
data {
int <lower=0> J; // number of schools
real y[J]; // estimated treatment
real<lower=0> sigma[J]; // std of estimated effect
}
parameters {
real theta[J]; // treatment effect in school j
real mu; // hyper-parameter of mean
real<lower=0> tau; // hyper-parameter of sdv
}
model {
tau ~ cauchy(0, 5); // a non-informative prior
theta ~ normal(mu, tau);
y ~ normal(theta , sigma);
mu ~ normal(0, 5);
}
>>> model.code_file_path("stan")
'/home/eero/posterior_database/content/models/stan/eight_schools_centered.stan'
>>> model.information
{'keywords': ['bda3_example', 'hiearchical'],
'description': 'A centered hiearchical model for the 8 schools example of Rubin (1981)',
'urls': ['http://www.stat.columbia.edu/~gelman/arm/examples/schools'],
'title': 'A centered hiearchical model for 8 schools',
'references': ['rubin1981estimation', 'gelman2013bayesian'],
'added_by': 'Mans Magnusson',
'added_date': '2019-08-12'}
请注意,参考文献引用的是可以在 content/references/references.bib
中找到的 BibTeX 项目。
从数据集中,我们可以访问数据值及其相关信息。
>>> data.values()
{'J': 8,
'y': [28, 8, -3, 7, -1, 1, 18, 12],
'sigma': [15, 10, 16, 11, 9, 11, 10, 18]}
>>> data.file_path()
'/tmp/tmpx16edu0w'
>>> data.information
{'keywords': ['bda3_example'],
'description': 'A study for the Educational Testing Service to analyze the effects of\nspecial coaching programs on test scores. See Gelman et. al. (2014), Section 5.5 for details.',
'urls': ['http://www.stat.columbia.edu/~gelman/arm/examples/schools'],
'title': 'The 8 schools dataset of Rubin (1981)',
'references': ['rubin1981estimation', 'gelman2013bayesian'],
'added_by': 'Mans Magnusson',
'added_date': '2019-08-12'}
要访问黄金标准后验抽样的值,可以使用 reference_draws
,如下所示。
>>> posterior.reference_draws_info()
{'name': 'eight_schools-eight_schools_noncentered',
'inference': {'method': 'stan_sampling',
'method_arguments': {'chains': 10,
'iter': 20000,
'warmup': 10000,
'thin': 10,
'seed': 4711,
'control': {'adapt_delta': 0.95}}},
'diagnostics': {'diagnostic_information': {'names': ['mu',
'tau',
'theta[1]',
...
>>> gs = posterior.reference_draws()
>>> import pandas as pd
>>> pd.DataFrame(gs)
theta[1] theta[2]
0 [10.6802773011458, 6.45383910854259, -2.241629... [9.71770681295263, 4.41030824418493, 0.7617047...
1 [5.70891361633589, 10.3012059848039, 4.2439533... [-2.32310565394337, 14.8121789773659, 6.517256...
2 [7.23747096507585, -0.427831558524343, 9.14782... [7.35425759420389, 8.69579738064637, 8.9058764...
3 [4.44915522912766, 2.34711393762556, 17.680378... [2.4368039319606, 5.89809320808632, 8.63031558...
...
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。
源分发
构建分发
posteriordb-0.2.0-py3-none-any.whl 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b6d6f3a349d34db6d4a68da899c818a95e5824c5e23824fc0ebe422f4bd6bac1 |
|
MD5 | cd1ad74848445518c3dc2af5b45e9edd |
|
BLAKE2b-256 | 8f4db72e0782abec07f3d8dabf24cf12673d26b173af2046eb4e67365c776ccf |