Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
935 B
2
Indexable
Never
import math

def calculate_dispersion(midpoints, frequencies):
    n = sum(frequencies)
    mean = sum(midpoints[i] * frequencies[i] for i in range(len(midpoints))) / n

    # Variance
    var = sum(((midpoints[i] - mean) ** 2) * frequencies[i] for i in range(len(midpoints))) / n

    # Standard Deviation
    std = math.sqrt(var)

    # Quartile Deviation
    q1 = n // 4
    q3 = 3 * n // 4
    q_deviation = (midpoints[q3] - midpoints[q1]) / 2

    # Mean Deviation
    mean_dev = sum(abs(midpoints[i] - mean) * frequencies[i] for i in range(len(midpoints))) / n

    return var, std, q_deviation, mean_dev

# Example data
midpoints = [10, 20, 30, 40, 50]
frequencies = [5, 8, 12, 7, 3]

var, std, q_dev, mean_dev = calculate_dispersion(midpoints, frequencies)

print(f"Variance: {var}")
print(f"Standard Deviation: {std}")
print(f"Quartile Deviation: {q_dev}")
print(f"Mean Deviation: {mean_dev}")
Leave a Comment