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

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]

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.2.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.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mcmc-estimator-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 2016bcc3b20d02b19e083d94c592a95a294a0cf00c8955ef940aeef3e25791dd
MD5 1c006d16896f57f097be9ec1254a3520
BLAKE2b-256 212478c247f1f1c0f671337fd7d382f086876217f853c9d7006f9338bd48d991

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mcmc_estimator-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eac5d90b565acc286126f638fa32849dbd134ea540ff2166c572cf252dee2c24
MD5 7300d75e376844d093755ab0f9bc5b31
BLAKE2b-256 c8fba80a412f3cf67fcf6cc3d2f0f87a5363e86938798559b25baeb49cb25ccd

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