Oevelse 6
In [2]:
Copied!
import math
from statistics import NormalDist
# --------------------------------------------------
# Given data
# --------------------------------------------------
lower = 45781 # lower end of "typical" interval
upper = 59892 # upper end of "typical" interval
n_class = 15
threshold_55000 = 55000
threshold_max = 67476
# --------------------------------------------------
# 1) Estimate mu and sigma assuming interval is mu ± sigma
# --------------------------------------------------
mu = (lower + upper) / 2
sigma = (upper - lower) / 2
print("1) Estimated parameters")
print(f"mu = {mu:.4f}")
print(f"sigma = {sigma:.4f}")
print()
# Standard normal distribution
std_normal = NormalDist()
# Survival function for X ~ N(mu, sigma)
def normal_sf(x, mu, sigma):
z = (x - mu) / sigma
return 1 - std_normal.cdf(z)
# --------------------------------------------------
# 2) Probability that one software engineer earns more than 55,000
# --------------------------------------------------
p_single_55000 = normal_sf(threshold_55000, mu, sigma)
print("2) Probability that a randomly selected software engineer")
print(f" earns more than {threshold_55000:,} DKK:")
print(f" P(X > {threshold_55000:,}) = {p_single_55000:.4f}")
print()
# --------------------------------------------------
# 3) Probability that the average salary of 15 students exceeds 55,000
# --------------------------------------------------
se = sigma / math.sqrt(n_class)
p_mean_55000 = normal_sf(threshold_55000, mu, se)
print("3) Probability that the average salary of the 15 software engineers")
print(f" in your class exceeds {threshold_55000:,} DKK:")
print(f" Standard error = {se:.4f}")
print(f" P(X_bar > {threshold_55000:,}) = {p_mean_55000:.4f}")
print()
# --------------------------------------------------
# 4) Probability that one software engineer earns more than 67,476
# --------------------------------------------------
p_single_max = normal_sf(threshold_max, mu, sigma)
print("4) Probability that a randomly selected software engineer")
print(f" earns more than {threshold_max:,} DKK:")
print(f" P(X > {threshold_max:,}) = {p_single_max:.4f}")
print()
# --------------------------------------------------
# 5) Expected proportion and expected number in a class of 15
# who earn more than 67,476
# --------------------------------------------------
expected_proportion = p_single_max
expected_number = n_class * p_single_max
print("5) For a class of 15 software engineers:")
print(f" Expected proportion earning more than {threshold_max:,} DKK = {expected_proportion:.4f}")
print(f" Expected number earning more than {threshold_max:,} DKK = {expected_number:.4f}")
import math
from statistics import NormalDist
# --------------------------------------------------
# Given data
# --------------------------------------------------
lower = 45781 # lower end of "typical" interval
upper = 59892 # upper end of "typical" interval
n_class = 15
threshold_55000 = 55000
threshold_max = 67476
# --------------------------------------------------
# 1) Estimate mu and sigma assuming interval is mu ± sigma
# --------------------------------------------------
mu = (lower + upper) / 2
sigma = (upper - lower) / 2
print("1) Estimated parameters")
print(f"mu = {mu:.4f}")
print(f"sigma = {sigma:.4f}")
print()
# Standard normal distribution
std_normal = NormalDist()
# Survival function for X ~ N(mu, sigma)
def normal_sf(x, mu, sigma):
z = (x - mu) / sigma
return 1 - std_normal.cdf(z)
# --------------------------------------------------
# 2) Probability that one software engineer earns more than 55,000
# --------------------------------------------------
p_single_55000 = normal_sf(threshold_55000, mu, sigma)
print("2) Probability that a randomly selected software engineer")
print(f" earns more than {threshold_55000:,} DKK:")
print(f" P(X > {threshold_55000:,}) = {p_single_55000:.4f}")
print()
# --------------------------------------------------
# 3) Probability that the average salary of 15 students exceeds 55,000
# --------------------------------------------------
se = sigma / math.sqrt(n_class)
p_mean_55000 = normal_sf(threshold_55000, mu, se)
print("3) Probability that the average salary of the 15 software engineers")
print(f" in your class exceeds {threshold_55000:,} DKK:")
print(f" Standard error = {se:.4f}")
print(f" P(X_bar > {threshold_55000:,}) = {p_mean_55000:.4f}")
print()
# --------------------------------------------------
# 4) Probability that one software engineer earns more than 67,476
# --------------------------------------------------
p_single_max = normal_sf(threshold_max, mu, sigma)
print("4) Probability that a randomly selected software engineer")
print(f" earns more than {threshold_max:,} DKK:")
print(f" P(X > {threshold_max:,}) = {p_single_max:.4f}")
print()
# --------------------------------------------------
# 5) Expected proportion and expected number in a class of 15
# who earn more than 67,476
# --------------------------------------------------
expected_proportion = p_single_max
expected_number = n_class * p_single_max
print("5) For a class of 15 software engineers:")
print(f" Expected proportion earning more than {threshold_max:,} DKK = {expected_proportion:.4f}")
print(f" Expected number earning more than {threshold_max:,} DKK = {expected_number:.4f}")
1) Estimated parameters mu = 52836.5000 sigma = 7055.5000 2) Probability that a randomly selected software engineer earns more than 55,000 DKK: P(X > 55,000) = 0.3796 3) Probability that the average salary of the 15 software engineers in your class exceeds 55,000 DKK: Standard error = 1821.7223 P(X_bar > 55,000) = 0.1175 4) Probability that a randomly selected software engineer earns more than 67,476 DKK: P(X > 67,476) = 0.0190 5) For a class of 15 software engineers: Expected proportion earning more than 67,476 DKK = 0.0190 Expected number earning more than 67,476 DKK = 0.2850