Skip to content

Instantly share code, notes, and snippets.

@annawoodard
Created August 14, 2023 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annawoodard/08b4904941feb291857929e5169df498 to your computer and use it in GitHub Desktop.
Save annawoodard/08b4904941feb291857929e5169df498 to your computer and use it in GitHub Desktop.
from scipy.stats import norm
def detectable_difference(alpha, power, proportion, n_positives):
"""
Calculate the detectable difference in proportions for a given sample size, power, significance level, and proportion.
Parameters:
alpha: Significance level
power: Desired power of the test
proportion: Proportion in the control group (e.g., existing provider detection rate)
n_positives: Number of positive examples
Returns:
detectable_difference: Detectable difference in proportions
"""
z_alpha = norm.ppf(1 - alpha / 2)
z_power = norm.ppf(power)
p1 = proportion
p2 = p1 # Assuming null hypothesis that the proportions are equal
p_bar = (p1 + p2) / 2
q_bar = 1 - p_bar
n1 = n_positives
n2 = n_positives # Assuming equal sample sizes
delta = z_alpha * ((p_bar * q_bar * (1 / n1 + 1 / n2)) ** 0.5) + z_power * (
(p1 * (1 - p1) / n1 + p2 * (1 - p2) / n2) ** 0.5
)
detectable_difference = delta / (1 + (p1 * (1 - p1) / p2 / (1 - p2)) ** 0.5)
return detectable_difference
# Given parameters
alpha = 0.05
power = 0.80
provider_detection_rate = 0.8
total_images = 2000
occult_femoral_fracture_incidence = 0.03
positive_cases = int(total_images * occult_femoral_fracture_incidence)
# Calculate detectable difference
difference = detectable_difference(
alpha, power, provider_detection_rate, positive_cases
)
print(
f"The detectable difference in detection rate is approximately {difference * 100:.2f}%"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment