Skip to main content

mcmc-estimator

A Markov Chain Monte Carlo (MCMC) process that estimates a function's parameters using Metropolis–Hastings to fit data. See the notebook for all examples (damped oscillation example shown below).

Notebook Example: Damped Oscillation

$$y(x)=A⋅e^{−kx}⋅cos(ωx+ϕ)$$

import numpy as np
from mcmc_estimator import MCMCParameterEstimator as mcmc

np.random.seed(42)

# Test data
A_true = 3.0      # Amplitude
k_true = 0.5      # Decay rate
omega_true = 2.0  # Angular frequency
phi_true = 0.5    # Phase offset
sigma_true = 0.3  # Noise level
np.random.seed(42)
x_data = np.linspace(0, 10, 100)  # x spans 0 to 10
y_true = A_true * np.exp(-k_true * x_data) * np.cos(omega_true * x_data + phi_true)
y_data = y_true + np.random.normal(0, sigma_true, size=len(x_data))

# Define model function
def damped_oscillation(params, x):
    A, k, omega, phi = params
    return A * np.exp(-k * x) * np.cos(omega * x + phi)

# MCMC Estimator
estimator = mcmc(
    model_fn=damped_oscillation,
    x_data=x_data,
    y_data=y_data,
    initial_params=[1.0, 1.0, 1.0, 0.0],  # Initial guess for A, k, omega, phi
    step_size=[0.2, 0.05, 0.1, 0.1],      # Step sizes tuned for different scales
    n_iterations=30000,
    burn_in=0.3,
    sigma_obs=sigma_true,
    random_seed=42
)
estimator.run()
estimator.summary()

Acceptance Rate = 0.113
Parameter Estimates:
Param 0: mean=3.157, std=0.211, 95% CI=(2.770, 3.606)
Param 1: mean=0.491, std=0.042, 95% CI=(0.417, 0.580)
Param 2: mean=2.002, std=0.044, 95% CI=(1.914, 2.087)
Param 3: mean=0.443, std=0.060, 95% CI=(0.323, 0.560)

estimator.plot_diagnostics()

mcmc-estimator

estimator.plot_fit()

mcmc-estimator

# Access the parameter samples and calculate the means
samples = estimator.get_samples()
print(samples.mean(axis=0))

[3.15697316 0.49097106 2.00200042 0.44327931]

Metadata

Release files for mcmc-estimator 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mcmc-estimator 0.0.2
File Size Uploaded
mcmc-estimator-0.0.2.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mcmc-estimator 0.0.2
File Interpreter ABI Platform
mcmc_estimator-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.9 kB

Release files / mcmc-estimator-0.0.2.tar.gz

Download URL mcmc-estimator-0.0.2.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
2016bcc3b20d02b19e083d94c592a95a294a0cf00c8955ef940aeef3e25791dd
BLAKE2b-256 checksum
How to use checksums
212478c247f1f1c0f671337fd7d382f086876217f853c9d7006f9338bd48d991
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.8

Release files / mcmc_estimator-0.0.2-py3-none-any.whl

Download URL mcmc_estimator-0.0.2-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eac5d90b565acc286126f638fa32849dbd134ea540ff2166c572cf252dee2c24
BLAKE2b-256 checksum
How to use checksums
c8fba80a412f3cf67fcf6cc3d2f0f87a5363e86938798559b25baeb49cb25ccd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.8

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page