Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
5
Indexable
Never
import math

def calculate_mean(data):
    return sum(data) / len(data)

def calculate_variance(data):
    mean = calculate_mean(data)
    variance = sum((x - mean) ** 2 for x in data) / len(data)
    return variance

def calculate_standard_deviation(data):
    variance = calculate_variance(data)
    return math.sqrt(variance)

def calculate_mean_deviation(data):
    mean = calculate_mean(data)
    deviations = [abs(x - mean) for x in data]
    return sum(deviations) / len(data)

def calculate_quartile_deviation(data):
    sorted_data = sorted(data)
    n = len(sorted_data)
    q1_index = n // 4
    q3_index = 3 * (n // 4)
    q1 = sorted_data[q1_index]
    q3 = sorted_data[q3_index]
    return (q3 - q1) / 2

# Example data
data = [25, 30, 35, 40, 45]

# Calculate and print measures of dispersion
print("Data:", data)
print("Standard Deviation:", calculate_standard_deviation(data))
print("Mean Deviation:", calculate_mean_deviation(data))
print("Quartile Deviation:", calculate_quartile_deviation(data))
print("Variance:", calculate_variance(data))
Leave a Comment