跳转到主要内容

使用最大似然和贝叶斯方法进行基于似然的参数估计

项目描述

docs/badges/tests-badge.svg docs/badges/coverage-badge.svg

dataprob被设计成允许科学家轻松地将用户定义的模型拟合到实验数据。它允许使用简单一致的用户界面进行最大似然、自举和贝叶斯分析。

设计原则

  • 易用性: 用户编写一个描述其模型的Python函数,然后将实验数据作为DataFrame加载。

  • 以DataFrame为中心: 使用pandas DataFrame来指定参数范围、猜测值、固定性和先验。观测数据可以作为DataFrame或NumPy向量传递。所有输出都是pandas DataFrame。

  • 一致的体验:用户可以使用相同界面和几乎相同的诊断输出进行最大似然、自助重采样或贝叶斯MCMC分析。

  • 可解释性:提供诊断图并运行测试以验证拟合结果。

简单示例

以下代码生成噪声线性数据并使用dataprob找到其斜率和截距的最大似然估计。 在Google Colab上运行

import dataprob
import numpy as np

# Generate "experimental" linear data (slope = 5, intercept = 5.7) that has
# random noise on each point.
x_array = np.linspace(0,10,25)
noise = np.random.normal(loc=0,scale=0.5,size=x_array.shape)
y_obs = 5*x_array + 5.7 + noise

# 1. Define a linear model
def linear_model(m=1,b=1,x=[]):
    return m*x + b

# 2. Set up the analysis. 'method' can be "ml", "mcmc", or "bootstrap"
f = dataprob.setup(linear_model,
                   method="ml",
                   non_fit_kwargs={"x":x_array})

# 3. Fit the parameters of linear_model model to y_obs, assuming uncertainty
#    of 0.5 on each observed point.
f.fit(y_obs=y_obs,
      y_std=0.5)

# 4. Access results
fig = dataprob.plot_summary(f)
fig = dataprob.plot_corner(f)
print(f.fit_df)
print(f.fit_quality)

图表将如下所示

data.plot_summary result data.plot_corner result

f.fit_df数据框将类似于

索引

名称

估计

标准

95%低值

95%高值

先验标准

m

m

5.009

0.045

4.817

5.202

NaN

b

b

5.644

0.274

4.465

6.822

NaN

f.fit_quality数据框将类似于

名称

描述

是否良好

观测数

观测数数量

True

25.000

参数数

拟合参数数量

True

2.000

lnL

对数似然

True

-18.761

chi2

卡方拟合优度

True

0.241

reduced_chi2

缩减卡方

True

1.192

mean0_resid

残差均值t检验 != 0

True

1.000

durbin-watson

相关残差Durbin-Watson检验

True

2.265

ljung-box

相关残差Ljung-Box检验

True

0.943

安装

我们建议使用pip安装dataprob

pip install dataprob

从源代码安装并运行测试

git clone https://github.com/harmslab/dataprob.git
cd dataprob
pip install .

# to run test-suite
pytest --runslow

示例

通过学习示例是学习如何使用库的好方法。以下笔记本包含在dataprob/examples/目录中。它们是包含dataprob用于分析各种实验数据的自包含演示。以下链接将每个笔记本在Google Colab中启动

文档

完整文档在readthedocs上。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。

源代码发行版

dataprob-0.9.4.tar.gz (50.1 kB 查看哈希值)

上传于

构建版本

dataprob-0.9.4-py2.py3-none-any.whl (62.5 kB 查看哈希值)

上传于 Python 2 Python 3

支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页面