Pythonfiles
This commit is contained in:
@@ -1,41 +1,66 @@
|
|||||||
from typing import Dict, Any
|
from typing import Dict, Any
|
||||||
import sympy as sp
|
import sympy as sp
|
||||||
|
from Stochastisches_Modell import StochastischesModell
|
||||||
|
|
||||||
def ausgleichung_mit_vks_iterativ(
|
def iterative_ausgleichung(
|
||||||
A: sp.Matrix,
|
A: sp.Matrix,
|
||||||
l: sp.Matrix,
|
l: sp.Matrix,
|
||||||
modell: StochastischesModell,
|
modell: StochastischesModell,
|
||||||
max_iter: int = 10,
|
max_iter: int = 100,
|
||||||
tol: float = 1e-3,
|
tol: float = 1e-3,
|
||||||
) -> Dict[str, Any]:
|
) -> Dict[str, Any]:
|
||||||
"""
|
|
||||||
Führt eine iterative Ausgleichung mit Varianzkomponentenschätzung durch.
|
|
||||||
|
|
||||||
Ablauf:
|
ergebnisse_iter = [] #Liste für Zwischenergebnisse
|
||||||
- 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).
|
|
||||||
"""
|
|
||||||
|
|
||||||
history = [] # optional: Zwischenergebnisse speichern
|
|
||||||
|
|
||||||
for it in range(max_iter):
|
for it in range(max_iter):
|
||||||
result = ausgleichung_einmal(A, l, modell)
|
Q_ll, P = modell.berechne_Qll_P() #Stochastisches Modell: Qll und P berechnen
|
||||||
history.append(result)
|
|
||||||
|
|
||||||
sigma_hat = result["sigma_hat"]
|
N = A.T * P * A #Normalgleichungsmatrix N
|
||||||
|
Q_xx = N.inv() #Kofaktormatrix der Unbekannten Qxx
|
||||||
|
n = A.T * P * l #Absolutgliedvektor n
|
||||||
|
|
||||||
# Prüfkriterium: alle σ̂ nahe bei 1.0?
|
dx = N.LUsolve(n) #Zuschlagsvektor dx
|
||||||
if all(abs(val - 1.0) < tol for val in sigma_hat.values()):
|
|
||||||
print(f"Konvergenz nach {it+1} Iterationen.")
|
v = l - A * dx #Residuenvektor v
|
||||||
|
|
||||||
|
Q_vv = modell.berechne_Qvv(A, Q_ll, Q_xx) #Kofaktormatrix der Verbesserungen Qvv
|
||||||
|
R = modell.berechne_R(Q_vv, P) #Redundanzmatrix R
|
||||||
|
r = modell.berechne_r(R) #Redundanzanteile als Vektor r
|
||||||
|
|
||||||
|
sigma_hat = modell.berechne_vks(v, P, r) #Varianzkomponentenschätzung durchführen
|
||||||
|
|
||||||
|
ergebnisse_iter.append({ #Zwischenergebnisse speichern in Liste
|
||||||
|
"iter": it + 1,
|
||||||
|
"Q_ll": Q_ll,
|
||||||
|
"P": P,
|
||||||
|
"N": N,
|
||||||
|
"Q_xx": Q_xx,
|
||||||
|
"dx": dx,
|
||||||
|
"v": v,
|
||||||
|
"Q_vv": Q_vv,
|
||||||
|
"R": R,
|
||||||
|
"r": r,
|
||||||
|
"sigma_hat": sigma_hat,
|
||||||
|
"sigma0_groups": dict(modell.sigma0_groups),
|
||||||
|
})
|
||||||
|
|
||||||
|
if all(abs(val - 1.0) < tol for val in sigma_hat.values()): #Abbruchkriterium
|
||||||
|
print(f"Konvergenz nach {it + 1} Iterationen erreicht.")
|
||||||
break
|
break
|
||||||
|
|
||||||
# sonst: Modell-σ0,g² mit VKS-Ergebnis updaten
|
|
||||||
modell.update_sigma0_von_vks(sigma_hat)
|
modell.update_sigma0_von_vks(sigma_hat)
|
||||||
|
|
||||||
# letztes Ergebnis + History zurückgeben
|
return {
|
||||||
result["history"] = history
|
"dx": dx,
|
||||||
return result
|
"v": v,
|
||||||
|
"Q_ll": Q_ll,
|
||||||
|
"P": P,
|
||||||
|
"N": N,
|
||||||
|
"Q_xx": Q_xx,
|
||||||
|
"Q_vv": Q_vv,
|
||||||
|
"R": R,
|
||||||
|
"r": r,
|
||||||
|
"sigma_hat": sigma_hat,
|
||||||
|
"sigma0_groups": dict(modell.sigma0_groups),
|
||||||
|
"history": ergebnisse_iter,
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user