Pythonfiles

This commit is contained in:
2025-12-09 16:03:27 +01:00
parent b99bca8623
commit 858cfbdde6
4 changed files with 69 additions and 54 deletions

View File

@@ -1,26 +1,41 @@
from typing import Dict, Any
import sympy as sp
#für Varianzkomponentenschätzung
MAX_ITER = 10
TOL = 1e-3 # 0.1%.
def ausgleichung_mit_vks_iterativ(
A: sp.Matrix,
l: sp.Matrix,
modell: StochastischesModell,
max_iter: int = 10,
tol: float = 1e-3,
) -> Dict[str, Any]:
"""
Führt eine iterative Ausgleichung mit Varianzkomponentenschätzung durch.
for loop in range(MAX_ITER):
Ablauf:
- starte mit σ0,g² aus modell.sigma0_groups (meist alle = 1.0)
- wiederhole:
* Ausgleichung
* VKS
* Aktualisierung σ0,g²
bis sich alle σ̂0,g² ~ 1.0 (oder max_iter erreicht).
"""
Q_ll, P = modell.aufstellen_Qll_P()
history = [] # optional: Zwischenergebnisse speichern
N = A.T * P * A
n_vec = A.T * P * l
dx = N.LUsolve(n_vec)
for it in range(max_iter):
result = ausgleichung_einmal(A, l, modell)
history.append(result)
v = l - A * dx
sigma_hat = result["sigma_hat"]
sigma_hat = modell.varianzkomponenten(v, A)
# Prüfkriterium: alle σ̂ nahe bei 1.0?
if all(abs(val - 1.0) < tol for val in sigma_hat.values()):
print(f"Konvergenz nach {it+1} Iterationen.")
break
print(f"Iteration {loop+1}, σ̂² Gruppen:", sigma_hat)
# sonst: Modell-σ0,g² mit VKS-Ergebnis updaten
modell.update_sigma0_von_vks(sigma_hat)
# Prüfen: ist jede Komponente ≈ 1?
if all(abs(val - 1) < TOL for val in sigma_hat.values()):
print("Konvergenz erreicht ✔")
break
modell.update_sigma(sigma_hat)
# letztes Ergebnis + History zurückgeben
result["history"] = history
return result