程序仍有问题

This commit is contained in:
2025-11-05 08:36:42 +08:00
parent b6a9feddc7
commit b1ba96b691
6 changed files with 1394 additions and 189 deletions
+468 -152
View File
@@ -48,8 +48,10 @@ class RJMCMC_Sampler:
self.lambda_b_z = 1e-3
# 初始化噪声方差 (从先验采样)
self.current_sigma2_w = 1.0 / np.random.gamma(self.lambda_a_w, 1.0/self.lambda_b_w)
self.current_sigma2_z = 1.0 / np.random.gamma(self.lambda_a_z, 1.0/self.lambda_b_z)
# 为避免除零错误,在分母上增加一个极小值
epsilon = 1e-9
self.current_sigma2_w = 1.0 / (np.random.gamma(self.lambda_a_w, 1.0/self.lambda_b_w) + epsilon)
self.current_sigma2_z = 1.0 / (np.random.gamma(self.lambda_a_z, 1.0/self.lambda_b_z) + epsilon)
def _update_current_eigenvalues(self):
@@ -59,6 +61,16 @@ class RJMCMC_Sampler:
self.current_eigenvalues = np.roots(poly_coeffs)
else:
self.current_eigenvalues = np.array([])
def _update_current_a_coeffs(self):
"""一个辅助函数,根据 current_eigenvalues 更新 current_a。"""
k = len(self.current_eigenvalues)
if k > 0:
# np.poly 返回 [1, a_{k-1}, ..., a_0],去掉首项“1”,反转剩下的
poly_coeffs = np.poly(self.current_eigenvalues)
self.current_a = np.real(poly_coeffs[1:][::-1])
else:
self.current_a = np.array([])
def _cal_current_eigenvalues(self, a_coeffs_temp):
"""一个辅助函数,根据给定的 a_coeffs 计算对应的特征值。"""
@@ -206,7 +218,7 @@ class RJMCMC_Sampler:
# --- 计算似然 ---
# 预测误差 nu_t
y_pred = C @ x_pred + D @ u_t
y_pred = C @ x_pred + D @ np.array([[u_t]])
nu_t = y_t - y_pred
# 预测误差协方差 S_t
@@ -228,7 +240,7 @@ class RJMCMC_Sampler:
P_update = I_KC @ P_pred @ I_KC.T + K_t @ Gamma @ K_t.T
# --- 为下一次循环准备预测 (t+1) ---
x_pred = A @ x_update + B @ u_t
x_pred = A @ x_update + B @ np.array([[u_t]])
P_pred = A @ P_update @ A.T + Sigma
return total_log_likelihood # 返回标量值
@@ -241,6 +253,14 @@ class RJMCMC_Sampler:
k = self.current_k
current_eigs = self.current_eigenvalues
# 获取实数特征值数目
real_eigs_indices = np.where(np.isreal(current_eigs))
num_real_eigs = len(real_eigs_indices[0])
# 获取复数特征值数目,只保留 imag > 0 的部分 (共轭对只算一次)
complex_eigs_indices = np.where((np.iscomplex(current_eigs)) & (current_eigs.imag > 0)) #type: ignore
num_complex_eigs = len(complex_eigs_indices[0])
# 决定是诞生一个实数根还是一对复共轭根
can_add_complex = (k + 2) <= self.k_max
add_real = True
@@ -252,33 +272,51 @@ class RJMCMC_Sampler:
if add_real:
k_new = k + 1
# 诞生实根的对数概率之比
log_birth_real_forward = np.log(0.5)
log_birth_real_backward = np.log(0.5)
# 从提议分布 q(u) 中采样辅助变量 u = (u_lambda, u_b)
u_lambda = np.random.uniform(-1.0, 1.0) # 新特征值
u_b = np.random.normal(0, 1) # 新 b 系数
# 计算对数提议密度 log(q(u))
log_q_forward = -np.log(2.0) + stats.norm.logpdf(u_b, 0, 1)
# 计算对数提议密度
log_q_forward = log_birth_real_forward + stats.norm.logpdf(u_b, 0, 1) - np.log(2.0)
log_q_backward = log_birth_real_backward - np.log(num_real_eigs + 1)
# 计算对数雅可比行列式 log|J|
if k == 0:
log_det_jacobian = 0.0
else:
log_det_jacobian = np.sum(np.log(np.abs(current_eigs - u_lambda)))
log_det_jacobian = 1
# 构造新状态
new_eigs = np.append(current_eigs, u_lambda)
new_a = np.real(np.poly(new_eigs)[1:][::-1])
new_b = np.append(self.current_b, u_b)
# 接受率中的项为 |J| / q(u),在对数空间中为 log|J| - log(q(u))
log_ratio = log_det_jacobian - log_q_forward
# 接受率中的项为 q_backward(u) * |J| / q_forward(u)
log_ratio = log_q_backward + log_det_jacobian - log_q_forward
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "birth_real", "new_eigs": new_eigs}
# 计算未归一化的后验之比
log_post_unnormalized_current = self._log_posterior_unnormalized(k, self.current_a, self.current_b,
self.current_sigma2_w, self.current_sigma2_z)
log_post_unnormalized_new = self._log_posterior_unnormalized(k_new, new_a, new_b, self.current_sigma2_w, self.current_sigma2_z)
# 计算接受率
log_acceptance_ratio = log_post_unnormalized_new - log_post_unnormalized_current + log_ratio
acceptance_ratio = np.exp(log_acceptance_ratio)
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "birth_real", "new_eigs": new_eigs, "acceptance_ratio": acceptance_ratio}
else:
# --- 诞生一对复共轭根 (k -> k+2) ---
k_new = k + 2
# 诞生实根的对数概率之比
log_birth_complex_forward = np.log(0.5)
log_birth_complex_backward = np.log(0.5)
# 采样辅助变量 u = (rho, theta, u_b1, u_b2)
rho = np.sqrt(np.random.uniform(0, 1.0))
theta = np.random.uniform(0, np.pi)
@@ -286,25 +324,31 @@ class RJMCMC_Sampler:
u_b1, u_b2 = np.random.normal(0, 1, 2)
# 计算对数提议密度 log(q(u))
log_q_forward = -np.log(np.pi) + stats.norm.logpdf(u_b1, 0, 1) + stats.norm.logpdf(u_b2, 0, 1)
log_q_forward = log_birth_complex_forward + stats.norm.logpdf(u_b1, 0, 1) + stats.norm.logpdf(u_b2, 0, 1) - np.log(np.pi) - np.log(1.0)
log_q_backward = log_birth_complex_backward - np.log(num_complex_eigs + 1)
# 计算对数雅可比行列式 log|J|
# |J| = |product(|u_lambda - lambda_i|^2) * (2*Im(u_lambda))|
if k == 0:
log_jacobian = np.log(np.abs(2 * u_lambda.imag))
else:
log_jacobian = np.sum(np.log(np.abs(u_lambda - current_eigs)**2)) + \
np.log(np.abs(2 * u_lambda.imag))
# |J| = 2*ρ
log_jacobian = np.log(2 * rho)
# 构造新状态
new_eigs = np.append(current_eigs, [u_lambda, np.conjugate(u_lambda)])
new_a = np.real(np.poly(new_eigs)[1:][::-1])
new_b = np.append(self.current_b, [u_b1, u_b2])
log_ratio = log_jacobian - log_q_forward
log_ratio = log_q_backward + log_jacobian - log_q_forward
# 计算未归一化的后验之比
log_post_unnormalized_current = self._log_posterior_unnormalized(k, self.current_a, self.current_b,
self.current_sigma2_w, self.current_sigma2_z)
log_post_unnormalized_new = self._log_posterior_unnormalized(k_new, new_a, new_b, self.current_sigma2_w, self.current_sigma2_z)
# 计算接受率
log_acceptance_ratio = log_post_unnormalized_new - log_post_unnormalized_current + log_ratio
acceptance_ratio = np.exp(log_acceptance_ratio)
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "birth_complex", "new_eigs": new_eigs, "acceptance_ratio": acceptance_ratio}
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "birth_real", "new_eigs": new_eigs}
def _propose_death(self):
"""
提议一个 "消亡" 转移 (k -> k-1 或 k -> k-2)。
@@ -317,8 +361,8 @@ class RJMCMC_Sampler:
real_eigs_indices = np.where(np.isreal(current_eigs))
complex_eigs_indices = np.where((np.iscomplex(current_eigs)) & (current_eigs.imag > 0)) #type: ignore
can_remove_real = len(real_eigs_indices) > 0
can_remove_complex = len(complex_eigs_indices) > 0
can_remove_real = len(real_eigs_indices[0]) > 0
can_remove_complex = len(complex_eigs_indices[0]) > 0
if not can_remove_real and not can_remove_complex:
return None # 无法执行消亡
@@ -335,66 +379,129 @@ class RJMCMC_Sampler:
# --- 消亡一个实数根 (k -> k-1) ---
k_new = k - 1
# 生成与消亡实根对应的诞生过程的对数概率之比
log_death_real_forward = np.log(0.5)
log_death_real_backward = np.log(0.5)
# 1. 随机选择一个实数根移除
idx_to_remove = np.random.choice(real_eigs_indices)
idx_to_remove = np.random.choice(real_eigs_indices[0])
lambda_removed = current_eigs[idx_to_remove]
# 2. 随机选择一个实数 b 系数移除
idx_b_to_remove = np.random.choice(len(self.current_b))
b_removed = self.current_b[idx_b_to_remove]
# 移除的根和b系数构成了逆向(诞生)提议的辅助变量 u
u_lambda = lambda_removed
u_b = self.current_b[-1]
u_b = b_removed
# 2. 计算逆向提议的对数密度 log(q(u))
log_q_reverse = -np.log(2.0) + stats.norm.logpdf(u_b, 0, 1)
log_q_forward = log_death_real_forward - np.log(len(real_eigs_indices[0])) - np.log(len(self.current_b))
log_q_backward = log_death_real_backward + stats.norm.logpdf(u_b, 0, 1) - np.log(2.0)
# 3. 计算对应诞生过程的对数雅可比行列式 log|J|
remaining_eigs = np.delete(current_eigs, idx_to_remove)
if k_new == 0:
log_jacobian_birth = 0.0
else:
log_jacobian_birth = np.sum(np.log(np.abs(u_lambda - remaining_eigs)))
log_jacobian_birth = 1
# 构造新状态
new_a = np.real(np.poly(remaining_eigs)[1:][::-1])
new_b = self.current_b[:-1]
if k_new == 0:
new_a = np.array([])
else:
new_a = np.real(np.poly(remaining_eigs)[1:][::-1])
new_b = np.delete(self.current_b, idx_b_to_remove)
# 接受率中的项为 q_reverse(u) / |J_birth|,在对数空间中为 log(q_reverse) - log|J_birth|
log_ratio = log_q_reverse - log_jacobian_birth
# 接受率中的项为 q_backward(u) / (|J| * q_forward(u))
log_ratio = -log_q_forward - log_jacobian_birth + log_q_backward
# 计算未归一化的后验之比
log_post_unnormalized_current = self._log_posterior_unnormalized(k, current_eigs, self.current_b, self.current_sigma2_w, self.current_sigma2_z)
log_post_unnormalized_new = self._log_posterior_unnormalized(k_new, remaining_eigs, new_b, self.current_sigma2_w, self.current_sigma2_z)
# 计算接受率
log_acceptance_ratio = log_post_unnormalized_new - log_post_unnormalized_current + log_ratio
acceptance_ratio = np.exp(log_acceptance_ratio)
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "death_real", "new_eigs": remaining_eigs, "acceptance_ratio": acceptance_ratio}
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "death_real", "new_eigs": remaining_eigs}
else:
# --- 消亡一对复共轭根 (k -> k-2) ---
k_new = k - 2
rho = np.sqrt(np.random.uniform(0, 1.0))
# 生成与消亡复共轭对对应的诞生过程的对数概率之比
log_death_complex_forward = np.log(0.5)
log_death_complex_backward = np.log(0.5)
# 随机选择一对共轭根移除
complex_idx_to_remove = np.random.choice(complex_eigs_indices)
complex_idx_to_remove = np.random.choice(complex_eigs_indices[0])
lambda_removed = current_eigs[complex_idx_to_remove]
# 随机选择一对 b 系数移除,需要确保两次选的不一样
idx_b1_to_remove, idx_b2_to_remove = np.random.choice(len(self.current_b), size=2, replace=False)
b1_removed = self.current_b[idx_b1_to_remove]
b2_removed = self.current_b[idx_b2_to_remove]
# 找到其共轭对
conjugate_idx_to_remove = np.where(current_eigs == np.conjugate(lambda_removed))
conjugate_idx_to_remove_array = np.where(current_eigs == np.conjugate(lambda_removed))[0]
if len(conjugate_idx_to_remove_array) == 0:
return None # 找不到共轭对,无法执行消亡
conjugate_idx = conjugate_idx_to_remove_array[0]
# 逆向提议的辅助变量 u
u_lambda = lambda_removed
u_b1, u_b2 = self.current_b[-2:]
# 计算逆向提议的对数密度 log(q(u))
log_q_reverse = -np.log(np.pi) + stats.norm.logpdf(u_b1, 0, 1) + stats.norm.logpdf(u_b2, 0, 1)
n_b = len(self.current_b)
pair_count = n_b * (n_b - 1) / 2.0
log_q_forward = log_death_complex_forward - np.log(len(complex_eigs_indices[0])) - np.log(pair_count)
log_q_backward = log_death_complex_backward + stats.norm.logpdf(u_b1, 0, 1) + stats.norm.logpdf(u_b2, 0, 1) - np.log(np.pi) - np.log(1.0)
# 计算对应诞生过程的对数雅可比行列式
remaining_eigs = np.delete(current_eigs, [complex_idx_to_remove, conjugate_idx_to_remove])
if k_new == 0:
log_jacobian_birth = np.log(np.abs(2 * u_lambda.imag)) #type: ignore
else:
log_jacobian_birth = np.sum(np.log(np.abs(u_lambda - remaining_eigs)**2)) + \
np.log(np.abs(2 * u_lambda.imag)) #type: ignore
remaining_eigs = np.delete(current_eigs, [complex_idx_to_remove, conjugate_idx])
log_jacobian = np.log(2 * rho)
# 构造新状态
new_a = np.real(np.poly(remaining_eigs)[1:][::-1])
new_b = self.current_b[:-2]
if k_new == 0:
new_a = np.array([])
else:
new_a = np.real(np.poly(remaining_eigs)[1:][::-1])
new_b = np.delete(self.current_b, [idx_b1_to_remove, idx_b2_to_remove])
log_ratio = log_q_reverse - log_jacobian_birth
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "death_complex", "new_eigs": remaining_eigs}
log_ratio = -log_q_forward - log_jacobian + log_q_backward
# 计算未归一化的后验之比
log_post_unnormalized_current = self._log_posterior_unnormalized(k, current_eigs, self.current_b, self.current_sigma2_w, self.current_sigma2_z)
log_post_unnormalized_new = self._log_posterior_unnormalized(k_new, remaining_eigs, new_b, self.current_sigma2_w, self.current_sigma2_z)
# 计算接受率
log_acceptance_ratio = log_post_unnormalized_new - log_post_unnormalized_current + log_ratio
acceptance_ratio = np.exp(log_acceptance_ratio)
return {"k_new": k_new, "a_new": new_a, "b_new": new_b, "log_ratio": log_ratio, "type": "death_complex", "new_eigs": remaining_eigs, "acceptance_ratio": acceptance_ratio}
def _propose_within_model(self):
"""
随机选择实根变复根,复根变实根,或微调现有根,调用现有函数_merge_eigenvalues_split_eigenvalues_disturbe_swimming。
"""
k = self.current_k
current_eigs = self.current_eigenvalues.copy()
if k == 0:
return None # 无法在 k=0 时进行模型内提议
proposal_type = np.random.choice(['merge', 'split', 'disturb'], p=[0.3, 0.3, 0.4])
if proposal_type == 'merge':
return self._merge_eigenvalues(current_eigs) # type: ignore
elif proposal_type == 'split':
return self._split_eigenvalues(current_eigs) # type: ignore
else:
return self._disturbe_swimming(current_eigs) # type: ignore
def _log_prior_b(self, b_coeffs):
"""
@@ -404,6 +511,7 @@ class RJMCMC_Sampler:
"""
#
return stats.norm.logpdf(b_coeffs, 0, 1).sum()
def _generate_stable_a(self, k):
"""
@@ -513,136 +621,344 @@ class RJMCMC_Sampler:
return A_c, B_c, C_c, D_c
def _log_prior_full(self, k, a_coeffs, b_coeffs, sigma2_w, sigma2_z):
"""计算所有参数的完整对数验。"""
log_prior_k = -np.log(self.k_max - self.k_min + 1)
if k > 0:
poly_coeffs = np.concatenate(([1], -a_coeffs[::-1]))
eigenvalues = np.roots(poly_coeffs)
log_prior_a = self._log_prior_eigenvalues_to_a(eigenvalues)
else:
log_prior_a = 0.0
log_prior_b = self._log_prior_b(b_coeffs)
precision_w = 1.0 / sigma2_w
precision_z = 1.0 / sigma2_z
log_prior_w = stats.gamma.logpdf(precision_w, a=self.lambda_a_w, scale=1.0/self.lambda_b_w)
log_prior_z = stats.gamma.logpdf(precision_z, a=self.lambda_a_z, scale=1.0/self.lambda_b_z)
return log_prior_k + log_prior_a + log_prior_b + log_prior_w + log_prior_z
def _log_posterior(self, k, b_coeffs, sigma2_w, sigma2_z, a_coeffs=None, eigenvalues=None):
"""计算给定参数下的完整对数后验概率。"""
# 1. 计算先验
def _log_posterior_unnormalized(self, k, eigenvalues, b_coeffs, sigma2_w, sigma2_z):
"""计算所有参数的非归一化对数验。"""
log_prior_k = -np.log(self.k_max - self.k_min + 1)
if eigenvalues is not None:
log_prior_a = self._log_prior_eigenvalues_to_a(eigenvalues)
a_coeffs = self._cal_current_a_coeffs(eigenvalues)
elif a_coeffs is not None:
if k > 0:
eigenvalues = self._cal_current_eigenvalues(a_coeffs)
log_prior_a = self._log_prior_eigenvalues_to_a(eigenvalues)
else:
log_prior_a = 0.0
if k > 0:
log_prior_eigen = self._log_prior_eigenvalues(eigenvalues)
else:
raise ValueError("Either a_coeffs or eigenvalues must be provided.")
if log_prior_a == -np.inf: return -np.inf
log_prior_eigen = 0.0
log_prior_b = self._log_prior_b(b_coeffs)
precision_w = 1.0 / sigma2_w
precision_z = 1.0 / sigma2_z
log_prior_w = stats.gamma.logpdf(precision_w, a=self.lambda_a_w, scale=1.0/self.lambda_b_w)
log_prior_z = stats.gamma.logpdf(precision_z, a=self.lambda_a_z, scale=1.0/self.lambda_b_z)
log_prior = log_prior_k + log_prior_a + log_prior_b + log_prior_w + log_prior_z
# 2. 计算似然
log_likelihood = self._log_likelihood(k, a_coeffs, b_coeffs, sigma2_w, sigma2_z)
return log_prior + log_likelihood
log_likelihood = self._log_likelihood(k, self._cal_current_a_coeffs(eigenvalues), b_coeffs, sigma2_w, sigma2_z)
return log_prior_k + log_prior_eigen + log_prior_b + log_prior_w + log_prior_z + log_likelihood
def _purpose_b_swimming(self):
"""
对 b 系数进行游动:对每个 b_i 添加一个小的高斯扰动, 返回b游动前后的先验
"""
k = self.current_k
current_b = self.current_b
proposal_b = np.copy(current_b)
# 对每个 b_i 添加高斯扰动
for i in range(k):
b_proposal = proposal_b[i] + np.random.normal(0, 0.1) # 小的高斯扰动
proposal_b[i] = b_proposal
# 计算新b的先验
log_prior_current = self._log_prior_b(current_b)
log_prior_proposal = self._log_prior_b(proposal_b)
return proposal_b, log_prior_current, log_prior_proposal
def _purpose_lumbda_swimming(self):
"""
对 lumbda 进行游动:对每个 lumbda 添加一个小的高斯扰动,虚部也要有扰动
"""
k = self.current_k
current_eigs = np.copy(self.current_eigenvalues)
proposal_eigs = np.copy(current_eigs)
# 对每个 lumbda 添加高斯扰动
for i in range(k):
eig_proposal = proposal_eigs[i] + np.random.normal(0, 0.1) # 小的高斯扰动
proposal_eigs[i] = eig_proposal
# 虚部也要有扰动,共轭的虚部记得相等
for i in range(k):
if np.iscomplex(proposal_eigs[i]) and proposal_eigs[i].imag != 0:
imag_perturbation = np.random.normal(0, 0.1)
proposal_eigs[i] = proposal_eigs[i].real + 1j * (proposal_eigs[i].imag + imag_perturbation)
# 找到共轭根并更新
conjugate_idx = np.where(proposal_eigs == np.conjugate(current_eigs[i]))[0]
if len(conjugate_idx) > 0:
proposal_eigs[conjugate_idx[0]] = proposal_eigs[conjugate_idx[0]].real - 1j * (current_eigs[i].imag + imag_perturbation)
# 计算新lumbda的先验
log_prior_current = self._log_prior_eigenvalues(current_eigs)
log_prior_proposal = self._log_prior_eigenvalues(proposal_eigs)
return proposal_eigs, log_prior_current, log_prior_proposal
def _merge_eigenvalues(self):
def _merge_eigenvalues(self, current_eigs):
"""合并游动:选择两个实数根,确定性地合并为一个复共轭对。"""
k = self.current_k
current_eigs = np.copy(self.current_eigenvalues)
# 随机选择两个不同的实数根
real_indices = np.where(np.isclose(current_eigs.imag, 0))
idx1, idx2 = np.random.choice(real_indices, 2, replace=False)
lambda1, lambda2 = current_eigs[idx1].real, current_eigs[idx2].real
# 随机选择两个不同的实数根(可能是虚部为0的复数),并删除他们
real_indices = np.where(np.isreal(current_eigs))
if len(real_indices[0]) < 2:
return # 不足两个实数根,无法合并
idx_pair = np.random.choice(real_indices[0], size=2, replace=False)
idx1, idx2 = idx_pair
lambda1 = current_eigs[idx1].real # type: ignore
lambda2 = current_eigs[idx2].real # type: ignore
# 确定性映射 -> 新的复共轭对
a = (lambda1 + lambda2) / 2.0
b = abs(lambda2 - lambda1) / 2.0
new_complex_pair = [a + 1j*b, a - 1j*b]
current_eigs = np.delete(current_eigs, [idx1, idx2])
# 构造提议的特征值集合
proposal_eigs = np.delete(current_eigs, [idx1, idx2])
proposal_eigs = np.append(proposal_eigs, new_complex_pair)
# 生成一对共轭复根
new_theta = np.random.uniform(0, np.pi)
new_rho = np.random.uniform(0, 1.0)
new_eigenvalue = new_rho * (np.cos(new_theta) + 1j * np.sin(new_theta))
current_eigs = np.append(current_eigs, [new_eigenvalue, np.conjugate(new_eigenvalue)])
# 检查稳定性
if np.any(np.abs(proposal_eigs) >= 1.0): return
# 对b进行游动
proposal_b, log_prior_b_current, log_prior_b_proposal = self._purpose_b_swimming()
# 计算提议密度
# 正向过程是任选两个实根,合并为复共轭对
log_q_merge_forward = -np.log(len(real_indices[0]) * (len(real_indices[0]) - 1) / 2.0) - np.log(np.pi) - np.log(1.0) # 提议密度 q_forward(u) 在 (0, pi) x (0,1)
# 逆向过程是从复共轭对中任选一个删去,然后抽样两个新实根,提议分布就是均匀分布
num_complex_eigs = len(np.where((np.iscomplex(current_eigs)) & (current_eigs.imag > 0))[0]) #type: ignore
log_q_merge_backward = -np.log(num_complex_eigs + 1) - np.log(2) - np.log(2)
# 计算对数提议比
log_proposal_ratio = log_q_merge_backward - log_q_merge_forward
# 计算后验比
log_post_unnormalized_current = self._log_posterior_unnormalized(k, self.current_eigenvalues, self.current_b, self.current_sigma2_w, self.current_sigma2_z)
log_post_unnormalized_proposal = self._log_posterior_unnormalized(k, current_eigs, proposal_b, self.current_sigma2_w, self.current_sigma2_z)
# 计算接受率
# a. 计算后验比
log_post_current = self._log_posterior(k, self.current_b, self.current_sigma2_w, self.current_sigma2_z, self.current_a, eigenvalues=current_eigs)
a_proposal = np.real(np.poly(proposal_eigs)[1:][::-1])
log_post_proposal = self._log_posterior(k, self.current_b, self.current_sigma2_w, self.current_sigma2_z, a_proposal, eigenvalues=proposal_eigs)
log_acceptance_ratio = (log_post_unnormalized_proposal - log_post_unnormalized_current) + log_proposal_ratio
# b. 计算提议比和雅可比项
# 正向 (merge): 确定性,q_forward = 1
# 逆向 (split): 需要一个辅助变量 u,我们设计 u ~ Beta(2, 2) 在 (0, 1) 上
# 对应的雅可比行列式 |J| = 2b
# 完整的对数项为 log(q_reverse / q_forward * 1/|J|) = log(q_reverse) - log|J|
log_proposal_ratio = stats.uniform.logpdf(0.5, -1, 1) - np.log(2*b) # u=0.5 in reverse
acceptance_ratio = np.exp(log_acceptance_ratio)
log_acceptance_ratio = (log_post_proposal - log_post_current) + log_proposal_ratio
return {"a_new": np.real(np.poly(current_eigs)[1:][::-1]), "b_new": proposal_b, "log_ratio": log_proposal_ratio, "type": "merge", "new_eigs": current_eigs, "acceptance_ratio": acceptance_ratio}
# 接受或拒绝
if np.log(np.random.rand()) < log_acceptance_ratio:
self.current_a = a_proposal
self.current_eigenvalues = proposal_eigs
def _split_eigenvalues(self):
"""分裂游动:选择一个复共轭对,随机地分裂为两个实数根。"""
def _split_eigenvalues(self, current_eigs):
"""
分裂游动:选择一个复共轭对,随机地分裂为两个实数根。
"""
k = self.current_k
current_eigs = np.copy(self.current_eigenvalues)
# 随机选择一个复共轭对
complex_indices = np.where((~np.isclose(current_eigs.imag, 0)) & (current_eigs.imag > 0))
idx_c = np.random.choice(complex_indices)
lambda_c = current_eigs[idx_c]
a, b = lambda_c.real, lambda_c.imag
# 随机选择一个复共轭对,并删除它们
complex_indices = np.where((np.iscomplex(current_eigs)) & (current_eigs.imag > 0)) #type: ignore
if len(complex_indices[0]) < 1:
return # 没有复共轭对,无法分裂
idx_to_remove = np.random.choice(complex_indices[0])
lambda_removed = current_eigs[idx_to_remove]
conjugate_idx_to_remove = np.where(current_eigs == np.conjugate(lambda_removed))[0]
if len(conjugate_idx_to_remove) == 0:
return # 找不到共轭对,无法分裂
conjugate_idx = conjugate_idx_to_remove[0]
# 映射到两个实数根
u = np.random.beta(2, 2)
lambda1 = a + b * u
lambda2 = a - b * u
new_real_pair = [lambda1, lambda2]
# 删除选中的复共轭对
current_eigs = np.delete(current_eigs, [idx_to_remove, conjugate_idx])
# 生成两个实数根
real_eig1 = np.random.uniform(-1.0, 1.0)
real_eig2 = np.random.uniform(-1.0, 1.0)
current_eigs = np.append(current_eigs, [real_eig1, real_eig2])
# 构造提议的特征值集合
idx_c_conj = np.where(np.isclose(current_eigs, np.conjugate(lambda_c)))
proposal_eigs = np.delete(current_eigs, [idx_c, idx_c_conj])
proposal_eigs = np.append(proposal_eigs, new_real_pair)
# 对b进行游动
proposal_b, log_prior_b_current, log_prior_b_proposal = self._purpose_b_swimming()
# 检查稳定性
if np.any(np.abs(proposal_eigs) >= 1.0): return
# 计算提议密度
# 正向过程是任选一个复共轭对,分裂为两个实根
log_q_split_forward = -np.log(len(complex_indices[0])) - np.log(2) - np.log(2) # 提议密度 q_forward(u) 在 (-1,1) x (-1,1)
# 逆向过程是从实根中任选两个删去,然后抽样一个复共轭对,提议分布就是均匀分布
num_real_eigs = len(np.where(np.isreal(current_eigs))[0])
log_q_split_backward = -np.log(num_real_eigs * (num_real_eigs - 1) / 2.0) - np.log(np.pi) - np.log(1.0)
# 计算对数提议比
log_proposal_ratio = log_q_split_backward - log_q_split_forward
# 计算后验比
log_post_unnormalized_current = self._log_posterior_unnormalized(k, self.current_eigenvalues, self.current_b, self.current_sigma2_w, self.current_sigma2_z)
log_post_unnormalized_proposal = self._log_posterior_unnormalized(k, current_eigs, proposal_b, self.current_sigma2_w, self.current_sigma2_z)
# 计算接受率
# a. 计算后验比
log_post_current = self._log_posterior(k, self.current_b, self.current_sigma2_w, self.current_sigma2_z, self.current_a, eigenvalues=current_eigs)
a_proposal = np.real(np.poly(proposal_eigs)[1:][::-1])
log_post_proposal = self._log_posterior(k, self.current_b, self.current_sigma2_w, self.current_sigma2_z, a_proposal, eigenvalues=proposal_eigs)
log_acceptance_ratio = (log_post_unnormalized_proposal - log_post_unnormalized_current) + log_proposal_ratio
acceptance_ratio = np.exp(log_acceptance_ratio)
return {"a_new": np.real(np.poly(current_eigs)[1:][::-1]), "b_new": proposal_b, "log_ratio": log_proposal_ratio, "type": "split", "new_eigs": current_eigs, "acceptance_ratio": acceptance_ratio}
def _disturbe_swimming(self, current_eigs):
"""
扰动游动:保持维度不变,保持实数根和复共轭对的数量不变,进行扰动。
"""
k = self.current_k
# 对特征值进行游动
proposal_eigs, log_prior_eigs_current, log_prior_eigs_proposal = self._purpose_lumbda_swimming()
# 对b进行游动
proposal_b, log_prior_b_current, log_prior_b_proposal = self._purpose_b_swimming()
# 计算提议密度比 (对称提议,密度相等)
log_proposal_ratio = 0.0
# 计算后验比
log_post_unnormalized_current = self._log_posterior_unnormalized(k, self.current_eigenvalues, self.current_b, self.current_sigma2_w, self.current_sigma2_z)
log_post_unnormalized_proposal = self._log_posterior_unnormalized(k, proposal_eigs, proposal_b, self.current_sigma2_w, self.current_sigma2_z)
# 计算接受率
log_acceptance_ratio = (log_post_unnormalized_proposal - log_post_unnormalized_current) + log_proposal_ratio
acceptance_ratio = np.exp(log_acceptance_ratio)
return {"a_new": np.real(np.poly(proposal_eigs)[1:][::-1]), "b_new": proposal_b, "log_ratio": log_proposal_ratio, "type": "disturbe", "new_eigs": proposal_eigs, "acceptance_ratio": acceptance_ratio}
def run_MCMC(self, num_iterations):
"""
运行 RJMCMC 采样器指定次数的迭代。
"""
# 存储采样历史
k_history = []
a_history = []
b_history = []
sigma2_w_history = []
sigma2_z_history = []
eigenvalues_history = []
print(f"Starting MCMC with initial k={self.current_k}")
for i in range(num_iterations):
# 随机选择一种转移类型
# 这里的概率可以根据需要调整
move_type = np.random.choice(['birth_death', 'within_model'], p=[0.5, 0.5])
proposal = None
accepted = False
move_name = 'None'
if move_type == 'birth_death':
# 决定是 birth 还是 death
if self.current_k == self.k_min:
proposal = self._propose_birth()
elif self.current_k == self.k_max:
proposal = self._propose_death()
else:
if np.random.rand() < 0.5:
proposal = self._propose_birth()
else:
proposal = self._propose_death()
elif move_type == 'within_model':
proposal = self._propose_within_model()
# 处理提议
if proposal and 'acceptance_ratio' in proposal:
move_name = proposal.get('type', 'N/A')
if np.random.rand() < proposal['acceptance_ratio']:
# 接受提议,更新状态
if 'k_new' in proposal: self.current_k = proposal['k_new']
if 'new_eigs' in proposal: self.current_eigenvalues = proposal['new_eigs']
self.current_a = self._cal_current_a_coeffs(self.current_eigenvalues)
if 'b_new' in proposal: self.current_b = proposal['b_new']
# sigma2_w 和 sigma2_z 在这些提议中没有更新,保持不变
accepted = True
# 存储当前状态 (无论是否接受,都存储当前链的状态)
k_history.append(self.current_k)
a_history.append(self.current_a)
b_history.append(self.current_b)
sigma2_w_history.append(self.current_sigma2_w)
sigma2_z_history.append(self.current_sigma2_z)
eigenvalues_history.append(self.current_eigenvalues)
if (i + 1) % 100 == 0:
status = "Accepted" if accepted else "Rejected"
print(f"Iteration {i+1}/{num_iterations}, k: {self.current_k}, Move: {move_name}, Status: {status}")
return {
"k": np.array(k_history),
"a": a_history,
"b": b_history,
"sigma2_w": np.array(sigma2_w_history),
"sigma2_z": np.array(sigma2_z_history),
"eigenvalues": eigenvalues_history
}
if __name__ == "__main__":
# 导入生成数据的模块
from generateSimData import simulate_lti_data
from generateGroudTruth import generate_ground_truth_system
import matplotlib.pyplot as plt
# 配置 matplotlib 支持中文显示
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# 1. 生成 Ground Truth 系统和仿真数据
print("--- 1. 生成仿真数据 ---")
# 真实系统阶数为 2
A_true, B_true, C_true, D_true = generate_ground_truth_system(dx=2, rng_seed=42)
T_steps = 400
sigma_proc = 0.1 # 过程噪声标准差
sigma_meas = 0.1 # 测量噪声标准差
u_data, y_data = simulate_lti_data(
A_true, B_true, C_true, D_true,
T=T_steps,
sigma_process=sigma_proc,
sigma_measurement=sigma_meas,
rng_seed=123
)
# RJMCMC_Sampler 需要一维的 y 和 u
y_data_flat = y_data.flatten()
u_data_flat = u_data.flatten()
print(f"仿真数据生成完毕。 y shape: {y_data_flat.shape}, u shape: {u_data_flat.shape}")
# 2. 初始化 RJMCMC 采样器
print("\n--- 2. 初始化 RJMCMC 采样器 ---")
sampler = RJMCMC_Sampler(
y=y_data_flat,
u=u_data_flat,
k_min=1,
k_max=4, # 探索的最大阶数
initial_k=3 # 从一个不等于真实阶数的阶数开始
)
# 3. 运行 MCMC
print("\n--- 3. 开始运行 MCMC 采样 ---")
num_iterations = 20000
results = sampler.run_MCMC(num_iterations)
print("MCMC 采样完成。")
# 4. 分析和可视化结果
print("\n--- 4. 分析结果 ---")
# 丢弃早期样本 (burn-in)
burn_in = 1000
k_samples = results['k'][burn_in:]
# 计算模型阶数的后验分布
# 使用 bincount 统计每个 k 出现的次数
k_posterior_counts = np.bincount(k_samples, minlength=sampler.k_max + 1)
k_posterior_prob = k_posterior_counts / len(k_samples)
# 打印后验概率
print("模型阶数的后验概率分布:")
for k_val in range(sampler.k_min, sampler.k_max + 1):
print(f" P(k={k_val} | y) ≈ {k_posterior_prob[k_val]:.4f}")
# 可视化 k 的后验分布
plt.figure(figsize=(10, 6))
plt.bar(range(sampler.k_min, sampler.k_max + 1),
k_posterior_prob[sampler.k_min:sampler.k_max + 1],
color='skyblue', alpha=0.8, label='Posterior Probability')
plt.axvline(x=2, color='red', linestyle='--', label='True Model Order (k=2)')
plt.xlabel("模型阶数 (k)")
plt.ylabel("后验概率 P(k|y)")
plt.title("模型阶数的后验分布 (After Burn-in)")
plt.xticks(range(sampler.k_min, sampler.k_max + 1))
plt.legend()
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()
# b. 计算提议比和雅可比项
# 正向 (split): 随机,q_forward = p(u)
# 逆向 (merge): 确定性,q_reverse = 1
# 雅可比行列式 |J| = 2b
# 完整的对数项为 log(q_reverse / q_forward * |J|) = -log(q_forward) + log|J|
log_proposal_ratio = -stats.beta.logpdf(u, 2, 2) + np.log(2*b)