Skip to main content

Estimates a function's parameters using Metropolis-Hastings to fit data.

Project description

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()

vizmath

estimator.plot_fit()

vizmath

# 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]

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

mcmc-estimator-0.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

mcmc_estimator-0.0.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file mcmc-estimator-0.0.1.tar.gz.

File metadata

  • Download URL: mcmc-estimator-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.8

File hashes

Hashes for mcmc-estimator-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f3e6d36373cc512dc1ffbc673818bef31dbc08ef3b3021ed69e58b4d25c6dadf
MD5 d5abef77086d5f73e45d9b5dfd15b7d6
BLAKE2b-256 89aec6b8e8f3fa6cbca9befd0fd9db18df5233d851db61bcf76a09db477538e1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mcmc_estimator-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.8

File hashes

Hashes for mcmc_estimator-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1dc12e131936e73f7181397a1b38a7f880e0af401fac9fed73753e7cd5c56112
MD5 0d13cfb1114df38d7e2dd6af56c112fa
BLAKE2b-256 ac52ca17b2a31e1c6f8d01a11bab55f80769a5eefb4e6aa3d3d697e221ea5ba0

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