Skip to main content

simple tool for computing probability on parameter grid and visualization

Project description

PonGrid : Probability on Grid

simple code for computing probability on parameter grid and visualization

Installation

  • github (latest) : pip install -U git+git://github.com/sarashenyy/PonGrid
  • pypi (stable): pip install pongrid

Requirements

  • numpy
  • scipy
  • tqdm
  • seaborn
  • matplotlib
  • (joblib)

Gaussian2D as an example

step1. make gaussian2D data

import numpy as np
from scipy.stats import multivariate_normal

np.random.seed(42)
mu1, mu2 = 0, 1 # 均值
sigma1, sigma2 = 2, 2 # 方差
mu_true = [mu1, mu2]  
cov_true = [[sigma1 ** 2, 0], [0, sigma2 ** 2]] 
data = np.random.multivariate_normal(mu_true, cov_true, size=5000)

step2. define posterior function

# 定义 log likelihood 函数
def log_likelihood(theta, data):
    mu1, mu2, sigma1, sigma2 = theta
    cov = [[sigma1 ** 2, 0], [0, sigma2 ** 2]]
    return np.sum(multivariate_normal.logpdf(data, mean=[mu1, mu2], cov=cov))


# 定义 prior 函数
def log_prior(theta):
    mu1, mu2, sigma1, sigma2 = theta
    if sigma1 > 0 and sigma2 > 0:
        return 0.0
    return -np.inf

# 定义 log posterior 函数
def log_posterior_wrapin(theta, data):
    return log_prior(theta) + log_likelihood(theta, data)

# 将数据包装进 log_posterior() 中,以便于后续被 PonGrid 调用,具体见 example/ex_grid_gaussian2D.py
def log_posterior(theta):
    return log_posterior_wrapin(theta, data)

step3. define the parameter range you want

from pongrid import PonGrid
pg = PonGrid(
    param_num=4,
    param_name=['mu1', 'mu2', 'sigma1', 'sigma2'],
    param_range=[[-0.5, 0.5, 0.05], # start, end, step
                 [0.5, 1.5, 0.05],
                 [1.5, 2.5, 0.05],
                 [1.5, 2.5, 0.05]]
)

step4. run grid

joint_log_posterior = pg.run_grid(
    log_posteriord=log_posterior)

# save joint_log_likelihood if needed
joblib.dump(joint_log_posterior, 'ex_gridpost_gaussian2D.joblib')
# if you want to read
# joint_log_likelihood = joblib.load('ex_gridpost_gaussian2D.joblib')

step5. check log(posterior) values

Because np.exp() can only maintain calculation accuracy in [-745, 705], we need to shift the log(posterior) value by adding a max number to the whole if they are smaller than -745. The recommand shift value is 10.

joint_posterior, joint_posterior_shifted = pg.check_log_posterior(shift=True, shifted_to=10)

step6. draw probability on grid

pg.show_grid_probability(
    figpath='ex_gridres_gaussian2D.png',  # save fig path
    labels=['$\mu_1$', '$\mu_2$', '$\sigma_1$', '$\sigma_2$'],
    truths=[mu1, mu2, sigma1, sigma2] 
    )

mcmc results

import emcee
import corner

# 使用 emcee 进行采样
ndim = 4  # 参数维度
nwalkers = 32  # 行走者数量
nsteps = 5000  # 步数

# 初始化行走者的起始位置
p0 = np.random.rand(nwalkers, ndim)

# 创建采样器
sampler = emcee.EnsembleSampler(nwalkers, ndim, log_posterior, args=[data])
sampler.run_mcmc(p0, nsteps, progress=True)

samples= sampler.get_chain(flat=True, discard=2000)
fig = corner.corner(
    samples, 
    truths=[mu1, mu2, sigma1, sigma2],
    labels=['$\mu_1$', '$\mu_2$', '$\sigma_1$', '$\sigma_2$'],
    quantiles=[0.16, 0.5, 0.84],
    show_titles=True,
    title_kwargs={'fontsize': 18},
    # title_fmt='.2f'
)
fig.savefig('ex_mcmc_gaussian2D.png')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pongrid-0.0.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pongrid-0.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pongrid-0.0.1.tar.gz.

File metadata

  • Download URL: pongrid-0.0.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for pongrid-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7a27134d7adfa15959909cec4d6ec485424d2e498b01dc6eb122366efb8818bc
MD5 d9b3d20e48f7f291c597be54b2cfbe3e
BLAKE2b-256 824abdc454d8e77e3d0d7d51839b933ddd91d1ab3d13f95e7bc51053f7f5444d

See more details on using hashes here.

File details

Details for the file pongrid-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pongrid-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for pongrid-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c67eb2c8dc7477e439581fdc8f9153de61f976ebbf65022abf5c094a5b9b65b4
MD5 7a106753fce52d42daf0b3c0753341e1
BLAKE2b-256 98506433adfed3fcad3df3d4a0057743f2b9c8d4508a5ff96f86aa0c41862ef0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page