gwexpy.fitting
gwexpy.fitting モジュールは、iminuit を使用した高度なフィッティング機能を提供します。
概要
最小二乗フィッティング: 実数・複素数データに対応
GLS (一般化最小二乗法): 共分散行列を考慮したフィッティング
MCMC: emcee を使ったベイズ推定
統合パイプライン: ブートストラップ → GLS → MCMC のワンライナー API
クラスと関数
メイン関数
名前 |
説明 |
|---|---|
|
Series オブジェクトをフィット |
|
統合スペクトル解析パイプライン |
クラス
名前 |
説明 |
|---|---|
|
フィット結果を格納するクラス |
|
GLS コスト関数クラス |
|
実数データ用コスト関数 |
|
複素数データ用コスト関数 |
fit_series
fit_series(
series,
model,
x_range=None,
sigma=None,
cov=None,
cost_function=None,
p0=None,
limits=None,
fixed=None,
**kwargs
)
パラメータ
名前 |
型 |
説明 |
|---|---|---|
|
Series |
フィットするデータ |
|
callable / str |
モデル関数または名前 (“gaussian”, “power_law” など) |
|
tuple |
フィット範囲 (xmin, xmax) |
|
array / scalar |
誤差( |
|
BifrequencyMap / ndarray |
共分散行列(GLS 用) |
|
callable |
カスタムコスト関数(最優先) |
|
dict / list |
初期パラメータ値 |
|
dict |
パラメータ制限 {“A”: (0, 100)} |
|
list |
固定するパラメータ名 |
戻り値
FitResult オブジェクト
使用例
from gwexpy.frequencyseries import FrequencySeries
from gwexpy.fitting import fit_series
# データ準備
fs = FrequencySeries(y, frequencies=frequencies)
# 基本的なフィット
result = fit_series(fs, "power_law", p0={"A": 10, "alpha": -1.5})
# GLS フィット(共分散行列を使用)
result = fit_series(fs, "power_law", cov=covariance_matrix, p0={"A": 10, "alpha": -1.5})
# 結果確認
print(result.params)
print(result.errors)
result.plot()
fit_bootstrap_spectrum
fit_bootstrap_spectrum(
data_or_spectrogram,
model_fn,
freq_range=None,
method="median",
rebin_width=None,
block_size=None,
ci=0.68,
window="hann",
fftlength=None,
overlap=None,
n_boot=1000,
initial_params=None,
bounds=None,
fixed=None,
run_mcmc=False,
mcmc_walkers=32,
mcmc_steps=5000,
mcmc_burn_in=500,
plot=True,
progress=True,
)
パラメータ
名前 |
型 |
説明 |
|---|---|---|
|
TimeSeries / Spectrogram |
入力データ |
|
callable |
モデル関数 |
|
tuple |
フィット周波数範囲 |
|
str |
ブートストラップ平均化方法 (“median” / “mean”) |
|
float |
周波数リビン幅 (Hz) |
|
float / Quantity / ‘auto’ |
ブロックブートストラップのブロック時間長(秒) |
|
float |
信頼区間 (デフォルト: 0.68) |
|
str |
FFT ウィンドウ関数 (デフォルト: “hann”) |
|
float or Quantity |
FFT セグメント長(秒) (デフォルト: None、自動計算) |
|
float or Quantity |
オーバーラップ長(秒) (デフォルト: None、ウィンドウ依存) |
|
int |
ブートストラップ回数 |
|
dict |
初期パラメータ |
|
dict |
パラメータ制限 |
|
bool |
MCMC を実行するか |
|
int |
MCMC ステップ数 |
|
bool |
結果をプロットするか |
戻り値
FitResult オブジェクト(追加属性付き):
psd: ブートストラップ PSDcov: 共分散 BifrequencyMapbootstrap_method: 使用した平均化方法
使用例
from gwexpy.fitting import fit_bootstrap_spectrum
def power_law(f, A, alpha):
return A * f**alpha
result = fit_bootstrap_spectrum(
spectrogram,
model_fn=power_law,
freq_range=(5, 50),
rebin_width=0.5,
block_size=2.0, # 2秒
initial_params={"A": 10, "alpha": -1.5},
run_mcmc=True,
mcmc_steps=3000,
)
# 結果
print(result.params)
print(result.parameter_intervals) # MCMC 信頼区間
result.plot_corner()
FitResult
フィット結果を格納するクラス。
プロパティ
名前 |
型 |
説明 |
|---|---|---|
|
dict |
ベストフィットパラメータ |
|
dict |
パラメータ誤差 |
|
float |
χ² 値 |
|
int |
自由度 |
|
float |
換算 χ² |
|
ndarray |
GLS 共分散逆行列 |
|
dict |
MCMC パーセンタイル (16, 50, 84) |
|
ndarray |
フル MCMC チェーン |
|
ndarray |
MCMC サンプル(バーンイン後) |
メソッド
名前 |
説明 |
|---|---|
|
データとフィット曲線をプロット |
|
ボード線図(複素数データ用) |
|
MCMC を実行 |
|
コーナープロット |
|
信頼帯付きフィットプロット |
使用例
result = fit_series(fs, "power_law", p0={"A": 10, "alpha": -1.5})
# 基本情報
print(result) # Minuit の出力
print(f"χ²/dof = {result.reduced_chi2:.2f}")
# プロット
result.plot()
# MCMC
result.run_mcmc(n_steps=5000, burn_in=500)
print(result.parameter_intervals)
result.plot_corner()
result.plot_fit_band()
GeneralizedLeastSquares
共分散逆行列を使った GLS コスト関数。
class GeneralizedLeastSquares:
errordef = 1.0 # Minuit.LEAST_SQUARES
def __init__(self, x, y, cov_inv, model):
...
def __call__(self, *args) -> float:
# χ² = r.T @ cov_inv @ r
...
@property
def ndata(self) -> int:
...
使用例
from gwexpy.fitting import GeneralizedLeastSquares, fit_series
from iminuit import Minuit
# 直接使用
def linear(x, a, b):
return a * x + b
gls = GeneralizedLeastSquares(x, y, cov_inv, linear)
m = Minuit(gls, a=1, b=0)
m.migrad()
# fit_series 経由
result = fit_series(ts, linear, cost_function=gls, p0={"a": 1, "b": 0})
組み込みモデル
gwexpy.fitting.models で利用可能なモデル:
名前 |
数式 |
パラメータ |
|---|---|---|
|
A * exp(-(x-μ)²/(2σ²)) |
A, mu, sigma |
|
A * x^α |
A, alpha |
|
A _ exp(-t/τ) _ cos(2πft + φ) |
A, tau, f, phi |
|
c₀ + c₁x + c₂x² + … |
p0, p1, … |
|
A / ((x-x₀)² + γ²) |
A, x0, gamma |
|
A * exp(-x/τ) |
A, tau |
依存関係
iminuit: 必須emcee: MCMC 機能に必要corner: コーナープロットに必要
単位とモデルのセマンティクス
gwexpy.fitting はデータとモデル間の単位の整合性を保証します:
単位の伝播:
result.model(x)でモデルを評価すると、入力データの単位が自動的に適用されます。入力
xが Hz 単位の場合、モデルは周波数単位で評価されます。出力
yはフィットしたデータの単位を保持します(例:m, V²/Hz)。
パラメータアクセス:
result.params['name']は.valueと.error属性を持つオブジェクトを返し、数値と統計的不確かさを分離します。