Add bpf_varianz_test/src/bpf_varianz_test/core.py
This commit is contained in:
commit
e22f8523da
1 changed files with 53 additions and 0 deletions
53
bpf_varianz_test/src/bpf_varianz_test/core.py
Normal file
53
bpf_varianz_test/src/bpf_varianz_test/core.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
from typing import List, Tuple
|
||||
import numpy as np
|
||||
from scipy import stats
|
||||
import statistics
|
||||
|
||||
|
||||
def perform_levene_test(kprobe_samples: List[float], bpf_samples: List[float]) -> Tuple[float, bool]:
|
||||
"""Führt den Levene-Test zur Prüfung der Varianzgleichheit durch.
|
||||
|
||||
Args:
|
||||
kprobe_samples: Messwerte der kprobe-Methode.
|
||||
bpf_samples: Messwerte der BPF-Methode.
|
||||
|
||||
Returns:
|
||||
Tuple aus p-Wert und boolescher Signifikanzentscheidung (True, wenn Varianzen gleich sind).
|
||||
"""
|
||||
if not kprobe_samples or not bpf_samples:
|
||||
raise ValueError("Beide Stichproben müssen Werte enthalten.")
|
||||
|
||||
# Levene-Test über scipy
|
||||
stat, p_value = stats.levene(kprobe_samples, bpf_samples, center='median')
|
||||
|
||||
alpha = 0.05
|
||||
variances_equal = p_value > alpha
|
||||
return float(p_value), variances_equal
|
||||
|
||||
|
||||
def bootstrap_variance_confidence_interval(samples: List[float], n_iterations: int) -> Tuple[float, float]:
|
||||
"""Berechnet Bootstrap-Konfidenzintervalle der Varianz einer Stichprobe.
|
||||
|
||||
Args:
|
||||
samples: Messwerte einer Methode.
|
||||
n_iterations: Anzahl der Bootstrap-Iterationen.
|
||||
|
||||
Returns:
|
||||
Tuple aus (untere, obere) Grenze des Konfidenzintervalls.
|
||||
"""
|
||||
if not samples:
|
||||
raise ValueError("Stichprobe darf nicht leer sein.")
|
||||
if n_iterations <= 0:
|
||||
raise ValueError("n_iterations muss positiv sein.")
|
||||
|
||||
data = np.array(samples)
|
||||
n = len(data)
|
||||
variances = np.empty(n_iterations)
|
||||
|
||||
for i in range(n_iterations):
|
||||
resampled = np.random.choice(data, size=n, replace=True)
|
||||
variances[i] = np.var(resampled, ddof=1)
|
||||
|
||||
lower = np.percentile(variances, 2.5)
|
||||
upper = np.percentile(variances, 97.5)
|
||||
return float(lower), float(upper)
|
||||
Loading…
Reference in a new issue